From 5b184236c1eb737db85bc8556e3f58a4f43b2bea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 8 Aug 2024 09:38:37 +0200 Subject: [PATCH 1/4] Dynamic keys in builds --- src/Appwrite/Platform/Workers/Builds.php | 14 ++++++++++++++ .../Functions/FunctionsCustomServerTest.php | 11 +++++++++++ tests/resources/functions/php-scopes/setup.sh | 6 ++++++ 3 files changed, 31 insertions(+) create mode 100644 tests/resources/functions/php-scopes/setup.sh diff --git a/src/Appwrite/Platform/Workers/Builds.php b/src/Appwrite/Platform/Workers/Builds.php index d0fb597258..818dbf89d1 100644 --- a/src/Appwrite/Platform/Workers/Builds.php +++ b/src/Appwrite/Platform/Workers/Builds.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Workers; +use Ahc\Jwt\JWT; use Appwrite\Event\Event; use Appwrite\Event\Func; use Appwrite\Event\Usage; @@ -393,8 +394,21 @@ class Builds extends Action $vars[$var->getAttribute('key')] = $var->getAttribute('value', ''); } + $jwtExpiry = (int) System::getEnv('_APP_FUNCTIONS_BUILD_TIMEOUT', 900); + $jwtObj = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', $jwtExpiry, 0); + $apiKey = $jwtObj->encode([ + 'projectId' => $project->getId(), + 'scopes' => $function->getAttribute('scopes', []) + ]); + + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https'; + $hostname = System::getEnv('_APP_DOMAIN'); + $endpoint = $protocol . '://' . $hostname . "/v1"; + // Appwrite vars $vars = \array_merge($vars, [ + 'APPWRITE_FUNCTION_API_ENDPOINT' => $endpoint, + 'APPWRITE_FUNCTION_API_KEY' => API_KEY_DYNAMIC . '_' . $apiKey, 'APPWRITE_FUNCTION_ID' => $function->getId(), 'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name'), 'APPWRITE_FUNCTION_DEPLOYMENT' => $deployment->getId(), diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index e3148752c8..9510276a80 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1544,6 +1544,7 @@ class FunctionsCustomServerTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'entrypoint' => 'index.php', + 'commands' => 'sh setup.sh', 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), 'activate' => true ]); @@ -1553,6 +1554,16 @@ class FunctionsCustomServerTest extends Scope $this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId, checkForSuccess: false); + $deployment = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/deployments/' . $deploymentId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $deployment['headers']['status-code']); + $this->assertStringContainsStringIgnoringCase("200 OK", $deployment['body']['buildLogs']); + $this->assertStringContainsStringIgnoringCase('"total":', $deployment['body']['buildLogs']); + $this->assertStringContainsStringIgnoringCase('"users":', $deployment['body']['buildLogs']); + $deployment = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], diff --git a/tests/resources/functions/php-scopes/setup.sh b/tests/resources/functions/php-scopes/setup.sh new file mode 100644 index 0000000000..a2f78a4f3d --- /dev/null +++ b/tests/resources/functions/php-scopes/setup.sh @@ -0,0 +1,6 @@ + +ENDPOINT="$APPWRITE_FUNCTION_API_ENDPOINT/users" +PROJECT_ID="$APPWRITE_FUNCTION_PROJECT_ID" +API_KEY="$APPWRITE_FUNCTION_API_KEY" + +curl -v -X GET $ENDPOINT -H "x-appwrite-project: $PROJECT_ID" -H "x-appwrite-key: $API_KEY" \ No newline at end of file From f561311d761040a4ec5cdd5cf779d9f62f19120b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 8 Aug 2024 08:50:01 +0000 Subject: [PATCH 2/4] Fix failing test --- tests/e2e/Services/Functions/FunctionsCustomServerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 9510276a80..f58f307871 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1544,7 +1544,7 @@ class FunctionsCustomServerTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'entrypoint' => 'index.php', - 'commands' => 'sh setup.sh', + 'commands' => 'sh setup.sh && composer install', 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), 'activate' => true ]); From f6dec62c507333a79904ebf18a730081b60d91c2 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:56:55 +0100 Subject: [PATCH 3/4] feat: add base/key runtime --- app/controllers/api/functions.php | 4 ++-- src/Appwrite/Utopia/Response/Model/Runtime.php | 6 ++++++ tests/e2e/Services/Functions/FunctionsCustomServerTest.php | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 443260ea90..f70895ad60 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -436,8 +436,8 @@ App::get('/v1/functions/runtimes') continue; } - $runtimes[$key]['$id'] = $key; - $allowed[] = $runtimes[$key]; + $runtime['$id'] = $key; + $allowed[] = $runtime; } $response->dynamic(new Document([ diff --git a/src/Appwrite/Utopia/Response/Model/Runtime.php b/src/Appwrite/Utopia/Response/Model/Runtime.php index 8bc42cb418..3c328c4d40 100644 --- a/src/Appwrite/Utopia/Response/Model/Runtime.php +++ b/src/Appwrite/Utopia/Response/Model/Runtime.php @@ -16,6 +16,12 @@ class Runtime extends Model 'default' => '', 'example' => 'python-3.8', ]) + ->addRule('key', [ + 'type' => self::TYPE_STRING, + 'description' => 'Parent runtime key.', + 'default' => '', + 'example' => 'python', + ]) ->addRule('name', [ 'type' => self::TYPE_STRING, 'description' => 'Runtime Name.', diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index e3148752c8..3e6a00a4cb 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1412,6 +1412,7 @@ class FunctionsCustomServerTest extends Scope $this->assertArrayHasKey('$id', $runtime); $this->assertArrayHasKey('name', $runtime); + $this->assertArrayHasKey('key', $runtime); $this->assertArrayHasKey('version', $runtime); $this->assertArrayHasKey('logo', $runtime); $this->assertArrayHasKey('image', $runtime); From e1c8cfc4a13334f225af572957866994f3f6e800 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:59:43 +0100 Subject: [PATCH 4/4] chore: refactor $key/$id names --- app/controllers/api/functions.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index f70895ad60..3232f6bfe1 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -431,12 +431,12 @@ App::get('/v1/functions/runtimes') $allowList = \array_filter(\explode(',', System::getEnv('_APP_FUNCTIONS_RUNTIMES', ''))); $allowed = []; - foreach ($runtimes as $key => $runtime) { - if (!empty($allowList) && !\in_array($key, $allowList)) { + foreach ($runtimes as $id => $runtime) { + if (!empty($allowList) && !\in_array($id, $allowList)) { continue; } - $runtime['$id'] = $key; + $runtime['$id'] = $id; $allowed[] = $runtime; }