Merge branch 'main' into 0.43.x

# Conflicts:
#	composer.json
#	composer.lock
This commit is contained in:
Jake Barnby committed 2025-01-13 15:05:29 +13:00
commit ba83ba8411
18 files changed
+581 -288

No files matched your search

+6
View File
@@ -29,5 +29,11 @@
"phpstan/phpstan": "^1.9",
"laravel/pint": "1.5.*",
"phpbench/phpbench": "^1.2"
},
"config": {
"allow-plugins": {
"php-http/discovery": true,
"tbachert/spi": true
}
}
}
+62 -3
View File
@@ -1,5 +1,3 @@
version: '3'
services:
mysql:
image: mysql:8
@@ -10,6 +8,15 @@ services:
ports:
- "9307:3306"
adminer:
image: adminer
container_name: abuse-adminer
restart: always
ports:
- 9506:8080
networks:
- abuse
redis:
image: redis:6.0-alpine
container_name: redis
@@ -30,5 +37,57 @@ services:
- ./src:/code/src
- ./tests:/code/tests
redis-insight:
image: redis/redisinsight:latest
restart: unless-stopped
networks:
- abuse
environment:
- REDIS_HOSTS=redis-cluster-0:6379
ports:
- "8081:5540"
volumes:
- redisinsight:/data
redis-cluster-0:
image: docker.io/bitnami/redis-cluster:7.4
environment:
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_NODES=redis-cluster-0 redis-cluster-1 redis-cluster-2 redis-cluster-3
- REDIS_CLUSTER_CREATOR=yes
- REDIS_CLUSTER_REPLICAS=0
networks:
- abuse
depends_on:
- redis-cluster-1
- redis-cluster-2
- redis-cluster-3
redis-cluster-1:
image: docker.io/bitnami/redis-cluster:7.4
environment:
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_NODES=redis-cluster-0 redis-cluster-1 redis-cluster-2 redis-cluster-3
networks:
- abuse
redis-cluster-2:
image: docker.io/bitnami/redis-cluster:7.4
environment:
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_NODES=redis-cluster-0 redis-cluster-1 redis-cluster-2 redis-cluster-3
networks:
- abuse
redis-cluster-3:
image: docker.io/bitnami/redis-cluster:7.4
environment:
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_NODES=redis-cluster-0 redis-cluster-1 redis-cluster-2 redis-cluster-3
networks:
- abuse
networks:
abuse:
abuse:
volumes:
redisinsight:
+3 -3
View File
@@ -1,6 +1,6 @@
{
"$schema":"vendor/phpbench/phpbench/phpbench.schema.json",
"$schema": "vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "tests",
"runner.file_pattern": "*Bench.php"
"runner.path": "tests/Abuse/Bench",
"runner.file_pattern": "*.php"
}
+4 -4
View File
@@ -44,13 +44,13 @@ class Abuse
}
/**
* Delete all logs older than $datetime
* Delete all logs older than $timestamp
*
* @param string $datetime
* @param int $timestamp
* @return bool
*/
public function cleanup(string $datetime): bool
public function cleanup(int $timestamp): bool
{
return $this->adapter->cleanup($datetime);
return $this->adapter->cleanup($timestamp);
}
}
+2 -2
View File
@@ -79,8 +79,8 @@ abstract class Adapter
/**
* Delete all logs older than $datetime
*
* @param string $datetime
* @param int $timestamp
* @return bool
*/
abstract public function cleanup(string $datetime): bool;
abstract public function cleanup(int $timestamp): bool;
}
+3 -3
View File
@@ -90,14 +90,14 @@ class ReCaptcha extends Adapter
}
/**
* Delete logs older than $datetime
* Delete logs older than $timestamp
*
* @param string $datetime
* @param int $timestamp
* @return bool
*
* @throws Exception
*/
public function cleanup(string $datetime): bool
public function cleanup(int $timestamp): bool
{
throw new Exception('Method not supported');
}
-139
View File
@@ -1,139 +0,0 @@
<?php
namespace Utopia\Abuse\Adapters\Redis;
use Redis as Client;
use Utopia\Abuse\Adapters\TimeLimit as TimeLimitAdapter;
class TimeLimit extends TimeLimitAdapter
{
public const NAMESPACE = 'abuse';
/**
* @var Client
*/
protected Client $redis;
public function __construct(string $key, int $limit, int $seconds, Client $redis)
{
$this->redis = $redis;
$this->key = $key;
$time = (int) \date('U', (int) (\floor(\time() / $seconds)) * $seconds);
$this->time = strval($time);
$this->limit = $limit;
}
/**
* Undocumented function
*
* @param string $key
* @param string $datetime
* @return integer
*/
protected function count(string $key, string $datetime): int
{
if (0 == $this->limit) { // No limit no point for counting
return 0;
}
if (! \is_null($this->count)) { // Get fetched result
return $this->count;
}
/** @var string $count */
$count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $datetime);
if (!$count) {
$this->count = 0;
} else {
$this->count = intval($count);
}
return $this->count;
}
/**
* @param string $key
* @param string $datetime
* @return void
*
*/
protected function hit(string $key, string $datetime): void
{
if (0 == $this->limit) { // No limit no point for counting
return;
}
/** @var string $count */
$count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $datetime);
if (!$count) {
$this->count = 0;
} else {
$this->count = intval($count);
}
$this->redis->incr(self::NAMESPACE . '__'. $key .'__'. $datetime);
$this->count++;
}
/**
* Get abuse logs
*
* Return logs with an offset and limit
*
* @param int|null $offset
* @param int|null $limit
* @return array<string, mixed>
*/
public function getLogs(?int $offset = null, ?int $limit = 25): array
{
// TODO limit potential is SCAN but needs cursor no offset
$cursor = null;
$keys = $this->redis->scan($cursor, self::NAMESPACE . '__*', $limit);
if (!$keys) {
return [];
}
$logs = [];
foreach ($keys as $key) {
$logs[$key] = $this->redis->get($key);
}
return $logs;
}
/**
* Delete all logs older than $datetime
*
* @param string $datetime
* @return bool
*/
public function cleanup(string $datetime): bool
{
$iterator = null;
while ($iterator !== 0) {
$keys = $this->redis->scan($iterator, self::NAMESPACE . '__*__*', 1000);
$keys = $this->filterKeys($keys ? $keys : [], (int) $datetime);
$this->redis->del($keys);
}
return true;
}
/**
* Filter keys
*
* @param array<string> $keys
* @param integer $timestamp
* @return array<string>
*/
protected function filterKeys(array $keys, int $timestamp): array
{
$filteredKeys = [];
foreach ($keys as $key) {
$parts = explode('__', $key);
$keyTimestamp = (int)end($parts); // Assuming the last part is always the timestamp
if ($keyTimestamp < $timestamp) {
$filteredKeys[] = $key;
}
}
return $filteredKeys;
}
}
+12 -14
View File
@@ -18,9 +18,9 @@ abstract class TimeLimit extends Adapter
protected ?int $count = null;
/**
* @var string
* @var int
*/
protected string $time;
protected int $timestamp;
/**
* Check
@@ -28,16 +28,14 @@ abstract class TimeLimit extends Adapter
* Checks if number of counts is bigger or smaller than current limit
*
* @param string $key
* @param string $datetime
* @param int $timestamp
* @return int
*
* @throws \Exception
*/
abstract protected function count(string $key, string $datetime): int;
abstract protected function hit(string $key, string $datetime): void;
abstract protected function count(string $key, int $timestamp): int;
abstract protected function hit(string $key, int $timestamp): void;
/**
* Check
@@ -56,8 +54,8 @@ abstract class TimeLimit extends Adapter
$key = $this->parseKey();
if ($this->limit > $this->count($key, $this->time)) {
$this->hit($key, $this->time);
if ($this->limit > $this->count($key, $this->timestamp)) {
$this->hit($key, $this->timestamp);
return false;
}
@@ -76,7 +74,7 @@ abstract class TimeLimit extends Adapter
*/
public function remaining(): int
{
$left = $this->limit - ($this->count($this->parseKey(), $this->time) + 1); // Add one because we need to say how many left not how many done
$left = $this->limit - ($this->count($this->parseKey(), $this->timestamp) + 1); // Add one because we need to say how many left not how many done
return (0 > $left) ? 0 : $left;
}
@@ -96,12 +94,12 @@ abstract class TimeLimit extends Adapter
/**
* Time
*
* Return the Datetime
* Return the timestamp
*
* @return string
* @return int
*/
public function time(): string
public function time(): int
{
return $this->time;
return $this->timestamp;
}
}
@@ -1,8 +1,8 @@
<?php
namespace Utopia\Abuse\Adapters\Database;
namespace Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit as TimeLimitAdapter;
use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Database\Database as UtopiaDB;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
@@ -13,7 +13,7 @@ use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Exception;
class TimeLimit extends TimeLimitAdapter
class Database extends TimeLimit
{
public const COLLECTION = 'abuse';
@@ -80,8 +80,8 @@ class TimeLimit extends TimeLimitAdapter
public function __construct(string $key, int $limit, int $seconds, UtopiaDB $db)
{
$this->key = $key;
$time = (int) \date('U', (int) (\floor(\time() / $seconds)) * $seconds); // todo: any good Idea without time()?
$this->time = DateTime::format((new \DateTime())->setTimestamp($time));
$now = \time();
$this->timestamp = (int)($now - ($now % $seconds));
$this->limit = $limit;
$this->db = $db;
}
@@ -106,7 +106,7 @@ class TimeLimit extends TimeLimitAdapter
try {
$this->db->createCollection(
TimeLimit::COLLECTION,
self::COLLECTION,
$attributes,
$indexes
);
@@ -121,12 +121,12 @@ class TimeLimit extends TimeLimitAdapter
* Checks if number of counts is bigger or smaller than current limit
*
* @param string $key
* @param string $datetime
* @param int $timestamp
* @return int
*
* @throws \Exception
*/
protected function count(string $key, string $datetime): int
protected function count(string $key, int $timestamp): int
{
if (0 == $this->limit) { // No limit no point for counting
return 0;
@@ -136,11 +136,13 @@ class TimeLimit extends TimeLimitAdapter
return $this->count;
}
$timestamp = $this->toDateTime($timestamp);
/** @var array<Document> $result */
$result = Authorization::skip(function () use ($key, $datetime) {
return $this->db->find(TimeLimit::COLLECTION, [
$result = Authorization::skip(function () use ($key, $timestamp) {
return $this->db->find(self::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
Query::equal('time', [$timestamp]),
]);
});
@@ -158,39 +160,40 @@ class TimeLimit extends TimeLimitAdapter
/**
* @param string $key
* @param string $datetime
* @param int $timestamp
* @return void
*
* @throws AuthorizationException|Structure|\Exception|\Throwable
*/
protected function hit(string $key, string $datetime): void
protected function hit(string $key, int $timestamp): void
{
if (0 == $this->limit) { // No limit no point for counting
return;
}
Authorization::skip(function () use ($datetime, $key) {
$data = $this->db->findOne(TimeLimit::COLLECTION, [
$timestamp = $this->toDateTime($timestamp);
Authorization::skip(function () use ($timestamp, $key) {
$data = $this->db->findOne(self::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
Query::equal('time', [$timestamp]),
]);
if ($data->isEmpty()) {
$data = [
'$permissions' => [],
'key' => $key,
'time' => $datetime,
'time' => $timestamp,
'count' => 1,
'$collection' => TimeLimit::COLLECTION,
'$collection' => self::COLLECTION,
];
try {
$this->db->createDocument(TimeLimit::COLLECTION, new Document($data));
$this->db->createDocument(self::COLLECTION, new Document($data));
} catch (Duplicate $e) {
// Duplicate in case of race condition
$data = $this->db->findOne(TimeLimit::COLLECTION, [
$data = $this->db->findOne(self::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
Query::equal('time', [$timestamp]),
]);
if (!$data->isEmpty()) {
@@ -198,14 +201,14 @@ class TimeLimit extends TimeLimitAdapter
if (\is_numeric($count)) {
$this->count = intval($count);
}
$this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(), 'count');
$this->db->increaseDocumentAttribute(self::COLLECTION, $data->getId(), 'count');
} else {
throw new \Exception('Document Not Found');
}
}
} else {
/** @var Document $data */
$this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(), 'count');
$this->db->increaseDocumentAttribute(self::COLLECTION, $data->getId(), 'count');
}
});
@@ -237,7 +240,7 @@ class TimeLimit extends TimeLimitAdapter
$queries[] = Query::limit($limit);
}
return $this->db->find(TimeLimit::COLLECTION, $queries);
return $this->db->find(self::COLLECTION, $queries);
});
return $results;
@@ -246,25 +249,31 @@ class TimeLimit extends TimeLimitAdapter
/**
* Delete logs older than $timestamp seconds
*
* @param string $datetime
* @param int $timestamp
* @return bool
*
* @throws AuthorizationException|\Exception
*/
public function cleanup(string $datetime): bool
public function cleanup(int $timestamp): bool
{
Authorization::skip(function () use ($datetime) {
$timestamp = $this->toDateTime($timestamp);
Authorization::skip(function () use ($timestamp) {
do {
$documents = $this->db->find(TimeLimit::COLLECTION, [
Query::lessThan('time', $datetime),
$documents = $this->db->find(self::COLLECTION, [
Query::lessThan('time', $timestamp),
]);
foreach ($documents as $document) {
$this->db->deleteDocument(TimeLimit::COLLECTION, $document->getId());
$this->db->deleteDocument(self::COLLECTION, $document->getId());
}
} while (! empty($documents));
});
return true;
}
protected function toDateTime(int $timestamp): string
{
return DateTime::format((new \DateTime())->setTimestamp($timestamp));
}
}
+116
View File
@@ -0,0 +1,116 @@
<?php
namespace Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit;
class Redis extends TimeLimit
{
public const NAMESPACE = 'abuse';
/**
* @var \Redis
*/
protected \Redis $redis;
/**
* @var int
*/
protected int $ttl;
public function __construct(string $key, int $limit, int $seconds, \Redis $redis)
{
$this->redis = $redis;
$this->key = $key;
$this->ttl = $seconds;
$now = \time();
$this->timestamp = (int)($now - ($now % $seconds));
$this->limit = $limit;
}
/**
* Undocumented function
*
* @param string $key
* @param int $timestamp
* @return integer
*/
protected function count(string $key, int $timestamp): int
{
if (0 == $this->limit) { // No limit no point for counting
return 0;
}
if (! \is_null($this->count)) { // Get fetched result
return $this->count;
}
/** @var string $count */
$count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $timestamp);
if (!$count) {
$this->count = 0;
} else {
$this->count = intval($count);
}
return $this->count;
}
/**
* @param string $key
* @param int $timestamp
* @return void
*
*/
protected function hit(string $key, int $timestamp): void
{
if (0 == $this->limit) { // No limit no point for counting
return;
}
$key = self::NAMESPACE . '__' . $key . '__' . $timestamp;
$this->redis->multi()
->incr($key)
->expire($key, $this->ttl)
->exec();
$this->count = ($this->count ?? 0) + 1;
}
/**
* Get abuse logs
*
* Return logs with an offset and limit
*
* @param int|null $offset
* @param int|null $limit
* @return array<string, mixed>
*/
public function getLogs(?int $offset = null, ?int $limit = 25): array
{
// TODO limit potential is SCAN but needs cursor no offset
$cursor = null;
$keys = $this->redis->scan($cursor, self::NAMESPACE . '__*', $limit);
if (!$keys) {
return [];
}
$logs = [];
foreach ($keys as $key) {
$logs[$key] = $this->redis->get($key);
}
return $logs;
}
/**
* Delete all logs older than $timestamp
*
* @param int $timestamp
* @return bool
*/
public function cleanup(int $timestamp): bool
{
// No need for manual cleanup - Redis TTL handles this automatically
return true;
}
}
@@ -0,0 +1,133 @@
<?php
namespace Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit;
class RedisCluster extends TimeLimit
{
public const NAMESPACE = 'abuse';
/**
* @var \RedisCluster
*/
protected \RedisCluster $redis;
/**
* @var int
*/
protected int $ttl;
public function __construct(string $key, int $limit, int $seconds, \RedisCluster $redis)
{
$this->redis = $redis;
$this->key = $key;
$this->ttl = $seconds;
$now = \time();
$this->timestamp = (int)($now - ($now % $seconds));
$this->limit = $limit;
}
/**
* Get count for a key at specific timestamp
*
* @param string $key
* @param int $timestamp
* @return integer
*/
protected function count(string $key, int $timestamp): int
{
if (0 == $this->limit) { // No limit no point for counting
return 0;
}
if (! \is_null($this->count)) { // Get fetched result
return $this->count;
}
/** @var string|false $count */
$count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $timestamp);
if ($count === false) {
$this->count = 0;
} else {
$this->count = intval($count);
}
return $this->count;
}
/**
* Record a hit for a key at specific timestamp
*
* @param string $key
* @param int $timestamp
* @return void
*/
protected function hit(string $key, int $timestamp): void
{
if (0 == $this->limit) { // No limit no point for counting
return;
}
$key = self::NAMESPACE . '__'. $key .'__'. $timestamp;
$this->redis->multi();
$this->redis->incr($key);
$this->redis->expire($key, $this->ttl);
$this->redis->exec();
$this->count = ($this->count ?? 0) + 1;
}
/**
* Get abuse logs with proper cursor-based pagination
*
* @param int|null $offset
* @param int|null $limit
* @return array<string, mixed>
*/
public function getLogs(?int $offset = 0, ?int $limit = 25): array
{
$offset = $offset ?? 0;
$limit = $limit ?? 25;
$matches = [];
$pattern = self::NAMESPACE . '__*';
// Get all keys from each master
foreach ($this->redis->_masters() as $master) {
$cursor = null;
do {
/** @phpstan-ignore-next-line */
$keys = $this->redis->scan($cursor, $master, $pattern, 100);
if ($keys !== false) {
$matches = array_merge($matches, $keys);
}
} while ($cursor > 0 && count($matches) < $offset + $limit);
}
// Sort to ensure consistent ordering
sort($matches);
// Apply offset and limit
$matches = array_slice($matches, $offset, $limit);
if (empty($matches)) {
return [];
}
// Batch fetch values using mget
$values = $this->redis->mget($matches);
return array_combine($matches, $values);
}
/**
* No need for manual cleanup - using Redis TTL
*
* @param int $timestamp
* @return bool
*/
public function cleanup(int $timestamp): bool
{
return true;
}
}
+74 -45
View File
@@ -4,67 +4,96 @@ namespace Utopia\Tests;
use PHPUnit\Framework\TestCase;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapter;
use Utopia\Abuse\Adapters\TimeLimit;
abstract class Base extends TestCase
{
protected Abuse $abuse;
abstract public function getAdapter(string $key, int $limit, int $seconds): TimeLimit;
protected Abuse $abuseIp;
abstract public function getAdapter(string $key, int $limit, int $seconds): Adapter;
abstract public function getCleanupDateTime(): string;
public function tearDown(): void
/**
* Test a static key with a limit of 2 requests per second
*/
public function testStaticKey(): void
{
unset($this->abuse);
$adapter = $this->getAdapter('static-key', 2, 1);
$abuse = new Abuse($adapter);
$this->assertEquals($abuse->check(), false);
$this->assertEquals($abuse->check(), false);
$this->assertEquals($abuse->check(), true);
}
public function testImitate2Requests(): void
/**
* Test a dynamic key with a limit of 2 requests per second
*/
public function testDynamicKey(): void
{
$key = '{{ip}}';
$value = '0.0.0.10';
$adapter = $this->getAdapter($key, 1, 1);
$adapter->setParam($key, $value);
$this->abuseIp = new Abuse($adapter);
$this->assertEquals($this->abuseIp->check(), false);
$this->assertEquals($this->abuseIp->check(), true);
sleep(1);
$adapter = $this->getAdapter($key, 1, 1);
$adapter->setParam($key, $value);
$this->abuseIp = new Abuse($adapter);
$this->assertEquals($this->abuseIp->check(), false);
$this->assertEquals($this->abuseIp->check(), true);
$adapter = $this->getAdapter('dynamic-key-{{ip}}', 2, 1);
$adapter->setParam('{{ip}}', '0.0.0.10');
$abuse = new Abuse($adapter);
$this->assertEquals($abuse->check(), false);
$this->assertEquals($abuse->check(), false);
$this->assertEquals($abuse->check(), true);
}
public function testIsValid(): void
/**
* Test a dynamic key with 2 params
*/
public function testDynamicKeyWith2Params(): void
{
// Use vars to resolve adapter key
$this->assertEquals($this->abuse->check(), false);
$this->assertEquals($this->abuse->check(), false);
$this->assertEquals($this->abuse->check(), false);
$this->assertEquals($this->abuse->check(), true);
$adapter = $this->getAdapter('two-params-{{ip}}-{{email}}', 2, 1);
$adapter->setParam('{{ip}}', '0.0.0.10');
$adapter->setParam('{{email}}', 'test@test.com');
$abuse = new Abuse($adapter);
$this->assertEquals($abuse->check(), false);
$this->assertEquals($abuse->check(), false);
$this->assertEquals($abuse->check(), true);
}
public function testCleanup(): void
/**
* Test a dynamic key with higher request rate like 100 requests per second
*/
public function testDynamicKeyFastRequests(): void
{
// Check that there is only one log
$logs = $this->abuse->getLogs(0, 10);
$this->assertEquals(3, \count($logs));
$adapter = $this->getAdapter('fast-requests-{{ip}}', 100, 1);
$adapter->setParam('{{ip}}', '0.0.0.10');
$abuse = new Abuse($adapter);
for ($i = 0; $i < 100; $i++) {
$this->assertEquals($abuse->check(), false);
}
$this->assertEquals($abuse->check(), true);
}
sleep(5);
// Delete the log
/**
* Test that the limit is reset after the time limit
*/
public function testLimitReset(): void
{
$adapter = $this->getAdapter('limit-reset-{{ip}}', 100, 2);
$adapter->setParam('{{ip}}', '127.0.0.1');
$abuse = new Abuse($adapter);
for ($i = 0; $i < 100; $i++) {
$this->assertEquals($abuse->check(), false);
}
$this->assertEquals($abuse->check(), true);
$status = $this->abuse->cleanup($this->getCleanupDateTime());
$this->assertEquals($status, true);
// Wait for the limit to reset
sleep(2);
// Check that there are no logs in the DB
$logs = $this->abuse->getLogs(0, 10);
$this->assertEquals(0, \count($logs));
/** Seems to be a bug in the code where if use the same adapter, it caches the result of the previous check */
$adapter = $this->getAdapter('limit-reset-{{ip}}', 100, 1);
$adapter->setParam('{{ip}}', '127.0.0.1');
$abuse = new Abuse($adapter);
$this->assertEquals($abuse->check(), false);
}
/**
* Verify that the time format is correct
*/
public function testTimeFormat(): void
{
$now = time();
$adapter = $this->getAdapter('', 1, 1);
$this->assertEquals($adapter->time(), $now);
$this->assertEquals(true, \is_int($adapter->time()));
}
}
@@ -1,10 +1,10 @@
<?php
namespace Abuse\Bench\Database;
namespace Abuse\Bench;
use PDO;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapters\Database\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit\Database as TimeLimit;
use Utopia\Cache\Adapter\None as NoCache;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MariaDB;
@@ -13,7 +13,7 @@ use Utopia\Database\Database;
use Utopia\Exception;
use Utopia\Tests\Bench\Base;
class TimeLimitBench extends Base
class DatabaseBench extends Base
{
protected Database $db;
@@ -1,13 +1,13 @@
<?php
namespace Abuse\Bench\Redis;
namespace Abuse\Bench;
use Redis as Client;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapters\Redis\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimit;
use Utopia\Tests\Bench\Base;
final class TimeLimitBench extends Base
final class RedisBench extends Base
{
protected Client $redis;
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace Abuse\Bench;
use RedisCluster as Client;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapters\TimeLimit\RedisCluster;
use Utopia\Tests\Bench\Base;
final class RedisClusterBench extends Base
{
protected Client $redis;
/**
* @throws \Exception
*/
public function setUp(): void
{
$this->redis = new Client(null, ['redis-cluster-0:6379', 'redis-cluster-1:6379', 'redis-cluster-2:6379', 'redis-cluster-3:6379']);
$this->adapter = new RedisCluster('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis);
$this->abuse = new Abuse($this->adapter);
}
}
+22 -22
View File
@@ -3,57 +3,57 @@
namespace Utopia\Tests;
use PDO;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapter;
use Utopia\Abuse\Adapters\Database\TimeLimit as AdaptersDatabase;
use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit\Database as AdapterDatabase;
use Utopia\Cache\Adapter\None as NoCache;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
use Utopia\Exception;
class DatabaseTest extends Base
{
protected Database $db;
protected static Database $db;
/**
* @throws Exception
* @throws \Exception
*/
public function setUp(): void
public static function setUpBeforeClass(): void
{
if (isset(self::$db)) {
return;
}
self::$db = self::initialiseDatabase();
}
private static function initialiseDatabase(): Database
{
// Limit login attempts to 3 time in 5 minutes time frame
$dbHost = 'mysql';
$dbUser = 'root';
$dbPort = '3306';
$dbPass = 'password';
$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, MariaDB::getPdoAttributes());
$db = new Database(new MySQL($pdo), new Cache(new NoCache()));
$db->setDatabase('utopiaTests');
$db->setNamespace('namespace');
$this->db = $db;
$adapter = new AdaptersDatabase('login-attempt-from-{{ip}}', 3, 60 * 5, $db);
if (! $db->exists('utopiaTests')) {
$adapter = new AdapterDatabase('', 1, 1, $db);
if (!$db->exists('utopiaTests')) {
$db->create();
$adapter->setup();
}
$adapter->setParam('{{ip}}', '127.0.0.1');
$this->abuse = new Abuse($adapter);
return $db;
}
public function getAdapter(string $key, int $limit, int $seconds): Adapter
public function getAdapter(string $key, int $limit, int $seconds): TimeLimit
{
return new AdaptersDatabase($key, $limit, $seconds, $this->db);
return new AdapterDatabase($key, $limit, $seconds, self::$db);
}
public function getCleanupDateTime(): string
public static function tearDownAfterClass(): void
{
return DateTime::addSeconds(new \DateTime(), -1);
if (isset(self::$db)) {
self::$db->delete();
}
}
}
+50
View File
@@ -0,0 +1,50 @@
<?php
namespace Utopia\Tests;
use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit\RedisCluster as AdapterRedisCluster;
use Utopia\Exception;
class RedisClusterTest extends Base
{
protected static \RedisCluster $redis;
/**
* @throws Exception
* @throws \Exception
*/
public static function setUpBeforeClass(): void
{
if (isset(self::$redis)) {
return;
}
self::$redis = self::initialiseRedis();
}
private static function initialiseRedis(): \RedisCluster
{
return new \RedisCluster(null, [
'redis-cluster-0:6379',
'redis-cluster-1:6379',
'redis-cluster-2:6379',
'redis-cluster-3:6379'
]);
}
public function getAdapter(string $key, int $limit, int $seconds): TimeLimit
{
return new AdapterRedisCluster($key, $limit, $seconds, self::$redis);
}
/**
* Clean up Redis connection after all tests
*/
public static function tearDownAfterClass(): void
{
if (isset(self::$redis)) {
self::$redis->close();
}
}
}
+26 -17
View File
@@ -2,38 +2,47 @@
namespace Utopia\Tests;
use DateInterval;
use Redis as Client;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapter;
use Utopia\Abuse\Adapters\Redis\TimeLimit;
use Redis;
use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit\Redis as AdapterRedis;
use Utopia\Exception;
class RedisTest extends Base
{
protected Client $redis;
protected static \Redis $redis;
/**
* @throws Exception
* @throws \Exception
*/
public function setUp(): void
public static function setUpBeforeClass(): void
{
$this->redis = new Client();
$this->redis->connect('redis', 6379);
$adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis);
$adapter->setParam('{{ip}}', '127.0.0.1');
$this->abuse = new Abuse($adapter);
if (isset(self::$redis)) {
return;
}
self::$redis = self::initialiseRedis();
}
public function getAdapter(string $key, int $limit, int $seconds): Adapter
private static function initialiseRedis(): \Redis
{
return new TimeLimit($key, $limit, $seconds, $this->redis);
$redis = new \Redis();
$redis->connect('redis', 6379);
return $redis;
}
public function getCleanupDateTime(): string
public function getAdapter(string $key, int $limit, int $seconds): TimeLimit
{
$interval = DateInterval::createFromDateString(1 . ' seconds');
return strval((new \DateTime())->sub($interval)->getTimestamp());
return new AdapterRedis($key, $limit, $seconds, self::$redis);
}
/**
* Clean up Redis connection after all tests
*/
public static function tearDownAfterClass(): void
{
if (isset(self::$redis)) {
self::$redis->close();
}
}
}