From 5747b33e38ba9df804dba3bd2355139f9a2d1d4d Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 20 Feb 2024 23:15:46 +1300 Subject: [PATCH] Make self-hosted and cloud specs consistent --- .env | 3 +- app/config/errors.php | 5 +++ app/config/regions.php | 71 +++++++++++++++++++++++++++++-- app/config/runtimes.php | 11 +---- app/controllers/api/functions.php | 8 ++++ app/controllers/api/projects.php | 9 +++- src/Appwrite/Extend/Exception.php | 2 + 7 files changed, 95 insertions(+), 14 deletions(-) diff --git a/.env b/.env index 0b5ddf24c6..f89386b5cf 100644 --- a/.env +++ b/.env @@ -102,4 +102,5 @@ _APP_ASSISTANT_OPENAI_API_KEY= _APP_MESSAGE_SMS_TEST_DSN= _APP_MESSAGE_EMAIL_TEST_DSN= _APP_MESSAGE_PUSH_TEST_DSN= -_APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 \ No newline at end of file +_APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 +_APP_PROJECT_REGIONS=default \ No newline at end of file diff --git a/app/config/errors.php b/app/config/errors.php index 3f12b5953a..c0efc2097d 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -718,6 +718,11 @@ return [ 'description' => 'You can\'t delete default template. If you are trying to reset your template changes, you can ignore this error as it\'s already been reset.', 'code' => 401, ], + Exception::PROJECT_REGION_UNSUPPORTED => [ + 'name' => Exception::PROJECT_REGION_UNSUPPORTED, + 'description' => 'The requested region is either inactive or unsupported. Please check the value of the _APP_REGIONS environment variable.', + 'code' => 400, + ], Exception::WEBHOOK_NOT_FOUND => [ 'name' => Exception::WEBHOOK_NOT_FOUND, 'description' => 'Webhook with the requested ID could not be found.', diff --git a/app/config/regions.php b/app/config/regions.php index 0dc5fab1ed..b40667ab5e 100644 --- a/app/config/regions.php +++ b/app/config/regions.php @@ -2,8 +2,73 @@ return [ 'default' => [ - 'name' => 'Default', - 'default' => true, + '$id' => 'default', + 'name' => 'Frankfurt', 'disabled' => false, - ] + 'flag' => 'de', + 'default' => true, + ], + 'fra' => [ + '$id' => 'fra', + 'name' => 'Frankfurt', + 'disabled' => false, + 'flag' => 'de', + 'default' => true, + ], + 'nyc' => [ + '$id' => 'nyc', + 'name' => 'New York', + 'disabled' => true, + 'flag' => 'us', + 'default' => true, + ], + 'sfo' => [ + '$id' => 'sfo', + 'name' => 'San Francisco', + 'disabled' => true, + 'flag' => 'us', + 'default' => true, + ], + 'blr' => [ + '$id' => 'blr', + 'name' => 'Banglore', + 'disabled' => true, + 'flag' => 'in', + 'default' => true, + ], + 'lon' => [ + '$id' => 'lon', + 'name' => 'London', + 'disabled' => true, + 'flag' => 'gb', + 'default' => true, + ], + 'ams' => [ + '$id' => 'ams', + 'name' => 'Amsterdam', + 'disabled' => true, + 'flag' => 'nl', + 'default' => true, + ], + 'sgp' => [ + '$id' => 'sgp', + 'name' => 'Singapore', + 'disabled' => true, + 'flag' => 'sg', + 'default' => true, + ], + 'tor' => [ + '$id' => 'tor', + 'name' => 'Toronto', + 'disabled' => true, + 'flag' => 'ca', + 'default' => true, + ], + 'syd' => [ + '$id' => 'syd', + 'name' => 'Sydney', + 'disabled' => true, + 'flag' => 'au', + 'default' => true, + ], ]; diff --git a/app/config/runtimes.php b/app/config/runtimes.php index 2cd73c1b70..a55e0b3fb4 100644 --- a/app/config/runtimes.php +++ b/app/config/runtimes.php @@ -1,16 +1,9 @@ getAll(true, $allowList); - -return $runtimes; +return (new Runtimes('v3'))->getAll(); diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 21d5928267..11eaa49393 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -172,6 +172,14 @@ App::post('/v1/functions') ->action(function (string $functionId, string $name, string $runtime, array $execute, array $events, string $schedule, int $timeout, bool $enabled, bool $logging, string $entrypoint, string $commands, string $installationId, string $providerRepositoryId, string $providerBranch, bool $providerSilentMode, string $providerRootDirectory, string $templateRepository, string $templateOwner, string $templateRootDirectory, string $templateBranch, Request $request, Response $response, Database $dbForProject, Document $project, Document $user, Event $queueForEvents, Build $queueForBuilds, Database $dbForConsole, GitHub $github) use ($redeployVcs) { $functionId = ($functionId == 'unique()') ? ID::unique() : $functionId; + $allowList = empty(App::getEnv('_APP_FUNCTIONS_RUNTIMES')) + ? [] + : \explode(',', App::getEnv('_APP_FUNCTIONS_RUNTIMES')); + + if (!empty($allowList) && !\in_array($runtime, $allowList)) { + throw new Exception(Exception::FUNCTION_RUNTIME_UNSUPPORTED, 'Runtime "' . $runtime . '" is not supported'); + } + // build from template $template = new Document([]); if ( diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index bea893064f..428e97ff50 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -78,13 +78,20 @@ App::post('/v1/projects') ->inject('pools') ->action(function (string $projectId, string $name, string $teamId, string $region, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Response $response, Database $dbForConsole, Cache $cache, Group $pools) { - $team = $dbForConsole->getDocument('teams', $teamId); if ($team->isEmpty()) { throw new Exception(Exception::TEAM_NOT_FOUND); } + $allowList = empty(App::getEnv('_APP_PROJECT_REGIONS')) + ? [] + : \explode(',', App::getEnv('_APP_PROJECT_REGIONS')); + + if (!empty($allowList) && !\in_array($region, $allowList)) { + throw new Exception(Exception::PROJECT_REGION_UNSUPPORTED, 'Region "' . $region . '" is not supported'); + } + $auth = Config::getParam('auth', []); $auths = ['limit' => 0, 'maxSessions' => APP_LIMIT_USER_SESSIONS_DEFAULT, 'passwordHistory' => 0, 'passwordDictionary' => false, 'duration' => Auth::TOKEN_EXPIRATION_LOGIN_LONG, 'personalDataCheck' => false]; foreach ($auth as $index => $method) { diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index eba88480c4..b508dbcc51 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -204,6 +204,8 @@ class Exception extends \Exception public const PROJECT_TEMPLATE_DEFAULT_DELETION = 'project_template_default_deletion'; + public const PROJECT_REGION_UNSUPPORTED = 'project_region_unsupported'; + /** Webhooks */ public const WEBHOOK_NOT_FOUND = 'webhook_not_found';