Skip to content

fix(ingress): keep deterministic TLS keys across renewals - #132

Merged
kvinwang merged 5 commits into
mainfrom
fix/ingress-deterministic-key-renewal
Oct 15, 2026
Merged

kvinwang merged 5 commits into
mainfrom
fix/ingress-deterministic-key-renewal

Conversation

@kvinwang

@kvinwang kvinwang commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

DETERMINISTIC_TLS_KEY only survived until the first renewal, so a pinned SPKI broke or the certificate expired:

  • dns-01: certbot --csr keeps no lineage, so certbot renew never renewed the certificate, and re-issuing failed because certbot won't overwrite --cert-path.
  • tls-alpn-01: lego ignored the setting and generated a new key on every renewal.
  • DELEGATION_ZONE: the CSR was never passed to certbot, and the key file pushed the lineage to <name>-0001.
  • Existing deployments: turning the flag on had no effect; the old, randomly keyed lineage kept being renewed.

Changes:

  • dns-01: certificates for derived keys live in /etc/letsencrypt/deterministic/<name>/ (_wildcard.<base>/ for a wildcard, so an apex and its wildcard don't share one). certman renews them at certbot's thresholds (2/3 of the lifetime, or RENEW_DAYS_BEFORE), and re-issues whenever the served certificate doesn't carry the derived key.
  • tls-alpn-01: uses lego run --csr --private-key, so lego's own renewal logic (including ARI) still decides when to renew. --renew-force is used when the key differs, and .key is kept in sync.
  • Upgrades from 2.4–2.6: those releases wrote plain files into live/<name>/ with no renewal config (2.6 wrote privkey.pem before issuing, so a failed issuance left only that file). certbot only ever puts symlinks there, so a plain privkey.pem marks that old output. With the flag on, the directory moves to the deterministic one if that is empty, so the certificate keeps being served; the next check re-issues it if it doesn't carry the domain's derived key. Otherwise it is removed so certbot can create the lineage. An empty renewal/<name>.conf left by certbot failing over those files is removed too; it would otherwise push the lineage to <name>-0001. (From the second pass on, 2.6 under DELEGATION_ZONE also left a real <name>-0001 lineage; fix(ingress): give wildcard certificates their own certbot lineage #142 migrates that.)
  • Certificate paths come from one place: certbot paths are rooted at LETSENCRYPT_DIR, and CertManager.cert_dir decides between live/ and deterministic/. dns01.sh asks certman.py cert-dir instead of repeating the rule, so cert_dir_name is gone from functions.sh.
  • The CSR now has a SAN. The README documents when the SPKI changes (new app ID, different KMS, no KMS).

Tested locally against Pebble (PEBBLE_VA_ALWAYS_VALID=1) with a fake GetKey socket, running the 2.6 image with these scripts mounted:

  • certbot: fresh issuance, a steady pass, forced renewal (same SPKI, new serial), toggle-on over an existing lineage, wildcard, and a KMS key change.
  • lego: the same cases.
  • Upgrade from the 2.4–2.6 layout (plain files in live/<name>/; also key-only, and with an empty renewal config), flag on and off, 3 passes each: no -0001 lineage. Flag on: a usable legacy certificate is moved and served with no issuance; otherwise one issuance. Flag off: one certbot lineage. Later passes are no-ops.
  • lego: --private-key alone would be enough for the first issuance, but lego 5.3.1 ignores it on renewal, even with --renew-force, and generates a new key. That is why the CSR is kept.
  • Delegation: checked the generated command only.
  • Apex and wildcard together (both orders) from the 2.6 layout, flag on: each serves its own derived-key certificate; one issuance each, then no-ops.

All passed.

DETERMINISTIC_TLS_KEY issued the first certificate and then stopped:

- certbot `--csr` keeps no lineage, so `certbot renew` never renewed it
  (and with `--cert-name` it now fails outright); re-issuing failed too,
  since certbot will not overwrite an explicit --cert-path.
- lego ignored the setting, so tls-alpn-01 rotated the key on every renewal.
- under DELEGATION_ZONE the CSR was never passed, and the key written into
  live/ pushed certbot's lineage to <name>-0001.
- turning it on for an existing deployment only ever renewed the old,
  randomly keyed lineage.

certbot path: certificates for derived keys now live in
/etc/letsencrypt/deterministic/<name>/, apart from certbot's lineages.
certman.py renews them itself at certbot's thresholds (2/3 of the
lifetime, or RENEW_DAYS_BEFORE), and re-issues whenever the served
certificate does not carry the derived key. Issuance goes to a fresh
staging directory and is moved into place after checking the key.

lego path: `lego run --csr --private-key`, which keeps lego's own renewal
decision (window + ARI). A certificate with a different key is renewed
with --renew-force, and the .key file, which lego does not rewrite on
CSR renewals, is kept in step.

The CSR now carries a SAN, the key is written PKCS#8 like both clients
do, and the README documents when the pinned SPKI changes.

certificate_exists also looks at the lineage name now, so a wildcard no
longer re-runs certonly on every pass.

Signed-off-by: Kevin Wang <wy721@qq.com>
With DETERMINISTIC_TLS_KEY, 2.4-2.6 issued with certbot --csr and wrote
plain files into live/<name>/ with no renewal config. Those block certbot
from creating the lineage there once the setting is off, so every pass
failed and the stale certificate kept being served.

certbot only ever links into live/, so a plain fullchain.pem there is the
old --csr output. Move it to deterministic/<name>/ when the setting is on
and nothing is there yet, otherwise remove it.

Signed-off-by: Kevin Wang <wy721@qq.com>
…ewal configs

Signed-off-by: Kevin Wang <wy721@qq.com>
kvinwang added a commit that referenced this pull request Oct 14, 2026
#132-only migration

#132, #141 and this ship together, so deterministic/<base> never holds a
wildcard's certificate. Also check that replacing a wildcard keeps its
lineage name.

Signed-off-by: Kevin Wang <wy721@qq.com>
…its base's

With DETERMINISTIC_TLS_KEY, a base domain and its wildcard both used
deterministic/<base>/, so each pass re-issued one over the other and one
host was served the other's certificate. The wildcard now uses
deterministic/_wildcard.<base>/, in certman.py and dns01.sh alike.

2.4-2.6 put both into live/<base>/ too, so adopt a certificate there into
the directory of the name it carries, and only when the key next to it
matches.

Signed-off-by: Kevin Wang <wy721@qq.com>
kvinwang added a commit that referenced this pull request Oct 14, 2026
#132-only migration

wildcard's certificate. Also check that replacing a wildcard keeps its
lineage name.

Signed-off-by: Kevin Wang <wy721@qq.com>
kvinwang added a commit that referenced this pull request Oct 14, 2026
#132-only migration

wildcard's certificate. Also check that replacing a wildcard keeps its
lineage name.

Signed-off-by: Kevin Wang <wy721@qq.com>
@kvinwang
kvinwang added this pull request to stack #150 October 15, 2026 00:39
Add LETSENCRYPT_DIR and CertManager.cert_dir, and let dns01.sh ask
certman.py for the directory instead of repeating the naming rule.

Simplify the 2.4-2.6 live/ cleanup: with the flag on, move the plain
files to the deterministic directory and let the next pass re-issue
them if the key does not match; otherwise remove them.

Signed-off-by: Kevin Wang <wy721@qq.com>
@kvinwang
kvinwang merged commit 2ff1754 into main Oct 15, 2026
6 checks passed
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.

1 participant