From 64b725a6f7f631a8897a4b6a1c9f6abda42bc65d Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Tue, 15 Nov 2022 10:19:35 +0000 Subject: [PATCH 1/8] Update Defaults --- app/controllers/api/projects.php | 2 +- src/Appwrite/Utopia/Response/Model/Project.php | 9 +++++---- .../e2e/Services/Projects/ProjectsConsoleClientTest.php | 9 +++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 65cfef04a..80cf55fad 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -81,7 +81,7 @@ App::post('/v1/projects') } $auth = Config::getParam('auth', []); - $auths = ['limit' => 0]; + $auths = ['limit' => 0, 'duration' => Auth::TOKEN_EXPIRATION_LOGIN_LONG]; foreach ($auth as $index => $method) { $auths[$method['key'] ?? ''] = true; } diff --git a/src/Appwrite/Utopia/Response/Model/Project.php b/src/Appwrite/Utopia/Response/Model/Project.php index a8b556193..42f360d6a 100644 --- a/src/Appwrite/Utopia/Response/Model/Project.php +++ b/src/Appwrite/Utopia/Response/Model/Project.php @@ -2,6 +2,7 @@ namespace Appwrite\Utopia\Response\Model; +use Appwrite\Auth\Auth; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model; use Utopia\Config\Config; @@ -102,10 +103,10 @@ class Project extends Model 'example' => '131102020', ]) ->addRule('authDuration', [ - 'type' => self::TYPE_STRING, + 'type' => self::TYPE_INTEGER, 'description' => 'Session duration in seconds.', - 'default' => '', - 'example' => '30', + 'default' => Auth::TOKEN_EXPIRATION_LOGIN_LONG, + 'example' => 60, ]) ->addRule('authLimit', [ 'type' => self::TYPE_INTEGER, @@ -231,7 +232,7 @@ class Project extends Model $auth = Config::getParam('auth', []); $document->setAttribute('authLimit', $authValues['limit'] ?? 0); - $document->setAttribute('authDuration', $authValues['duration'] ?? 0); + $document->setAttribute('authDuration', $authValues['duration'] ?? Auth::TOKEN_EXPIRATION_LOGIN_LONG); foreach ($auth as $index => $method) { $key = $method['key']; diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index a9bfaa965..3fd06f07e 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -417,6 +417,15 @@ class ProjectsConsoleClientTest extends Scope { $id = $data['projectId']; + // Check defaults + $response = $this->client->call(Client::METHOD_GET, '/projects/' . $id, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(Auth::TOKEN_EXPIRATION_LOGIN_LONG, $response['body']['authDuration']); // 1 Year + /** * Test for SUCCESS */ From a51288da5761c1a909a749f0236d4e7bb2df7868 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Tue, 15 Nov 2022 10:25:34 +0000 Subject: [PATCH 2/8] Update projects.php --- app/controllers/api/projects.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 80cf55fad..d1dfea724 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -522,7 +522,7 @@ App::patch('/v1/projects/:projectId/auth/duration') ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROJECT) ->param('projectId', '', new UID(), 'Project unique ID.') - ->param('duration', 525600, new Range(0, 525600), 'Project session length in minutes. Max length: 525600 minutes.') + ->param('duration', 31536000, new Range(0, 31536000), 'Project session length in seconds. Max length: 31536000 seconds.') ->inject('response') ->inject('dbForConsole') ->action(function (string $projectId, int $duration, Response $response, Database $dbForConsole) { @@ -534,7 +534,7 @@ App::patch('/v1/projects/:projectId/auth/duration') } $auths = $project->getAttribute('auths', []); - $auths['duration'] = $duration * 60; + $auths['duration'] = $duration; $dbForConsole->updateDocument('projects', $project->getId(), $project ->setAttribute('auths', $auths)); From f0052cbd8ec99b4567d6c6b1c2ce817cd379c7c1 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Tue, 15 Nov 2022 10:31:32 +0000 Subject: [PATCH 3/8] Update Tests --- .../Projects/ProjectsConsoleClientTest.php | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index 3fd06f07e..3a09ce72a 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -434,7 +434,7 @@ class ProjectsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'duration' => '1', // Set session duration to 2 minutes + 'duration' => 60, // Set session duration to 2 minutes ]); $this->assertEquals(200, $response['headers']['status-code']); @@ -484,8 +484,21 @@ class ProjectsConsoleClientTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); + // Check session doesn't expire too soon. + + sleep(30); + + // Get User + $response = $this->client->call(Client::METHOD_GET, '/account', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'Cookie' => $sessionCookie, + ])); + + $this->assertEquals(200, $response['headers']['status-code']); + // Wait just over a minute - sleep(65); + sleep(35); // Get User $response = $this->client->call(Client::METHOD_GET, '/account', array_merge([ @@ -501,7 +514,7 @@ class ProjectsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'duration' => 525600, + 'duration' => Auth::TOKEN_EXPIRATION_LOGIN_LONG, ]); $this->assertEquals(200, $response['headers']['status-code']); @@ -514,7 +527,7 @@ class ProjectsConsoleClientTest extends Scope ], $this->getHeaders())); $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(31536000, $response['body']['authDuration']); // 1 Year + $this->assertEquals(Auth::TOKEN_EXPIRATION_LOGIN_LONG, $response['body']['authDuration']); // 1 Year return ['projectId' => $projectId]; } From f5a47019441bd1992b89b61ba84f8d0f60c1d890 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Tue, 15 Nov 2022 10:38:02 +0000 Subject: [PATCH 4/8] Run Linter --- tests/e2e/Services/Projects/ProjectsConsoleClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index 3a09ce72a..cd9f032d6 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -494,7 +494,7 @@ class ProjectsConsoleClientTest extends Scope 'x-appwrite-project' => $projectId, 'Cookie' => $sessionCookie, ])); - + $this->assertEquals(200, $response['headers']['status-code']); // Wait just over a minute From 7288520f38208288ecb994b3fb4fe87587e5dc98 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 15 Nov 2022 13:30:18 +0100 Subject: [PATCH 5/8] fix: realtime console project --- app/realtime.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index 90c96de21..ac4e2d669 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -306,7 +306,7 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats, if ($realtime->hasSubscriber($projectId, 'user:' . $userId)) { $connection = array_key_first(reset($realtime->subscriptions[$projectId]['user:' . $userId])); [$consoleDatabase, $returnConsoleDatabase] = getDatabase($register, '_console'); - $project = Authorization::skip(fn() => $consoleDatabase->getDocument('projects', $projectId)); + $project = Authorization::skip(fn () => $consoleDatabase->getDocument('projects', $projectId)); [$database, $returnDatabase] = getDatabase($register, "_{$project->getInternalId()}"); $user = $database->getDocument('users', $userId); @@ -484,6 +484,7 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server, $server->onMessage(function (int $connection, string $message) use ($server, $register, $realtime, $containerId) { try { + $app = new App('UTC'); $response = new Response(new SwooleResponse()); $db = $register->get('dbPool')->get(); $redis = $register->get('redisPool')->get(); @@ -493,12 +494,8 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); $database->setNamespace("_console"); $projectId = $realtime->connections[$connection]['projectId']; - - if ($projectId !== 'console') { - $project = Authorization::skip(fn() => $database->getDocument('projects', $projectId)); - $database->setNamespace("_{$project->getInternalId()}"); - } - + $project = $projectId === 'console' ? $app->getResource('console') : Authorization::skip(fn () => $database->getDocument('projects', $projectId)); + $database->setNamespace("_{$project->getInternalId()}"); /* * Abuse Check * From 522ae8d53805368156ad1041728fdd82de872a69 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 15 Nov 2022 13:59:35 +0100 Subject: [PATCH 6/8] feat: migration for 1.1.x --- CHANGES.md | 10 +-- src/Appwrite/Migration/Migration.php | 2 +- src/Appwrite/Migration/Version/V16.php | 115 +++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 8 deletions(-) create mode 100644 src/Appwrite/Migration/Version/V16.php diff --git a/CHANGES.md b/CHANGES.md index 488a4fe83..519107cc3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,19 +1,15 @@ # Version 1.1.0 - ## Features -- Added new property to projects configuration: `sessionDuration` which allows you to alter the duration of signed in sessions for your project. [#4618](https://github.com/appwrite/appwrite/pull/4618) +- Added new property to projects configuration: `authDuration` which allows you to alter the duration of signed in sessions for your project. [#4618](https://github.com/appwrite/appwrite/pull/4618) ## Bugs - Fix license detection for Flutter and Dart SDKs [#4435](https://github.com/appwrite/appwrite/pull/4435) -- Fix missing status, buildStderr and buildStderr from get deployment response [#4611](https://github.com/appwrite/appwrite/pull/4611) +- Fix missing `status`, `buildStderr` and `buildStderr` from get deployment response [#4611](https://github.com/appwrite/appwrite/pull/4611) +- Fix project pagination in DB usage aggregation [#4517](https://github.com/appwrite/appwrite/pull/4517) # Features - Added Auth Duration API to allow users to set the duration of their sessions. [#4618](https://github.com/appwrite/appwrite/pull/4618) -# Version 1.0.4 - -- Fix project pagination in DB usage collector [#4517](https://github.com/appwrite/appwrite/pull/4517) - # Version 1.0.3 ## Bugs - Fix document audit deletion [#4429](https://github.com/appwrite/appwrite/pull/4429) diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index d3833b6e7..9c6b03391 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -45,7 +45,7 @@ abstract class Migration '1.0.0' => 'V15', '1.0.1' => 'V15', '1.0.3' => 'V15', - '1.1.0' => 'V15', + '1.1.0' => 'V16', ]; /** diff --git a/src/Appwrite/Migration/Version/V16.php b/src/Appwrite/Migration/Version/V16.php new file mode 100644 index 000000000..7bc1b418d --- /dev/null +++ b/src/Appwrite/Migration/Version/V16.php @@ -0,0 +1,115 @@ + null, + fn () => [] + ); + } + + Console::log('Migrating Project: ' . $this->project->getAttribute('name') . ' (' . $this->project->getId() . ')'); + + Console::info('Migrating Collections'); + $this->migrateCollections(); + + Console::info('Migrating Documents'); + $this->forEachDocument([$this, 'fixDocument']); + } + + /** + * Migrate all Collections. + * + * @return void + */ + protected function migrateCollections(): void + { + foreach ($this->collections as $collection) { + $id = $collection['$id']; + + Console::log("Migrating Collection \"{$id}\""); + + $this->projectDB->setNamespace("_{$this->project->getInternalId()}"); + + switch ($id) { + case 'sessions': + try { + /** + * Create 'compression' attribute + */ + $this->projectDB->deleteAttribute($id, 'expire'); + } catch (\Throwable $th) { + Console::warning("'expire' from {$id}: {$th->getMessage()}"); + } + + break; + + case 'projects': + try { + /** + * Create 'region' attribute + */ + $this->createAttributeFromCollection($this->projectDB, $id, 'region'); + } catch (\Throwable $th) { + Console::warning("'region' from {$id}: {$th->getMessage()}"); + } + + try { + /** + * Create '_key_team' index + */ + $this->createIndexFromCollection($this->projectDB, $id, '_key_team'); + } catch (\Throwable $th) { + Console::warning("'_key_team' from {$id}: {$th->getMessage()}"); + } + + default: + break; + } + + usleep(50000); + } + } + + /** + * Fix run on each document + * + * @param \Utopia\Database\Document $document + * @return \Utopia\Database\Document + */ + protected function fixDocument(Document $document) + { + switch ($document->getCollection()) { + case 'projects': + /** + * Bump version number. + */ + $document->setAttribute('version', '1.1.0'); + + /** + * Set default authDuration + */ + $document->setAttribute('auths', array_merge($document->getAttribute('auths', []), [ + 'duration' => Auth::TOKEN_EXPIRATION_LOGIN_LONG + ])); + break; + } + + return $document; + } +} From 35b7a2ee96524004656374d5149a9994693a38f0 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 15 Nov 2022 15:22:57 +0100 Subject: [PATCH 7/8] fix: linter --- app/console | 1 + phpcs.xml | 2 ++ src/Appwrite/Migration/Version/V16.php | 1 + 3 files changed, 4 insertions(+) create mode 160000 app/console diff --git a/app/console b/app/console new file mode 160000 index 000000000..0ed6e0c49 --- /dev/null +++ b/app/console @@ -0,0 +1 @@ +Subproject commit 0ed6e0c497931f16fcb0750fe351d1d3577a7d97 diff --git a/phpcs.xml b/phpcs.xml index cb31d549e..ccde5812b 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -7,6 +7,8 @@ ./app/sdks + + ./app/console * diff --git a/src/Appwrite/Migration/Version/V16.php b/src/Appwrite/Migration/Version/V16.php index 7bc1b418d..311cbaff2 100644 --- a/src/Appwrite/Migration/Version/V16.php +++ b/src/Appwrite/Migration/Version/V16.php @@ -77,6 +77,7 @@ class V16 extends Migration } catch (\Throwable $th) { Console::warning("'_key_team' from {$id}: {$th->getMessage()}"); } + break; default: break; From 10695a021a2235ca77e22f9ce4913b734d265d37 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 15 Nov 2022 15:35:42 +0100 Subject: [PATCH 8/8] fix: usage tests timeout --- tests/e2e/General/UsageTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/e2e/General/UsageTest.php b/tests/e2e/General/UsageTest.php index 572bc4abf..e5f02837b 100644 --- a/tests/e2e/General/UsageTest.php +++ b/tests/e2e/General/UsageTest.php @@ -85,7 +85,7 @@ class UsageTest extends Scope #[Retry(count: 1)] public function testUsersStats(array $data): array { - sleep(10); + sleep(20); $projectId = $data['projectId']; $headers = $data['headers']; @@ -256,7 +256,7 @@ class UsageTest extends Scope $filesCreate = $data['filesCreate']; $filesDelete = $data['filesDelete']; - sleep(10); + sleep(20); // console request $headers = [ @@ -414,7 +414,7 @@ class UsageTest extends Scope $this->assertEquals('name', $res['body']['key']); $collectionsUpdate++; $requestsCount++; - sleep(10); + sleep(20); for ($i = 0; $i < 10; $i++) { $name = uniqid() . ' collection'; @@ -496,7 +496,7 @@ class UsageTest extends Scope $documentsRead = $data['documentsRead']; $documentsDelete = $data['documentsDelete']; - sleep(10); + sleep(20); // check datbase stats $headers = [ @@ -704,7 +704,7 @@ class UsageTest extends Scope $executions = $data['executions']; $failures = $data['failures']; - sleep(10); + sleep(20); $response = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/usage', $headers, [ 'range' => '30d'