From 0e038421736013e4edb70ade8a8b5cef609eb7e3 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 25 Nov 2024 17:20:27 +1300 Subject: [PATCH 1/3] Move pool access inside not V2 block --- app/controllers/api/projects.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 92d3103293..5f185c091a 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -195,8 +195,6 @@ App::post('/v1/projects') $dsn = new DSN('mysql://' . $dsn); } - $adapter = $pools->get($dsn->getHost())->pop()->getResource(); - $dbForProject = new Database($adapter, $cache); $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); $sharedTablesV1 = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES_V1', '')); @@ -206,6 +204,9 @@ App::post('/v1/projects') $sharedTables = $sharedTablesV1 || $sharedTablesV2; if (!$sharedTablesV2) { + $adapter = $pools->get($dsn->getHost())->pop()->getResource(); + $dbForProject = new Database($adapter, $cache); + if ($sharedTables) { $dbForProject ->setSharedTables(true) From 928590b1750c099b4ba7293c7c2d3f5cf2a2cdc7 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 Nov 2024 16:20:11 +1300 Subject: [PATCH 2/3] Only create databases for current region --- app/http.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/http.php b/app/http.php index c80fccee54..e276dac519 100644 --- a/app/http.php +++ b/app/http.php @@ -264,9 +264,15 @@ $http->on(Constant::EVENT_START, function (Server $http) use ($payloadSize, $reg $sharedTablesV1 = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES_V1', '')); $sharedTablesV2 = \array_diff($sharedTables, $sharedTablesV1); + $region = System::getEnv('_APP_REGION', 'fra'); $cache = $app->getResource('cache'); - foreach ($sharedTablesV2 as $hostname) { + $regionDatabases = \array_filter( + $sharedTablesV2, + fn ($hostname) => \str_contains($hostname, $region) + ); + + foreach ($regionDatabases as $hostname) { $adapter = $pools ->get($hostname) ->pop() From ddb86127b7bcc44e8f761349a72d4aa9f09985e2 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 Nov 2024 18:29:10 +1300 Subject: [PATCH 3/3] Fix lint --- app/controllers/api/projects.php | 2 +- app/init.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 6399a5e61e..45799ce86f 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -133,7 +133,7 @@ App::post('/v1/projects') $databaseKeys = System::getEnv('_APP_DATABASE_KEYS', ''); $keys = explode(',', $databaseKeys); $databases = array_filter($keys, function ($value) use ($region) { - return str_contains($value, $region); + return str_contains($value, $region); }); } diff --git a/app/init.php b/app/init.php index 97efbcf134..de4a411933 100644 --- a/app/init.php +++ b/app/init.php @@ -1334,7 +1334,7 @@ App::setResource('project', function ($dbForConsole, $request, $console) { $project = Authorization::skip(fn () => $dbForConsole->getDocument('projects', $projectId)); - if($project->getAttribute('region') !== System::getEnv('_APP_REGION')){ + if ($project->getAttribute('region') !== System::getEnv('_APP_REGION')) { throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Project is not accessible in this region. Please make sure you are using the correct endpoint'); }