From afbadf93229645ea192d8d699397e30585764153 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Sun, 20 Aug 2023 15:13:05 +0530 Subject: [PATCH 1/6] Add search attribute to redeploy endpoint --- app/controllers/api/functions.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index c2c4be2d07..3e390d47a7 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1299,12 +1299,15 @@ App::post('/v1/functions/:functionId/deployments/:deploymentId/builds/:buildId') throw new Exception(Exception::BUILD_NOT_FOUND); } + $deploymentId = ID::unique(); + $deployment = $dbForProject->createDocument('deployments', $deployment->setAttributes([ - '$id' => ID::unique(), + '$id' => $deploymentId, 'buildId' => '', 'buildInternalId' => '', 'entrypoint' => $function->getAttribute('entrypoint'), 'commands' => $function->getAttribute('commands', ''), + 'search' => implode(' ', [$deploymentId, $function->getAttribute('entrypoint')]), ])); $buildEvent = new Build(); From c016ad7415cdbe8ad359b3dde2f7cd5ea28c9208 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 21 Aug 2023 17:03:07 +0530 Subject: [PATCH 2/6] Added commitHash to git clone command --- app/workers/builds.php | 3 ++- composer.json | 2 +- composer.lock | 27 ++++++++++++++++++--------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/workers/builds.php b/app/workers/builds.php index f8ee40172a..f25a2ec7ce 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -174,7 +174,8 @@ class BuildsV1 extends Worker $cloneRepository = $deployment->getAttribute('providerRepositoryName', $repositoryName); $branchName = $deployment->getAttribute('providerBranch'); - $gitCloneCommand = $github->generateCloneCommand($cloneOwner, $cloneRepository, $branchName, $tmpDirectory, $rootDirectory); + $commitHash = $deployment->getAttribute('providerCommitHash', ''); + $gitCloneCommand = $github->generateCloneCommand($cloneOwner, $cloneRepository, $branchName, $tmpDirectory, $rootDirectory, $commitHash); $stdout = ''; $stderr = ''; Console::execute('mkdir -p /tmp/builds/' . \escapeshellcmd($buildId), '', $stdout, $stderr); diff --git a/composer.json b/composer.json index 567e3ebf1c..96d5a27f92 100644 --- a/composer.json +++ b/composer.json @@ -65,7 +65,7 @@ "utopia-php/registry": "0.5.*", "utopia-php/storage": "0.14.*", "utopia-php/swoole": "0.5.*", - "utopia-php/vcs": "0.1.*", + "utopia-php/vcs": "0.2.*", "utopia-php/websocket": "0.1.*", "resque/php-resque": "1.3.6", "matomo/device-detector": "6.1.*", diff --git a/composer.lock b/composer.lock index 215f82fcb4..669e1d7f43 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": "82bd6c552d9c44aa106e5f3f9f28e55b", + "content-hash": "83427d18095b42c274d2bd6eb302e4aa", "packages": [ { "name": "adhocore/jwt", @@ -2564,16 +2564,16 @@ }, { "name": "utopia-php/vcs", - "version": "0.1.0", + "version": "dev-feat-clone-commit-hash", "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "fc9c38a3f84a4391470cc7184199dec6f953b144" + "reference": "5ed5d1685b3b714d6f20f7922ab4f5f5188313ee" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/fc9c38a3f84a4391470cc7184199dec6f953b144", - "reference": "fc9c38a3f84a4391470cc7184199dec6f953b144", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/5ed5d1685b3b714d6f20f7922ab4f5f5188313ee", + "reference": "5ed5d1685b3b714d6f20f7922ab4f5f5188313ee", "shasum": "" }, "require": { @@ -2607,9 +2607,9 @@ ], "support": { "issues": "https://github.com/utopia-php/vcs/issues", - "source": "https://github.com/utopia-php/vcs/tree/0.1.0" + "source": "https://github.com/utopia-php/vcs/tree/feat-clone-commit-hash" }, - "time": "2023-08-09T20:48:51+00:00" + "time": "2023-08-21T09:02:54+00:00" }, { "name": "utopia-php/websocket", @@ -5406,9 +5406,18 @@ "time": "2023-07-26T07:16:09+00:00" } ], - "aliases": [], + "aliases": [ + { + "package": "utopia-php/vcs", + "version": "dev-feat-clone-commit-hash", + "alias": "0.1.99", + "alias_normalized": "0.1.99.0" + } + ], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "utopia-php/vcs": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { From b7d053f60dc53fccc3adff576655818fca507ebf Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Mon, 21 Aug 2023 13:42:02 +0100 Subject: [PATCH 3/6] Update migrations.php --- app/controllers/api/migrations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/migrations.php b/app/controllers/api/migrations.php index a74b460e91..77d352fee9 100644 --- a/app/controllers/api/migrations.php +++ b/app/controllers/api/migrations.php @@ -889,7 +889,7 @@ App::get('/v1/migrations/nhost/report') ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_MIGRATION_REPORT) ->param('resources', [], new ArrayList(new WhiteList(NHost::getSupportedResources())), 'List of resources to migrate') - ->param('subdomain', '', new URL(), 'Source\'s Subdomain') + ->param('subdomain', '', new Text(512), 'Source\'s Subdomain') ->param('region', '', new Text(512), 'Source\'s Region') ->param('adminSecret', '', new Text(512), 'Source\'s Admin Secret') ->param('database', '', new Text(512), 'Source\'s Database Name') From 9418ba87750cf600c2e56d06c8e81a75661307c2 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Mon, 21 Aug 2023 13:43:21 +0100 Subject: [PATCH 4/6] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 8865661984..34e264c383 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -761,7 +761,7 @@ services: appwrite-assistant: container_name: appwrite-assistant hostname: appwrite-assistant - image: appwrite/assistant:0.1.2 + image: appwrite/assistant:0.1.3 networks: - appwrite environment: From 7c184498bbb79792f6de260162f01b1d7f704ed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 21 Aug 2023 15:16:02 +0200 Subject: [PATCH 5/6] Fix GitHub app auth and migrations --- app/controllers/api/vcs.php | 21 ++++++--------------- app/controllers/general.php | 2 +- docker-compose.yml | 2 +- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index 0097da3e04..ff1853f3f9 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -277,6 +277,11 @@ App::get('/v1/vcs/github/callback') ->inject('response') ->inject('dbForConsole') ->action(function (string $providerInstallationId, string $setupAction, string $state, string $code, GitHub $github, Document $user, Document $project, Request $request, Response $response, Database $dbForConsole) { + if (empty($state)) { + $error = 'Installation requests from organisation members for the Appwrite GitHub App are currently unsupported. To proceed with the installation, login to the Appwrite Console and install the GitHub App.'; + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $error); + } + $state = \json_decode($state, true); $projectId = $state['projectId'] ?? ''; @@ -285,25 +290,11 @@ App::get('/v1/vcs/github/callback') 'failure' => $request->getProtocol() . '://' . $request->getHostname() . "/console/project-$projectId/settings/git-installations", ]; - $state = \array_merge($defaultState, $state); + $state = \array_merge($defaultState, $state ?? []); $redirectSuccess = $state['success'] ?? ''; $redirectFailure = $state['failure'] ?? ''; - if (empty($state)) { - $error = 'Installation requests from organisation members for the Appwrite GitHub App are currently unsupported. To proceed with the installation, login to the Appwrite Console and install the GitHub App.'; - - if (!empty($redirectFailure)) { - $separator = \str_contains($redirectFailure, '?') ? '&' : ':'; - return $response - ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') - ->addHeader('Pragma', 'no-cache') - ->redirect($redirectFailure . $separator . \http_build_query(['error' => $error])); - } - - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $error); - } - $project = $dbForConsole->getDocument('projects', $projectId); if ($project->isEmpty()) { diff --git a/app/controllers/general.php b/app/controllers/general.php index e8ab7f1320..59821296d8 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -319,7 +319,7 @@ App::init() * @see https://www.owasp.org/index.php/List_of_useful_HTTP_headers */ if (App::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'enabled') { // Force HTTPS - if ($request->getProtocol() !== 'https' && ($swooleRequest->header['host'] ?? '') !== 'localhost') { // Localhost allowed for proxy + if ($request->getProtocol() !== 'https' && ($swooleRequest->header['host'] ?? '') !== 'localhost' && ($swooleRequest->header['host'] ?? '') !== APP_HOSTNAME_INTERNAL) { // Localhost allowed for proxy, APP_HOSTNAME_INTERNAL allowed for migrations if ($request->getMethod() !== Request::METHOD_GET) { throw new AppwriteException(AppwriteException::GENERAL_PROTOCOL_UNSUPPORTED, 'Method unsupported over HTTP.'); } diff --git a/docker-compose.yml b/docker-compose.yml index 34e264c383..f55c0d48c0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -772,7 +772,7 @@ services: hostname: executor <<: *x-logging stop_signal: SIGINT - image: openruntimes/executor:0.3.2 + image: openruntimes/executor:0.3.3 networks: - appwrite - runtimes From fedac9592ba46fa7e847986e10a92ae343ece275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 21 Aug 2023 19:43:03 +0200 Subject: [PATCH 6/6] Fix id null schedule bug --- app/controllers/api/functions.php | 39 ++++++++++++++++--------------- app/controllers/api/proxy.php | 5 ++++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 46b6100cd2..fcc9d1fea2 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -200,6 +200,7 @@ App::post('/v1/functions') 'events' => $events, 'schedule' => $schedule, 'scheduleInternalId' => '', + 'scheduleId' => '', 'timeout' => $timeout, 'entrypoint' => $entrypoint, 'commands' => $commands, @@ -215,6 +216,22 @@ App::post('/v1/functions') 'providerSilentMode' => $providerSilentMode, ])); + $schedule = Authorization::skip( + fn () => $dbForConsole->createDocument('schedules', new Document([ + 'region' => App::getEnv('_APP_REGION', 'default'), // Todo replace with projects region + 'resourceType' => 'function', + 'resourceId' => $function->getId(), + 'resourceInternalId' => $function->getInternalId(), + 'resourceUpdatedAt' => DateTime::now(), + 'projectId' => $project->getId(), + 'schedule' => $function->getAttribute('schedule'), + 'active' => false, + ])) + ); + + $function->setAttribute('scheduleId', $schedule->getId()); + $function->setAttribute('scheduleInternalId', $schedule->getInternalId()); + // Git connect logic if (!empty($providerRepositoryId)) { $repository = $dbForConsole->createDocument('repositories', new Document([ @@ -235,23 +252,11 @@ App::post('/v1/functions') 'providerPullRequestIds' => [] ])); - $function = $dbForProject->updateDocument('functions', $function->getId(), $function - ->setAttribute('repositoryId', $repository->getId()) - ->setAttribute('repositoryInternalId', $repository->getInternalId())); + $function->setAttribute('repositoryId', $repository->getId()); + $function->setAttribute('repositoryInternalId', $repository->getInternalId()); } - $schedule = Authorization::skip( - fn () => $dbForConsole->createDocument('schedules', new Document([ - 'region' => App::getEnv('_APP_REGION', 'default'), // Todo replace with projects region - 'resourceType' => 'function', - 'resourceId' => $function->getId(), - 'resourceInternalId' => $function->getInternalId(), - 'resourceUpdatedAt' => DateTime::now(), - 'projectId' => $project->getId(), - 'schedule' => $function->getAttribute('schedule'), - 'active' => false, - ])) - ); + $function = $dbForProject->updateDocument('functions', $function->getId(), $function); // Redeploy vcs logic if (!empty($providerRepositoryId)) { @@ -320,10 +325,6 @@ App::post('/v1/functions') ); } - $function->setAttribute('scheduleId', $schedule->getId()); - $function->setAttribute('scheduleInternalId', $schedule->getInternalId()); - $dbForProject->updateDocument('functions', $function->getId(), $function); - $eventsInstance->setParam('functionId', $function->getId()); $response diff --git a/app/controllers/api/proxy.php b/app/controllers/api/proxy.php index e8f4670417..1bd01a76e6 100644 --- a/app/controllers/api/proxy.php +++ b/app/controllers/api/proxy.php @@ -41,6 +41,11 @@ App::post('/v1/proxy/rules') ->inject('dbForConsole') ->inject('dbForProject') ->action(function (string $domain, string $resourceType, string $resourceId, Response $response, Document $project, Event $events, Database $dbForConsole, Database $dbForProject) { + $mainDomain = App::getEnv('_APP_DOMAIN', ''); + if ($domain === $mainDomain || $domain === 'localhost' || $domain === APP_HOSTNAME_INTERNAL) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed for security reasons.'); + } + $document = $dbForConsole->findOne('rules', [ Query::equal('domain', [$domain]), ]);