Repository navigation
fix(certificates): issue a certificate when the provider holds none instead of skipping - #13571
HarshMN2345 wants to merge 7 commits into
Conversation
…nstead of skipping When a generation job found no renewal due it stood down without ever asking the provider whether a certificate existed. With the Fastly provider that answer is also what a domain on the active service version reports when it has no TLS subscription at all, so a rule whose subscription was never created, or was deleted, stayed in certificate-generating for good: every job skipped, nothing was issued and nothing surfaced in the rule logs. Ask for the provider's certificate status before standing down. An issued certificate verifies the rule and resets attempts; an in-flight one keeps the rule generating; anything else falls through to issuance so the missing subscription is repaired. Instant providers have no status to report, so a valid certificate there is treated as issued.
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
|
This PR touches only Generated by Claude Code |
…ertificates-reconcile-provider-status # Conflicts: # tests/e2e/General/Certificates/Database.php
55392c8 to
ff91d29
Compare
ad1305f to
b667f6c
Compare
Picks up main via #13570 so CI runs against current main.
Picks up main via #13571 -> #13570 so CI runs against current main. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MeytoiisEusFMCU44PDYav
Picks up main via #13572 -> #13571 -> #13570 so CI runs against current main. Conflict in Certificates.php: main added an inline notifyError() call in catch and gave notifyError() a sixth $console parameter. This branch defers that email to the finally block, which sends only once the lease is confirmed still ours, so a stale worker cannot email an admin about a failure that a newer retry has already superseded. Kept the deferred form, dropped the inline call, and carried main's new parameter into the deferred call site -- git would have merged the signature change without touching the call. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MeytoiisEusFMCU44PDYav
75b6731 to
193ce56
Compare
bdb52ee to
51abda3
Compare
Picks up main via #13572 -> #13571 -> #13570 so CI runs against current main. Conflict in Certificates.php: main added an inline notifyError() call in catch and gave notifyError() a sixth $console parameter. This branch defers that email to the finally block, which sends only once the lease is confirmed still ours, so a stale worker cannot email an admin about a failure that a newer retry has already superseded. Kept the deferred form, dropped the inline call, and carried main's new parameter into the deferred call site -- git would have merged the signature change without touching the call.
…ertificates-reconcile-provider-status
main dropped the worker-level deletion test from #13570 before merging, and with it the Certificates/ fixtures. This branch still needs two of them: Database.php builds the throwaway schema every certificate test runs against, and Provider.php is the recording certificate provider WorkerTest drives. Both are kept; DeletesTest.php and Deletion.php belonged to the deletion test and go with it.
… renewable RENEWING sat with PENDING and PROCESSING, so a rule that was already verified got written back as verifying for the whole provider-side renewal window. The certificate is live and serving the entire time. It also dropped the domain out of the migration source check, which selects on a verified rule. Neither early return set renewDate. Maintenance selects renewals on a non-null renewDate, so reconciling a stuck rule to verified retired its certificate from renewal for good: the console read healthy while the certificate expired. Both paths now keep the existing date, or stamp one so the next cycle re-examines it. Move the status decision onto the Certificates library next to isAutoIssueEnabled and inject certificateIssuer into the worker, as Maintenance and the rule creates already do. The decision is a pure function of the provider's answer, so it is covered by the existing unit suite rather than through the worker. Refresh the log timestamp after the provider round trip, and restore the full wording on the in-flight message so both paths say the same thing.
| // Keep the document eligible for the renewal scan. Maintenance | ||
| // filters on a non-null renewDate, so leaving it unset would | ||
| // retire this certificate from renewal for good. | ||
| 'renewDate' => $certificate->getAttribute('renewDate') ?? DateTime::now(), |
There was a problem hiding this comment.
When an already-issued certificate has no stored renewDate, this writes the current time even though the provider says renewal is not required. The maintenance scan selects certificates whose renewDate is at or before the current time, so it will enqueue the certificate again on every cycle. This branch also resets attempts while preserving the overdue timestamp, causing unbounded generation-job churn. Persist a genuine future renewal date or otherwise avoid making an issued certificate immediately eligible for renewal.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Workers/Certificates.php
Line: 341
Comment:
**Renewal Jobs Repeat Forever**
When an already-issued certificate has no stored `renewDate`, this writes the current time even though the provider says renewal is not required. The maintenance scan selects certificates whose `renewDate` is at or before the current time, so it will enqueue the certificate again on every cycle. This branch also resets `attempts` while preserving the overdue timestamp, causing unbounded generation-job churn. Persist a genuine future renewal date or otherwise avoid making an issued certificate immediately eligible for renewal.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| #[DataProvider('statusProvider')] | ||
| public function testProviderStatus(string $status, bool $issued, bool $inFlight): void | ||
| { | ||
| $certificates = new Certificates(); | ||
|
|
||
| $this->assertSame($issued, $certificates->isIssued($status)); | ||
| $this->assertSame($inFlight, $certificates->isInFlight($status)); | ||
| } | ||
|
|
||
| public static function statusProvider(): \Iterator | ||
| { | ||
| // A renewing certificate is still live, so it counts as issued, not in flight. | ||
| yield 'issued' => [Status::ISSUED, true, false]; | ||
| yield 'renewing' => [Status::RENEWING, true, false]; | ||
| yield 'pending' => [Status::PENDING, false, true]; | ||
| yield 'processing' => [Status::PROCESSING, false, true]; | ||
| // Neither: the provider holds nothing, so the worker issues. | ||
| yield 'unknown' => [Status::UNKNOWN, false, false]; | ||
| yield 'failed' => [Status::FAILED, false, false]; | ||
| } |
There was a problem hiding this comment.
Test Mirrors Status Implementation
This unit test repeats the helpers' complete status-to-boolean classification table instead of testing externally observable certificate behavior. That violates the repository directive not to mirror source code in assertions and creates redundant, refactor-coupled coverage alongside the worker tests, which already verify persisted rule state, attempt counts, and issuance behavior. This repository requirement must be satisfied before merging.
| #[DataProvider('statusProvider')] | |
| public function testProviderStatus(string $status, bool $issued, bool $inFlight): void | |
| { | |
| $certificates = new Certificates(); | |
| $this->assertSame($issued, $certificates->isIssued($status)); | |
| $this->assertSame($inFlight, $certificates->isInFlight($status)); | |
| } | |
| public static function statusProvider(): \Iterator | |
| { | |
| // A renewing certificate is still live, so it counts as issued, not in flight. | |
| yield 'issued' => [Status::ISSUED, true, false]; | |
| yield 'renewing' => [Status::RENEWING, true, false]; | |
| yield 'pending' => [Status::PENDING, false, true]; | |
| yield 'processing' => [Status::PROCESSING, false, true]; | |
| // Neither: the provider holds nothing, so the worker issues. | |
| yield 'unknown' => [Status::UNKNOWN, false, false]; | |
| yield 'failed' => [Status::FAILED, false, false]; | |
| } |
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/unit/Certificates/CertificatesTest.php
Line: 36-55
Comment:
**Test Mirrors Status Implementation**
This unit test repeats the helpers' complete status-to-boolean classification table instead of testing externally observable certificate behavior. That violates the repository directive not to mirror source code in assertions and creates redundant, refactor-coupled coverage alongside the worker tests, which already verify persisted rule state, attempt counts, and issuance behavior. This repository requirement must be satisfied before merging.
```suggestion
```
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
What does this PR do?
This is the root cause of the "SSL generation failed with no error" ticket. On
main, the generation worker does:It never asks the provider whether a certificate actually exists — "no renewal due" is taken as "all good". With the Fastly provider in the pinned
utopia-php/cdn0.0.8,isRenewRequired()returnsfalsefor a classic domain on the active service version without checking for a TLS subscription at all:So a rule whose TLS subscription was never created, or was deleted, sits in
certificate-generatingforever. Every job the scheduler, the console retry, or the CLI queues hits the same early return, nothing is issued, and the rule's logs show nothing because the worker exits before writing anything useful. The status poller then readsUNKNOWN(FastlyTls::getCertificateStatus()returns that when no subscription exists), and there is no path back.The fix asks for the certificate status before standing down:
getCertificateStatus()ISSUEDverifiedPENDING/PROCESSING/RENEWINGcertificate-generatingUNKNOWN/FAILEDissueCertificate()Instant providers (Let's Encrypt) have no status to report, so a valid certificate there is treated as
ISSUED. That's theisInstantGeneration()ternary; without it the instant path would fall through and re-issue a perfectly good certificate.Two side benefits: a rule stuck in
certificate-generatingwhose subscription actually completed is now moved toverifiedon the next job rather than left as-is, and each stand-down writes a line to the rule logs so the console shows why nothing happened.finallyalready persists$ruleand$certificateviaupsertCertificate()/updateRuleAndSendEvents(), so the two early returns need no extra writes. Nothing else in the worker changes — no lease, no attempt accounting; those are a separate PR.Test Plan
tests/e2e/General/Certificates/WorkerTest.phpdrives the real worker against a throwaway PostgreSQL schema and a recording certificate provider. Both fixtures —Certificates/Database.php(throwaway schema built from the canonicalapp/config/collections/platform.phpdefinitions) andCertificates/Provider.php— now belong to this PR: #13570 introduced them, then dropped them along with its own worker-level test before merging, so this branch carries them.Every case sets
renew = false— the exact state wheremainreturns early — and seedsattempts = 3so resets are observable:ISSUED→ ruleverified, attempts 0, provider not asked to issuePENDING,PROCESSING,RENEWING→ rule stayscertificate-generating, attempts stay 3, nothing issuedUNKNOWN,FAILED→ provider is asked to issue forexample.com/api, attempts reset to 0 — onmainthese two cases never reachissueCertificate()validateDomain()is a no-op here because the message'svalidationDomainequals the domain, so nothing touches DNS.Runs in
Tests / E2E / General; skips locally without PostgreSQL.On the provider fake: #13570 dropped its worker-level test on the grounds that a deletion race against a real CDN cannot be exercised in CI. That reasoning applies less directly here — this PR's logic is a decision table over
getCertificateStatus()return values, and what is faked is one interface returning one of five enum values, not a race. Happy to drop it if the preference is that no worker test should stand in for the provider at all; say so and it goes.Related PRs and Issues
main's worker structure.issueCertificate()runs — is fix(cdn): recover Fastly certificates and preserve DNS failure details utopia-php/monorepo#222. Until that lands, the fall-through will attempt issuance and surface the provider's real error in the rule logs instead of silently skipping, which is already an improvement for support.Checklist
🤖 Generated with Claude Code
https://claude.ai/code/session_01MeytoiisEusFMCU44PDYav