From c3a8db0ea4747ff8e714176eda7b8c2ceba027a8 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 5 Nov 2024 17:14:23 +0200 Subject: [PATCH] Rollback cross regions connections --- app/controllers/api/databases.php | 1 + app/controllers/api/functions.php | 2 +- app/controllers/api/messaging.php | 4 ++-- app/controllers/api/projects.php | 8 +------- app/controllers/general.php | 11 ++--------- app/http.php | 3 +-- src/Appwrite/Event/Database.php | 1 - src/Appwrite/Event/Event.php | 1 - src/Appwrite/Messaging/Adapter.php | 2 -- src/Appwrite/Migration/Version/V19.php | 2 +- src/Appwrite/Platform/Workers/Certificates.php | 2 +- src/Appwrite/Utopia/Response/Model/Func.php | 2 +- 12 files changed, 11 insertions(+), 28 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 9334c449df..70374b5331 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -3,6 +3,7 @@ use Appwrite\Auth\Auth; use Appwrite\Detector\Detector; use Appwrite\Event\Database as EventDatabase; +use Appwrite\Event\Delete; use Appwrite\Event\Event; use Appwrite\Event\Usage; use Appwrite\Extend\Exception; diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 785bccb206..cd35f42e8b 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -184,7 +184,7 @@ App::post('/v1/functions') ->inject('queueForBuilds') ->inject('dbForConsole') ->inject('gitHub') - ->action(function (string $functionId, string $name, string $runtime, array $execute, array $events, string $schedule, int $timeout, bool $enabled, bool $logging, string $entrypoint, string $commands, array $scopes, string $installationId, string $providerRepositoryId, string $providerBranch, bool $providerSilentMode, string $providerRootDirectory, string $templateRepository, string $templateOwner, string $templateRootDirectory, string $templateVersion, string $specification, Request $request, Response $response, Database $dbForProject, Document $project, Document $user, Event $queueForEvents, Build $queueForBuilds, Database $dbForConsole, GitHub $githubgithub) use ($redeployVcs) { + ->action(function (string $functionId, string $name, string $runtime, array $execute, array $events, string $schedule, int $timeout, bool $enabled, bool $logging, string $entrypoint, string $commands, array $scopes, string $installationId, string $providerRepositoryId, string $providerBranch, bool $providerSilentMode, string $providerRootDirectory, string $templateRepository, string $templateOwner, string $templateRootDirectory, string $templateVersion, string $specification, 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 = \array_filter(\explode(',', System::getEnv('_APP_FUNCTIONS_RUNTIMES', ''))); diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 2d5b386c9d..57d3a1ea61 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -3037,7 +3037,7 @@ App::post('/v1/messaging/messages/push') break; case MessageStatus::SCHEDULED: $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => $project->getAttribute('region'), + 'region' => System::getEnv('_APP_REGION', 'default'), 'resourceType' => 'message', 'resourceId' => $message->getId(), 'resourceInternalId' => $message->getInternalId(), @@ -3592,7 +3592,7 @@ App::patch('/v1/messaging/messages/sms/:messageId') if (\is_null($currentScheduledAt) && !\is_null($scheduledAt)) { $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => $project->getAttribute('region'), + 'region' => System::getEnv('_APP_REGION', 'default'), 'resourceType' => 'message', 'resourceId' => $message->getId(), 'resourceInternalId' => $message->getInternalId(), diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 7867b42aab..fcbe9dea05 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -124,18 +124,12 @@ App::post('/v1/projects') } $databases = Config::getParam('pools-database', []); + $databaseOverride = System::getEnv('_APP_DATABASE_OVERRIDE'); $index = \array_search($databaseOverride, $databases); if ($index !== false) { $dsn = $databases[$index]; } else { - - if ($region !== 'default') { - $databases = array_filter($databases, function ($value) use ($region) { - return str_contains($value, $region); - }); - } - $dsn = $databases[array_rand($databases)]; } diff --git a/app/controllers/general.php b/app/controllers/general.php index e996342cfd..7763566159 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -59,12 +59,6 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo ]) )[0] ?? null; -// var_dump(System::getEnv('_APP_DOMAIN_FUNCTIONS', '')); -// var_dump($host); -// var_dump(APP_HOSTNAME_INTERNAL); -// var_dump($request->getHeader('host')); -// var_dump($request->getHeader('x-forwarded-host')); - if ($route === null) { 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.'); @@ -472,8 +466,7 @@ App::init() /* * Appwrite Router */ - - $host = $request->getHostname() ?? ''; + $host = $request->getHostname() ?? ''; $mainDomain = System::getEnv('_APP_DOMAIN', ''); // Only run Router when external domain if ($host !== $mainDomain) { @@ -674,7 +667,6 @@ App::init() ) { throw new AppwriteException(AppwriteException::GENERAL_UNKNOWN_ORIGIN, $originValidator->getDescription()); } - }); App::options() @@ -1030,6 +1022,7 @@ App::get('/.well-known/acme-challenge/*') ->action(function (Request $request, Response $response) { $uriChunks = \explode('/', $request->getURI()); $token = $uriChunks[\count($uriChunks) - 1]; + $validator = new Text(100, allowList: [ ...Text::NUMBERS, ...Text::ALPHABET_LOWER, diff --git a/app/http.php b/app/http.php index 9754263046..f4582ee6b7 100644 --- a/app/http.php +++ b/app/http.php @@ -16,7 +16,6 @@ use Utopia\CLI\Console; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; -use Utopia\Database\Exception\Duplicate; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; @@ -225,7 +224,7 @@ $http->on(Constant::EVENT_START, function (Server $http) use ($payloadSize, $reg }); }); -$http->on(Constant::EVENT_REQUEST, function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register) { +$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register) { App::setResource('swooleRequest', fn () => $swooleRequest); App::setResource('swooleResponse', fn () => $swooleResponse); diff --git a/src/Appwrite/Event/Database.php b/src/Appwrite/Event/Database.php index f5f29f5d04..f9eb7d9a7d 100644 --- a/src/Appwrite/Event/Database.php +++ b/src/Appwrite/Event/Database.php @@ -108,7 +108,6 @@ class Database extends Event */ public function trigger(): string|bool { - try { $dsn = new DSN($this->getProject()->getAttribute('database')); } catch (\InvalidArgumentException) { diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 0cf28ebe5c..43eda511df 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -6,7 +6,6 @@ use InvalidArgumentException; use Utopia\Database\Document; use Utopia\Queue\Client; use Utopia\Queue\Connection; -use Utopia\System\System; class Event { diff --git a/src/Appwrite/Messaging/Adapter.php b/src/Appwrite/Messaging/Adapter.php index 794fd9b3ed..27dd7f68eb 100644 --- a/src/Appwrite/Messaging/Adapter.php +++ b/src/Appwrite/Messaging/Adapter.php @@ -5,8 +5,6 @@ namespace Appwrite\Messaging; abstract class Adapter { abstract public function subscribe(string $projectId, mixed $identifier, array $roles, array $channels): void; - abstract public function unsubscribe(mixed $identifier): void; - abstract public static function send(string $projectId, array $payload, array $events, array $channels, array $roles, array $options): void; } diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index 039169f3a6..18234ebdc4 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -731,7 +731,7 @@ class V19 extends Migration if (empty($document->getAttribute('scheduleId', null))) { $schedule = $this->consoleDB->createDocument('schedules', new Document([ - 'region' => $project->getAttribute('region'), + 'region' => System::getEnv('_APP_REGION', 'default'), // Todo replace with projects region 'resourceType' => 'function', 'resourceId' => $document->getId(), 'resourceInternalId' => $document->getInternalId(), diff --git a/src/Appwrite/Platform/Workers/Certificates.php b/src/Appwrite/Platform/Workers/Certificates.php index 66fc14c2ca..a14f164295 100644 --- a/src/Appwrite/Platform/Workers/Certificates.php +++ b/src/Appwrite/Platform/Workers/Certificates.php @@ -381,7 +381,7 @@ class Certificates extends Action * @return void * @throws Exception */ - protected function applyCertificateFiles(string $folder, string $domain, array $letsEncryptData): void + private function applyCertificateFiles(string $folder, string $domain, array $letsEncryptData): void { // Prepare folder in storage for domain diff --git a/src/Appwrite/Utopia/Response/Model/Func.php b/src/Appwrite/Utopia/Response/Model/Func.php index ab2d7ba051..f4ff214d0b 100644 --- a/src/Appwrite/Utopia/Response/Model/Func.php +++ b/src/Appwrite/Utopia/Response/Model/Func.php @@ -94,7 +94,7 @@ class Func extends Model ]) ->addRule('schedule', [ 'type' => self::TYPE_STRING, - 'description' => 'Function execution schedule in CRON format.', + 'description' => 'Function execution schedult in CRON format.', 'default' => '', 'example' => '5 4 * * *', ])