Skip to content

Commit 7eb3401

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, start haproxy first and run the pass in the background loop. Without one, keep issuing first and exit if the pass yields nothing to serve. Serving before the pass would also serve what the last run left in /evidences, which attests a container that may no longer be running -- as tls-alpn-01 already did. So both modes now prepare serving the same way (prepare_serving in cert-lib.sh): republish the evidence before haproxy starts, or withdraw it if that is not possible yet. haproxy reads the PEMs built there itself, so the first pass no longer reloads it either. Signed-off-by: Kevin Wang <wy721@qq.com>
1 parent 192d029 commit 7eb3401

6 files changed

Lines changed: 51 additions & 25 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 before
138+
serving; no pass after that may reload haproxy or regenerate the evidence.
139+
One that does is a bug — it means the renewal decision is not being read
140+
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/cert-lib.sh‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ collect_evidence() {
6363
# Empty when the domain has no certificate yet.
6464
cert_checksum() { sha256sum "$(cert_fullchain_path "$1")" 2>/dev/null || true; }
6565

66+
# Run once, just before haproxy starts. haproxy reads the PEMs itself, so there
67+
# is nothing to reload yet. What the last run left in /evidences attests a
68+
# container that may no longer be running, so republish the evidence unless a
69+
# startup pass already has -- it is local and quick -- or, if that is not
70+
# possible yet, serve none. A first start has no ACME account to collect yet.
71+
prepare_serving() {
72+
build_combined_pems
73+
RELOAD_PENDING=false
74+
[ "$EVIDENCE_PENDING" = true ] || return 0
75+
if acme_account_file >/dev/null 2>&1 && collect_evidence; then
76+
EVIDENCE_PENDING=false
77+
else
78+
evidence_withdraw
79+
fi
80+
}
81+
6682
# One pass over every domain: make sure the records are right, then issue or
6783
# renew. Idempotent, so the first pass and every later one are the same code.
6884
run_pass() {

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

Lines changed: 23 additions & 19 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,29 @@ 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
362-
build_combined_pems
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.
348+
#
349+
# With none there is nothing to serve, and dns-01 needs no inbound traffic, so
350+
# issue first. A pass that ends with still no certificate is fatal; one that
351+
# got some serves them and leaves the loop to retry the rest with backoff.
352+
next_check=0
353+
if ! have_any_cert; then
354+
startup_status=0
355+
run_pass || startup_status=$?
356+
case $startup_status in
357+
0) next_check=${RENEW_INTERVAL:-43200} ;;
358+
3) next_check=60 ;;
359+
*)
360+
have_any_cert || exit "$startup_status"
361+
echo "Certificate pass failed; serving the certificates there are and retrying" >&2
362+
next_check=60
363+
;;
364+
esac
365+
fi
366+
prepare_serving
363367

364368
haproxy_emit_global
365369
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,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ require_instance_id
235235
# first certificate can exist, so start on a placeholder and let the loop
236236
# converge behind it.
237237
ensure_placeholder_certs
238-
build_combined_pems
238+
prepare_serving
239239

240240
haproxy_emit_global
241241
emit_peek_frontend

0 commit comments

Comments
 (0)