Skip to content

Commit a91142a

Browse files
authored
Merge pull request #139 from Dstack-TEE/fix/ingress-caa
fix(ingress): CAA correctness for Route53, wildcards and the dnsguide check
2 parents 1e3ef33 + 20c4d90 commit a91142a

16 files changed

Lines changed: 527 additions & 221 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ PolicyDocument:
108108
109109
**Important Notes for Route53:**
110110
- The certbot plugin uses the format `certbot-dns-route53` package
111-
- CAA will merge AWS & Let's Encrypt CA domains to existing records if they exist
111+
- CAA values are merged into the existing record set at the name: only values with the same tag are replaced, so `iodef` and other tags are kept
112+
- Where a CAA record shares the name (`DELEGATION_ZONE`, or `SET_CAA=true` on a non-wildcard domain), the gateway pointer is an A record to the resolved gateway IP instead of a CNAME, replacing any CNAME in the same change. It is refreshed every `RENEW_INTERVAL`
113+
- Wildcard record names are listed by Route53 as `\052.<domain>`; they are matched as `*.<domain>`
112114
- It is essential that the AWS service account used can only assume the limited role. See cloudformation example.
113115

114116
## Docker Compose Examples

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ dstack-ingress is a HAProxy-based L4 (TCP) proxy that provides:
2929
You can use a wildcard domain (e.g. `*.myapp.com`) to route all subdomains to a single dstack application:
3030

