From 5f29af2c271d63f1320d84cfb2384d4fc01a1bd3 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 8 Nov 2024 22:08:13 +0100 Subject: [PATCH 1/7] feat: use getDocument instead of find() for rules --- app/controllers/api/functions.php | 2 +- app/controllers/api/proxy.php | 2 +- app/controllers/general.php | 11 ++++------- src/Appwrite/Migration/Version/V19.php | 1 + src/Appwrite/Platform/Workers/Certificates.php | 4 +--- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 0ca73ee6ac..2309defe5a 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -326,9 +326,9 @@ App::post('/v1/functions') $functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS', ''); if (!empty($functionsDomain)) { - $ruleId = ID::unique(); $routeSubdomain = ID::unique(); $domain = "{$routeSubdomain}.{$functionsDomain}"; + $ruleId = md5($domain); $rule = Authorization::skip( fn () => $dbForConsole->createDocument('rules', new Document([ diff --git a/app/controllers/api/proxy.php b/app/controllers/api/proxy.php index 02a3ec8e9d..9860bd8276 100644 --- a/app/controllers/api/proxy.php +++ b/app/controllers/api/proxy.php @@ -103,7 +103,7 @@ App::post('/v1/proxy/rules') throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); } - $ruleId = ID::unique(); + $ruleId = md5($domain->get()); $rule = new Document([ '$id' => $ruleId, 'projectId' => $project->getId(), diff --git a/app/controllers/general.php b/app/controllers/general.php index b08ecb3d12..908ffdcb71 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -52,14 +52,10 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo $host = $request->getHostname() ?? ''; - $route = Authorization::skip( - fn () => $dbForConsole->find('rules', [ - Query::equal('domain', [$host]), - Query::limit(1) - ]) - )[0] ?? null; + $ruleId = md5($host); + $route = Authorization::skip(fn () => $dbForConsole->getDocument('rules', $ruleId)); - if ($route === null) { + if ($route->isEmpty()) { if ($host === System::getEnv('_APP_DOMAIN_FUNCTIONS', '')) { throw new AppwriteException(AppwriteException::GENERAL_ACCESS_FORBIDDEN, 'This domain cannot be used for security reasons. Please use any subdomain instead.'); } @@ -531,6 +527,7 @@ App::init() if ($domainDocument->isEmpty()) { $domainDocument = new Document([ + '$id' => md5($domain->get()), 'domain' => $domain->get(), 'resourceType' => 'api', 'status' => 'verifying', diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index 18234ebdc4..2546ae9baf 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -72,6 +72,7 @@ class V19 extends Migration } $ruleDocument = new Document([ + '$id' => md5($domain->getAttribute('domain')), 'projectId' => $domain->getAttribute('projectId'), 'projectInternalId' => $domain->getAttribute('projectInternalId'), 'domain' => $domain->getAttribute('domain'), diff --git a/src/Appwrite/Platform/Workers/Certificates.php b/src/Appwrite/Platform/Workers/Certificates.php index a14f164295..21d967d9e1 100644 --- a/src/Appwrite/Platform/Workers/Certificates.php +++ b/src/Appwrite/Platform/Workers/Certificates.php @@ -478,9 +478,7 @@ class Certificates extends Action private function updateDomainDocuments(string $certificateId, string $domain, bool $success, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions): void { - $rule = $dbForConsole->findOne('rules', [ - Query::equal('domain', [$domain]), - ]); + $rule = $dbForConsole->getDocument('rules', md5($domain)); if (!$rule->isEmpty()) { $rule->setAttribute('certificateId', $certificateId); From 6ae9952fa64b105e6881459e89e2e82cb97ae205 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 8 Nov 2024 22:18:05 +0100 Subject: [PATCH 2/7] chore: linter --- app/controllers/api/proxy.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/api/proxy.php b/app/controllers/api/proxy.php index 9860bd8276..e3bef01417 100644 --- a/app/controllers/api/proxy.php +++ b/app/controllers/api/proxy.php @@ -11,7 +11,6 @@ use Utopia\App; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Query as QueryException; -use Utopia\Database\Helpers\ID; use Utopia\Database\Query; use Utopia\Database\Validator\Query\Cursor; use Utopia\Database\Validator\UID; From e4cd914d574ee38375200c262277ee88a5004cc5 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 9 Nov 2024 11:34:56 +0100 Subject: [PATCH 3/7] feat: downgrade db library --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5c2441fd1b..a04ca51d43 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "utopia-php/cache": "0.11.*", "utopia-php/cli": "0.15.*", "utopia-php/config": "0.2.*", - "utopia-php/database": "0.53.19", + "utopia-php/database": "0.53.16", "utopia-php/domains": "0.5.*", "utopia-php/dsn": "0.2.1", "utopia-php/framework": "0.33.*", From e04551ab6e380d90f64ea8cae9d03a5a758728e7 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 9 Nov 2024 11:43:52 +0100 Subject: [PATCH 4/7] feat: adjust more proxy queries --- app/controllers/api/proxy.php | 5 ++--- app/controllers/general.php | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/proxy.php b/app/controllers/api/proxy.php index e3bef01417..56fd31e88c 100644 --- a/app/controllers/api/proxy.php +++ b/app/controllers/api/proxy.php @@ -59,9 +59,8 @@ App::post('/v1/proxy/rules') throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please pick another one.'); } - $document = $dbForConsole->findOne('rules', [ - Query::equal('domain', [$domain]), - ]); + $ruleId = md5($domain); + $document = $dbForConsole->getDocument('rules', $ruleId); if (!$document->isEmpty()) { if ($document->getAttribute('projectId') === $project->getId()) { diff --git a/app/controllers/general.php b/app/controllers/general.php index 908ffdcb71..35a55d195f 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -521,9 +521,7 @@ App::init() if ($mainDomain !== $domain->get()) { Console::warning($domain->get() . ' is not a main domain. Skipping SSL certificate generation.'); } else { - $domainDocument = $dbForConsole->findOne('rules', [ - Query::equal('domain', [$domain->get()]) - ]); + $domainDocument = $dbForConsole->getDocument('rules', md5($domain->get())); if ($domainDocument->isEmpty()) { $domainDocument = new Document([ From e8ddda079d01e9fe65e03d8f6bb80f4af374b468 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 9 Nov 2024 16:13:44 +0100 Subject: [PATCH 5/7] chore: update more find methods --- app/controllers/general.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 35a55d195f..b4ff3a1de4 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -514,7 +514,7 @@ App::init() if (!empty($envDomain) && $envDomain !== 'localhost') { $mainDomain = $envDomain; } else { - $domainDocument = $dbForConsole->findOne('rules', [Query::orderAsc('$id')]); + $domainDocument = $dbForConsole->getDocument('rules', md5($envDomain)); $mainDomain = !$domainDocument->isEmpty() ? $domainDocument->getAttribute('domain') : $domain->get(); } From 6c7f484383229cd62bde3016db3f3a99f1bc3ddf Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 9 Nov 2024 16:18:22 +0100 Subject: [PATCH 6/7] chore: update more find methods --- app/controllers/general.php | 3 +-- src/Appwrite/Migration/Version/V19.php | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index b4ff3a1de4..7cfee0e2f4 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -52,8 +52,7 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo $host = $request->getHostname() ?? ''; - $ruleId = md5($host); - $route = Authorization::skip(fn () => $dbForConsole->getDocument('rules', $ruleId)); + $route = Authorization::skip(fn () => $dbForConsole->getDocument('rules', md5($host))); if ($route->isEmpty()) { if ($host === System::getEnv('_APP_DOMAIN_FUNCTIONS', '')) { diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index 2546ae9baf..18234ebdc4 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -72,7 +72,6 @@ class V19 extends Migration } $ruleDocument = new Document([ - '$id' => md5($domain->getAttribute('domain')), 'projectId' => $domain->getAttribute('projectId'), 'projectInternalId' => $domain->getAttribute('projectInternalId'), 'domain' => $domain->getAttribute('domain'), From ba91d90c40fa1709ecd3f9a5ad076b856217e335 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 9 Nov 2024 16:31:33 +0100 Subject: [PATCH 7/7] chore: linter --- app/controllers/general.php | 1 - composer.lock | 28 ++++++++++++++-------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 7cfee0e2f4..663242882a 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -29,7 +29,6 @@ use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; -use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Domains\Domain; use Utopia\DSN\DSN; diff --git a/composer.lock b/composer.lock index 097cee9868..62a7a755f2 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "5be1b916c221b97b77b0e7f14491aabf", + "content-hash": "b358198535c1867eabed7c0f99135a57", "packages": [ { "name": "adhocore/jwt", @@ -1770,16 +1770,16 @@ }, { "name": "utopia-php/database", - "version": "0.53.19", + "version": "0.53.16", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "48951885f2787df30ad8581a0e94423558619daa" + "reference": "6661edffeef05b59e16d102b989a72f7f78cf7de" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/48951885f2787df30ad8581a0e94423558619daa", - "reference": "48951885f2787df30ad8581a0e94423558619daa", + "url": "https://api.github.com/repos/utopia-php/database/zipball/6661edffeef05b59e16d102b989a72f7f78cf7de", + "reference": "6661edffeef05b59e16d102b989a72f7f78cf7de", "shasum": "" }, "require": { @@ -1820,9 +1820,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.53.19" + "source": "https://github.com/utopia-php/database/tree/0.53.16" }, - "time": "2024-11-08T07:00:24+00:00" + "time": "2024-11-06T03:07:16+00:00" }, { "name": "utopia-php/domains", @@ -4115,23 +4115,23 @@ }, { "name": "phpdocumentor/type-resolver", - "version": "1.9.0", + "version": "1.10.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "1fb5ba8d045f5dd984ebded5b1cc66f29459422d" + "reference": "679e3ce485b99e84c775d28e2e96fade9a7fb50a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/1fb5ba8d045f5dd984ebded5b1cc66f29459422d", - "reference": "1fb5ba8d045f5dd984ebded5b1cc66f29459422d", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/679e3ce485b99e84c775d28e2e96fade9a7fb50a", + "reference": "679e3ce485b99e84c775d28e2e96fade9a7fb50a", "shasum": "" }, "require": { "doctrine/deprecations": "^1.0", "php": "^7.3 || ^8.0", "phpdocumentor/reflection-common": "^2.0", - "phpstan/phpdoc-parser": "^1.18" + "phpstan/phpdoc-parser": "^1.18|^2.0" }, "require-dev": { "ext-tokenizer": "*", @@ -4167,9 +4167,9 @@ "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", "support": { "issues": "https://github.com/phpDocumentor/TypeResolver/issues", - "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.9.0" + "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.10.0" }, - "time": "2024-11-03T20:11:34+00:00" + "time": "2024-11-09T15:12:26+00:00" }, { "name": "phpspec/prophecy",