From 9c9bde2ce689a0bfec68e4b69323a0f66b7d2a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 May 2026 11:36:54 +0200 Subject: [PATCH 1/8] Introduce project key console tests --- .env | 2 +- tests/e2e/Services/Proxy/ProxyBase.php | 923 +++++++++++++----- .../Services/Proxy/ProxyConsoleClientTest.php | 14 + .../Services/Proxy/ProxyCustomServerTest.php | 745 -------------- tests/e2e/Services/Proxy/ProxyHelpers.php | 293 ++++++ 5 files changed, 997 insertions(+), 980 deletions(-) create mode 100644 tests/e2e/Services/Proxy/ProxyConsoleClientTest.php create mode 100644 tests/e2e/Services/Proxy/ProxyHelpers.php diff --git a/.env b/.env index 4a6a3ac344..d57e7cf421 100644 --- a/.env +++ b/.env @@ -20,7 +20,7 @@ _APP_EMAIL_CERTIFICATES=certificates@appwrite.io _APP_SYSTEM_RESPONSE_FORMAT= _APP_CUSTOM_DOMAIN_DENY_LIST= _APP_OPTIONS_ABUSE=disabled -_APP_OPTIONS_ROUTER_PROTECTION=disabled +_APP_OPTIONS_ROUTER_PROTECTION=enabled _APP_OPTIONS_FORCE_HTTPS=disabled _APP_OPTIONS_ROUTER_FORCE_HTTPS=disabled _APP_OPENSSL_KEY_V1=your-secret-key diff --git a/tests/e2e/Services/Proxy/ProxyBase.php b/tests/e2e/Services/Proxy/ProxyBase.php index 59a853bfc8..33ef64591b 100644 --- a/tests/e2e/Services/Proxy/ProxyBase.php +++ b/tests/e2e/Services/Proxy/ProxyBase.php @@ -2,298 +2,753 @@ namespace Tests\E2E\Services\Proxy; -use Appwrite\ID; -use Appwrite\Tests\Async; -use CURLFile; use Tests\E2E\Client; -use Utopia\Console; +use Utopia\Database\Query; +use Utopia\System\System; trait ProxyBase { - use Async; + use ProxyHelpers; - protected function listRules(array $params = []): mixed + protected function tearDown(): void { - $rule = $this->client->call(Client::METHOD_GET, '/proxy/rules', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), $params); - - return $rule; - } - - protected function createAPIRule(string $domain): mixed - { - $rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/api', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'domain' => $domain, + // Cleanup for testRuleVerification test + // Required as it uses static domain name + $rules = $this->listRules([ + 'queries' => [ + Query::endsWith('domain', 'webapp.com')->toString(), + Query::limit(1000)->toString(), + ] ]); + $this->assertEquals(200, $rules['headers']['status-code']); + foreach ($rules['body']['rules'] as $rule) { + $ruleId = $rule['$id']; + $response = $this->deleteRule($ruleId); + $this->assertEquals(204, $response['headers']['status-code']); + } - return $rule; + if ($rules['body']['total'] > 0) { + $rules = $this->listRules([ + 'queries' => [ + Query::endsWith('domain', 'webapp.com')->toString(), + Query::limit(1)->toString() + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(0, count($rules['body']['rules'])); + $this->assertEquals(0, $rules['body']['total']); + } } - protected function updateRuleVerification(string $ruleId): mixed - { - $rule = $this->client->call(Client::METHOD_PATCH, '/proxy/rules/' . $ruleId . '/verification', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - return $rule; - } - - protected function createSiteRule(string $domain, string $siteId, string $branch = ''): mixed - { - $rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/site', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'domain' => $domain, - 'siteId' => $siteId, - 'branch' => $branch, - ]); - - return $rule; - } - - protected function getRule(string $ruleId): mixed - { - $rule = $this->client->call(Client::METHOD_GET, '/proxy/rules/' . $ruleId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - return $rule; - } - - protected function createRedirectRule(string $domain, string $url, int $statusCode, string $resourceType, string $resourceId): mixed - { - $rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/redirect', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'domain' => $domain, - 'url' => $url, - 'statusCode' => $statusCode, - 'resourceType' => $resourceType, - 'resourceId' => $resourceId, - ]); - - return $rule; - } - - protected function createFunctionRule(string $domain, string $functionId, string $branch = ''): mixed - { - $rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/function', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'domain' => $domain, - 'functionId' => $functionId, - 'branch' => $branch, - ]); - - return $rule; - } - - protected function deleteRule(string $ruleId): mixed - { - $rule = $this->client->call(Client::METHOD_DELETE, '/proxy/rules/' . $ruleId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - return $rule; - } - - protected function setupAPIRule(string $domain): string + public function testCreateRule(): void { + $domain = \uniqid() . '-api.myapp.com'; $rule = $this->createAPIRule($domain); - $this->assertEquals(201, $rule['headers']['status-code'], 'Failed to setup rule: ' . \json_encode($rule)); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals($domain, $rule['body']['domain']); + $this->assertEquals('manual', $rule['body']['trigger']); + $this->assertArrayHasKey('$id', $rule['body']); + $this->assertArrayHasKey('domain', $rule['body']); + $this->assertArrayHasKey('type', $rule['body']); + $this->assertArrayHasKey('redirectUrl', $rule['body']); + $this->assertArrayHasKey('redirectStatusCode', $rule['body']); + $this->assertArrayHasKey('deploymentResourceType', $rule['body']); + $this->assertArrayHasKey('deploymentId', $rule['body']); + $this->assertArrayHasKey('deploymentResourceId', $rule['body']); + $this->assertArrayHasKey('deploymentVcsProviderBranch', $rule['body']); + $this->assertArrayHasKey('logs', $rule['body']); + $this->assertArrayHasKey('renewAt', $rule['body']); - return $rule['body']['$id']; - } + $ruleId = $rule['body']['$id']; - protected function setupRedirectRule(string $domain, string $url, int $statusCode, string $resourceType, string $resourceId): string - { - $rule = $this->createRedirectRule($domain, $url, $statusCode, $resourceType, $resourceId); + $rule = $this->createAPIRule($domain); + $this->assertEquals(409, $rule['headers']['status-code']); - $this->assertEquals(201, $rule['headers']['status-code'], 'Failed to setup rule: ' . \json_encode($rule)); - - return $rule['body']['$id']; - } - - protected function setupFunctionRule(string $domain, string $functionId, string $branch = ''): string - { - $rule = $this->createFunctionRule($domain, $functionId, $branch); - - $this->assertEquals(201, $rule['headers']['status-code'], 'Failed to setup rule: ' . \json_encode($rule)); - - return $rule['body']['$id']; - } - - protected function setupSiteRule(string $domain, string $siteId, string $branch = ''): string - { - $rule = $this->createSiteRule($domain, $siteId, $branch); - - $this->assertEquals(201, $rule['headers']['status-code'], 'Failed to setup rule: ' . \json_encode($rule)); - - return $rule['body']['$id']; - } - - protected function cleanupRule(string $ruleId): void - { $rule = $this->deleteRule($ruleId); - $this->assertEquals(204, $rule['headers']['status-code'], 'Failed to cleanup rule: ' . \json_encode($rule)); + + $this->assertEquals(204, $rule['headers']['status-code']); } - protected function cleanupSite(string $siteId): void + public function testCreateRuleSetup(): void { - $site = $this->client->call(Client::METHOD_DELETE, '/sites/' . $siteId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - $this->assertEquals(204, $site['headers']['status-code'], 'Failed to cleanup site: ' . \json_encode($site)); + $ruleId = $this->setupAPIRule(\uniqid() . '-api2.myapp.com'); + $this->cleanupRule($ruleId); } - protected function cleanupFunction(string $functionId): void + public function testCreateRuleApex(): void { - $function = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - $this->assertEquals(204, $function['headers']['status-code'], 'Failed to cleanup function: ' . \json_encode($function)); + $domain = \uniqid() . '.com'; + $rule = $this->createAPIRule($domain); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); } - protected function setupSite(): mixed + public function testCreateRuleVcs(): void { - // Site - $site = $this->client->call(Client::METHOD_POST, '/sites', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - ]), [ - 'siteId' => ID::unique(), - 'name' => 'Proxy site', - 'framework' => 'other', - 'adapter' => 'static', - 'buildRuntime' => 'static-1', - 'outputDirectory' => './', - 'buildCommand' => '', - 'installCommand' => '', - 'fallbackFile' => '', - ]); + $domain = \uniqid() . '-vcs.myapp.com'; - $this->assertEquals($site['headers']['status-code'], 201, 'Setup site failed with status code: ' . $site['headers']['status-code'] . ' and response: ' . json_encode($site['body'], JSON_PRETTY_PRINT)); + $setup = $this->setupSite(); + $siteId = $setup['siteId']; + $deploymentId = $setup['deploymentId']; - $siteId = $site['body']['$id']; + $this->assertNotEmpty($siteId); + $this->assertNotEmpty($deploymentId); - // Deployment - $deployment = $this->client->call(Client::METHOD_POST, '/sites/' . $siteId . '/deployments', array_merge([ - 'content-type' => 'multipart/form-data', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - ]), [ - 'code' => $this->packageSite('static'), - 'activate' => 'true' - ]); + $rule = $this->createSiteRule('commit-' . $domain, $siteId); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->cleanupRule($rule['body']['$id']); - $this->assertEquals($deployment['headers']['status-code'], 202, 'Setup deployment failed with status code: ' . $deployment['headers']['status-code'] . ' and response: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT)); - $deploymentId = $deployment['body']['$id'] ?? ''; + $rule = $this->createSiteRule('branch-' . $domain, $siteId); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->cleanupRule($rule['body']['$id']); - $this->assertEventually(function () use ($siteId, $deploymentId) { - $site = $this->client->call(Client::METHOD_GET, '/sites/' . $siteId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - ])); - $this->assertEquals($deploymentId, $site['body']['deploymentId'], 'Deployment is not activated, deployment: ' . json_encode($site['body'], JSON_PRETTY_PRINT)); - }, 120000, 500); + $rule = $this->createSiteRule('anything-' . $domain, $siteId); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->cleanupRule($rule['body']['$id']); - return ['siteId' => $siteId, 'deploymentId' => $deploymentId]; + $sitesDomain = \explode(',', System::getEnv('_APP_DOMAIN_SITES', ''))[0]; + $domain = \uniqid() . '-vcs.' . $sitesDomain; + + $rule = $this->createSiteRule('commit-' . $domain, $siteId); + $this->assertEquals(400, $rule['headers']['status-code']); + + $rule = $this->createSiteRule('branch-' . $domain, $siteId); + $this->assertEquals(400, $rule['headers']['status-code']); + + $rule = $this->createSiteRule('subdomain.anything-' . $domain, $siteId); + $this->assertEquals(400, $rule['headers']['status-code']); + + $rule = $this->createSiteRule('anything-' . $domain, $siteId); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->cleanupRule($rule['body']['$id']); } - protected function setupFunction(): mixed + public function testCreateAPIRule(): void { - // Function - $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - ]), [ - 'functionId' => ID::unique(), - 'runtime' => 'node-22', - 'name' => 'Proxy Function', - 'entrypoint' => 'index.js', - 'commands' => '', - 'execute' => ['any'] + $domain = \uniqid() . '-api.custom.localhost'; + + $proxyClient = new Client(); + $proxyClient->setEndpoint('http://appwrite.test'); + $proxyClient->addHeader('x-appwrite-hostname', $domain); + + $response = $proxyClient->call(Client::METHOD_GET, '/versions'); + $this->assertEquals(401, $response['headers']['status-code']); + + $ruleId = $this->setupAPIRule($domain); + + $this->assertNotEmpty($ruleId); + + $response = $proxyClient->call(Client::METHOD_GET, '/versions'); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(APP_VERSION_STABLE, $response['body']['server']); + + $this->cleanupRule($ruleId); + + $rule = $this->createAPIRule('http://' . $domain); + $this->assertEquals(400, $rule['headers']['status-code']); + + $rule = $this->createAPIRule('https://' . $domain); + $this->assertEquals(400, $rule['headers']['status-code']); + + $rule = $this->createAPIRule('wss://' . $domain); + $this->assertEquals(400, $rule['headers']['status-code']); + + $rule = $this->createAPIRule($domain . '/some-path'); + $this->assertEquals(400, $rule['headers']['status-code']); + } + + public function testCreateRedirectRule(): void + { + $domain = \uniqid() . '-redirect.custom.localhost'; + + $proxyClient = new Client(); + $proxyClient->setEndpoint('http://appwrite.test'); + $proxyClient->addHeader('x-appwrite-hostname', $domain); + + $response = $proxyClient->call(Client::METHOD_GET, '/todos/1'); + $this->assertEquals(401, $response['headers']['status-code']); + + $siteId = $this->setupSite()['siteId']; + + $ruleId = $this->setupRedirectRule($domain, 'https://jsonplaceholder.typicode.com/todos/1', 301, 'site', $siteId); + $this->assertNotEmpty($ruleId); + + $response = $proxyClient->call(Client::METHOD_GET, '/todos/1'); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(1, $response['body']['id']); + + $response = $proxyClient->call(Client::METHOD_GET, '/'); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(1, $response['body']['id']); + + $response = $proxyClient->call(Client::METHOD_GET, '/', followRedirects: false); + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertEquals('https://jsonplaceholder.typicode.com/todos/1', $response['headers']['location']); + + $domain = \uniqid() . '-redirect-307.custom.localhost'; + $ruleId = $this->setupRedirectRule($domain, 'https://jsonplaceholder.typicode.com/todos/1', 307, 'site', $siteId); + $this->assertNotEmpty($ruleId); + + $proxyClient = new Client(); + $proxyClient->setEndpoint('http://appwrite.test'); + $proxyClient->addHeader('x-appwrite-hostname', $domain); + + $response = $proxyClient->call(Client::METHOD_GET, '/', followRedirects: false); + $this->assertEquals(307, $response['headers']['status-code']); + $this->assertEquals('https://jsonplaceholder.typicode.com/todos/1', $response['headers']['location']); + + $rules = $this->listRules([ + 'queries' => [ + Query::equal('type', ['redirect'])->toString(), + Query::equal('trigger', ['manual'])->toString(), + Query::equal('deploymentResourceType', ['site'])->toString(), + Query::equal('deploymentResourceId', [$siteId])->toString(), + ], ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(2, $rules['body']['total']); - $this->assertEquals($function['headers']['status-code'], 201, 'Setup function failed with status code: ' . $function['headers']['status-code'] . ' and response: ' . json_encode($function['body'], JSON_PRETTY_PRINT)); + $this->cleanupSite($siteId); + $this->cleanupRule($ruleId); + } - $functionId = $function['body']['$id']; + public function testCreateFunctionRule(): void + { + $domain = \uniqid() . '-function.custom.localhost'; - // Deployment - $deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([ - 'content-type' => 'multipart/form-data', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - ]), [ - 'code' => $this->packageFunction('basic'), - 'activate' => 'true' - ]); + $proxyClient = new Client(); + $proxyClient->setEndpoint('http://appwrite.test'); + $proxyClient->addHeader('x-appwrite-hostname', $domain); - $this->assertEquals($deployment['headers']['status-code'], 202, 'Setup deployment failed with status code: ' . $deployment['headers']['status-code'] . ' and response: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT)); - $deploymentId = $deployment['body']['$id'] ?? ''; + $response = $proxyClient->call(Client::METHOD_GET, '/ping'); + $this->assertEquals(401, $response['headers']['status-code']); + + $setup = $this->setupFunction(); + $functionId = $setup['functionId']; + $deploymentId = $setup['deploymentId']; + + $this->assertNotEmpty($functionId); + $this->assertNotEmpty($deploymentId); + + $ruleId = $this->setupFunctionRule($domain, $functionId); + $this->assertNotEmpty($ruleId); + + $response = $proxyClient->call(Client::METHOD_GET, '/ping'); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($functionId, $response['body']['APPWRITE_FUNCTION_ID']); + + $this->cleanupRule($ruleId); + + $this->cleanupFunction($functionId); $this->assertEventually(function () use ($functionId, $deploymentId) { - $function = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - ])); - $this->assertEquals($deploymentId, $function['body']['deploymentId'], 'Deployment is not activated, deployment: ' . json_encode($function['body'], JSON_PRETTY_PRINT)); - }, 100000, 500); + $rules = $this->listRules([ + 'queries' => [ + Query::limit(1)->toString(), + Query::equal('type', ['deployment'])->toString(), + Query::equal('deploymentResourceType', ['function'])->toString(), + Query::equal('deploymentResourceId', [$functionId])->toString(), + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(0, $rules['body']['total']); + $this->assertCount(0, $rules['body']['rules']); - return ['functionId' => $functionId, 'deploymentId' => $deploymentId]; + $rules = $this->listRules([ + 'queries' => [ + Query::limit(1)->toString(), + Query::equal('type', ['deployment'])->toString(), + Query::equal('deploymentId', [$deploymentId])->toString() + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(0, $rules['body']['total']); + $this->assertCount(0, $rules['body']['rules']); + }); } - private function packageSite(string $site): CURLFile + public function testCreateSiteRule(): void { - $stdout = ''; - $stderr = ''; + $domain = \uniqid() . '-site.custom.localhost'; - $folderPath = realpath(__DIR__ . '/../../../resources/sites') . "/$site"; - $tarPath = "$folderPath/code.tar.gz"; + $proxyClient = new Client(); + $proxyClient->setEndpoint('http://appwrite.test'); + $proxyClient->addHeader('x-appwrite-hostname', $domain); - Console::execute("cd $folderPath && tar --exclude code.tar.gz --exclude node_modules -czf code.tar.gz .", '', $stdout, $stderr); + $response = $proxyClient->call(Client::METHOD_GET, '/contact'); + $this->assertEquals(401, $response['headers']['status-code']); - if (filesize($tarPath) > 1024 * 1024 * 5) { - throw new \Exception('Code package is too large. Use the chunked upload method instead.'); + $setup = $this->setupSite(); + $siteId = $setup['siteId']; + $deploymentId = $setup['deploymentId']; + + $this->assertNotEmpty($siteId); + $this->assertNotEmpty($deploymentId); + + $ruleId = $this->setupSiteRule($domain, $siteId); + $this->assertNotEmpty($ruleId); + $rule = $this->getRule($ruleId); + $this->assertSame(200, $rule['headers']['status-code']); + $this->assertSame('created', $rule['body']['status']); + + $response = $proxyClient->call(Client::METHOD_GET, '/contact'); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertStringContainsString('Contact page', $response['body']); + + // Wildcard domains automatically get verified status + $domains = [ + \uniqid() . '.sites.localhost', + \uniqid() . '.rebranded.localhost', + ]; + foreach ($domains as $domain) { + $wildcardRuleId = $this->setupSiteRule($domain, $siteId); + $this->assertNotEmpty($wildcardRuleId); + $rule = $this->getRule($wildcardRuleId); + $this->assertSame(200, $rule['headers']['status-code']); + $this->assertSame('verified', $rule['body']['status']); + $this->cleanupRule($wildcardRuleId); } - return new CURLFile($tarPath, 'application/x-gzip', \basename($tarPath)); + $rules = $this->listRules([ + 'queries' => [ + Query::limit(1)->toString(), + Query::equal('trigger', ['deployment'])->toString(), + Query::equal('type', ['deployment'])->toString(), + Query::equal('deploymentResourceType', ['site'])->toString(), + Query::equal('deploymentResourceId', [$siteId])->toString(), + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertGreaterThan(0, $rules['body']['total']); + + $this->cleanupRule($ruleId); + + $this->cleanupSite($siteId); + + $this->assertEventually(function () use ($siteId, $deploymentId) { + $rules = $this->listRules([ + 'queries' => [ + Query::limit(1)->toString(), + Query::equal('type', ['deployment'])->toString(), + Query::equal('deploymentResourceType', ['site'])->toString(), + Query::equal('deploymentResourceId', [$siteId])->toString(), + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(0, $rules['body']['total']); + $this->assertCount(0, $rules['body']['rules']); + + $rules = $this->listRules([ + 'queries' => [ + Query::limit(1)->toString(), + Query::equal('type', ['deployment'])->toString(), + Query::equal('deploymentId', [$deploymentId])->toString() + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(0, $rules['body']['total']); + $this->assertCount(0, $rules['body']['rules']); + }); } - private function packageFunction(string $function): CURLFile + public function testCreateSiteBranchRule(): void { - $stdout = ''; - $stderr = ''; + $domain = \uniqid() . '-site-branch.custom.localhost'; - $folderPath = realpath(__DIR__ . '/../../../resources/functions') . "/$function"; - $tarPath = "$folderPath/code.tar.gz"; + $setup = $this->setupSite(); + $siteId = $setup['siteId']; + $deploymentId = $setup['deploymentId']; - Console::execute("cd $folderPath && tar --exclude code.tar.gz --exclude node_modules -czf code.tar.gz .", '', $stdout, $stderr); + $this->assertNotEmpty($siteId); + $this->assertNotEmpty($deploymentId); - if (filesize($tarPath) > 1024 * 1024 * 5) { - throw new \Exception('Code package is too large. Use the chunked upload method instead.'); + $ruleId = $this->setupSiteRule($domain, $siteId, 'dev'); + $this->assertNotEmpty($ruleId); + + $rule = $this->getRule($ruleId); + $this->assertEquals(200, $rule['headers']['status-code']); + + $this->cleanupRule($ruleId); + } + + public function testCreateFunctionBranchRule(): void + { + $domain = \uniqid() . '-function-branch.custom.localhost'; + + $setup = $this->setupFunction(); + $functionId = $setup['functionId']; + $deploymentId = $setup['deploymentId']; + + $this->assertNotEmpty($functionId); + $this->assertNotEmpty($deploymentId); + + $ruleId = $this->setupFunctionRule($domain, $functionId, 'dev'); + $this->assertNotEmpty($ruleId); + + $rule = $this->getRule($ruleId); + $this->assertEquals(200, $rule['headers']['status-code']); + + $this->cleanupRule($ruleId); + + $this->cleanupFunction($functionId); + } + + public function testUpdateRule(): void + { + // Create function appwrite-network domain + $functionsDomain = \explode(',', System::getEnv('_APP_DOMAIN_FUNCTIONS', ''))[0]; + $domain = \uniqid() . '-cname-api.' . $functionsDomain; + + $rule = $this->createAPIRule($domain); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('verified', $rule['body']['status']); + + $this->cleanupRule($rule['body']['$id']); + + // Create site appwrite-network domain + $sitesDomain = \explode(',', System::getEnv('_APP_DOMAIN_SITES', ''))[0]; + $domain = \uniqid() . '-cname-api.' . $sitesDomain; + + $rule = $this->createAPIRule($domain); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('verified', $rule['body']['status']); + + $this->cleanupRule($rule['body']['$id']); + + // Create + update + $domain = \uniqid() . '-cname-api.custom.com'; + + $rule = $this->createAPIRule($domain); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + + $ruleId = $rule['body']['$id']; + + $rule = $this->updateRuleVerification($ruleId); + $this->assertEquals(400, $rule['headers']['status-code']); + + $this->cleanupRule($ruleId); + } + + public function testGetRule() + { + $domain = \uniqid() . '-get.custom.localhost'; + $ruleId = $this->setupAPIRule($domain); + + $this->assertNotEmpty($ruleId); + + $rule = $this->getRule($ruleId); + $this->assertEquals(200, $rule['headers']['status-code']); + $this->assertEquals($domain, $rule['body']['domain']); + $this->assertEquals('manual', $rule['body']['trigger']); + $this->assertArrayHasKey('$id', $rule['body']); + $this->assertArrayHasKey('domain', $rule['body']); + $this->assertArrayHasKey('type', $rule['body']); + $this->assertArrayHasKey('redirectUrl', $rule['body']); + $this->assertArrayHasKey('redirectStatusCode', $rule['body']); + $this->assertArrayHasKey('deploymentResourceType', $rule['body']); + $this->assertArrayHasKey('deploymentId', $rule['body']); + $this->assertArrayHasKey('deploymentResourceId', $rule['body']); + $this->assertArrayHasKey('deploymentVcsProviderBranch', $rule['body']); + $this->assertArrayHasKey('logs', $rule['body']); + $this->assertArrayHasKey('renewAt', $rule['body']); + + $this->cleanupRule($ruleId); + } + + public function testListRules() + { + $rules = $this->listRules(); + $this->assertEquals(200, $rules['headers']['status-code']); + foreach ($rules['body']['rules'] as $rule) { + $rule = $this->deleteRule($rule['$id']); + $this->assertEquals(204, $rule['headers']['status-code']); } - return new CURLFile($tarPath, 'application/x-gzip', \basename($tarPath)); + $rules = $this->listRules(); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(0, $rules['body']['total']); + $this->assertCount(0, $rules['body']['rules']); + + $rule1Domain = \uniqid() . '-list1.custom.localhost'; + $rule1Id = $this->setupAPIRule($rule1Domain); + $this->assertNotEmpty($rule1Id); + + $rules = $this->listRules(); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(1, $rules['body']['total']); + $this->assertCount(1, $rules['body']['rules']); + $this->assertEquals($rule1Domain, $rules['body']['rules'][0]['domain']); + + $this->assertEquals('manual', $rules['body']['rules'][0]['trigger']); + $this->assertArrayHasKey('$id', $rules['body']['rules'][0]); + $this->assertArrayHasKey('domain', $rules['body']['rules'][0]); + $this->assertArrayHasKey('type', $rules['body']['rules'][0]); + $this->assertArrayHasKey('redirectUrl', $rules['body']['rules'][0]); + $this->assertArrayHasKey('redirectStatusCode', $rules['body']['rules'][0]); + $this->assertArrayHasKey('deploymentResourceType', $rules['body']['rules'][0]); + $this->assertArrayHasKey('deploymentId', $rules['body']['rules'][0]); + $this->assertArrayHasKey('deploymentResourceId', $rules['body']['rules'][0]); + $this->assertArrayHasKey('deploymentVcsProviderBranch', $rules['body']['rules'][0]); + $this->assertArrayHasKey('logs', $rules['body']['rules'][0]); + $this->assertArrayHasKey('renewAt', $rules['body']['rules'][0]); + + $rule2Domain = \uniqid() . '-list1.custom.localhost'; + $rule2Id = $this->setupAPIRule($rule2Domain); + $this->assertNotEmpty($rule2Id); + + $rules = $this->listRules(); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(2, $rules['body']['total']); + $this->assertCount(2, $rules['body']['rules']); + + $rules = $this->listRules([ + 'queries' => [ + Query::limit(1)->toString() + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(2, $rules['body']['total']); + $this->assertCount(1, $rules['body']['rules']); + + $rules = $this->listRules([ + 'queries' => [ + Query::equal('$id', [$rule1Id])->toString() + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertCount(1, $rules['body']['rules']); + $this->assertEquals($rule1Domain, $rules['body']['rules'][0]['domain']); + + $rules = $this->listRules([ + 'queries' => [ + Query::orderDesc('$id')->toString() + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertCount(2, $rules['body']['rules']); + $this->assertEquals($rule2Id, $rules['body']['rules'][0]['$id']); + + $rules = $this->listRules([ + 'queries' => [ + Query::equal('domain', [$rule2Domain])->toString() + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertCount(1, $rules['body']['rules']); + $this->assertEquals($rule2Id, $rules['body']['rules'][0]['$id']); + + $rules = $this->listRules([ + 'search' => $rule1Domain, + 'queries' => [ Query::orderDesc('$createdAt')->toString() ] + ]); + + $this->assertEquals(200, $rules['headers']['status-code']); + $ruleIds = \array_column($rules['body']['rules'], '$id'); + $this->assertContains($rule1Id, $ruleIds); + + $rules = $this->listRules([ + 'search' => $rule2Domain, + 'queries' => [ Query::orderDesc('$createdAt')->toString() ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $ruleIds = \array_column($rules['body']['rules'], '$id'); + $this->assertContains($rule2Id, $ruleIds); + + $rules = $this->listRules([ + 'search' => $rule1Id, + 'queries' => [ Query::orderDesc('$createdAt')->toString() ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $ruleDomains = \array_column($rules['body']['rules'], 'domain'); + $this->assertContains($rule1Domain, $ruleDomains); + + $rules = $this->listRules([ + 'search' => $rule2Id, + 'queries' => [ Query::orderDesc('$createdAt')->toString() ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $ruleDomains = \array_column($rules['body']['rules'], 'domain'); + $this->assertContains($rule2Domain, $ruleDomains); + + $rules = $this->listRules(); + $this->assertEquals(200, $rules['headers']['status-code']); + foreach ($rules['body']['rules'] as $rule) { + $rule = $this->deleteRule($rule['$id']); + $this->assertEquals(204, $rule['headers']['status-code']); + } + + $rules = $this->listRules(); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertEquals(0, $rules['body']['total']); + $this->assertCount(0, $rules['body']['rules']); + } + + public function testRuleVerification(): void + { + + // 1. Site rule can verify + $site = $this->setupSite(); + $siteId = $site['siteId']; + + $rule = $this->createSiteRule('stage-site.webapp.com', $siteId); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('verifying', $rule['body']['status']); + $this->assertEmpty($rule['body']['logs']); + $this->assertNotEmpty($rule['body']['$id']); + $ruleId = $rule['body']['$id']; + + $rule = $this->updateRuleVerification($ruleId); + $this->assertEquals(200, $rule['headers']['status-code']); + $this->assertEquals($ruleId, $rule['body']['$id']); + $this->assertEquals('verifying', $rule['body']['status']); + $this->assertEmpty($rule['body']['logs']); + + $this->cleanupRule($rule['body']['$id']); + $this->cleanupSite($siteId); + + // 2. Function rule can verify + $function = $this->setupFunction(); + $functionId = $function['functionId']; + + $rule = $this->createFunctionRule('stage-function.webapp.com', $functionId); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('verifying', $rule['body']['status']); + $this->assertEmpty($rule['body']['logs']); + $this->cleanupRule($rule['body']['$id']); + + $rule = $this->createAPIRule('stage-site.webapp.com'); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + $this->assertStringContainsString('has incorrect CNAME value', $rule['body']['logs']); + $this->cleanupRule($rule['body']['$id']); + + $this->cleanupFunction($functionId); + + // 3. Wrong A record fails to verify + $rule = $this->createAPIRule('wrong-a-webapp.com'); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + $this->assertStringContainsString('is missing CNAME record', $rule['body']['logs']); + + $ruleId = $rule['body']['$id']; + $rule = $this->updateRuleVerification($ruleId); + $this->assertEquals(400, $rule['headers']['status-code']); + $this->assertStringContainsString('is missing CNAME record', $rule['body']['message']); + + $rule = $this->getRule($ruleId); + $this->assertEquals(200, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + + $this->cleanupRule($ruleId); + + // 4. Correct A record can verify + $rule = $this->createAPIRule('webapp.com'); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('verifying', $rule['body']['status']); + $this->assertEmpty($rule['body']['logs']); + + $this->cleanupRule($rule['body']['$id']); + + // 5. Correct CNAME record can verify (no CAA record) + $rule = $this->createAPIRule('stage.webapp.com'); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('verifying', $rule['body']['status']); + $this->assertEmpty($rule['body']['logs']); + + $this->cleanupRule($rule['body']['$id']); + + // 6. Missing CNAME record fails to verify + $rule = $this->createAPIRule('stage-missing-cname.webapp.com'); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + $this->assertStringContainsString('is missing CNAME record', $rule['body']['logs']); + + $ruleId = $rule['body']['$id']; + $rule = $this->updateRuleVerification($ruleId); + $this->assertEquals(400, $rule['headers']['status-code']); + $this->assertStringContainsString('is missing CNAME record', $rule['body']['message']); + + $rule = $this->getRule($ruleId); + $this->assertEquals(200, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + + $this->cleanupRule($ruleId); + + // 7. Wrong CNAME record fails to verify + $rule = $this->createAPIRule('stage-wrong-cname.webapp.com'); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + $this->assertStringContainsString('has incorrect CNAME value', $rule['body']['logs']); + + $ruleId = $rule['body']['$id']; + $rule = $this->updateRuleVerification($ruleId); + $this->assertEquals(400, $rule['headers']['status-code']); + $this->assertStringContainsString('has incorrect CNAME value', $rule['body']['message']); + + $rule = $this->getRule($ruleId); + $this->assertEquals(200, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + + $this->cleanupRule($ruleId); + + // 8. Wrong CAA record fails to verify + $rule = $this->createAPIRule('stage-wrong-caa.webapp.com'); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + $this->assertStringContainsString('has incorrect CAA value', $rule['body']['logs']); + + $ruleId = $rule['body']['$id']; + $rule = $this->updateRuleVerification($ruleId); + $this->assertEquals(400, $rule['headers']['status-code']); + $this->assertStringContainsString('has incorrect CAA value', $rule['body']['message']); + + $rule = $this->getRule($ruleId); + $this->assertEquals(200, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + + $this->cleanupRule($ruleId); + + // 9. Correct CAA record can verify + $rule = $this->createAPIRule('stage-correct-caa.webapp.com'); + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('verifying', $rule['body']['status']); + $this->assertEmpty($rule['body']['logs']); + + $this->cleanupRule($rule['body']['$id']); + } + + public function testUpdateRuleVerificationWithSameDataUpdatesTimestamp(): void + { + $domain = \uniqid() . '-timestamp-test.webapp.com'; + $rule = $this->createAPIRule($domain); + + $this->assertEquals(201, $rule['headers']['status-code']); + $this->assertEquals('created', $rule['body']['status']); + $this->assertNotEmpty($rule['body']['logs']); + + $ruleId = $rule['body']['$id']; + $initialUpdatedAt = $rule['body']['$updatedAt']; + $initiallogs = $rule['body']['logs']; + + sleep(1); + + $updatedRule = $this->updateRuleVerification($ruleId); + + $this->assertEquals(400, $updatedRule['headers']['status-code']); + $this->assertStringContainsString($initiallogs, $updatedRule['body']['message']); + + $ruleAfterUpdate = $this->getRule($ruleId); + $this->assertEquals(200, $ruleAfterUpdate['headers']['status-code']); + $this->assertEquals('created', $ruleAfterUpdate['body']['status']); + $this->assertEquals($initiallogs, $ruleAfterUpdate['body']['logs']); + $this->assertNotEquals($initialUpdatedAt, $ruleAfterUpdate['body']['$updatedAt']); + + $initialTime = new \DateTime($initialUpdatedAt); + $updatedTime = new \DateTime($ruleAfterUpdate['body']['$updatedAt']); + $this->assertGreaterThan($initialTime, $updatedTime); + + $this->cleanupRule($ruleId); } } diff --git a/tests/e2e/Services/Proxy/ProxyConsoleClientTest.php b/tests/e2e/Services/Proxy/ProxyConsoleClientTest.php new file mode 100644 index 0000000000..68761f34a9 --- /dev/null +++ b/tests/e2e/Services/Proxy/ProxyConsoleClientTest.php @@ -0,0 +1,14 @@ +listRules([ - 'queries' => [ - Query::endsWith('domain', 'webapp.com')->toString(), - Query::limit(1000)->toString(), - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - foreach ($rules['body']['rules'] as $rule) { - $ruleId = $rule['$id']; - $response = $this->deleteRule($ruleId); - $this->assertEquals(204, $response['headers']['status-code']); - } - - if ($rules['body']['total'] > 0) { - $rules = $this->listRules([ - 'queries' => [ - Query::endsWith('domain', 'webapp.com')->toString(), - Query::limit(1)->toString() - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(0, count($rules['body']['rules'])); - $this->assertEquals(0, $rules['body']['total']); - } - } - - public function testCreateRule(): void - { - $domain = \uniqid() . '-api.myapp.com'; - $rule = $this->createAPIRule($domain); - - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals($domain, $rule['body']['domain']); - $this->assertEquals('manual', $rule['body']['trigger']); - $this->assertArrayHasKey('$id', $rule['body']); - $this->assertArrayHasKey('domain', $rule['body']); - $this->assertArrayHasKey('type', $rule['body']); - $this->assertArrayHasKey('redirectUrl', $rule['body']); - $this->assertArrayHasKey('redirectStatusCode', $rule['body']); - $this->assertArrayHasKey('deploymentResourceType', $rule['body']); - $this->assertArrayHasKey('deploymentId', $rule['body']); - $this->assertArrayHasKey('deploymentResourceId', $rule['body']); - $this->assertArrayHasKey('deploymentVcsProviderBranch', $rule['body']); - $this->assertArrayHasKey('logs', $rule['body']); - $this->assertArrayHasKey('renewAt', $rule['body']); - - $ruleId = $rule['body']['$id']; - - $rule = $this->createAPIRule($domain); - $this->assertEquals(409, $rule['headers']['status-code']); - - $rule = $this->deleteRule($ruleId); - - $this->assertEquals(204, $rule['headers']['status-code']); - } - - public function testCreateRuleSetup(): void - { - $ruleId = $this->setupAPIRule(\uniqid() . '-api2.myapp.com'); - $this->cleanupRule($ruleId); - } - - public function testCreateRuleApex(): void - { - $domain = \uniqid() . '.com'; - $rule = $this->createAPIRule($domain); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - } - - public function testCreateRuleVcs(): void - { - $domain = \uniqid() . '-vcs.myapp.com'; - - $setup = $this->setupSite(); - $siteId = $setup['siteId']; - $deploymentId = $setup['deploymentId']; - - $this->assertNotEmpty($siteId); - $this->assertNotEmpty($deploymentId); - - $rule = $this->createSiteRule('commit-' . $domain, $siteId); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->cleanupRule($rule['body']['$id']); - - $rule = $this->createSiteRule('branch-' . $domain, $siteId); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->cleanupRule($rule['body']['$id']); - - $rule = $this->createSiteRule('anything-' . $domain, $siteId); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->cleanupRule($rule['body']['$id']); - - $sitesDomain = \explode(',', System::getEnv('_APP_DOMAIN_SITES', ''))[0]; - $domain = \uniqid() . '-vcs.' . $sitesDomain; - - $rule = $this->createSiteRule('commit-' . $domain, $siteId); - $this->assertEquals(400, $rule['headers']['status-code']); - - $rule = $this->createSiteRule('branch-' . $domain, $siteId); - $this->assertEquals(400, $rule['headers']['status-code']); - - $rule = $this->createSiteRule('subdomain.anything-' . $domain, $siteId); - $this->assertEquals(400, $rule['headers']['status-code']); - - $rule = $this->createSiteRule('anything-' . $domain, $siteId); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->cleanupRule($rule['body']['$id']); - } - - public function testCreateAPIRule(): void - { - $domain = \uniqid() . '-api.custom.localhost'; - - $proxyClient = new Client(); - $proxyClient->setEndpoint('http://appwrite.test'); - $proxyClient->addHeader('x-appwrite-hostname', $domain); - - $response = $proxyClient->call(Client::METHOD_GET, '/versions'); - $this->assertEquals(401, $response['headers']['status-code']); - - $ruleId = $this->setupAPIRule($domain); - - $this->assertNotEmpty($ruleId); - - $response = $proxyClient->call(Client::METHOD_GET, '/versions'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(APP_VERSION_STABLE, $response['body']['server']); - - $this->cleanupRule($ruleId); - - $rule = $this->createAPIRule('http://' . $domain); - $this->assertEquals(400, $rule['headers']['status-code']); - - $rule = $this->createAPIRule('https://' . $domain); - $this->assertEquals(400, $rule['headers']['status-code']); - - $rule = $this->createAPIRule('wss://' . $domain); - $this->assertEquals(400, $rule['headers']['status-code']); - - $rule = $this->createAPIRule($domain . '/some-path'); - $this->assertEquals(400, $rule['headers']['status-code']); - } - - public function testCreateRedirectRule(): void - { - $domain = \uniqid() . '-redirect.custom.localhost'; - - $proxyClient = new Client(); - $proxyClient->setEndpoint('http://appwrite.test'); - $proxyClient->addHeader('x-appwrite-hostname', $domain); - - $response = $proxyClient->call(Client::METHOD_GET, '/todos/1'); - $this->assertEquals(401, $response['headers']['status-code']); - - $siteId = $this->setupSite()['siteId']; - - $ruleId = $this->setupRedirectRule($domain, 'https://jsonplaceholder.typicode.com/todos/1', 301, 'site', $siteId); - $this->assertNotEmpty($ruleId); - - $response = $proxyClient->call(Client::METHOD_GET, '/todos/1'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(1, $response['body']['id']); - - $response = $proxyClient->call(Client::METHOD_GET, '/'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(1, $response['body']['id']); - - $response = $proxyClient->call(Client::METHOD_GET, '/', followRedirects: false); - $this->assertEquals(301, $response['headers']['status-code']); - $this->assertEquals('https://jsonplaceholder.typicode.com/todos/1', $response['headers']['location']); - - $domain = \uniqid() . '-redirect-307.custom.localhost'; - $ruleId = $this->setupRedirectRule($domain, 'https://jsonplaceholder.typicode.com/todos/1', 307, 'site', $siteId); - $this->assertNotEmpty($ruleId); - - $proxyClient = new Client(); - $proxyClient->setEndpoint('http://appwrite.test'); - $proxyClient->addHeader('x-appwrite-hostname', $domain); - - $response = $proxyClient->call(Client::METHOD_GET, '/', followRedirects: false); - $this->assertEquals(307, $response['headers']['status-code']); - $this->assertEquals('https://jsonplaceholder.typicode.com/todos/1', $response['headers']['location']); - - $rules = $this->listRules([ - 'queries' => [ - Query::equal('type', ['redirect'])->toString(), - Query::equal('trigger', ['manual'])->toString(), - Query::equal('deploymentResourceType', ['site'])->toString(), - Query::equal('deploymentResourceId', [$siteId])->toString(), - ], - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(2, $rules['body']['total']); - - $this->cleanupSite($siteId); - $this->cleanupRule($ruleId); - } - - public function testCreateFunctionRule(): void - { - $domain = \uniqid() . '-function.custom.localhost'; - - $proxyClient = new Client(); - $proxyClient->setEndpoint('http://appwrite.test'); - $proxyClient->addHeader('x-appwrite-hostname', $domain); - - $response = $proxyClient->call(Client::METHOD_GET, '/ping'); - $this->assertEquals(401, $response['headers']['status-code']); - - $setup = $this->setupFunction(); - $functionId = $setup['functionId']; - $deploymentId = $setup['deploymentId']; - - $this->assertNotEmpty($functionId); - $this->assertNotEmpty($deploymentId); - - $ruleId = $this->setupFunctionRule($domain, $functionId); - $this->assertNotEmpty($ruleId); - - $response = $proxyClient->call(Client::METHOD_GET, '/ping'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals($functionId, $response['body']['APPWRITE_FUNCTION_ID']); - - $this->cleanupRule($ruleId); - - $this->cleanupFunction($functionId); - - $this->assertEventually(function () use ($functionId, $deploymentId) { - $rules = $this->listRules([ - 'queries' => [ - Query::limit(1)->toString(), - Query::equal('type', ['deployment'])->toString(), - Query::equal('deploymentResourceType', ['function'])->toString(), - Query::equal('deploymentResourceId', [$functionId])->toString(), - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(0, $rules['body']['total']); - $this->assertCount(0, $rules['body']['rules']); - - $rules = $this->listRules([ - 'queries' => [ - Query::limit(1)->toString(), - Query::equal('type', ['deployment'])->toString(), - Query::equal('deploymentId', [$deploymentId])->toString() - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(0, $rules['body']['total']); - $this->assertCount(0, $rules['body']['rules']); - }); - } - - public function testCreateSiteRule(): void - { - $domain = \uniqid() . '-site.custom.localhost'; - - $proxyClient = new Client(); - $proxyClient->setEndpoint('http://appwrite.test'); - $proxyClient->addHeader('x-appwrite-hostname', $domain); - - $response = $proxyClient->call(Client::METHOD_GET, '/contact'); - $this->assertEquals(401, $response['headers']['status-code']); - - $setup = $this->setupSite(); - $siteId = $setup['siteId']; - $deploymentId = $setup['deploymentId']; - - $this->assertNotEmpty($siteId); - $this->assertNotEmpty($deploymentId); - - $ruleId = $this->setupSiteRule($domain, $siteId); - $this->assertNotEmpty($ruleId); - $rule = $this->getRule($ruleId); - $this->assertSame(200, $rule['headers']['status-code']); - $this->assertSame('created', $rule['body']['status']); - - $response = $proxyClient->call(Client::METHOD_GET, '/contact'); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertStringContainsString('Contact page', $response['body']); - - // Wildcard domains automatically get verified status - $domains = [ - \uniqid() . '.sites.localhost', - \uniqid() . '.rebranded.localhost', - ]; - foreach ($domains as $domain) { - $wildcardRuleId = $this->setupSiteRule($domain, $siteId); - $this->assertNotEmpty($wildcardRuleId); - $rule = $this->getRule($wildcardRuleId); - $this->assertSame(200, $rule['headers']['status-code']); - $this->assertSame('verified', $rule['body']['status']); - $this->cleanupRule($wildcardRuleId); - } - - $rules = $this->listRules([ - 'queries' => [ - Query::limit(1)->toString(), - Query::equal('trigger', ['deployment'])->toString(), - Query::equal('type', ['deployment'])->toString(), - Query::equal('deploymentResourceType', ['site'])->toString(), - Query::equal('deploymentResourceId', [$siteId])->toString(), - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertGreaterThan(0, $rules['body']['total']); - - $this->cleanupRule($ruleId); - - $this->cleanupSite($siteId); - - $this->assertEventually(function () use ($siteId, $deploymentId) { - $rules = $this->listRules([ - 'queries' => [ - Query::limit(1)->toString(), - Query::equal('type', ['deployment'])->toString(), - Query::equal('deploymentResourceType', ['site'])->toString(), - Query::equal('deploymentResourceId', [$siteId])->toString(), - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(0, $rules['body']['total']); - $this->assertCount(0, $rules['body']['rules']); - - $rules = $this->listRules([ - 'queries' => [ - Query::limit(1)->toString(), - Query::equal('type', ['deployment'])->toString(), - Query::equal('deploymentId', [$deploymentId])->toString() - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(0, $rules['body']['total']); - $this->assertCount(0, $rules['body']['rules']); - }); - } - - public function testCreateSiteBranchRule(): void - { - $domain = \uniqid() . '-site-branch.custom.localhost'; - - $setup = $this->setupSite(); - $siteId = $setup['siteId']; - $deploymentId = $setup['deploymentId']; - - $this->assertNotEmpty($siteId); - $this->assertNotEmpty($deploymentId); - - $ruleId = $this->setupSiteRule($domain, $siteId, 'dev'); - $this->assertNotEmpty($ruleId); - - $rule = $this->getRule($ruleId); - $this->assertEquals(200, $rule['headers']['status-code']); - - $this->cleanupRule($ruleId); - } - - public function testCreateFunctionBranchRule(): void - { - $domain = \uniqid() . '-function-branch.custom.localhost'; - - $setup = $this->setupFunction(); - $functionId = $setup['functionId']; - $deploymentId = $setup['deploymentId']; - - $this->assertNotEmpty($functionId); - $this->assertNotEmpty($deploymentId); - - $ruleId = $this->setupFunctionRule($domain, $functionId, 'dev'); - $this->assertNotEmpty($ruleId); - - $rule = $this->getRule($ruleId); - $this->assertEquals(200, $rule['headers']['status-code']); - - $this->cleanupRule($ruleId); - - $this->cleanupFunction($functionId); - } - - public function testUpdateRule(): void - { - // Create function appwrite-network domain - $functionsDomain = \explode(',', System::getEnv('_APP_DOMAIN_FUNCTIONS', ''))[0]; - $domain = \uniqid() . '-cname-api.' . $functionsDomain; - - $rule = $this->createAPIRule($domain); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('verified', $rule['body']['status']); - - $this->cleanupRule($rule['body']['$id']); - - // Create site appwrite-network domain - $sitesDomain = \explode(',', System::getEnv('_APP_DOMAIN_SITES', ''))[0]; - $domain = \uniqid() . '-cname-api.' . $sitesDomain; - - $rule = $this->createAPIRule($domain); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('verified', $rule['body']['status']); - - $this->cleanupRule($rule['body']['$id']); - - // Create + update - $domain = \uniqid() . '-cname-api.custom.com'; - - $rule = $this->createAPIRule($domain); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - - $ruleId = $rule['body']['$id']; - - $rule = $this->updateRuleVerification($ruleId); - $this->assertEquals(400, $rule['headers']['status-code']); - - $this->cleanupRule($ruleId); - } - - public function testGetRule() - { - $domain = \uniqid() . '-get.custom.localhost'; - $ruleId = $this->setupAPIRule($domain); - - $this->assertNotEmpty($ruleId); - - $rule = $this->getRule($ruleId); - $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals($domain, $rule['body']['domain']); - $this->assertEquals('manual', $rule['body']['trigger']); - $this->assertArrayHasKey('$id', $rule['body']); - $this->assertArrayHasKey('domain', $rule['body']); - $this->assertArrayHasKey('type', $rule['body']); - $this->assertArrayHasKey('redirectUrl', $rule['body']); - $this->assertArrayHasKey('redirectStatusCode', $rule['body']); - $this->assertArrayHasKey('deploymentResourceType', $rule['body']); - $this->assertArrayHasKey('deploymentId', $rule['body']); - $this->assertArrayHasKey('deploymentResourceId', $rule['body']); - $this->assertArrayHasKey('deploymentVcsProviderBranch', $rule['body']); - $this->assertArrayHasKey('logs', $rule['body']); - $this->assertArrayHasKey('renewAt', $rule['body']); - - $this->cleanupRule($ruleId); - } - - public function testListRules() - { - $rules = $this->listRules(); - $this->assertEquals(200, $rules['headers']['status-code']); - foreach ($rules['body']['rules'] as $rule) { - $rule = $this->deleteRule($rule['$id']); - $this->assertEquals(204, $rule['headers']['status-code']); - } - - $rules = $this->listRules(); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(0, $rules['body']['total']); - $this->assertCount(0, $rules['body']['rules']); - - $rule1Domain = \uniqid() . '-list1.custom.localhost'; - $rule1Id = $this->setupAPIRule($rule1Domain); - $this->assertNotEmpty($rule1Id); - - $rules = $this->listRules(); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(1, $rules['body']['total']); - $this->assertCount(1, $rules['body']['rules']); - $this->assertEquals($rule1Domain, $rules['body']['rules'][0]['domain']); - - $this->assertEquals('manual', $rules['body']['rules'][0]['trigger']); - $this->assertArrayHasKey('$id', $rules['body']['rules'][0]); - $this->assertArrayHasKey('domain', $rules['body']['rules'][0]); - $this->assertArrayHasKey('type', $rules['body']['rules'][0]); - $this->assertArrayHasKey('redirectUrl', $rules['body']['rules'][0]); - $this->assertArrayHasKey('redirectStatusCode', $rules['body']['rules'][0]); - $this->assertArrayHasKey('deploymentResourceType', $rules['body']['rules'][0]); - $this->assertArrayHasKey('deploymentId', $rules['body']['rules'][0]); - $this->assertArrayHasKey('deploymentResourceId', $rules['body']['rules'][0]); - $this->assertArrayHasKey('deploymentVcsProviderBranch', $rules['body']['rules'][0]); - $this->assertArrayHasKey('logs', $rules['body']['rules'][0]); - $this->assertArrayHasKey('renewAt', $rules['body']['rules'][0]); - - $rule2Domain = \uniqid() . '-list1.custom.localhost'; - $rule2Id = $this->setupAPIRule($rule2Domain); - $this->assertNotEmpty($rule2Id); - - $rules = $this->listRules(); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(2, $rules['body']['total']); - $this->assertCount(2, $rules['body']['rules']); - - $rules = $this->listRules([ - 'queries' => [ - Query::limit(1)->toString() - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(2, $rules['body']['total']); - $this->assertCount(1, $rules['body']['rules']); - - $rules = $this->listRules([ - 'queries' => [ - Query::equal('$id', [$rule1Id])->toString() - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertCount(1, $rules['body']['rules']); - $this->assertEquals($rule1Domain, $rules['body']['rules'][0]['domain']); - - $rules = $this->listRules([ - 'queries' => [ - Query::orderDesc('$id')->toString() - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertCount(2, $rules['body']['rules']); - $this->assertEquals($rule2Id, $rules['body']['rules'][0]['$id']); - - $rules = $this->listRules([ - 'queries' => [ - Query::equal('domain', [$rule2Domain])->toString() - ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertCount(1, $rules['body']['rules']); - $this->assertEquals($rule2Id, $rules['body']['rules'][0]['$id']); - - $rules = $this->listRules([ - 'search' => $rule1Domain, - 'queries' => [ Query::orderDesc('$createdAt')->toString() ] - ]); - - $this->assertEquals(200, $rules['headers']['status-code']); - $ruleIds = \array_column($rules['body']['rules'], '$id'); - $this->assertContains($rule1Id, $ruleIds); - - $rules = $this->listRules([ - 'search' => $rule2Domain, - 'queries' => [ Query::orderDesc('$createdAt')->toString() ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $ruleIds = \array_column($rules['body']['rules'], '$id'); - $this->assertContains($rule2Id, $ruleIds); - - $rules = $this->listRules([ - 'search' => $rule1Id, - 'queries' => [ Query::orderDesc('$createdAt')->toString() ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $ruleDomains = \array_column($rules['body']['rules'], 'domain'); - $this->assertContains($rule1Domain, $ruleDomains); - - $rules = $this->listRules([ - 'search' => $rule2Id, - 'queries' => [ Query::orderDesc('$createdAt')->toString() ] - ]); - $this->assertEquals(200, $rules['headers']['status-code']); - $ruleDomains = \array_column($rules['body']['rules'], 'domain'); - $this->assertContains($rule2Domain, $ruleDomains); - - $rules = $this->listRules(); - $this->assertEquals(200, $rules['headers']['status-code']); - foreach ($rules['body']['rules'] as $rule) { - $rule = $this->deleteRule($rule['$id']); - $this->assertEquals(204, $rule['headers']['status-code']); - } - - $rules = $this->listRules(); - $this->assertEquals(200, $rules['headers']['status-code']); - $this->assertEquals(0, $rules['body']['total']); - $this->assertCount(0, $rules['body']['rules']); - } - - public function testRuleVerification(): void - { - - // 1. Site rule can verify - $site = $this->setupSite(); - $siteId = $site['siteId']; - - $rule = $this->createSiteRule('stage-site.webapp.com', $siteId); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('verifying', $rule['body']['status']); - $this->assertEmpty($rule['body']['logs']); - $this->assertNotEmpty($rule['body']['$id']); - $ruleId = $rule['body']['$id']; - - $rule = $this->updateRuleVerification($ruleId); - $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals($ruleId, $rule['body']['$id']); - $this->assertEquals('verifying', $rule['body']['status']); - $this->assertEmpty($rule['body']['logs']); - - $this->cleanupRule($rule['body']['$id']); - $this->cleanupSite($siteId); - - // 2. Function rule can verify - $function = $this->setupFunction(); - $functionId = $function['functionId']; - - $rule = $this->createFunctionRule('stage-function.webapp.com', $functionId); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('verifying', $rule['body']['status']); - $this->assertEmpty($rule['body']['logs']); - $this->cleanupRule($rule['body']['$id']); - - $rule = $this->createAPIRule('stage-site.webapp.com'); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - $this->assertStringContainsString('has incorrect CNAME value', $rule['body']['logs']); - $this->cleanupRule($rule['body']['$id']); - - $this->cleanupFunction($functionId); - - // 3. Wrong A record fails to verify - $rule = $this->createAPIRule('wrong-a-webapp.com'); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - $this->assertStringContainsString('is missing CNAME record', $rule['body']['logs']); - - $ruleId = $rule['body']['$id']; - $rule = $this->updateRuleVerification($ruleId); - $this->assertEquals(400, $rule['headers']['status-code']); - $this->assertStringContainsString('is missing CNAME record', $rule['body']['message']); - - $rule = $this->getRule($ruleId); - $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - - $this->cleanupRule($ruleId); - - // 4. Correct A record can verify - $rule = $this->createAPIRule('webapp.com'); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('verifying', $rule['body']['status']); - $this->assertEmpty($rule['body']['logs']); - - $this->cleanupRule($rule['body']['$id']); - - // 5. Correct CNAME record can verify (no CAA record) - $rule = $this->createAPIRule('stage.webapp.com'); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('verifying', $rule['body']['status']); - $this->assertEmpty($rule['body']['logs']); - - $this->cleanupRule($rule['body']['$id']); - - // 6. Missing CNAME record fails to verify - $rule = $this->createAPIRule('stage-missing-cname.webapp.com'); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - $this->assertStringContainsString('is missing CNAME record', $rule['body']['logs']); - - $ruleId = $rule['body']['$id']; - $rule = $this->updateRuleVerification($ruleId); - $this->assertEquals(400, $rule['headers']['status-code']); - $this->assertStringContainsString('is missing CNAME record', $rule['body']['message']); - - $rule = $this->getRule($ruleId); - $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - - $this->cleanupRule($ruleId); - - // 7. Wrong CNAME record fails to verify - $rule = $this->createAPIRule('stage-wrong-cname.webapp.com'); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - $this->assertStringContainsString('has incorrect CNAME value', $rule['body']['logs']); - - $ruleId = $rule['body']['$id']; - $rule = $this->updateRuleVerification($ruleId); - $this->assertEquals(400, $rule['headers']['status-code']); - $this->assertStringContainsString('has incorrect CNAME value', $rule['body']['message']); - - $rule = $this->getRule($ruleId); - $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - - $this->cleanupRule($ruleId); - - // 8. Wrong CAA record fails to verify - $rule = $this->createAPIRule('stage-wrong-caa.webapp.com'); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - $this->assertStringContainsString('has incorrect CAA value', $rule['body']['logs']); - - $ruleId = $rule['body']['$id']; - $rule = $this->updateRuleVerification($ruleId); - $this->assertEquals(400, $rule['headers']['status-code']); - $this->assertStringContainsString('has incorrect CAA value', $rule['body']['message']); - - $rule = $this->getRule($ruleId); - $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - - $this->cleanupRule($ruleId); - - // 9. Correct CAA record can verify - $rule = $this->createAPIRule('stage-correct-caa.webapp.com'); - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('verifying', $rule['body']['status']); - $this->assertEmpty($rule['body']['logs']); - - $this->cleanupRule($rule['body']['$id']); - } - - public function testUpdateRuleVerificationWithSameDataUpdatesTimestamp(): void - { - $domain = \uniqid() . '-timestamp-test.webapp.com'; - $rule = $this->createAPIRule($domain); - - $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); - $this->assertNotEmpty($rule['body']['logs']); - - $ruleId = $rule['body']['$id']; - $initialUpdatedAt = $rule['body']['$updatedAt']; - $initiallogs = $rule['body']['logs']; - - sleep(1); - - $updatedRule = $this->updateRuleVerification($ruleId); - - $this->assertEquals(400, $updatedRule['headers']['status-code']); - $this->assertStringContainsString($initiallogs, $updatedRule['body']['message']); - - $ruleAfterUpdate = $this->getRule($ruleId); - $this->assertEquals(200, $ruleAfterUpdate['headers']['status-code']); - $this->assertEquals('created', $ruleAfterUpdate['body']['status']); - $this->assertEquals($initiallogs, $ruleAfterUpdate['body']['logs']); - $this->assertNotEquals($initialUpdatedAt, $ruleAfterUpdate['body']['$updatedAt']); - - $initialTime = new \DateTime($initialUpdatedAt); - $updatedTime = new \DateTime($ruleAfterUpdate['body']['$updatedAt']); - $this->assertGreaterThan($initialTime, $updatedTime); - - $this->cleanupRule($ruleId); - } } diff --git a/tests/e2e/Services/Proxy/ProxyHelpers.php b/tests/e2e/Services/Proxy/ProxyHelpers.php new file mode 100644 index 0000000000..ef962ce725 --- /dev/null +++ b/tests/e2e/Services/Proxy/ProxyHelpers.php @@ -0,0 +1,293 @@ +client->call(Client::METHOD_GET, '/proxy/rules', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), $params); + + return $rule; + } + + protected function createAPIRule(string $domain): mixed + { + $rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/api', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'domain' => $domain, + ]); + + return $rule; + } + + protected function updateRuleVerification(string $ruleId): mixed + { + $rule = $this->client->call(Client::METHOD_PATCH, '/proxy/rules/' . $ruleId . '/verification', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + return $rule; + } + + protected function createSiteRule(string $domain, string $siteId, string $branch = ''): mixed + { + $rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/site', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'domain' => $domain, + 'siteId' => $siteId, + 'branch' => $branch, + ]); + + return $rule; + } + + protected function getRule(string $ruleId): mixed + { + $rule = $this->client->call(Client::METHOD_GET, '/proxy/rules/' . $ruleId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + return $rule; + } + + protected function createRedirectRule(string $domain, string $url, int $statusCode, string $resourceType, string $resourceId): mixed + { + $rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/redirect', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'domain' => $domain, + 'url' => $url, + 'statusCode' => $statusCode, + 'resourceType' => $resourceType, + 'resourceId' => $resourceId, + ]); + + return $rule; + } + + protected function createFunctionRule(string $domain, string $functionId, string $branch = ''): mixed + { + $rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/function', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'domain' => $domain, + 'functionId' => $functionId, + 'branch' => $branch, + ]); + + return $rule; + } + + protected function deleteRule(string $ruleId): mixed + { + $rule = $this->client->call(Client::METHOD_DELETE, '/proxy/rules/' . $ruleId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + return $rule; + } + + protected function setupAPIRule(string $domain): string + { + $rule = $this->createAPIRule($domain); + + $this->assertEquals(201, $rule['headers']['status-code'], 'Failed to setup rule: ' . \json_encode($rule)); + + return $rule['body']['$id']; + } + + protected function setupRedirectRule(string $domain, string $url, int $statusCode, string $resourceType, string $resourceId): string + { + $rule = $this->createRedirectRule($domain, $url, $statusCode, $resourceType, $resourceId); + + $this->assertEquals(201, $rule['headers']['status-code'], 'Failed to setup rule: ' . \json_encode($rule)); + + return $rule['body']['$id']; + } + + protected function setupFunctionRule(string $domain, string $functionId, string $branch = ''): string + { + $rule = $this->createFunctionRule($domain, $functionId, $branch); + + $this->assertEquals(201, $rule['headers']['status-code'], 'Failed to setup rule: ' . \json_encode($rule)); + + return $rule['body']['$id']; + } + + protected function setupSiteRule(string $domain, string $siteId, string $branch = ''): string + { + $rule = $this->createSiteRule($domain, $siteId, $branch); + + $this->assertEquals(201, $rule['headers']['status-code'], 'Failed to setup rule: ' . \json_encode($rule)); + + return $rule['body']['$id']; + } + + protected function cleanupRule(string $ruleId): void + { + $rule = $this->deleteRule($ruleId); + $this->assertEquals(204, $rule['headers']['status-code'], 'Failed to cleanup rule: ' . \json_encode($rule)); + } + + protected function cleanupSite(string $siteId): void + { + $site = $this->client->call(Client::METHOD_DELETE, '/sites/' . $siteId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(204, $site['headers']['status-code'], 'Failed to cleanup site: ' . \json_encode($site)); + } + + protected function cleanupFunction(string $functionId): void + { + $function = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(204, $function['headers']['status-code'], 'Failed to cleanup function: ' . \json_encode($function)); + } + + protected function setupSite(): mixed + { + // Site + $site = $this->client->call(Client::METHOD_POST, '/sites', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'siteId' => ID::unique(), + 'name' => 'Proxy site', + 'framework' => 'other', + 'adapter' => 'static', + 'buildRuntime' => 'static-1', + 'outputDirectory' => './', + 'buildCommand' => '', + 'installCommand' => '', + 'fallbackFile' => '', + ]); + + $this->assertEquals($site['headers']['status-code'], 201, 'Setup site failed with status code: ' . $site['headers']['status-code'] . ' and response: ' . json_encode($site['body'], JSON_PRETTY_PRINT)); + + $siteId = $site['body']['$id']; + + // Deployment + $deployment = $this->client->call(Client::METHOD_POST, '/sites/' . $siteId . '/deployments', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'code' => $this->packageSite('static'), + 'activate' => 'true' + ]); + + $this->assertEquals($deployment['headers']['status-code'], 202, 'Setup deployment failed with status code: ' . $deployment['headers']['status-code'] . ' and response: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT)); + $deploymentId = $deployment['body']['$id'] ?? ''; + + $this->assertEventually(function () use ($siteId, $deploymentId) { + $site = $this->client->call(Client::METHOD_GET, '/sites/' . $siteId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals($deploymentId, $site['body']['deploymentId'], 'Deployment is not activated, deployment: ' . json_encode($site['body'], JSON_PRETTY_PRINT)); + }, 120000, 500); + + return ['siteId' => $siteId, 'deploymentId' => $deploymentId]; + } + + protected function setupFunction(): mixed + { + // Function + $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'functionId' => ID::unique(), + 'runtime' => 'node-22', + 'name' => 'Proxy Function', + 'entrypoint' => 'index.js', + 'commands' => '', + 'execute' => ['any'] + ]); + + $this->assertEquals($function['headers']['status-code'], 201, 'Setup function failed with status code: ' . $function['headers']['status-code'] . ' and response: ' . json_encode($function['body'], JSON_PRETTY_PRINT)); + + $functionId = $function['body']['$id']; + + // Deployment + $deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'code' => $this->packageFunction('basic'), + 'activate' => 'true' + ]); + + $this->assertEquals($deployment['headers']['status-code'], 202, 'Setup deployment failed with status code: ' . $deployment['headers']['status-code'] . ' and response: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT)); + $deploymentId = $deployment['body']['$id'] ?? ''; + + $this->assertEventually(function () use ($functionId, $deploymentId) { + $function = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals($deploymentId, $function['body']['deploymentId'], 'Deployment is not activated, deployment: ' . json_encode($function['body'], JSON_PRETTY_PRINT)); + }, 100000, 500); + + return ['functionId' => $functionId, 'deploymentId' => $deploymentId]; + } + + private function packageSite(string $site): CURLFile + { + $stdout = ''; + $stderr = ''; + + $folderPath = realpath(__DIR__ . '/../../../resources/sites') . "/$site"; + $tarPath = "$folderPath/code.tar.gz"; + + Console::execute("cd $folderPath && tar --exclude code.tar.gz --exclude node_modules -czf code.tar.gz .", '', $stdout, $stderr); + + if (filesize($tarPath) > 1024 * 1024 * 5) { + throw new \Exception('Code package is too large. Use the chunked upload method instead.'); + } + + return new CURLFile($tarPath, 'application/x-gzip', \basename($tarPath)); + } + + private function packageFunction(string $function): CURLFile + { + $stdout = ''; + $stderr = ''; + + $folderPath = realpath(__DIR__ . '/../../../resources/functions') . "/$function"; + $tarPath = "$folderPath/code.tar.gz"; + + Console::execute("cd $folderPath && tar --exclude code.tar.gz --exclude node_modules -czf code.tar.gz .", '', $stdout, $stderr); + + if (filesize($tarPath) > 1024 * 1024 * 5) { + throw new \Exception('Code package is too large. Use the chunked upload method instead.'); + } + + return new CURLFile($tarPath, 'application/x-gzip', \basename($tarPath)); + } +} From ec75f101bb0d1082c2aed451ddf763a86a4f4f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 May 2026 11:37:00 +0200 Subject: [PATCH 2/8] SDK quality fix --- .../Modules/Project/Http/Project/Keys/Ephemeral/Create.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Ephemeral/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Ephemeral/Create.php index 7fdefca218..4130effe69 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Ephemeral/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Ephemeral/Create.php @@ -59,7 +59,7 @@ class Create extends Base ], )) ->param('scopes', [], new ArrayList(new WhiteList(array_keys(Config::getParam('projectScopes')), true), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Key scopes list. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' scopes are allowed.', optional: false) - ->param('duration', null, new Range(1, 3600), 'Time in seconds before ephemeral key expires. Maximum duration is 3600 seconds.', optional: false) + ->param('duration', null, new Range(1, 3600), 'Time in seconds before ephemeral key expires. Maximum duration is 3600 seconds.', optional: false, example: 600) ->inject('response') ->inject('queueForEvents') ->inject('project') From d8349544d20b698e559de53aae0b3bcf38c4e3b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 May 2026 11:37:26 +0200 Subject: [PATCH 3/8] Revert unwanted change --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index d57e7cf421..4a6a3ac344 100644 --- a/.env +++ b/.env @@ -20,7 +20,7 @@ _APP_EMAIL_CERTIFICATES=certificates@appwrite.io _APP_SYSTEM_RESPONSE_FORMAT= _APP_CUSTOM_DOMAIN_DENY_LIST= _APP_OPTIONS_ABUSE=disabled -_APP_OPTIONS_ROUTER_PROTECTION=enabled +_APP_OPTIONS_ROUTER_PROTECTION=disabled _APP_OPTIONS_FORCE_HTTPS=disabled _APP_OPTIONS_ROUTER_FORCE_HTTPS=disabled _APP_OPENSSL_KEY_V1=your-secret-key From cd6f5c64f08eff7d5c78cc1efbda528bbcecda17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 May 2026 11:48:02 +0200 Subject: [PATCH 4/8] Improve proxy API quality --- app/config/scopes/project.php | 18 ++++++++------- .../Modules/Proxy/Http/Rules/API/Create.php | 22 ++++++++++++++---- .../Modules/Proxy/Http/Rules/Delete.php | 11 +++++---- .../Platform/Modules/Proxy/Http/Rules/Get.php | 11 +++++---- .../Proxy/Http/Rules/Verification/Update.php | 23 +++++++++++-------- .../Modules/Proxy/Http/Rules/XList.php | 19 ++++++++------- 6 files changed, 66 insertions(+), 38 deletions(-) diff --git a/app/config/scopes/project.php b/app/config/scopes/project.php index a048920de9..5f9e4877b3 100644 --- a/app/config/scopes/project.php +++ b/app/config/scopes/project.php @@ -286,6 +286,16 @@ return [ 'category' => 'Messaging', ], + // Proxy + 'rules.read' => [ + 'description' => 'Access to read proxy rules.', + 'category' => 'Proxy', + ], + 'rules.write' => [ + 'description' => 'Access to create, update, and delete proxy rules.', + 'category' => 'Proxy', + ], + // Other "webhooks.read" => [ "description" => @@ -339,12 +349,4 @@ return [ 'description' => 'Access to create, update, and delete resources under VCS service.', 'category' => 'Other', ], - 'rules.read' => [ - 'description' => 'Access to read proxy rules.', - 'category' => 'Other', - ], - 'rules.write' => [ - 'description' => 'Access to create, update, and delete proxy rules.', - 'category' => 'Other', - ], ]; diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php index a6a3e44194..13c06c3e91 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -9,11 +9,13 @@ use Appwrite\Platform\Modules\Proxy\Action; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; +use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate; use Utopia\Database\Helpers\ID; +use Utopia\Database\Validator\Authorization; use Utopia\Logger\Log; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; @@ -47,8 +49,10 @@ class Create extends Action name: 'createAPIRule', description: <<inject('dbForPlatform') ->inject('platform') ->inject('log') + ->inject('authorization') ->callback($this->action(...)); } - public function action(string $domain, Response $response, Document $project, Certificate $publisherForCertificates, Event $queueForEvents, Database $dbForPlatform, array $platform, Log $log) - { + public function action( + string $domain, + Response $response, + Document $project, + Certificate $publisherForCertificates, + Event $queueForEvents, + Database $dbForPlatform, + array $platform, + Log $log, + Authorization $authorization, + ) { $this->validateDomainRestrictions($domain, $platform); // TODO: (@Meldiron) Remove after 1.7.x migration @@ -108,7 +122,7 @@ class Create extends Action } try { - $rule = $dbForPlatform->createDocument('rules', $rule); + $rule = $authorization->skip(fn() => $dbForPlatform->createDocument('rules', $rule)); } catch (Duplicate $e) { throw new Exception(Exception::RULE_ALREADY_EXISTS); } diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php index 1d5b770496..2bbe4a530a 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php @@ -12,6 +12,7 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -43,7 +44,7 @@ class Delete extends Action description: <<inject('dbForPlatform') ->inject('queueForDeletes') ->inject('queueForEvents') + ->inject('authorization') ->callback($this->action(...)); } @@ -67,15 +69,16 @@ class Delete extends Action Document $project, Database $dbForPlatform, DeleteEvent $queueForDeletes, - Event $queueForEvents + Event $queueForEvents, + Authorization $authorization, ) { - $rule = $dbForPlatform->getDocument('rules', $ruleId); + $rule = $authorization->skip(fn() => $dbForPlatform->getDocument('rules', $ruleId)); if ($rule->isEmpty() || $rule->getAttribute('projectInternalId') !== $project->getSequence()) { throw new Exception(Exception::RULE_NOT_FOUND); } - $dbForPlatform->deleteDocument('rules', $rule->getId()); + $authorization->skip(fn() => $dbForPlatform->deleteDocument('rules', $rule->getId())); $queueForDeletes ->setType(DELETE_TYPE_DOCUMENT) diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Get.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Get.php index b88a4ffc06..03841bf1e5 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Get.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Get.php @@ -10,6 +10,7 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Platform\Scope\HTTP; @@ -39,7 +40,7 @@ class Get extends Action description: <<inject('response') ->inject('project') ->inject('dbForPlatform') + ->inject('authorization') ->callback($this->action(...)); } @@ -58,15 +60,16 @@ class Get extends Action string $ruleId, Response $response, Document $project, - Database $dbForPlatform + Database $dbForPlatform, + Authorization $authorization, ) { - $rule = $dbForPlatform->getDocument('rules', $ruleId); + $rule = $authorization->skip(fn() => $dbForPlatform->getDocument('rules', $ruleId)); if ($rule->isEmpty() || $rule->getAttribute('projectInternalId') !== $project->getSequence()) { throw new Exception(Exception::RULE_NOT_FOUND); } - $certificate = $dbForPlatform->getDocument('certificates', $rule->getAttribute('certificateId', '')); + $certificate = $authorization->skip(fn() => $dbForPlatform->getDocument('certificates', $rule->getAttribute('certificateId', ''))); // Give priority to certificate generation logs if present if (!empty($certificate->getAttribute('logs', ''))) { diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Verification/Update.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Verification/Update.php index 9e81f6ff18..f567077c23 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Verification/Update.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Verification/Update.php @@ -13,6 +13,7 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Logger\Log; use Utopia\Platform\Scope\HTTP; @@ -44,9 +45,9 @@ class Update extends Action group: null, name: 'updateRuleVerification', description: <<inject('project') ->inject('dbForPlatform') ->inject('log') + ->inject('authorization') ->callback($this->action(...)); } @@ -71,9 +73,10 @@ class Update extends Action Event $queueForEvents, Document $project, Database $dbForPlatform, - Log $log + Log $log, + Authorization $authorization, ) { - $rule = $dbForPlatform->getDocument('rules', $ruleId); + $rule = $authorization->skip(fn() => $dbForPlatform->getDocument('rules', $ruleId)); if ($rule->isEmpty() || $rule->getAttribute('projectInternalId') !== $project->getSequence()) { throw new Exception(Exception::RULE_NOT_FOUND); @@ -90,22 +93,22 @@ class Update extends Action try { $this->verifyRule($rule, $log); // Reset logs and status for the rule - $rule = $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([ + $rule = $authorization->skip(fn() => $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([ 'logs' => '', 'status' => RULE_STATUS_CERTIFICATE_GENERATING, - ])); + ]))); $certificateId = $rule->getAttribute('certificateId', ''); // Reset logs for the associated certificate. if (!empty($certificateId)) { - $certificate = $dbForPlatform->updateDocument('certificates', $certificateId, new Document([ + $certificate = $authorization->skip(fn() => $dbForPlatform->updateDocument('certificates', $certificateId, new Document([ 'logs' => '', - ])); + ]))); } } catch (Exception $err) { - $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([ + $authorization->skip(fn() => $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([ '$updatedAt' => DateTime::now(), - ])); + ]))); throw $err; } diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/XList.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/XList.php index 19daf8c8d2..655563aa3b 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/XList.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/XList.php @@ -13,6 +13,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Query as QueryException; use Utopia\Database\Query; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Query\Cursor; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -44,7 +45,7 @@ class XList extends Action description: <<param('queries', [], new Rules(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/databases#querying-documents). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Rules::ALLOWED_ATTRIBUTES), true) - ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('total', true, new Boolean(true), 'When set to false, the total count returned will be 0 and will not be calculated.', true) + ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true, deprecated: true) ->inject('response') ->inject('project') ->inject('dbForPlatform') + ->inject('authorization') ->callback($this->action(...)); } public function action( array $queries, + bool $total, string $search, - bool $includeTotal, Response $response, Document $project, - Database $dbForPlatform + Database $dbForPlatform, + Authorization $authorization, ) { try { $queries = Query::parseQueries($queries); @@ -91,7 +94,7 @@ class XList extends Action } $ruleId = $cursor->getValue(); - $cursorDocument = $dbForPlatform->getDocument('rules', $ruleId); + $cursorDocument = $authorization->skip(fn() => $dbForPlatform->getDocument('rules', $ruleId)); if ($cursorDocument->isEmpty()) { throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Rule '{$ruleId}' for the 'cursor' value not found."); @@ -102,9 +105,9 @@ class XList extends Action $filterQueries = Query::groupByType($queries)['filters']; - $rules = $dbForPlatform->find('rules', $queries); + $rules = $authorization->skip(fn() => $dbForPlatform->find('rules', $queries)); foreach ($rules as $rule) { - $certificate = $dbForPlatform->getDocument('certificates', $rule->getAttribute('certificateId', '')); + $certificate = $authorization->skip(fn() => $dbForPlatform->getDocument('certificates', $rule->getAttribute('certificateId', ''))); // Give priority to certificate generation logs if present if (!empty($certificate->getAttribute('logs', ''))) { @@ -116,7 +119,7 @@ class XList extends Action $response->dynamic(new Document([ 'rules' => $rules, - 'total' => $includeTotal ? $dbForPlatform->count('rules', $filterQueries, APP_LIMIT_COUNT) : 0, + 'total' => $total ? $authorization->skip(fn() => $dbForPlatform->count('rules', $filterQueries, APP_LIMIT_COUNT)) : 0, ]), Response::MODEL_PROXY_RULE_LIST); } } From 22318308260309c8228883a1cdab067fa09cdb45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 May 2026 11:59:04 +0200 Subject: [PATCH 5/8] Further proxy API improvements --- .../Modules/Proxy/Http/Rules/API/Create.php | 12 +++++++++--- .../Modules/Proxy/Http/Rules/Delete.php | 8 ++++---- .../Platform/Modules/Proxy/Http/Rules/Get.php | 13 ++++++++++--- .../Rules/{Verification => Status}/Update.php | 19 ++++++++++--------- .../Modules/Proxy/Http/Rules/XList.php | 19 +++++++++++++------ .../Platform/Modules/Proxy/Services/Http.php | 4 ++-- src/Appwrite/Utopia/Response/Model/Rule.php | 8 ++++---- tests/e2e/Services/Proxy/ProxyBase.php | 14 +++++++------- tests/e2e/Services/Proxy/ProxyHelpers.php | 4 ++-- 9 files changed, 61 insertions(+), 40 deletions(-) rename src/Appwrite/Platform/Modules/Proxy/Http/Rules/{Verification => Status}/Update.php (84%) diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php index 13c06c3e91..aaf94ef427 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -9,7 +9,6 @@ use Appwrite\Platform\Modules\Proxy\Action; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; -use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; @@ -45,7 +44,7 @@ class Create extends Action ->label('audits.resource', 'rule/{response.$id}') ->label('sdk', new Method( namespace: 'proxy', - group: null, + group: 'rules', name: 'createAPIRule', description: <<skip(fn() => $dbForPlatform->createDocument('rules', $rule)); + $rule = $authorization->skip(fn () => $dbForPlatform->createDocument('rules', $rule)); } catch (Duplicate $e) { throw new Exception(Exception::RULE_ALREADY_EXISTS); } @@ -140,6 +139,13 @@ class Create extends Action $queueForEvents->setParam('ruleId', $rule->getId()); + // Rename 'created' status to 'unverified' for consistency. + // 'verifying' and 'verified' statuses stay as is. + // 'unverified' in the meaning of failed certificate generation stays as is. + if ($rule->getAttribute('status') === 'created') { + $rule->setAttribute('status', 'unverified'); + } + $response ->setStatusCode(Response::STATUS_CODE_CREATED) ->dynamic($rule, Response::MODEL_PROXY_RULE); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php index 2bbe4a530a..29751ff20a 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php @@ -39,7 +39,7 @@ class Delete extends Action ->label('audits.resource', 'rule/{request.ruleId}') ->label('sdk', new Method( namespace: 'proxy', - group: null, + group: 'rules', name: 'deleteRule', description: <<inject('dbForPlatform') ->inject('queueForDeletes') ->inject('queueForEvents') - ->inject('authorization') + ->inject('authorization') ->callback($this->action(...)); } @@ -72,13 +72,13 @@ class Delete extends Action Event $queueForEvents, Authorization $authorization, ) { - $rule = $authorization->skip(fn() => $dbForPlatform->getDocument('rules', $ruleId)); + $rule = $authorization->skip(fn () => $dbForPlatform->getDocument('rules', $ruleId)); if ($rule->isEmpty() || $rule->getAttribute('projectInternalId') !== $project->getSequence()) { throw new Exception(Exception::RULE_NOT_FOUND); } - $authorization->skip(fn() => $dbForPlatform->deleteDocument('rules', $rule->getId())); + $authorization->skip(fn () => $dbForPlatform->deleteDocument('rules', $rule->getId())); $queueForDeletes ->setType(DELETE_TYPE_DOCUMENT) diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Get.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Get.php index 03841bf1e5..103ab1fddc 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Get.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Get.php @@ -35,7 +35,7 @@ class Get extends Action ->label('scope', 'rules.read') ->label('sdk', new Method( namespace: 'proxy', - group: null, + group: 'rules', name: 'getRule', description: <<skip(fn() => $dbForPlatform->getDocument('rules', $ruleId)); + $rule = $authorization->skip(fn () => $dbForPlatform->getDocument('rules', $ruleId)); if ($rule->isEmpty() || $rule->getAttribute('projectInternalId') !== $project->getSequence()) { throw new Exception(Exception::RULE_NOT_FOUND); } - $certificate = $authorization->skip(fn() => $dbForPlatform->getDocument('certificates', $rule->getAttribute('certificateId', ''))); + $certificate = $authorization->skip(fn () => $dbForPlatform->getDocument('certificates', $rule->getAttribute('certificateId', ''))); // Give priority to certificate generation logs if present if (!empty($certificate->getAttribute('logs', ''))) { @@ -78,6 +78,13 @@ class Get extends Action $rule->setAttribute('renewAt', $certificate->getAttribute('renewDate', '')); + // Rename 'created' status to 'unverified' for consistency. + // 'verifying' and 'verified' statuses stay as is. + // 'unverified' in the meaning of failed certificate generation stays as is. + if ($rule->getAttribute('status') === 'created') { + $rule->setAttribute('status', 'unverified'); + } + $response->dynamic($rule, Response::MODEL_PROXY_RULE); } } diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Verification/Update.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php similarity index 84% rename from src/Appwrite/Platform/Modules/Proxy/Http/Rules/Verification/Update.php rename to src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php index f567077c23..1ad6f730b3 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Verification/Update.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php @@ -1,6 +1,6 @@ setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) - ->setHttpPath('/v1/proxy/rules/:ruleId/verification') - ->desc('Update rule verification status') + ->setHttpPath('/v1/proxy/rules/:ruleId/status') + ->httpAlias('/v1/proxy/rules/:ruleId/verification') + ->desc('Update rule status') ->groups(['api', 'proxy']) ->label('scope', 'rules.write') ->label('event', 'rules.[ruleId].update') @@ -42,8 +43,8 @@ class Update extends Action ->label('audits.resource', 'rule/{response.$id}') ->label('sdk', new Method( namespace: 'proxy', - group: null, - name: 'updateRuleVerification', + group: 'rules', + name: 'updateRuleStatus', description: <<skip(fn() => $dbForPlatform->getDocument('rules', $ruleId)); + $rule = $authorization->skip(fn () => $dbForPlatform->getDocument('rules', $ruleId)); if ($rule->isEmpty() || $rule->getAttribute('projectInternalId') !== $project->getSequence()) { throw new Exception(Exception::RULE_NOT_FOUND); @@ -93,7 +94,7 @@ class Update extends Action try { $this->verifyRule($rule, $log); // Reset logs and status for the rule - $rule = $authorization->skip(fn() => $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([ + $rule = $authorization->skip(fn () => $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([ 'logs' => '', 'status' => RULE_STATUS_CERTIFICATE_GENERATING, ]))); @@ -101,12 +102,12 @@ class Update extends Action $certificateId = $rule->getAttribute('certificateId', ''); // Reset logs for the associated certificate. if (!empty($certificateId)) { - $certificate = $authorization->skip(fn() => $dbForPlatform->updateDocument('certificates', $certificateId, new Document([ + $certificate = $authorization->skip(fn () => $dbForPlatform->updateDocument('certificates', $certificateId, new Document([ 'logs' => '', ]))); } } catch (Exception $err) { - $authorization->skip(fn() => $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([ + $authorization->skip(fn () => $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([ '$updatedAt' => DateTime::now(), ]))); throw $err; diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/XList.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/XList.php index 655563aa3b..999b4c8d74 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/XList.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/XList.php @@ -40,7 +40,7 @@ class XList extends Action ->label('scope', 'rules.read') ->label('sdk', new Method( namespace: 'proxy', - group: null, + group: 'rules', name: 'listRules', description: <<inject('response') ->inject('project') ->inject('dbForPlatform') - ->inject('authorization') + ->inject('authorization') ->callback($this->action(...)); } @@ -94,7 +94,7 @@ class XList extends Action } $ruleId = $cursor->getValue(); - $cursorDocument = $authorization->skip(fn() => $dbForPlatform->getDocument('rules', $ruleId)); + $cursorDocument = $authorization->skip(fn () => $dbForPlatform->getDocument('rules', $ruleId)); if ($cursorDocument->isEmpty()) { throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Rule '{$ruleId}' for the 'cursor' value not found."); @@ -105,9 +105,9 @@ class XList extends Action $filterQueries = Query::groupByType($queries)['filters']; - $rules = $authorization->skip(fn() => $dbForPlatform->find('rules', $queries)); + $rules = $authorization->skip(fn () => $dbForPlatform->find('rules', $queries)); foreach ($rules as $rule) { - $certificate = $authorization->skip(fn() => $dbForPlatform->getDocument('certificates', $rule->getAttribute('certificateId', ''))); + $certificate = $authorization->skip(fn () => $dbForPlatform->getDocument('certificates', $rule->getAttribute('certificateId', ''))); // Give priority to certificate generation logs if present if (!empty($certificate->getAttribute('logs', ''))) { @@ -115,11 +115,18 @@ class XList extends Action } $rule->setAttribute('renewAt', $certificate->getAttribute('renewDate', '')); + + // Rename 'created' status to 'unverified' for consistency. + // 'verifying' and 'verified' statuses stay as is. + // 'unverified' in the meaning of failed certificate generation stays as is. + if ($rule->getAttribute('status') === 'created') { + $rule->setAttribute('status', 'unverified'); + } } $response->dynamic(new Document([ 'rules' => $rules, - 'total' => $total ? $authorization->skip(fn() => $dbForPlatform->count('rules', $filterQueries, APP_LIMIT_COUNT)) : 0, + 'total' => $total ? $authorization->skip(fn () => $dbForPlatform->count('rules', $filterQueries, APP_LIMIT_COUNT)) : 0, ]), Response::MODEL_PROXY_RULE_LIST); } } diff --git a/src/Appwrite/Platform/Modules/Proxy/Services/Http.php b/src/Appwrite/Platform/Modules/Proxy/Services/Http.php index 980c64cc54..b2a9de1933 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Proxy/Services/Http.php @@ -8,7 +8,7 @@ use Appwrite\Platform\Modules\Proxy\Http\Rules\Function\Create as CreateFunction use Appwrite\Platform\Modules\Proxy\Http\Rules\Get as GetRule; use Appwrite\Platform\Modules\Proxy\Http\Rules\Redirect\Create as CreateRedirectRule; use Appwrite\Platform\Modules\Proxy\Http\Rules\Site\Create as CreateSiteRule; -use Appwrite\Platform\Modules\Proxy\Http\Rules\Verification\Update as UpdateRuleVerification; +use Appwrite\Platform\Modules\Proxy\Http\Rules\Status\Update as UpdateRuleStatus; use Appwrite\Platform\Modules\Proxy\Http\Rules\XList as ListRules; use Utopia\Platform\Service; @@ -26,6 +26,6 @@ class Http extends Service $this->addAction(GetRule::getName(), new GetRule()); $this->addAction(ListRules::getName(), new ListRules()); $this->addAction(DeleteRule::getName(), new DeleteRule()); - $this->addAction(UpdateRuleVerification::getName(), new UpdateRuleVerification()); + $this->addAction(UpdateRuleStatus::getName(), new UpdateRuleStatus()); } } diff --git a/src/Appwrite/Utopia/Response/Model/Rule.php b/src/Appwrite/Utopia/Response/Model/Rule.php index 1ff854e7ce..d5ea9ee0b7 100644 --- a/src/Appwrite/Utopia/Response/Model/Rule.php +++ b/src/Appwrite/Utopia/Response/Model/Rule.php @@ -74,7 +74,7 @@ class Rule extends Model ]) ->addRule('deploymentResourceId', [ 'type' => self::TYPE_STRING, - 'description' => 'ID deployment\'s resource. Used if type is "deployment"', + 'description' => 'ID of deployment\'s resource (site or function ID). Used if type is "deployment"', 'default' => '', 'example' => 'n3u9feiwmf', ]) @@ -86,10 +86,10 @@ class Rule extends Model ]) ->addRule('status', [ 'type' => self::TYPE_ENUM, - 'description' => 'Domain verification status. Possible values are "created", "verifying", "verified" and "unverified"', - 'default' => 'created', + 'description' => 'Domain verification status. Possible values are "unverified", "verifying", "verified"', + 'default' => 'unverified', 'example' => 'verified', - 'enum' => ['created', 'verifying', 'verified', 'unverified'], + 'enum' => ['unverified', 'verifying', 'verified'], ]) ->addRule('logs', [ 'type' => self::TYPE_STRING, diff --git a/tests/e2e/Services/Proxy/ProxyBase.php b/tests/e2e/Services/Proxy/ProxyBase.php index 33ef64591b..c33dfbe778 100644 --- a/tests/e2e/Services/Proxy/ProxyBase.php +++ b/tests/e2e/Services/Proxy/ProxyBase.php @@ -425,7 +425,7 @@ trait ProxyBase $ruleId = $rule['body']['$id']; - $rule = $this->updateRuleVerification($ruleId); + $rule = $this->updateRuleStatus($ruleId); $this->assertEquals(400, $rule['headers']['status-code']); $this->cleanupRule($ruleId); @@ -599,7 +599,7 @@ trait ProxyBase $this->assertNotEmpty($rule['body']['$id']); $ruleId = $rule['body']['$id']; - $rule = $this->updateRuleVerification($ruleId); + $rule = $this->updateRuleStatus($ruleId); $this->assertEquals(200, $rule['headers']['status-code']); $this->assertEquals($ruleId, $rule['body']['$id']); $this->assertEquals('verifying', $rule['body']['status']); @@ -633,7 +633,7 @@ trait ProxyBase $this->assertStringContainsString('is missing CNAME record', $rule['body']['logs']); $ruleId = $rule['body']['$id']; - $rule = $this->updateRuleVerification($ruleId); + $rule = $this->updateRuleStatus($ruleId); $this->assertEquals(400, $rule['headers']['status-code']); $this->assertStringContainsString('is missing CNAME record', $rule['body']['message']); @@ -666,7 +666,7 @@ trait ProxyBase $this->assertStringContainsString('is missing CNAME record', $rule['body']['logs']); $ruleId = $rule['body']['$id']; - $rule = $this->updateRuleVerification($ruleId); + $rule = $this->updateRuleStatus($ruleId); $this->assertEquals(400, $rule['headers']['status-code']); $this->assertStringContainsString('is missing CNAME record', $rule['body']['message']); @@ -683,7 +683,7 @@ trait ProxyBase $this->assertStringContainsString('has incorrect CNAME value', $rule['body']['logs']); $ruleId = $rule['body']['$id']; - $rule = $this->updateRuleVerification($ruleId); + $rule = $this->updateRuleStatus($ruleId); $this->assertEquals(400, $rule['headers']['status-code']); $this->assertStringContainsString('has incorrect CNAME value', $rule['body']['message']); @@ -700,7 +700,7 @@ trait ProxyBase $this->assertStringContainsString('has incorrect CAA value', $rule['body']['logs']); $ruleId = $rule['body']['$id']; - $rule = $this->updateRuleVerification($ruleId); + $rule = $this->updateRuleStatus($ruleId); $this->assertEquals(400, $rule['headers']['status-code']); $this->assertStringContainsString('has incorrect CAA value', $rule['body']['message']); @@ -734,7 +734,7 @@ trait ProxyBase sleep(1); - $updatedRule = $this->updateRuleVerification($ruleId); + $updatedRule = $this->updateRuleStatus($ruleId); $this->assertEquals(400, $updatedRule['headers']['status-code']); $this->assertStringContainsString($initiallogs, $updatedRule['body']['message']); diff --git a/tests/e2e/Services/Proxy/ProxyHelpers.php b/tests/e2e/Services/Proxy/ProxyHelpers.php index ef962ce725..6f15abdad8 100644 --- a/tests/e2e/Services/Proxy/ProxyHelpers.php +++ b/tests/e2e/Services/Proxy/ProxyHelpers.php @@ -34,9 +34,9 @@ trait ProxyHelpers return $rule; } - protected function updateRuleVerification(string $ruleId): mixed + protected function updateRuleStatus(string $ruleId): mixed { - $rule = $this->client->call(Client::METHOD_PATCH, '/proxy/rules/' . $ruleId . '/verification', array_merge([ + $rule = $this->client->call(Client::METHOD_PATCH, '/proxy/rules/' . $ruleId . '/status', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), []); From 2263632f7d269f2e49e8c6006a048340b7c5becc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 May 2026 12:06:37 +0200 Subject: [PATCH 6/8] Fix all proxy create endpoints --- .../Proxy/Http/Rules/Function/Create.php | 34 +++++++++++++++--- .../Proxy/Http/Rules/Redirect/Create.php | 36 ++++++++++++++++--- .../Modules/Proxy/Http/Rules/Site/Create.php | 34 +++++++++++++++--- 3 files changed, 89 insertions(+), 15 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php index 4a8bd4897e..200182cf03 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php @@ -14,6 +14,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate; use Utopia\Database\Helpers\ID; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Logger\Log; use Utopia\Platform\Scope\HTTP; @@ -45,12 +46,14 @@ class Create extends Action ->label('audits.resource', 'rule/{response.$id}') ->label('sdk', new Method( namespace: 'proxy', - group: null, + group: 'rules', name: 'createFunctionRule', description: <<inject('dbForProject') ->inject('platform') ->inject('log') + ->inject('authorization') ->callback($this->action(...)); } - public function action(string $domain, string $functionId, string $branch, Response $response, Document $project, Certificate $publisherForCertificates, Event $queueForEvents, Database $dbForPlatform, Database $dbForProject, array $platform, Log $log) - { + public function action( + string $domain, + string $functionId, + string $branch, + Response $response, + Document $project, + Certificate $publisherForCertificates, + Event $queueForEvents, + Database $dbForPlatform, + Database $dbForProject, + array $platform, + Log $log, + Authorization $authorization, + ) { + $this->validateDomainRestrictions($domain, $platform); $function = $dbForProject->getDocument('functions', $functionId); @@ -126,7 +143,7 @@ class Create extends Action } try { - $rule = $dbForPlatform->createDocument('rules', $rule); + $rule = $authorization->skip(fn () => $dbForPlatform->createDocument('rules', $rule)); } catch (Duplicate $e) { throw new Exception(Exception::RULE_ALREADY_EXISTS); } @@ -144,6 +161,13 @@ class Create extends Action $queueForEvents->setParam('ruleId', $rule->getId()); + // Rename 'created' status to 'unverified' for consistency. + // 'verifying' and 'verified' statuses stay as is. + // 'unverified' in the meaning of failed certificate generation stays as is. + if ($rule->getAttribute('status') === 'created') { + $rule->setAttribute('status', 'unverified'); + } + $response ->setStatusCode(Response::STATUS_CODE_CREATED) ->dynamic($rule, Response::MODEL_PROXY_RULE); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php index 5964a20772..f5105704ce 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php @@ -14,6 +14,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate; use Utopia\Database\Helpers\ID; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Logger\Log; use Utopia\Platform\Scope\HTTP; @@ -46,12 +47,14 @@ class Create extends Action ->label('audits.resource', 'rule/{response.$id}') ->label('sdk', new Method( namespace: 'proxy', - group: null, + group: 'rules', name: 'createRedirectRule', description: <<inject('dbForProject') ->inject('platform') ->inject('log') + ->inject('authorization') ->callback($this->action(...)); } - public function action(string $domain, string $url, int $statusCode, string $resourceId, string $resourceType, Response $response, Document $project, Certificate $publisherForCertificates, Event $queueForEvents, Database $dbForPlatform, Database $dbForProject, array $platform, Log $log) - { + public function action( + string $domain, + string $url, + int $statusCode, + string $resourceId, + string $resourceType, + Response $response, + Document $project, + Certificate $publisherForCertificates, + Event $queueForEvents, + Database $dbForPlatform, + Database $dbForProject, + array $platform, + Log $log, + Authorization $authorization, + ) { + $this->validateDomainRestrictions($domain, $platform); $collection = match ($resourceType) { @@ -131,7 +150,7 @@ class Create extends Action } try { - $rule = $dbForPlatform->createDocument('rules', $rule); + $rule = $authorization->skip(fn () => $dbForPlatform->createDocument('rules', $rule)); } catch (Duplicate $e) { throw new Exception(Exception::RULE_ALREADY_EXISTS); } @@ -149,6 +168,13 @@ class Create extends Action $queueForEvents->setParam('ruleId', $rule->getId()); + // Rename 'created' status to 'unverified' for consistency. + // 'verifying' and 'verified' statuses stay as is. + // 'unverified' in the meaning of failed certificate generation stays as is. + if ($rule->getAttribute('status') === 'created') { + $rule->setAttribute('status', 'unverified'); + } + $response ->setStatusCode(Response::STATUS_CODE_CREATED) ->dynamic($rule, Response::MODEL_PROXY_RULE); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php index a9dfa93a49..7b959055dc 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php @@ -14,6 +14,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate; use Utopia\Database\Helpers\ID; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Logger\Log; use Utopia\Platform\Scope\HTTP; @@ -45,12 +46,14 @@ class Create extends Action ->label('audits.resource', 'rule/{response.$id}') ->label('sdk', new Method( namespace: 'proxy', - group: null, + group: 'rules', name: 'createSiteRule', description: <<inject('dbForProject') ->inject('platform') ->inject('log') + ->inject('authorization') ->callback($this->action(...)); } - public function action(string $domain, string $siteId, ?string $branch, Response $response, Document $project, Certificate $publisherForCertificates, Event $queueForEvents, Database $dbForPlatform, Database $dbForProject, array $platform, Log $log) - { + public function action( + string $domain, + string $siteId, + ?string $branch, + Response $response, + Document $project, + Certificate $publisherForCertificates, + Event $queueForEvents, + Database $dbForPlatform, + Database $dbForProject, + array $platform, + Log $log, + Authorization $authorization, + ) { + $this->validateDomainRestrictions($domain, $platform); $site = $dbForProject->getDocument('sites', $siteId); @@ -126,7 +143,7 @@ class Create extends Action } try { - $rule = $dbForPlatform->createDocument('rules', $rule); + $rule = $authorization->skip(fn () => $dbForPlatform->createDocument('rules', $rule)); } catch (Duplicate $e) { throw new Exception(Exception::RULE_ALREADY_EXISTS); } @@ -144,6 +161,13 @@ class Create extends Action $queueForEvents->setParam('ruleId', $rule->getId()); + // Rename 'created' status to 'unverified' for consistency. + // 'verifying' and 'verified' statuses stay as is. + // 'unverified' in the meaning of failed certificate generation stays as is. + if ($rule->getAttribute('status') === 'created') { + $rule->setAttribute('status', 'unverified'); + } + $response ->setStatusCode(Response::STATUS_CODE_CREATED) ->dynamic($rule, Response::MODEL_PROXY_RULE); From 879dc6873ec3f27e43f8453a1159e362e233082f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 May 2026 12:07:52 +0200 Subject: [PATCH 7/8] review fixes --- .../Modules/Proxy/Http/Rules/API/Create.php | 2 +- .../Proxy/Http/Rules/Function/Create.php | 2 +- .../Proxy/Http/Rules/Redirect/Create.php | 2 +- .../Modules/Proxy/Http/Rules/Site/Create.php | 2 +- tests/e2e/Services/Proxy/ProxyBase.php | 24 +++++++++---------- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php index aaf94ef427..6f2e40d13f 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -49,7 +49,7 @@ class Create extends Action description: <<createAPIRule($domain); $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); } public function testCreateRuleVcs(): void @@ -421,7 +421,7 @@ trait ProxyBase $rule = $this->createAPIRule($domain); $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $ruleId = $rule['body']['$id']; @@ -620,7 +620,7 @@ trait ProxyBase $rule = $this->createAPIRule('stage-site.webapp.com'); $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $this->assertStringContainsString('has incorrect CNAME value', $rule['body']['logs']); $this->cleanupRule($rule['body']['$id']); @@ -629,7 +629,7 @@ trait ProxyBase // 3. Wrong A record fails to verify $rule = $this->createAPIRule('wrong-a-webapp.com'); $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $this->assertStringContainsString('is missing CNAME record', $rule['body']['logs']); $ruleId = $rule['body']['$id']; @@ -639,7 +639,7 @@ trait ProxyBase $rule = $this->getRule($ruleId); $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $this->cleanupRule($ruleId); @@ -662,7 +662,7 @@ trait ProxyBase // 6. Missing CNAME record fails to verify $rule = $this->createAPIRule('stage-missing-cname.webapp.com'); $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $this->assertStringContainsString('is missing CNAME record', $rule['body']['logs']); $ruleId = $rule['body']['$id']; @@ -672,14 +672,14 @@ trait ProxyBase $rule = $this->getRule($ruleId); $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $this->cleanupRule($ruleId); // 7. Wrong CNAME record fails to verify $rule = $this->createAPIRule('stage-wrong-cname.webapp.com'); $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $this->assertStringContainsString('has incorrect CNAME value', $rule['body']['logs']); $ruleId = $rule['body']['$id']; @@ -689,14 +689,14 @@ trait ProxyBase $rule = $this->getRule($ruleId); $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $this->cleanupRule($ruleId); // 8. Wrong CAA record fails to verify $rule = $this->createAPIRule('stage-wrong-caa.webapp.com'); $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $this->assertStringContainsString('has incorrect CAA value', $rule['body']['logs']); $ruleId = $rule['body']['$id']; @@ -706,7 +706,7 @@ trait ProxyBase $rule = $this->getRule($ruleId); $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $this->cleanupRule($ruleId); @@ -725,7 +725,7 @@ trait ProxyBase $rule = $this->createAPIRule($domain); $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('created', $rule['body']['status']); + $this->assertEquals('unverified', $rule['body']['status']); $this->assertNotEmpty($rule['body']['logs']); $ruleId = $rule['body']['$id']; From d7d0ecb106f385ddeb53d59f4831badfbfa132e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 4 May 2026 12:17:34 +0200 Subject: [PATCH 8/8] Fix failing tests --- tests/e2e/Services/Proxy/ProxyBase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/e2e/Services/Proxy/ProxyBase.php b/tests/e2e/Services/Proxy/ProxyBase.php index 307c4b20db..32ee13b5f7 100644 --- a/tests/e2e/Services/Proxy/ProxyBase.php +++ b/tests/e2e/Services/Proxy/ProxyBase.php @@ -290,7 +290,7 @@ trait ProxyBase $this->assertNotEmpty($ruleId); $rule = $this->getRule($ruleId); $this->assertSame(200, $rule['headers']['status-code']); - $this->assertSame('created', $rule['body']['status']); + $this->assertSame('unverified', $rule['body']['status']); $response = $proxyClient->call(Client::METHOD_GET, '/contact'); $this->assertEquals(200, $response['headers']['status-code']); @@ -741,7 +741,7 @@ trait ProxyBase $ruleAfterUpdate = $this->getRule($ruleId); $this->assertEquals(200, $ruleAfterUpdate['headers']['status-code']); - $this->assertEquals('created', $ruleAfterUpdate['body']['status']); + $this->assertEquals('unverified', $ruleAfterUpdate['body']['status']); $this->assertEquals($initiallogs, $ruleAfterUpdate['body']['logs']); $this->assertNotEquals($initialUpdatedAt, $ruleAfterUpdate['body']['$updatedAt']);