mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #13765 from appwrite/fix/skip-domain-validation-after-api-check
fix(certificates): do not verify DNS again for a job the endpoint just verified
This commit is contained in:
9 files changed
+233
-5
No files matched your search
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Event\Message;
|
||||
|
||||
use Appwrite\Event\Certificate as CertificateEvent;
|
||||
use Appwrite\Event\Message\Certificate as CertificateMessage;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Database\Document;
|
||||
|
||||
require_once __DIR__ . '/../../../../app/init.php';
|
||||
|
||||
final class CertificateTest extends TestCase
|
||||
{
|
||||
public function testSkipDomainValidationRoundTripsThroughTheQueuePayload(): void
|
||||
{
|
||||
$message = new CertificateMessage(
|
||||
project: new Document(['$id' => '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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Workers;
|
||||
|
||||
use Appwrite\Event\Certificate as CertificateEvent;
|
||||
use Appwrite\Event\Event;
|
||||
use Appwrite\Event\Message\Certificate as CertificateMessage;
|
||||
use Appwrite\Event\Publisher\Certificate as CertificatePublisher;
|
||||
use Appwrite\Event\Publisher\Func as FunctionPublisher;
|
||||
use Appwrite\Event\Publisher\Mail as MailPublisher;
|
||||
use Appwrite\Event\Realtime;
|
||||
use Appwrite\Event\Webhook;
|
||||
use Appwrite\Platform\Workers\Certificates;
|
||||
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
|
||||
use PHPUnit\Framework\MockObject\Stub;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Tests\Unit\Event\MockPublisher;
|
||||
use Utopia\Bus\Bus;
|
||||
use Utopia\Cdn\Certificates\Provider;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\DateTime;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Queue\Message;
|
||||
use Utopia\Queue\Queue;
|
||||
|
||||
require_once __DIR__ . '/../../../app/init.php';
|
||||
|
||||
/**
|
||||
* The proxy endpoints verify a domain's DNS before they enqueue generation.
|
||||
* The worker ran the same check again seconds later, and a transient failure
|
||||
* there wrote `unverified` over a status the endpoint had just written. The
|
||||
* domain here sits under a TLD no public suffix list knows, which the check
|
||||
* refuses before touching the network, so what the tests observe is whether
|
||||
* the check ran: a job that reaches issuance skipped it, a job that fails the
|
||||
* rule did not.
|
||||
*/
|
||||
#[AllowMockObjectsWithoutExpectations]
|
||||
final class CertificatesDomainValidationTest extends TestCase
|
||||
{
|
||||
private const DOMAIN = 'www.example.invalid';
|
||||
|
||||
public function testAJobFromAVerifiedEndpointReachesIssuanceWithoutASecondDnsCheck(): void
|
||||
{
|
||||
$certificates = $this->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<string, array<string, mixed>> 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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user