3131
- The TXT record is automatically set as `_dstack-app-address-wildcard.myapp.com` (instead of `_dstack-app-address.*.myapp.com`)
32-
- CAA records use the `issuewild` tag on the base domain
32+
- CAA records use both the `issuewild` and `issue` tags on the base domain: `issuewild` alone would leave other CAs free to issue for non-wildcard subdomains
33+
- If the base domain is a zone apex (`*.myapp.com` served off the `myapp.com` zone), that `issue` record replaces any existing `issue` values there and governs every name in the zone without its own CAA record, so other CAs or ACME accounts can no longer issue for them. The container warns about this; give those names their own CAA records, or set `SET_CAA=false`. On Route53 this also removes the Amazon `issue` values that older versions wrongly added at the apex
34+
- Two deployments with different ACME accounts, one serving `myapp.com` and one `*.myapp.com`, both write the `issue` record at `myapp.com` and overwrite each other's on every pass. Serve both names from one deployment, or set `SET_CAA=false` on one of them
3335
- Requires dstack-gateway with wildcard TXT resolution support ([dstack#545](https://github.com/Dstack-TEE/dstack/pull/545))
3436

3537
```yaml
@@ -173,7 +175,7 @@ otherwise arbitrary — the gateway answers every name under its domain.
173175
| `PORT` | `443` | HAProxy listen port |
174176
| `DOMAINS` | | Multiple domains, one per line or one comma-separated line. Without `ROUTING_MAP`, all of them go to `TARGET_ENDPOINT` |
175177
| `ROUTING_MAP` | | Multi-domain routing: `domain=host:port` per line. A wildcard entry (`*.example.com=host:port`) matches one label, and exact entries take precedence over it. Unmatched names go to the first entry |
176-
| `SET_CAA` | `false` | Enable CAA DNS record (dns-01 only; tls-alpn-01 cannot write DNS) |
178+
| `SET_CAA` | `false` | Enable CAA DNS record (dns-01 only; tls-alpn-01 cannot write DNS). Replaces existing values with the same tag; for a wildcard whose base is a zone apex this restricts the whole zone (see [Wildcard Domain Support](#wildcard-domain-support)). Except on Cloudflare, a non-wildcard domain is then published as an A record to the gateway's current address instead of a CNAME, because a CNAME would hide the CAA beside it; the address is refreshed every `RENEW_INTERVAL`. Turning `SET_CAA` off later leaves the CAA record in place, so the A record stays until you delete the CAA |
177179
| `TXT_PREFIX` | `_dstack-app-address` | DNS TXT record prefix |
178180
| `ACME_EMAIL` | | ACME contact address, in either mode. `CERTBOT_EMAIL` is the historical name and still works. See [below](#the-acme-contact-address-is-optional-and-public) — it is optional, and published |
179181
| `ACME_STAGING` | `false` | Use Let's Encrypt staging, in either mode. `CERTBOT_STAGING` is the historical name and still works |
@@ -238,22 +240,23 @@ _acme-challenge.svc.example.com CNAME _acme-challenge.svc.example.com.dele
238240
dstack-ingress publishes what they point at, in the delegation zone:
239241

240242
```
241-
svc.example.com.deleg.example.net CNAME <GATEWAY_DOMAIN>
243+
svc.example.com.deleg.example.net A <address of GATEWAY_DOMAIN>
242244
svc.example.com.deleg.example.net CAA 0 issue "letsencrypt.org;validationmethods=dns-01;accounturi=…"
243245
_dstack-app-address.svc.example.com.deleg.example.net TXT "<app-id>:<port>"
244246
_acme-challenge.svc.example.com.deleg.example.net TXT <challenge> (transient)
245247
```
246248

247-
The gateway pointer and the CAA share a name, which DNS does not allow for a
248-
CNAME. Which form works is a property of the provider — Cloudflare tolerates the
249-
pair, Linode publishes an address record instead — and `set_alias_record` in the
250-
provider layer is where that choice is made. Delegation asks for an alias and
251-
takes what it gets. A provider that needs a form other than CNAME overrides that
252-
method, as Linode does.
249+
The gateway pointer and the CAA share a name. Cloudflare answers a CAA query
250+
with the CAA beside a CNAME, so the pointer stays a CNAME there. Elsewhere it is
251+
an A record: by the DNS rules resolvers follow a CNAME without looking beside it,
252+
and Route53 and Linode refuse the pair. The address
253+
is the one `GATEWAY_DOMAIN` resolves to when the container writes it, and it is
254+
refreshed every `RENEW_INTERVAL`, so a gateway move reaches DNS only then, and
255+
only one of the gateway's addresses is published.
253256

254257
**A wildcard needs a fourth CNAME.** RFC 8659 evaluates `*.app.example.com` at
255-
`app.example.com`, so the base gets its own alias and the container publishes an
256-
`issuewild` CAA behind it:
258+
`app.example.com`, so the base gets its own alias and the container publishes
259+
`issuewild` and `issue` CAA records behind it:
257260

258261
```
259262
*.app.example.com CNAME app.example.com.deleg.example.net
@@ -577,6 +580,10 @@ by both, so use it: reject envelopes more than a few minutes old, and remember,
577580
the last one you applied. Reject anything older. A failed POST is retried with the identical envelope, so treat a
578581
byte-identical `payload` as a retry of the one already applied rather than a new request.
579582

583+
`records` can hold several records with the same type and name: a wildcard asks
584+
for both an `issue` and an `issuewild` CAA at its base. Create each one; do not
585+
replace by type and name alone.
586+
580587
### Why lego and not certbot
581588

582589
certbot cannot do tls-alpn-01. Its standalone plugin is HTTP-01 only, the

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ authoritative nameserver — it should be gone).
251251
The CAA comes last and needs nothing from you: it names the ACME account, so the
252252
account has to exist first, and the container publishes it into the delegation
253253
zone itself once it does. Expect `Adding CAA record for <domain>.<zone> with tag
254-
issue` on the first pass — `issuewild` for a wildcard, on the base name. Confirm
254+
issue` on the first pass — `issue` and `issuewild` for a wildcard, on the base name. Confirm
255255
it at the authoritative nameserver, and confirm it resolves *through* the
256256
operator's CNAME, since that is what the CA follows:
257257

@@ -351,6 +351,7 @@ this before suspecting the proxy.
351351

352352
```bash
353353
python3 scripts/tests/test_dnsguide.py # DNS/CAA parsing and record building
354+
python3 scripts/tests/test_route53.py # Route53 record names and changes
354355
bash scripts/tests/test_sanitizers.sh # env var validation
355356
```
356357

‎custom-domain/dstack-ingress/scripts/acme-dns-alias-hook.sh‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ case "$action" in
3535
auth)
3636
: "${CERTBOT_VALIDATION:?CERTBOT_VALIDATION not set}"
3737
echo "acme-dns-alias-hook: writing challenge TXT to delegated record $record"
38-
dnsman.py set_txt --domain "$record" --content "$CERTBOT_VALIDATION"
38+
# A stale challenge value that cannot be deleted is harmless: the CA
39+
# accepts the record set as long as one value matches.
40+
dnsman.py set_txt --tolerate-stale --domain "$record" --content "$CERTBOT_VALIDATION"
3941
# certbot asks Let's Encrypt to validate immediately after this hook
4042
# returns, so wait for the record to propagate before returning.
4143
#

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

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ EOF
9393
}
9494

9595
set_alias_record() {
96-
local domain="$1"
96+
local domain="$1" with_caa=()
97+
# A plain name gets its CAA beside this record.
98+
if [ "$SET_CAA" = "true" ] && [[ "$domain" != \*.* ]]; then
99+
with_caa=(--with-caa)
100+
fi
97101
echo "Setting alias record for $domain"
98-
dnsman.py set_alias \
102+
dnsman.py set_alias "${with_caa[@]}" \
99103
--domain "$domain" \
100104
--content "$GATEWAY_DOMAIN"
101105

@@ -130,10 +134,9 @@ set_txt_record() {
130134
# never needs touching again -- not when the app id changes with the compose,
131135
# not when the ACME account is recreated, not when the gateway moves.
132136
#
133-
# The gateway pointer and the CAA end up on one name, which DNS does not allow
134-
# for a CNAME. Which form works is a provider property -- Cloudflare tolerates
135-
# the pair, Linode publishes an address record instead -- and set_alias_record
136-
# is where that knowledge lives, so ask for an alias and let the provider pick.
137+
# The gateway pointer and the CAA end up on one name. Which form of pointer
138+
# lets the CAA take effect is a provider property, so tell the provider and let
139+
# it pick (an address record unless its servers answer CAA beside a CNAME).
137140
delegated_name() {
138141
local domain="${1#\*.}"
139142
echo "${domain}.${DELEGATION_ZONE}"
@@ -143,7 +146,7 @@ delegation_publish() {
143146
local domain="$1" target txt_name
144147
target=$(delegated_name "$domain")
145148

146-
dnsman.py set_alias --domain "$target" --content "$GATEWAY_DOMAIN" || return 1
149+
dnsman.py set_alias --with-caa --domain "$target" --content "$GATEWAY_DOMAIN" || return 1
147150

148151
txt_name="$(txt_record_name "$domain").${DELEGATION_ZONE}"
149152
dnsman.py set_txt --domain "$txt_name" --content "$(txt_record_value)" || return 1
@@ -152,18 +155,21 @@ delegation_publish() {
152155
# The accounturi CAA goes on the delegated name, beside the gateway pointer.
153156
#
154157
# A wildcard is evaluated at its base (RFC 8659), so it needs the `issuewild`
155-
# tag and it needs the operator to have aliased the base as well -- dnsguide
158+
# tag (beside `issue`) and it needs the operator to have aliased the base as well -- dnsguide
156159
# asks for that fourth CNAME. A base that is a zone apex cannot be aliased at
157160
# all, so such a domain is not a fit for delegation; the CAA check reports the
158161
# record missing rather than the container pretending to have set it.
159162
delegation_publish_caa() {
160-
local domain="$1" account_file account_uri
163+
local domain="$1" account_file account_uri tag
161164
account_file=$(get_letsencrypt_account_file) || return 1
162165
account_uri=$(jq -j '.uri' "$account_file")
163-
dnsman.py set_caa \
164-
--domain "$(delegated_name "$domain")" \
165-
--caa-tag "$(caa_tag_for "$domain")" \
166-
--caa-value "letsencrypt.org;validationmethods=dns-01;accounturi=$account_uri"
166+
for tag in $(caa_tags_for "$domain"); do
167+
dnsman.py set_caa \
168+
--domain "$(delegated_name "$domain")" \
169+
--caa-tag "$tag" \
170+
--caa-value "letsencrypt.org;validationmethods=dns-01;accounturi=$account_uri" ||
171+
return 1
172+
done
167173
}
168174

169175
# Challenge delegation: this container holds no token for the served domain's
@@ -194,6 +200,16 @@ delegation_guide() {
194200
"$@"
195201
}
196202

203+
# Only a zone apex answers SOA for itself. Best effort: a failed lookup says no.
204+
is_zone_apex() {
205+
local resolvers="${DOH_RESOLVERS:-${DOH_RESOLVER:-https://dns.google/resolve}}"
206+
curl -fsS --max-time 10 -H 'accept: application/dns-json' \
207+
"${resolvers%%,*}?name=$1&type=SOA" 2>/dev/null |
208+
jq -e --arg name "${1,,}" \
209+
'any(.Answer[]?; .type == 6 and (.name | ascii_downcase | rtrimstr(".")) == $name)' \
210+
>/dev/null
211+
}
212+
197213
set_caa_record() {
198214
local domain="$1"
199215

@@ -224,20 +240,26 @@ set_caa_record() {
224240

225241
local caa_domain caa_tag
226242
caa_domain="${domain#\*.}"
227-
caa_tag=$(caa_tag_for "$domain")
228-
229-
ACCOUNT_URI=$(jq -j '.uri' "$account_file")
230-
echo "Adding CAA record ($caa_tag) for $caa_domain, accounturi=$ACCOUNT_URI"
231-
dnsman.py set_caa \
232-
--domain "$caa_domain" \
233-
--caa-tag "$caa_tag" \
234-
--caa-value "letsencrypt.org;validationmethods=dns-01;accounturi=$ACCOUNT_URI"
235243

236-
if [ $? -ne 0 ]; then
237-
echo "Warning: Failed to set CAA record for $domain"
238-
echo "This is not critical - certificates can still be issued without CAA records"
239-
echo "Consider disabling CAA records by setting SET_CAA=false if this continues to fail"
244+
if [ "$FIRST_PASS" = "true" ] && [[ "$domain" == \*.* ]] && is_zone_apex "$caa_domain"; then
245+
echo "Warning: $caa_domain is a zone apex. The CAA issue record published there for $domain"
246+
echo "replaces every existing issue value and governs every name in the zone that has no CAA"
247+
echo "record of its own, so other CAs and ACME accounts can no longer issue for them."
248+
echo "Give those names their own CAA records, or set SET_CAA=false."
240249
fi
250+
251+
ACCOUNT_URI=$(jq -j '.uri' "$account_file")
252+
for caa_tag in $(caa_tags_for "$domain"); do
253+
echo "Adding CAA record ($caa_tag) for $caa_domain, accounturi=$ACCOUNT_URI"
254+
if ! dnsman.py set_caa \
255+
--domain "$caa_domain" \
256+
--caa-tag "$caa_tag" \
257+
--caa-value "letsencrypt.org;validationmethods=dns-01;accounturi=$ACCOUNT_URI"; then
258+
echo "Warning: Failed to set CAA record for $domain"
259+
echo "This is not critical - certificates can still be issued without CAA records"
260+
echo "Consider disabling CAA records by setting SET_CAA=false if this continues to fail"
261+
fi
262+
done
241263
}
242264

243265
process_domain() {

0 commit comments

Comments
 (0)