diff --git a/.env b/.env index 8ff8164a21..28477256ac 100644 --- a/.env +++ b/.env @@ -107,4 +107,4 @@ _APP_MESSAGE_SMS_TEST_DSN= _APP_MESSAGE_EMAIL_TEST_DSN= _APP_MESSAGE_PUSH_TEST_DSN= _APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 -_APP_PROJECT_REGIONS=default +_APP_PROJECT_REGIONS=default \ No newline at end of file diff --git a/app/console b/app/console new file mode 160000 index 0000000000..0959b594b3 --- /dev/null +++ b/app/console @@ -0,0 +1 @@ +Subproject commit 0959b594b32f176819d4afb3a769afea212db789 diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 5dc67677ac..fe5640d940 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -250,7 +250,7 @@ App::post('/v1/functions') $schedule = Authorization::skip( fn () => $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), // Todo replace with projects region + 'region' => $project->getAttribute('region'), 'resourceType' => 'function', 'resourceId' => $function->getId(), 'resourceInternalId' => $function->getInternalId(), @@ -1957,7 +1957,7 @@ App::post('/v1/functions/:functionId/executions') ]; $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), + 'region' => $project->getAttribute('region'), 'resourceType' => ScheduleExecutions::getSupportedResource(), 'resourceId' => $execution->getId(), 'resourceInternalId' => $execution->getInternalId(), diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index e4a627d027..57d3a1ea61 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -2743,7 +2743,7 @@ App::post('/v1/messaging/messages/email') break; case MessageStatus::SCHEDULED: $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), + 'region' => $project->getAttribute('region'), 'resourceType' => 'message', 'resourceId' => $message->getId(), 'resourceInternalId' => $message->getInternalId(), @@ -2860,7 +2860,7 @@ App::post('/v1/messaging/messages/sms') break; case MessageStatus::SCHEDULED: $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), + 'region' => $project->getAttribute('region'), 'resourceType' => 'message', 'resourceId' => $message->getId(), 'resourceInternalId' => $message->getInternalId(), @@ -3757,7 +3757,7 @@ App::patch('/v1/messaging/messages/push/:messageId') if (\is_null($currentScheduledAt) && !\is_null($scheduledAt)) { $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => System::getEnv('_APP_REGION', 'default'), + 'region' => $project->getAttribute('region'), 'resourceType' => 'message', 'resourceId' => $message->getId(), 'resourceInternalId' => $message->getInternalId(), diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 5f185c091a..6399a5e61e 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -128,6 +128,15 @@ App::post('/v1/projects') $databases = Config::getParam('pools-database', []); + + if ($region !== 'default') { + $databaseKeys = System::getEnv('_APP_DATABASE_KEYS', ''); + $keys = explode(',', $databaseKeys); + $databases = array_filter($keys, function ($value) use ($region) { + return str_contains($value, $region); + }); + } + $databaseOverride = System::getEnv('_APP_DATABASE_OVERRIDE'); $index = \array_search($databaseOverride, $databases); if ($index !== false) { @@ -197,12 +206,15 @@ App::post('/v1/projects') $sharedTables = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES', '')); $sharedTablesV1 = \explode(',', System::getEnv('_APP_DATABASE_SHARED_TABLES_V1', '')); - $projectTables = !\in_array($dsn->getHost(), $sharedTables); $sharedTablesV1 = \in_array($dsn->getHost(), $sharedTablesV1); $sharedTablesV2 = !$projectTables && !$sharedTablesV1; $sharedTables = $sharedTablesV1 || $sharedTablesV2; - + var_dump($dsn->getHost()); + var_dump($sharedTables); + var_dump($projectTables); + var_dump($sharedTablesV1); + var_dump($sharedTablesV2); if (!$sharedTablesV2) { $adapter = $pools->get($dsn->getHost())->pop()->getResource(); $dbForProject = new Database($adapter, $cache); @@ -227,6 +239,8 @@ App::post('/v1/projects') $create = false; } + + if ($create || $projectTables) { $audit = new Audit($dbForProject); $audit->setup(); diff --git a/app/controllers/general.php b/app/controllers/general.php index 4c4761bba9..d223594cf5 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -449,7 +449,7 @@ App::init() }); App::init() - ->groups(['api', 'web']) + ->groups(['api']) ->inject('utopia') ->inject('swooleRequest') ->inject('request') diff --git a/app/init.php b/app/init.php index 047e9402b6..97efbcf134 100644 --- a/app/init.php +++ b/app/init.php @@ -1334,6 +1334,10 @@ App::setResource('project', function ($dbForConsole, $request, $console) { $project = Authorization::skip(fn () => $dbForConsole->getDocument('projects', $projectId)); + 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'); + } + return $project; }, ['dbForConsole', 'request', 'console']); @@ -1397,6 +1401,7 @@ App::setResource('console', function () { 'githubSecret' => System::getEnv('_APP_CONSOLE_GITHUB_SECRET', ''), 'githubAppid' => System::getEnv('_APP_CONSOLE_GITHUB_APP_ID', '') ], + 'region' => System::getEnv('_APP_REGION', 'default') ]); }, []); diff --git a/composer.lock b/composer.lock index 87d8b47583..bec97d88c3 100644 --- a/composer.lock +++ b/composer.lock @@ -8557,7 +8557,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": {}, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { @@ -8581,5 +8581,5 @@ "platform-overrides": { "php": "8.3" }, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.2.0" } diff --git a/docker-compose.yml b/docker-compose.yml index bae2cc7811..6c9f79520d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -195,6 +195,7 @@ services: - _APP_DATABASE_SHARED_TABLES - _APP_DATABASE_SHARED_TABLES_V1 - _APP_DATABASE_SHARED_NAMESPACE + - _APP_DATABASE_KEYS appwrite-console: <<: *x-logging