diff --git a/composer.json b/composer.json index 2d6cc7e384..055967504e 100755 --- a/composer.json +++ b/composer.json @@ -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 + } } } diff --git a/docker-compose.yml b/docker-compose.yml index 3e2b3faa35..1756697386 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: \ No newline at end of file + abuse: +volumes: + redisinsight: diff --git a/phpbench.json b/phpbench.json index adc40d1294..19e8aa0594 100644 --- a/phpbench.json +++ b/phpbench.json @@ -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" } \ No newline at end of file diff --git a/src/Abuse/Abuse.php b/src/Abuse/Abuse.php index 7386c8a4fa..948939913d 100644 --- a/src/Abuse/Abuse.php +++ b/src/Abuse/Abuse.php @@ -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); } } diff --git a/src/Abuse/Adapter.php b/src/Abuse/Adapter.php index 4af27dc40a..e96fa6f7de 100644 --- a/src/Abuse/Adapter.php +++ b/src/Abuse/Adapter.php @@ -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; } diff --git a/src/Abuse/Adapters/ReCaptcha.php b/src/Abuse/Adapters/ReCaptcha.php index ae117ac5b9..15ecbdda84 100644 --- a/src/Abuse/Adapters/ReCaptcha.php +++ b/src/Abuse/Adapters/ReCaptcha.php @@ -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'); } diff --git a/src/Abuse/Adapters/Redis/TimeLimit.php b/src/Abuse/Adapters/Redis/TimeLimit.php deleted file mode 100644 index f2349212b3..0000000000 --- a/src/Abuse/Adapters/Redis/TimeLimit.php +++ /dev/null @@ -1,139 +0,0 @@ -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 - */ - 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 $keys - * @param integer $timestamp - * @return array - */ - 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; - } -} diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index 8040dc3b9a..d0bd655a7a 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -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; } } diff --git a/src/Abuse/Adapters/Database/TimeLimit.php b/src/Abuse/Adapters/TimeLimit/Database.php similarity index 74% rename from src/Abuse/Adapters/Database/TimeLimit.php rename to src/Abuse/Adapters/TimeLimit/Database.php index 47f7e899b7..ae36f01cd0 100644 --- a/src/Abuse/Adapters/Database/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit/Database.php @@ -1,8 +1,8 @@ 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 $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)); + } } diff --git a/src/Abuse/Adapters/TimeLimit/Redis.php b/src/Abuse/Adapters/TimeLimit/Redis.php new file mode 100644 index 0000000000..19602613a2 --- /dev/null +++ b/src/Abuse/Adapters/TimeLimit/Redis.php @@ -0,0 +1,116 @@ +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 + */ + 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; + } +} diff --git a/src/Abuse/Adapters/TimeLimit/RedisCluster.php b/src/Abuse/Adapters/TimeLimit/RedisCluster.php new file mode 100644 index 0000000000..6acc99a8f4 --- /dev/null +++ b/src/Abuse/Adapters/TimeLimit/RedisCluster.php @@ -0,0 +1,133 @@ +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 + */ + 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; + } +} diff --git a/tests/Abuse/Base.php b/tests/Abuse/Base.php index 1b88f411f6..5cb59c7da7 100644 --- a/tests/Abuse/Base.php +++ b/tests/Abuse/Base.php @@ -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())); } } diff --git a/tests/Abuse/Bench/Database/TimeLimitBench.php b/tests/Abuse/Bench/Database.php similarity index 91% rename from tests/Abuse/Bench/Database/TimeLimitBench.php rename to tests/Abuse/Bench/Database.php index e2a110c6b7..a24c9ed4a7 100644 --- a/tests/Abuse/Bench/Database/TimeLimitBench.php +++ b/tests/Abuse/Bench/Database.php @@ -1,10 +1,10 @@ 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); + } +} diff --git a/tests/Abuse/DatabaseTest.php b/tests/Abuse/DatabaseTest.php index b3820e6aa4..58356b2fe1 100755 --- a/tests/Abuse/DatabaseTest.php +++ b/tests/Abuse/DatabaseTest.php @@ -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(); + } } } diff --git a/tests/Abuse/RedisClusterTest.php b/tests/Abuse/RedisClusterTest.php new file mode 100644 index 0000000000..e1029f946a --- /dev/null +++ b/tests/Abuse/RedisClusterTest.php @@ -0,0 +1,50 @@ +close(); + } + } +} diff --git a/tests/Abuse/RedisTest.php b/tests/Abuse/RedisTest.php index 517ee94777..085da1e4fa 100644 --- a/tests/Abuse/RedisTest.php +++ b/tests/Abuse/RedisTest.php @@ -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(); + } } }