Skip to content

Commit c5e01e2

Browse files
committed
fix(ingress): serve existing dns-01 certificates before the first pass
dns-01 finished a whole certificate pass before starting haproxy, so on a restart every domain stayed unserved for as long as the slowest one took: a certbot run up to its timeout for a domain that fails, or up to DNS_SETUP_TIMEOUT (30 min) per domain whose delegation CNAMEs are still being created. When any certificate is on disk, republish the evidence, start haproxy and run the pass in the background loop. The evidence goes first because it is quick and what the last run left attests a container that may no longer be running; if it cannot be regenerated yet, the old one is withdrawn. haproxy reads the PEMs built at startup itself, so the first pass no longer reloads it. Without a certificate, keep issuing first and exit if the pass yields nothing to serve. Signed-off-by: Kevin Wang <wy721@qq.com>
1 parent 637ba48 commit c5e01e2

4 files changed

Lines changed: 44 additions & 23 deletions

File tree

‎custom-domain/dstack-ingress/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dstack-ingress is a HAProxy-based L4 (TCP) proxy that provides:
2020

2121
2. **TLS Termination**: HAProxy terminates TLS and forwards the decrypted TCP stream to your backend. No HTTP inspection — the proxy operates entirely at L4.
2222

23-
3. **Certificate Renewal**: A background daemon checks for renewal every 12 hours. On renewal, HAProxy is gracefully reloaded with zero downtime. A failed pass is retried after 60s, backing off by 60s per attempt up to 30 minutes. A domain that fails does not stop the others from being served: in dns-01 mode the container exits at startup only if no domain has a certificate yet, and in tls-alpn-01 mode it serves a placeholder certificate until issuance succeeds.
23+
3. **Certificate Renewal**: A background daemon checks for renewal every 12 hours. On renewal, HAProxy is gracefully reloaded with zero downtime. A failed pass is retried after 60s, backing off by 60s per attempt up to 30 minutes. A domain that fails does not stop the others from being served: in dns-01 mode a restart serves the certificates already on disk right away and checks them in the background, and the container exits at startup only if no domain has a certificate yet. In tls-alpn-01 mode it serves a placeholder certificate until issuance succeeds.
2424

2525
4. **Evidence Generation**: Generates cryptographically linked attestation evidence (ACME account, certificates, TDX quote / dstack attestation) for TEE verification.
2626

‎custom-domain/dstack-ingress/TESTING.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ renewal window, and `RENEW_DAYS_BEFORE` sets it in either mode:
134134

135135
- **not due**: leave `RENEW_DAYS_BEFORE` unset, set a short `RENEW_INTERVAL`,
136136
and restart the container. Expect lego `Skip renewal` / certbot `No
137-
certificates need renewal`. The first pass after a start always republishes
138-
the evidence (and reloads haproxy in tls-alpn-01 mode); the passes after it
139-
must not. A later pass that reloads or regenerates evidence is a bug — it
140-
means the renewal decision is not being read correctly.
137+
certificates need renewal`. Every start republishes the evidence (and
138+
tls-alpn-01's first pass reloads haproxy); the passes after it must not. A
139+
later pass that reloads or regenerates evidence is a bug — it means the
140+
renewal decision is not being read correctly.
141141
- **due**: set `RENEW_DAYS_BEFORE=365` and restart. Expect a new certificate,
142142
a reload, and regenerated evidence.
143143

‎custom-domain/dstack-ingress/scripts/dns01.sh‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,6 @@ setup_py_env
332332
setup_certbot_env
333333
load_dstack_identity
334334

335-
# dns-01 needs no inbound traffic, so keep the original order: certificate
336-
# first, serving second. A certificate failure here is fatal only when there is
337-
# nothing to serve. Otherwise exiting would just restart into the same failure
338-
# -- hitting the CA again on every restart -- while every domain goes unserved,
339-
# including those whose certificates are fine (a newly added domain failing its
340-
# first issuance would take down the others). Serve what there is and let the
341-
# loop retry with backoff instead.
342335
have_any_cert() {
343336
local domain
344337
while IFS= read -r domain; do
@@ -348,18 +341,40 @@ have_any_cert() {
348341
return 1
349342
}
350343

351-
startup_status=0
352-
run_pass || startup_status=$?
353-
case $startup_status in
354-
0) next_check=${RENEW_INTERVAL:-43200} ;;
355-
3) next_check=60 ;;
356-
*)
357-
have_any_cert || exit "$startup_status"
358-
echo "Certificate pass failed; serving the certificates there are and retrying" >&2
359-
next_check=60
360-
;;
361-
esac
344+
# With a certificate on disk, serve it first and run the pass behind haproxy.
345+
# Running it first would leave every domain unserved until it finishes, and one
346+
# new domain can hold it for long: a certbot run up to its timeout, or up to
347+
# DNS_SETUP_TIMEOUT while its delegation CNAMEs are still being created. The
348+
# evidence is local and quick, so republish it before serving, or withdraw it:
349+
# what the last run left in /evidences attests a container that may no longer
350+
# be running.
351+
#
352+
# With none there is nothing to serve, and dns-01 needs no inbound traffic, so
353+
# issue first. A pass that ends with still no certificate is fatal; one that
354+
# got some serves them and leaves the loop to retry the rest with backoff.
355+
next_check=0
356+
# shellcheck disable=SC2034 # cert-lib.sh state
357+
if ! have_any_cert; then
358+
startup_status=0
359+
run_pass || startup_status=$?
360+
case $startup_status in
361+
0) next_check=${RENEW_INTERVAL:-43200} ;;
362+
3) next_check=60 ;;
363+
*)
364+
have_any_cert || exit "$startup_status"
365+
echo "Certificate pass failed; serving the certificates there are and retrying" >&2
366+
next_check=60
367+
;;
368+
esac
369+
elif collect_evidence; then
370+
EVIDENCE_PENDING=false
371+
else
372+
evidence_withdraw
373+
fi
362374
build_combined_pems
375+
# haproxy is not running yet and reads these when it starts.
376+
# shellcheck disable=SC2034
377+
RELOAD_PENDING=false
363378

364379
haproxy_emit_global
365380
haproxy_emit_tls_frontend ":${PORT}$(haproxy_accept_proxy)"

‎custom-domain/dstack-ingress/scripts/evidence-lib.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ evidence_publish() {
115115
rm -rf "$stage"
116116
}
117117

118+
# Take down what an earlier run published, for when this one cannot replace it
119+
# yet: no evidence is better than evidence for another container.
120+
evidence_withdraw() {
121+
rm -f "$EVIDENCE_DIR"/{acme-account.json,cert-*.pem,sha256sum.txt,quote.json,attestation.json}
122+
}
123+
118124
evidence_start_server() {
119125
[ "${EVIDENCE_SERVER:-true}" = "true" ] || return 0
120126
# Bind loopback explicitly. Left to itself mini_httpd binds [::]:port first,

0 commit comments

Comments
 (0)