1
0
Fork 0
mirror of synced 2024-06-03 03:14:50 +12:00

audit + abuse datetime changes

This commit is contained in:
fogelito 2022-07-19 17:52:06 +03:00
parent bded3da5df
commit 169fb4d6f4
6 changed files with 53 additions and 38 deletions

View file

@ -59,13 +59,16 @@ App::init(function (App $utopia, Request $request, Response $response, Document
}
$abuse = new Abuse($timeLimit);
$remaining = $timeLimit->remaining();
$limit = $timeLimit->limit();
$time = (new DateTime($timeLimit->time()))->getTimestamp() + $route->getLabel('abuse-time', 3600);
if ($timeLimit->limit() && ($timeLimit->remaining() < $closestLimit || is_null($closestLimit))) {
$closestLimit = $timeLimit->remaining();
if ($limit && ($remaining < $closestLimit || is_null($closestLimit))) {
$closestLimit = $remaining;
$response
->addHeader('X-RateLimit-Limit', $timeLimit->limit())
->addHeader('X-RateLimit-Remaining', $timeLimit->remaining())
->addHeader('X-RateLimit-Reset', $timeLimit->time() + $route->getLabel('abuse-time', 3600))
->addHeader('X-RateLimit-Limit', $limit)
->addHeader('X-RateLimit-Remaining', $remaining)
->addHeader('X-RateLimit-Reset', $time)
;
}
@ -161,7 +164,6 @@ App::init(function (App $utopia, Request $request, Document $project) {
default:
throw new Exception('Unsupported authentication route', 501, Exception::USER_AUTH_METHOD_UNSUPPORTED);
break;
}
}, ['utopia', 'request', 'project'], 'auth');

View file

@ -315,8 +315,7 @@ class DeletesV1 extends Worker
$dbForProject = $this->getProjectDB($projectId);
$timeLimit = new TimeLimit("", 0, 1, $dbForProject);
$abuse = new Abuse($timeLimit);
$timestamp = (new \DateTime($datetime))->getTimestamp(); //todo:make abuse get datetime
$status = $abuse->cleanup($timestamp);
$status = $abuse->cleanup($datetime);
if (!$status) {
throw new Exception('Failed to delete Abuse logs for project ' . $projectId);
}
@ -336,8 +335,7 @@ class DeletesV1 extends Worker
$this->deleteForProjectIds(function (string $projectId) use ($datetime) {
$dbForProject = $this->getProjectDB($projectId);
$audit = new Audit($dbForProject);
$timestamp = (new \DateTime($datetime))->getTimestamp(); //todo:make audit get datetime
$status = $audit->cleanup($timestamp);
$status = $audit->cleanup($datetime);
if (!$status) {
throw new Exception('Failed to delete Audit logs for project' . $projectId);
}

View file

@ -44,9 +44,9 @@
"appwrite/php-runtimes": "0.10.*",
"utopia-php/framework": "0.19.*",
"utopia-php/logger": "0.3.*",
"utopia-php/abuse": "0.7.*",
"utopia-php/abuse": "dev-origin/timestamp-to-datetime as 0.7.2",
"utopia-php/analytics": "0.2.*",
"utopia-php/audit": "0.8.*",
"utopia-php/audit": "dev-origin/unix-to-datetime as 0.8.2",
"utopia-php/cache": "0.6.*",
"utopia-php/cli": "0.12.*",
"utopia-php/config": "0.2.*",

55
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "bb684eecf9a077098f34b678042cbbd8",
"content-hash": "ee1576a5196e871bbb6e4c5e9216798a",
"packages": [
{
"name": "adhocore/jwt",
@ -1733,22 +1733,23 @@
},
{
"name": "utopia-php/abuse",
"version": "0.7.0",
"version": "dev-origin/timestamp-to-datetime",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/abuse.git",
"reference": "52fb20e39e2e9619948bc0a73b52e10caa71350d"
"reference": "8fc98103195ad014a66388e0986da78fbd65a8fd"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/abuse/zipball/52fb20e39e2e9619948bc0a73b52e10caa71350d",
"reference": "52fb20e39e2e9619948bc0a73b52e10caa71350d",
"url": "https://api.github.com/repos/utopia-php/abuse/zipball/8fc98103195ad014a66388e0986da78fbd65a8fd",
"reference": "8fc98103195ad014a66388e0986da78fbd65a8fd",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-pdo": "*",
"php": ">=8.0",
"utopia-php/database": ">=0.11 <1.0"
"utopia-php/database": "dev-feat-attr-datetime as 0.18.7"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
@ -1780,9 +1781,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/abuse/issues",
"source": "https://github.com/utopia-php/abuse/tree/0.7.0"
"source": "https://github.com/utopia-php/abuse/tree/origin/timestamp-to-datetime"
},
"time": "2021-12-27T13:06:45+00:00"
"time": "2022-07-19T10:31:15+00:00"
},
{
"name": "utopia-php/analytics",
@ -1841,22 +1842,22 @@
},
{
"name": "utopia-php/audit",
"version": "0.8.0",
"version": "dev-origin/unix-to-datetime",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/audit.git",
"reference": "b46dc42614a69437c45eb229249b6a6d000122c1"
"reference": "eb77d1d12f06c4faa8b5e19812dbebbbe59248fa"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/audit/zipball/b46dc42614a69437c45eb229249b6a6d000122c1",
"reference": "b46dc42614a69437c45eb229249b6a6d000122c1",
"url": "https://api.github.com/repos/utopia-php/audit/zipball/eb77d1d12f06c4faa8b5e19812dbebbbe59248fa",
"reference": "eb77d1d12f06c4faa8b5e19812dbebbbe59248fa",
"shasum": ""
},
"require": {
"ext-pdo": "*",
"php": ">=8.0",
"utopia-php/database": ">=0.11 <1.0"
"utopia-php/database": "dev-feat-attr-datetime as 0.18.7"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
@ -1888,9 +1889,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/audit/issues",
"source": "https://github.com/utopia-php/audit/tree/0.8.0"
"source": "https://github.com/utopia-php/audit/tree/origin/unix-to-datetime"
},
"time": "2021-12-27T13:05:56+00:00"
"time": "2022-07-19T10:12:18+00:00"
},
{
"name": "utopia-php/cache",
@ -2055,12 +2056,12 @@
"source": {
"type": "git",
"url": "https://github.com/utopia-php/database.git",
"reference": "c6f3671766127aa8c8bef853494a4059d3076b20"
"reference": "407c0e95a4f5cf9b8932dc5b314a93b634339c09"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/database/zipball/c6f3671766127aa8c8bef853494a4059d3076b20",
"reference": "c6f3671766127aa8c8bef853494a4059d3076b20",
"url": "https://api.github.com/repos/utopia-php/database/zipball/407c0e95a4f5cf9b8932dc5b314a93b634339c09",
"reference": "407c0e95a4f5cf9b8932dc5b314a93b634339c09",
"shasum": ""
},
"require": {
@ -2111,7 +2112,7 @@
"issues": "https://github.com/utopia-php/database/issues",
"source": "https://github.com/utopia-php/database/tree/feat-attr-datetime"
},
"time": "2022-07-13T13:01:13+00:00"
"time": "2022-07-19T10:06:50+00:00"
},
{
"name": "utopia-php/domains",
@ -5347,6 +5348,18 @@
}
],
"aliases": [
{
"package": "utopia-php/abuse",
"version": "dev-origin/timestamp-to-datetime",
"alias": "0.7.2",
"alias_normalized": "0.7.2.0"
},
{
"package": "utopia-php/audit",
"version": "dev-origin/unix-to-datetime",
"alias": "0.8.2",
"alias_normalized": "0.8.2.0"
},
{
"package": "utopia-php/database",
"version": "dev-feat-attr-datetime",
@ -5356,6 +5369,8 @@
],
"minimum-stability": "stable",
"stability-flags": {
"utopia-php/abuse": 20,
"utopia-php/audit": 20,
"utopia-php/database": 20
},
"prefer-stable": false,
@ -5379,5 +5394,5 @@
"platform-overrides": {
"php": "8.0"
},
"plugin-api-version": "2.3.0"
"plugin-api-version": "2.2.0"
}

View file

@ -47,10 +47,10 @@ class Log extends Model
'example' => '127.0.0.1',
])
->addRule('time', [
'type' => self::TYPE_INTEGER,
'description' => 'Log creation time in Unix timestamp.',
'default' => 0,
'example' => 1592981250,
'type' => self::TYPE_DATETIME,
'description' => get_class() . ' creation date in Datetime.',
'default' => '',
'example' => '1975-12-06 13:30:59',
])
->addRule('osCode', [
'type' => self::TYPE_STRING,

View file

@ -341,7 +341,7 @@ trait AccountBase
$this->assertIsNumeric($response['body']['total']);
$this->assertContains($response['body']['logs'][1]['event'], ["users.{$userId}.create", "users.{$userId}.sessions.{$sessionId}.create"]);
$this->assertEquals($response['body']['logs'][1]['ip'], filter_var($response['body']['logs'][1]['ip'], FILTER_VALIDATE_IP));
$this->assertIsNumeric($response['body']['logs'][1]['time']);
$this->assertEquals(true, DateTime::isValid($response['body']['logs'][1]['time']));
$this->assertEquals('Windows', $response['body']['logs'][1]['osName']);
$this->assertEquals('WIN', $response['body']['logs'][1]['osCode']);
@ -363,7 +363,7 @@ trait AccountBase
$this->assertContains($response['body']['logs'][2]['event'], ["users.{$userId}.create", "users.{$userId}.sessions.{$sessionId}.create"]);
$this->assertEquals($response['body']['logs'][2]['ip'], filter_var($response['body']['logs'][2]['ip'], FILTER_VALIDATE_IP));
$this->assertIsNumeric($response['body']['logs'][2]['time']);
$this->assertEquals(true, DateTime::isValid($response['body']['logs'][2]['time']));
$this->assertEquals('Windows', $response['body']['logs'][2]['osName']);
$this->assertEquals('WIN', $response['body']['logs'][2]['osCode']);