You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(decdn_gateway): keep a failed reissue pending, reissue on a CA change, allow a zero burst, re-own restored state - #189
Fixes the four findings from Copilot's review of #187, which came in after it merged.
A failed domain-change issuance is no longer dropped.
Before: tls.yml reissued only when there was no certificate or the certificate unit had changed. After a failed run the new unit was already on disk, so the next converge skipped issuance.
Now the role records what the certificate was issued for (<server> <domain>) in lego's state directory, and reissues while that record differs from what it wants.
The record is written only by lego's deploy hook (files/certs-deployed), which lego runs only after it actually created or renewed the certificate. The hook also removes force.env and reloads nginx. So a run that renewed nothing (lego still exits 0) acknowledges nothing, and a timer run that succeeds clears a pending reissue by itself.
The role fails a pending reissue that the hook didn't record.
A CA change (e.g. letsencrypt-staging → letsencrypt) reissues.
Before: lego keeps a certificate that isn't due.
Now, while the record differs, force.env (an optional EnvironmentFile of the unit) sets LEGO_RENEW_FORCE=true and LEGO_ARI_DISABLE=true. Without the second, ARI's replaces would name the old CA's certificate, and the new CA refuses that.
A certificate from before the record existed is adopted when its subjectAltNames are <domain> and *.<domain>.
decdn_gateway_pointer_burst: 0 used to render burst=0, which nginx rejects. The parameter is now left out.
Restored state is re-owned, without following links. A restore made as root before the first deploy left gateway/ and the pointers with the old host's owners.
The role now refuses a symlinked decdn_gateway_data_dir.
It runs chown -R -P -h only when find reports something the gateway's account doesn't own. That account can write the directory, so a recursive chown that followed links would hand root's files to it.
docs/lifecycle.md says so.
Test plan
molecule/decdn-gateway: a converge with burst: 0 validates, and its limit_req line has no burst=.
molecule/decdn-gateway-lifecycle, new cases:
Pending reissue: a certificate is planted with a record naming the staging CA. Two acme converges against a dead ACME server each attempt issuance and fail; force.env (LEGO_RENEW_FORCE, LEGO_ARI_DISABLE) and the old record stay.
Timer runs: a lego stand-in that exits 0 without issuing acknowledges nothing. One that issues (runs its deploy hook) records the new CA and clears force.env.
Ownership: the state is chowned to root and a link to a root-only canary is planted in it. The next converge must give the state back and leave the canary root's.
…ange, allow a zero burst, re-own restored state
Copilot's review of #187:
- A certificate is now recorded as issued for "<server> <domain>" only after a run
succeeds; while the record differs, force.env (LEGO_RENEW_FORCE, read by the
certificate unit) forces a reissue, on every converge and timer run, until one
succeeds. A failed domain change used to drop out once the unit file stopped
changing, and a move off letsencrypt-staging kept the staging certificate until
it was due. A certificate from before the record is adopted when its names match.
- decdn_gateway_pointer_burst: 0 rendered burst=0, which nginx rejects; the
parameter is now left out.
- The role re-owns decdn_gateway_data_dir for the gateway's account, so a restore
made as root before the first deploy (docs/lifecycle.md) works.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Unresolved critical issues can trigger repeated certificate issuance that exhausts CA limits and symlink-following ownership changes that enable privilege escalation.
The recursive file task ran as root and followed symlinks in a directory the
gateway's account can write, so a planted link could hand root's files to it.
The data dir must be a real directory, and chown -R -P -h runs only when find
reports something the account does not own. The lifecycle scenario plants a
link to a root-only canary and checks it stays root's.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…cceeds
The record and force.env were cleaned up only by the role's tasks, so after a
failed converge every later timer run kept forcing a reissue (Copilot review on
#189). The certificate unit now records the issue and removes force.env in an
ExecStartPost=, which systemd runs only after lego succeeds; the lifecycle
scenario checks it with a lego that succeeds.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Critical CA-migration defects can cause issuance to fail or record a CA switch without obtaining a new certificate, while lifecycle tests do not exercise the forced-renewal path.
This fixture omits lego's certificates/decdn-gateway.json resource, so it selects fresh issuance rather than renewal of the existing certificate; the dead endpoint also fails registration before either issuance path runs. The retries therefore pass their assertions even if the force flag has no effect. Add a complete lego resource and a controlled ACME endpoint that distinguishes a non-forced skip from successful forced issuance, including a CA change.
Clear stale force state when the existing certificate matches
ansible/roles/decdn_gateway/tasks/tls.yml:272
After a switch from CA A to CA B fails, reverting to A makes decdn_gateway_need_issue false but leaves the pending force.env in place until the end of the block. The unit changes back, so the role starts it with that stale force flag and unnecessarily reissues the already-correct A certificate, consuming CA issuance quotas. Remove force.env before starting the service whenever the existing certificate already matches the requested configuration.
…d skip ARI's replaces
Copilot review on #189: ExecStartPost recorded the new CA after any run lego
exited 0 from, including one that renewed nothing (a timer run before the role
forced it), and a forced renewal still sent ARI's replaces naming the old CA's
certificate. lego's deploy hook, which runs only after an issuance, now records
it, removes force.env and reloads nginx (files/certs-deployed); the role fails a
pending reissue the hook did not record; force.env also sets LEGO_ARI_DISABLE.
The lifecycle scenario runs a lego that renews nothing (nothing acknowledged)
and one that issues (recorded, force cleared).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The symlink guard can be bypassed to transfer ownership of external root-owned files to a compromised gateway account, and an issuance race can discard a pending CA-switch request.
No open issues 🎉
2 previously missed issues
In code that hasn't changed since last review
Symlinked ancestors let recursive chown escape the data directory
The symlink guard can be bypassed by supported paths such as .../state/ssh with a symlinked state, or .../gateway/ with a symlinked gateway: both appear as directories to lstat. In either case, chown -R -P -h traverses the external target, allowing a compromised gateway account to obtain ownership of root-owned files on the next converge. Normalize or reject trailing slashes and prevent traversal through symlinked ancestors, using safely anchored ownership repair or restricting the data directory to a direct child of a verified home.
In-flight issuance can delete a newer CA's pending force request
If a timer issuance for CA A is already running when the role configures CA B and writes force.env, A's hook still uses its original arguments and unconditionally deletes B's pending force request. The role then fails its record check, while subsequent B timer runs lack forcing and ARI suppression and can retain A's not-yet-due certificate. Serialize reconfiguration with in-flight issuance, or associate each pending request with its server/domain and remove it only when the hook matches that request.
…y the force a run belongs to
Copilot review on #189 (previously missed):
- decdn_gateway_data_dir must be one plain directory directly under the home,
with no trailing slash: a symlinked ancestor, or a link a trailing slash
resolves, passed the lstat guard and took the recursive chown outside it.
validation-decdn-gateway rejects both shapes.
- force.env names the server and domain it forces, and the deploy hook removes it
only when they are its own: a run already under way for another CA no longer
clears a newer pending reissue. The lifecycle scenario runs another CA's hook
against a pending force.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nted one
After a failed switch from one CA to another, reverting to the first left the
other's force.env in place: every timer run reissued from the first CA, whose
deploy hook never clears a force naming another (Copilot review on #189). The
role now removes force.env whenever no reissue is needed, before the service can
run; the lifecycle scenario reverts with another CA's force left behind.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016nMiELpJumeQUrXH5BrAE8
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed because a critical race between force-file replacement and hook cleanup can discard a newer CA-switch request, leaving the previous certificate in use until another converge.
A renewal already under way for the previous server or domain could finish
between the role writing the newer force.env and that run's hook checking it,
and clear the newer force (Copilot review on #189, flagged for human
assessment). While a reissue is pending the role now stops the timer, waits for
any certificate run in flight, writes the force and runs the issuance, and
starts the timer again in an always: whatever the outcome. The lifecycle
scenario checks the timer is running after each failed issuance.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016nMiELpJumeQUrXH5BrAE8
On the race the last Copilot review flagged for human assessment (a renewal for the previous CA clearing a newer force.env between its hook's check and its delete): serialized in f97768d. While a reissue is pending, the role stops decdn-gateway-certs.timer, waits for any certificate run in flight (ActiveState inactive/failed), writes force.env and runs the issuance, then starts the timer again in an always:. The lifecycle scenario checks the timer is back after each failed issuance; the wait for an in-flight run is not exercised by a test (it would need a run held open across a converge).
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
CA rollback can race with an active timer renewal and install a certificate from the unwanted CA, while lego’s enabled jitter can also exceed the new synchronization timeout.
…icate state
Copilot review on #189: the timer was paused only once a reissue was already
decided, so on a revert a renewal in flight for the unwanted CA could still
replace the certificate after the decision, and lego's own random sleep (up to
minutes) could outlast the five-minute wait. The role now stops the timer and
waits (up to 15 minutes, for DNS-01 propagation) for any run in flight before it
writes the units or reads the record, on every acme converge, and starts it
again in an always:. The unit passes --no-random-sleep: the timer already
randomizes. The lifecycle scenario starts a 90-second renewal and checks the
converge waited it out.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016nMiELpJumeQUrXH5BrAE8
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Successful issuance, renewal, and CA migration rely on lego stand-ins, leaving pinned-lego validation against an ACME test server outstanding for deploy hooks, ARI-disabled migration, and nginx certificate activation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the four findings from Copilot's review of #187, which came in after it merged.
tls.ymlreissued only when there was no certificate or the certificate unit had changed. After a failed run the new unit was already on disk, so the next converge skipped issuance.<server> <domain>) in lego's state directory, and reissues while that record differs from what it wants.files/certs-deployed), which lego runs only after it actually created or renewed the certificate. The hook also removesforce.envand reloads nginx. So a run that renewed nothing (lego still exits 0) acknowledges nothing, and a timer run that succeeds clears a pending reissue by itself.letsencrypt-staging→letsencrypt) reissues.force.env(an optionalEnvironmentFileof the unit) setsLEGO_RENEW_FORCE=trueandLEGO_ARI_DISABLE=true. Without the second, ARI'sreplaceswould name the old CA's certificate, and the new CA refuses that.<domain>and*.<domain>.decdn_gateway_pointer_burst: 0used to renderburst=0, which nginx rejects. The parameter is now left out.gateway/and the pointers with the old host's owners.decdn_gateway_data_dir.chown -R -P -honly whenfindreports something the gateway's account doesn't own. That account can write the directory, so a recursive chown that followed links would hand root's files to it.docs/lifecycle.mdsays so.Test plan
molecule/decdn-gateway: a converge withburst: 0validates, and itslimit_reqline has noburst=.molecule/decdn-gateway-lifecycle, new cases:force.env(LEGO_RENEW_FORCE,LEGO_ARI_DISABLE) and the old record stay.force.env.make lint,make lint-ansible,make test-scripts🤖 Generated with Claude Code