Skip to content

fix(decdn_gateway): keep a failed reissue pending, reissue on a CA change, allow a zero burst, re-own restored state - #189

Merged
thiras merged 8 commits into
mainfrom
fix/decdn-gateway-review
Oct 31, 2026
Merged

thiras merged 8 commits into
mainfrom
fix/decdn-gateway-review

Conversation

@thiras

@thiras thiras commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

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.
  • make lint, make lint-ansible, make test-scripts
  • CI
  • Copilot review

🤖 Generated with Claude Code

…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]>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 23:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

2 open issues
What changed in this PR

This PR follows up on #187 with fixes to the Ansible gateway role’s certificate reissuance, nginx rate limiting, and restored-state ownership.

Changes:

  • Tracks the certificate’s CA and domain, preserving forced reissuance across failed deployments.
  • Omits nginx’s burst parameter when configured as zero.
  • Repairs restored gateway-state ownership and adds regression tests and restore guidance.
File Description
docs/​lifecycle.md Clarifies root-run restores and ownership repair.
ansible/​roles/​decdn_gateway/​vars/​main/​paths.yml Defines issuance-record and force-file paths.
ansible/​roles/​decdn_gateway/​templates/​decdn-gateway.nginx.j2 Omits the burst parameter when zero.
ansible/​roles/​decdn_gateway/​templates/​decdn-gateway-certs.service.j2 Loads the optional forced-renewal environment file.
ansible/​roles/​decdn_gateway/​tasks/​tls.yml Tracks certificate identity and retries pending issuance.
ansible/​roles/​decdn_gateway/​tasks/​main.yml Recursively repairs gateway-state ownership.
ansible/​molecule/​decdn-gateway/​side_effect.yml Tests zero-burst nginx configuration.
ansible/​molecule/​decdn-gateway-lifecycle/​side_effect.yml Adds pending-reissue and ownership-repair scenarios.
ansible/​molecule/​decdn-gateway-lifecycle/​includes/​acme-retry.yml Checks repeated issuance failures preserve pending state.

Review effort: Balanced

thiras and others added 2 commits October 31, 2026 03:02
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]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

2 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Make the fixture verify forced renewal with a valid ACME setup

ansible/​molecule/​decdn-gateway-lifecycle/​side_effect.yml:107

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.

Medium severity 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.

2 resolved since last review

Review effort: Balanced

…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]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

High severity Symlinked ancestors let recursive chown escape the data directory

ansible/​roles/​decdn_gateway/​tasks/​main.yml:362

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.

Medium severity In-flight issuance can delete a newer CA's pending force request

ansible/​roles/​decdn_gateway/​files/​certs-deployed:13

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.

2 resolved since last review

Review effort: Balanced

…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]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A failed CA switch followed by rollback leaves stale force.env state that can trigger repeated certificate issuance and exhaust issuance limits.

1 open issue

Review effort: Balanced

…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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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
@thiras

thiras commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

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).

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

2 open issues

Review effort: Balanced

…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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@thiras
thiras merged commit e85896a into main Oct 31, 2026
59 checks passed
@thiras
thiras deleted the fix/decdn-gateway-review branch October 31, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants