diff --git a/src/Appwrite/Event/Message/Certificate.php b/src/Appwrite/Event/Message/Certificate.php index a189bb8187..d23d674785 100644 --- a/src/Appwrite/Event/Message/Certificate.php +++ b/src/Appwrite/Event/Message/Certificate.php @@ -12,6 +12,12 @@ final class Certificate extends Base public readonly bool $skipRenewCheck = false, public readonly ?string $validationDomain = null, public readonly string $action = \Appwrite\Event\Certificate::ACTION_GENERATION, + /** + * The enqueuer verified the domain's DNS itself, moments before enqueueing, + * so the worker does not verify it again. Unlike `skipRenewCheck` this + * leaves the renew check in place. + */ + public readonly bool $skipDomainValidation = false, ) { } @@ -27,6 +33,7 @@ final class Certificate extends Base 'skipRenewCheck' => $this->skipRenewCheck, 'validationDomain' => $this->validationDomain, 'action' => $this->action, + 'skipDomainValidation' => $this->skipDomainValidation, ]; } @@ -38,6 +45,7 @@ final class Certificate extends Base skipRenewCheck: $data['skipRenewCheck'] ?? false, validationDomain: $data['validationDomain'] ?? null, action: $data['action'] ?? \Appwrite\Event\Certificate::ACTION_GENERATION, + skipDomainValidation: $data['skipDomainValidation'] ?? false, ); } } 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 8b3f5137d6..0450de3d9c 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -134,6 +134,8 @@ class Create extends Action 'domainType' => $rule->getAttribute('deploymentResourceType', $rule->getAttribute('type')), ]), action: \Appwrite\Event\Certificate::ACTION_GENERATION, + // verifyRule() above passed, which is what put the rule in this status. + skipDomainValidation: true, )); } 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 c2313b8825..75f193d8f3 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php @@ -162,6 +162,8 @@ class Create extends Action 'domainType' => $rule->getAttribute('deploymentResourceType', $rule->getAttribute('type')), ]), action: \Appwrite\Event\Certificate::ACTION_GENERATION, + // A rule reaches this status only through verifyRule() above. + skipDomainValidation: $rule->getAttribute('status', '') === RULE_STATUS_CERTIFICATE_GENERATING, )); } 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 e44308e912..86fd81244c 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php @@ -178,6 +178,8 @@ class Create extends Action 'domainType' => $rule->getAttribute('deploymentResourceType', $rule->getAttribute('type')), ]), action: \Appwrite\Event\Certificate::ACTION_GENERATION, + // verifyRule() above passed, which is what put the rule in this status. + skipDomainValidation: true, )); } 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 5d768a3f22..4c89fd38b0 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php @@ -162,6 +162,8 @@ class Create extends Action 'domainType' => $rule->getAttribute('deploymentResourceType', $rule->getAttribute('type')), ]), action: \Appwrite\Event\Certificate::ACTION_GENERATION, + // A rule reaches this status only through verifyRule() above. + skipDomainValidation: $rule->getAttribute('status', '') === RULE_STATUS_CERTIFICATE_GENERATING, )); } diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php index b6631071cf..b2af897aee 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php @@ -116,13 +116,15 @@ class Update extends Action throw $err; } - // Issue a TLS certificate when DNS verification is successful + // Issue a TLS certificate when DNS verification is successful. The worker + // does not verify DNS again: verifyRule() above just did. $publisherForCertificates->enqueue(new \Appwrite\Event\Message\Certificate( project: $project, domain: new Document([ 'domain' => $rule->getAttribute('domain'), 'domainType' => $rule->getAttribute('deploymentResourceType', $rule->getAttribute('type')), ]), + skipDomainValidation: true, )); if (!empty($certificate)) { diff --git a/src/Appwrite/Platform/Workers/Certificates.php b/src/Appwrite/Platform/Workers/Certificates.php index b9a139b32a..7d0638038b 100644 --- a/src/Appwrite/Platform/Workers/Certificates.php +++ b/src/Appwrite/Platform/Workers/Certificates.php @@ -108,6 +108,7 @@ class Certificates extends Action $domain = new Domain($document->getAttribute('domain', '')); $domainType = $document->getAttribute('domainType'); $skipRenewCheck = $certificateMessage->skipRenewCheck; + $skipDomainValidation = $certificateMessage->skipDomainValidation; $validationDomain = $certificateMessage->validationDomain; $action = $certificateMessage->action; @@ -119,7 +120,7 @@ class Certificates extends Action break; case \Appwrite\Event\Certificate::ACTION_GENERATION: - $this->handleCertificateGenerationAction($domain, $domainType, $dbForPlatform, $publisherForMails, $queueForEvents, $queueForWebhooks, $publisherForFunctions, $queueForRealtime, $certificates, $authorization, $bus, $skipRenewCheck, $plan, $validationDomain); + $this->handleCertificateGenerationAction($domain, $domainType, $dbForPlatform, $publisherForMails, $queueForEvents, $queueForWebhooks, $publisherForFunctions, $queueForRealtime, $certificates, $authorization, $bus, $skipRenewCheck, $plan, $validationDomain, $skipDomainValidation); break; default: @@ -201,6 +202,7 @@ class Certificates extends Action 'domainType' => $rule->getAttribute('deploymentResourceType', $rule->getAttribute('type')), ]), action: \Appwrite\Event\Certificate::ACTION_GENERATION, + skipDomainValidation: true, )); Console::success('Certificate generation triggered successfully.'); @@ -221,6 +223,7 @@ class Certificates extends Action * @param bool $skipRenewCheck * @param array $plan * @param string|null $validationDomain + * @param bool $skipDomainValidation The enqueuer verified DNS itself moments ago * @return void * @throws Authorization * @throws Conflict @@ -244,7 +247,8 @@ class Certificates extends Action Bus $bus, bool $skipRenewCheck = false, array $plan = [], - ?string $validationDomain = null + ?string $validationDomain = null, + bool $skipDomainValidation = false ): void { /** * 1. Read arguments and validate domain @@ -310,9 +314,14 @@ class Certificates extends Action // Ensure certificate is associated with the rule $rule->setAttribute('certificateId', $certificate->getId()); - // Validate domain and DNS records. Skip if job is forced + // Validate domain and DNS records. Skip if job is forced, or if the + // enqueuer verified DNS itself moments ago: a second run of the same + // check can only agree, or fail on a transient and contradict the + // status the enqueuer just wrote. if (!$skipRenewCheck) { - $this->validateDomain($rule, $domain, $validationDomain); + if (!$skipDomainValidation) { + $this->validateDomain($rule, $domain, $validationDomain); + } // If certificate exists already, double-check expiry date. Skip if job is forced if (!$certificates->isRenewRequired($domain->get(), $domainType)) { diff --git a/tests/unit/Event/Message/CertificateTest.php b/tests/unit/Event/Message/CertificateTest.php new file mode 100644 index 0000000000..fcd617a369 --- /dev/null +++ b/tests/unit/Event/Message/CertificateTest.php @@ -0,0 +1,46 @@ + 'project-1', '$sequence' => '1']), + domain: new Document(['domain' => 'www.example.com', 'domainType' => 'site']), + action: CertificateEvent::ACTION_GENERATION, + skipDomainValidation: true, + ); + + $restored = CertificateMessage::fromArray($message->toArray()); + + $this->assertTrue($restored->skipDomainValidation); + // The renew check is a separate decision and stays in place. + $this->assertFalse($restored->skipRenewCheck); + $this->assertSame('www.example.com', $restored->domain->getAttribute('domain')); + } + + public function testAPayloadFromBeforeTheFlagStillValidatesTheDomain(): void + { + // Jobs already sitting in the queue when this ships carry no key. + $restored = CertificateMessage::fromArray([ + 'project' => ['$id' => 'project-1', '$sequence' => '1', 'database' => ''], + 'domain' => ['domain' => 'www.example.com', 'domainType' => 'site'], + 'skipRenewCheck' => false, + 'validationDomain' => null, + 'action' => CertificateEvent::ACTION_GENERATION, + ]); + + $this->assertFalse($restored->skipDomainValidation); + } +} diff --git a/tests/unit/Workers/CertificatesDomainValidationTest.php b/tests/unit/Workers/CertificatesDomainValidationTest.php new file mode 100644 index 0000000000..7671cbf16d --- /dev/null +++ b/tests/unit/Workers/CertificatesDomainValidationTest.php @@ -0,0 +1,155 @@ +createMock(Provider::class); + $certificates->method('isRenewRequired')->willReturn(true); + $certificates->method('isInstantGeneration')->willReturn(false); + $certificates->expects($this->once())->method('issueCertificate'); + + $writes = $this->generate($certificates, skipDomainValidation: true); + + $this->assertSame(RULE_STATUS_CERTIFICATE_GENERATING, $writes['rules']['status']); + } + + public function testTheRenewCheckStaysInPlaceWhenOnlyDomainValidationIsSkipped(): void + { + // Unlike a forced job, this one still asks whether a renewal is needed. + $certificates = $this->createMock(Provider::class); + $certificates->method('isRenewRequired')->willReturn(false); + $certificates->expects($this->never())->method('issueCertificate'); + + $writes = $this->generate($certificates, skipDomainValidation: true); + + $this->assertSame(RULE_STATUS_CERTIFICATE_GENERATING, $writes['rules']['status']); + } + + public function testAJobWithoutTheFlagStillChecksDns(): void + { + $certificates = $this->createMock(Provider::class); + $certificates->expects($this->never())->method('issueCertificate'); + + $writes = $this->generate($certificates, skipDomainValidation: false); + + $this->assertSame(RULE_STATUS_CERTIFICATE_GENERATION_FAILED, $writes['rules']['status']); + } + + /** + * Runs one generation job for a rule in `verifying`. + * + * @return array> the attributes written per collection + */ + private function generate(Provider&Stub $certificates, bool $skipDomainValidation): array + { + $rule = new Document([ + '$id' => md5(self::DOMAIN), + '$collection' => 'rules', + 'domain' => self::DOMAIN, + 'type' => 'deployment', + 'deploymentResourceType' => 'site', + 'status' => RULE_STATUS_CERTIFICATE_GENERATING, + 'owner' => '', + 'certificateId' => 'cert-1', + 'projectId' => 'project-1', + 'projectInternalId' => '1', + ]); + + $certificate = new Document([ + '$id' => 'cert-1', + '$collection' => 'certificates', + '$updatedAt' => DateTime::now(), + 'domain' => self::DOMAIN, + 'attempts' => 0, + 'logs' => '', + ]); + + $writes = []; + + $dbForPlatform = $this->createStub(Database::class); + $dbForPlatform->method('getDocument') + ->willReturnCallback(fn (string $collection, string $id): Document => match ($collection) { + 'rules' => $rule, + 'certificates' => $certificate, + // No project row: the event fan-out stops there. + default => new Document(), + }); + $dbForPlatform->method('findOne')->willReturn($rule); + $dbForPlatform->method('updateDocument') + ->willReturnCallback(function (string $collection, string $id, Document $document) use (&$writes): Document { + $writes[$collection] = $document->getArrayCopy(); + + return new Document(\array_merge(['$id' => $id], $document->getArrayCopy())); + }); + + $message = (new Message())->setPayload((new CertificateMessage( + project: new Document(['$id' => 'project-1', '$sequence' => '1']), + domain: new Document(['domain' => self::DOMAIN, 'domainType' => 'site']), + action: CertificateEvent::ACTION_GENERATION, + skipDomainValidation: $skipDomainValidation, + ))->toArray()); + + try { + (new Certificates())->action( + $message, + $dbForPlatform, + new MailPublisher(new MockPublisher(), new Queue('v1-mails')), + $this->createStub(Event::class), + $this->createStub(Webhook::class), + new FunctionPublisher(new MockPublisher(), new Queue('v1-functions')), + $this->createStub(Realtime::class), + new CertificatePublisher(new MockPublisher(), new Queue('v1-certificates')), + $certificates, + [], + $this->createStub(Authorization::class), + (new Bus())->setResolver(static fn (): null => null), + ); + } catch (\Throwable) { + // The worker rethrows an issuance failure after recording it; the + // records are what these tests read. + } + + return $writes; + } +}