diff --git a/.github/dns-handover.json b/.github/dns-handover.json new file mode 100644 index 0000000..fffae63 --- /dev/null +++ b/.github/dns-handover.json @@ -0,0 +1,6 @@ +{ + "externalDnsHandover": [ + { "type": "A", "name": "www.devantler.tech" }, + { "type": "AAAA", "name": "www.devantler.tech" } + ] +} diff --git a/dns.tf b/dns.tf index 0192f2f..17172fb 100644 --- a/dns.tf +++ b/dns.tf @@ -11,24 +11,31 @@ # The site entry points. They stay separate resources so a reviewed change can # hand one to external-dns with a `removed` block that keeps the record. +# +# The apex follows www, which external-dns points at the platform gateway, so +# the site keeps working if the gateway address changes. It was already a +# CNAME, so this is an in-place edit of one record and never leaves a gap. +# Roll back by restoring `devantler.github.io` with `proxied = false`. resource "cloudflare_dns_record" "apex" { zone_id = cloudflare_zone.primary.id name = "devantler.tech" type = "CNAME" - content = "devantler.github.io" + content = "www.devantler.tech" ttl = 1 - proxied = false - comment = "GitHub Pages Personal" + proxied = true + comment = "Follows www to the platform gateway" settings = { flatten_cname = false } } -# www moves to the platform: external-dns serves it from the business site's -# public route. This ownership record lets external-dns take over the existing -# CNAME and replace it with the gateway addresses in one batch, so www always -# resolves. The CNAME itself stays in Cloudflare; this repository only forgets it. -resource "cloudflare_dns_record" "www_external_dns_owner" { +# external-dns serves www from the business site's public route. It took over +# the old www CNAME through a one-time ownership record (forgotten below) and +# replaced it with the gateway addresses. Its own ownership records for those +# addresses then disappeared, so these two restore them with external-dns's +# exact content. external-dns re-owns the existing www records without +# changing them. .github/dns-handover.json lists the two records they claim. +resource "cloudflare_dns_record" "www_external_dns_owner_a" { zone_id = cloudflare_zone.primary.id - name = "_externaldns.cname-www.devantler.tech" + name = "_externaldns.a-www.devantler.tech" type = "TXT" content = "\"heritage=external-dns,external-dns/owner=platform.devantler.tech,external-dns/resource=httproute/business-site/business-site-public\"" ttl = 1 @@ -36,6 +43,24 @@ resource "cloudflare_dns_record" "www_external_dns_owner" { settings = {} } +resource "cloudflare_dns_record" "www_external_dns_owner_aaaa" { + zone_id = cloudflare_zone.primary.id + name = "_externaldns.aaaa-www.devantler.tech" + type = "TXT" + content = "\"heritage=external-dns,external-dns/owner=platform.devantler.tech,external-dns/resource=httproute/business-site/business-site-public\"" + ttl = 1 + proxied = false + settings = {} +} + +removed { + from = cloudflare_dns_record.www_external_dns_owner + + lifecycle { + destroy = false + } +} + removed { from = cloudflare_dns_record.www diff --git a/docs/runbooks/bootstrap-and-recovery.md b/docs/runbooks/bootstrap-and-recovery.md index d3b3639..a7fc82a 100644 --- a/docs/runbooks/bootstrap-and-recovery.md +++ b/docs/runbooks/bootstrap-and-recovery.md @@ -368,9 +368,11 @@ The saved plan must then pass `scripts/assert-no-destructive-plan.sh` and their record exactly as it is. Creates, updates and `removed` blocks with `destroy = false` are admitted; deletes and replacements are not. A created or updated record must have a known name and must not be owned by external-dns. -An `_externaldns.` ownership record must claim at least one live record, and -only records the same plan stops managing, so it can never hand a mail record, -or any record nothing here manages, to external-dns. The public +An `_externaldns.` ownership record must claim at least one live record. Each +record it claims must be one the same plan stops managing, or one nothing here +manages that `.github/dns-handover.json` names. That reviewed list hands a +record to external-dns explicitly, so a marker can never hand over a mail +record, or any record nobody chose, by accident. The public log shows `DNS_PLAN` and `DNS_DRIFT` lines, with the `dns.tf` addresses, actions and changed attribute names, plus counts for anything keyed by the private configuration and one `DNS_UNMANAGED` count, by type, of live records diff --git a/recovery/backend-source/configuration.json.age b/recovery/backend-source/configuration.json.age index 9d832a1..4405ae6 100644 Binary files a/recovery/backend-source/configuration.json.age and b/recovery/backend-source/configuration.json.age differ diff --git a/recovery/backend-source/manifest.json b/recovery/backend-source/manifest.json index 732162d..1d69a04 100644 --- a/recovery/backend-source/manifest.json +++ b/recovery/backend-source/manifest.json @@ -1,16 +1,16 @@ { "schemaVersion": 1, "kind": "cloudflare-state-recovery", - "capturedAt": "2026-11-15T03:59:45Z", - "sourceCommit": "457727e207af0d4acc448eff21937b9c9b7a2e9b", - "sourceRun": 43674749476, + "capturedAt": "2026-11-15T04:06:38Z", + "sourceCommit": "81ab7a9c9679795c71a31921054ed839eefbde4b", + "sourceRun": 43675268687, "recipient": "age1rk6fs67kly3h4zux5za429z3grjtvs8vcunav4sa28pxk738najsk8wgs6", "lineage": "d981e029-9986-63a7-2f54-379ce10ee6a4", - "serial": 4, + "serial": 5, "accountIdSha256": "3080d90d2ff8a5d38ec8c5dae7b9207b928b353c68f4dd81e0a378cf932a5f95", "zoneNameSha256": "1fc6c364e0d592421f88ba2d2cf6cf15691bbc711113d7907c159a638f35aeb0", - "stateCiphertextSha256": "d0758c769aa6f20bea6ff1f88d23e3eed70e54d92026baa895a7e3d3281ce6d6", - "configurationCiphertextSha256": "803b3e4140e92b9d8eb8691e9cdee1bcb12e1f2b44c58e999a4273cea1c7bcaa", - "statePlaintextSha256": "c4bc70c78223477b27d842eb75d2e7a8f6a3d95e74deb16e9098f016eb2a70e4", + "stateCiphertextSha256": "98834b1ab15abb6a04ffb5d49271e7b79aeab077e535e9135c9dc5afeec8ede4", + "configurationCiphertextSha256": "a2850916bd1e7839cb93aec4549d27d965053beba9ad7bc2aee04855eb84861f", + "statePlaintextSha256": "80624e0a313e0383040b73a53970aeaa2daaa14ef59a913f4747d7a1b5cc7d3e", "configurationPlaintextSha256": "84c9b6ea504f82a9fd694e701287e30d54fe9b42ecf81dfccfbf2cba9873239a" } diff --git a/recovery/backend-source/state.json.age b/recovery/backend-source/state.json.age index 0e5442a..142fc53 100644 Binary files a/recovery/backend-source/state.json.age and b/recovery/backend-source/state.json.age differ diff --git a/scripts/apply-dns.sh b/scripts/apply-dns.sh index 37a0139..fb62ad1 100755 --- a/scripts/apply-dns.sh +++ b/scripts/apply-dns.sh @@ -208,8 +208,13 @@ jq --slurpfile plan "$scratch/evidence/change.json" --slurpfile imports "$scratc ' "$scratch/evidence/snapshot.json" > "$scratch/evidence/unmanaged.json" || stop jq -r '"DNS_UNMANAGED count=\(length) types=\(group_by(.type)|map("\(.[0].type):\(length)")|join(","))"' "$scratch/evidence/unmanaged.json" stage=boundary +# The reviewed list of live records nothing here manages that a new +# external-dns ownership record may claim (see check-dns-plan.jq). +handover="$root/.github/dns-handover.json" +[[ -f $handover && ! -L $handover && $(wc -c < "$handover") -le 65536 ]] || stop +state_unique_json "$handover" || stop bash "$root/scripts/assert-no-destructive-plan.sh" "$scratch/evidence/change.json" > "$scratch/evidence/destructive.log" 2>&1 || stop -jq -e --slurpfile imports "$scratch/evidence/imports.json" --slurpfile snapshot "$scratch/evidence/snapshot.json" -f "$root/scripts/check-dns-plan.jq" "$scratch/evidence/change.json" >/dev/null 2>&1 || stop +jq -e --slurpfile imports "$scratch/evidence/imports.json" --slurpfile snapshot "$scratch/evidence/snapshot.json" --slurpfile handover "$handover" -f "$root/scripts/check-dns-plan.jq" "$scratch/evidence/change.json" >/dev/null 2>&1 || stop if [[ $DNS_MODE == plan ]]; then unset root_token; exit 0; fi stage=before-capture capture before || stop diff --git a/scripts/check-dns-plan.jq b/scripts/check-dns-plan.jq index 833b8d9..a6aa447 100644 --- a/scripts/check-dns-plan.jq +++ b/scripts/check-dns-plan.jq @@ -5,10 +5,11 @@ # known and leaves its record exactly as it is. A created or updated record # has a known name and an upper-case type, and external-dns owns it neither # before nor after the change. Its name uses only hostname characters. An -# external-dns ownership record must claim at least one live record, and every -# live record it claims must be one this plan stops managing. Otherwise it -# could hand a mail record, or a record nothing here manages, to external-dns, -# which would delete it. The typeless apex registry name is always refused, +# external-dns ownership record must claim at least one live record. Every live +# record it claims must be one this plan stops managing, or one that nothing +# here manages and that the reviewed $handover[0] list (.github/dns-handover.json) +# names. Otherwise it could hand a mail record, or any other record, to +# external-dns, which would delete it. The typeless apex registry name is always refused, # because it claims every apex record, the Email MX and TXT records included. def fqdn: ascii_downcase | rtrimstr(".") | if .=="@" then "devantler.tech" @@ -27,6 +28,11 @@ def claims: def hits($claim): (.name|fqdn)==$claim.name and ($claim.type==null or (.type|ascii_downcase)==$claim.type); def writes: .change.actions==["create"] or .change.actions==["update"]; . as $plan | $imports[0] as $wanted | $snapshot[0].records as $live | +($handover[0] | if type=="object" and keys==["externalDnsHandover"] and (.externalDnsHandover|type=="array" and length<=20) and + all(.externalDnsHandover[]; type=="object" and keys==["name","type"] and (.type|IN("A","AAAA","CNAME")) and + (.name|type=="string" and test("\\A[A-Za-z0-9.-]+\\z"))) + then [.externalDnsHandover[] | {name:(.name|fqdn), type:(.type|ascii_downcase)}] + else error("dns-handover: invalid list") end) as $listed | [.resource_changes[] | select(.type=="cloudflare_dns_record" and .change.actions!=["forget"]) | .change.after // .change.before | select(type=="object") | {name:(.name // "" | fqdn), type:(.type // "" | ascii_downcase)}] as $kept | @@ -58,7 +64,10 @@ all(.resource_changes[]; (if ($after.type=="TXT" and ($after.name|fqdn|startswith("_externaldns."))) then ($after|claims) as $claims | [$live[] | . as $r | select(any($claims[]; . as $c | $r|hits($c)))] as $claimed | - ($claimed|length)>0 and all($claimed[]; .id|IN($forgotten[])) and + ($claimed|length)>0 and + all($claimed[]; (.id|IN($forgotten[])) or + ({name:(.name|fqdn), type:(.type|ascii_downcase)} as $record | + ($record|IN($listed[])) and ($after.name|fqdn)=="_externaldns."+$record.type+"-"+$record.name)) and all($claims[]; . as $claim | all($kept[]; .name!=$claim.name or ($claim.type!=null and .type!=$claim.type))) else ($after|owned($live)|not) end) diff --git a/scripts/tests/test-dns-apply.sh b/scripts/tests/test-dns-apply.sh index 823cae6..7f03395 100755 --- a/scripts/tests/test-dns-apply.sh +++ b/scripts/tests/test-dns-apply.sh @@ -47,7 +47,9 @@ jq -n --arg zone "$zone" '{zone:{id:$zone}, records:[ {id:"00000000000000000000000000000008",name:"_externaldns.a-ghost.devantler.tech",type:"TXT",content:"\"heritage=external-dns\""}, {id:"00000000000000000000000000000009",name:"www.devantler.tech",type:"CNAME",content:"pages.example.test"}, {id:"0000000000000000000000000000000a",name:"sig1._domainkey.devantler.tech",type:"CNAME",content:"dkim.example.test"}, - {id:"0000000000000000000000000000000b",name:"sig2._domainkey.devantler.tech",type:"CNAME",content:"dkim2.example.test"} + {id:"0000000000000000000000000000000b",name:"sig2._domainkey.devantler.tech",type:"CNAME",content:"dkim2.example.test"}, + {id:"0000000000000000000000000000000c",name:"www.devantler.tech",type:"A",content:"192.0.2.10"}, + {id:"0000000000000000000000000000000d",name:"www.devantler.tech",type:"AAAA",content:"2001:db8::10"} ]}' > "$scratch/snapshot.json" create() { jq -n --arg address "$1" --arg name "$2" --arg type "$3" --arg content "$4" \ '{address:$address,mode:"managed",type:"cloudflare_dns_record",change:{actions:["create"],after:{name:$name,type:$type,content:$content}}}'; } @@ -97,7 +99,8 @@ www='{"id":"00000000000000000000000000000009","name":"www.devantler.tech","type" change() { jq -n --arg address "$1" --arg type "$2" --argjson actions "$3" --argjson before "$4" --argjson after "$5" --argjson importing "$6" \ '{address:$address,mode:"managed",type:$type,change:({actions:$actions,before:$before,after:$after,after_unknown:{}} + (if $importing then {importing:$importing} else {} end))}'; } plan() { jq -s '{resource_changes:.}'; } -check() { jq -e --slurpfile imports "$scratch/imports.json" --slurpfile snapshot "$scratch/snapshot.json" -f "$root/scripts/check-dns-plan.jq" >/dev/null; } +printf '%s\n' '{"externalDnsHandover":[]}' > "$scratch/handover.json" +check() { jq -e --slurpfile imports "$scratch/imports.json" --slurpfile snapshot "$scratch/snapshot.json" --slurpfile handover "$scratch/handover.json" -f "$root/scripts/check-dns-plan.jq" >/dev/null; } noop_import=$(change cloudflare_dns_record.mail_mx_01 cloudflare_dns_record '["no-op"]' "$record" "$record" "{\"id\":\"$import_id\"}") zone_noop=$(change cloudflare_zone.primary cloudflare_zone '["no-op"]' '{}' '{}' null) dkim_kept=$(change cloudflare_dns_record.mail_dkim_sig1 cloudflare_dns_record '["no-op"]' "$dkim" "$dkim" null) @@ -134,6 +137,30 @@ done for name in _externaldns.cname-sig2._domainkey.devantler.tech _externaldns.sig2._domainkey.devantler.tech; do { printf '%s\n' "$noop_import"; marker "$name"; } | reject "marker for an unmanaged live record $name" done +# The reviewed handover list admits a claim on an unmanaged live record it names, and nothing else. +{ printf '%s\n' "$noop_import"; marker _externaldns.a-www.devantler.tech; } | reject "unlisted handover of www A" +printf '%s\n' '{"externalDnsHandover":[{"type":"A","name":"www.devantler.tech"}]}' > "$scratch/handover.json" +{ printf '%s\n' "$noop_import"; marker _externaldns.a-www.devantler.tech; } | plan | check +{ printf '%s\n' "$noop_import"; marker _externaldns.cname-sig2._domainkey.devantler.tech; } | reject "handover of an unlisted record" +{ printf '%s\n' "$noop_import"; change cloudflare_dns_record.www_a cloudflare_dns_record '["no-op"]' '{"name":"www.devantler.tech","type":"A","content":"192.0.2.10"}' '{"name":"www.devantler.tech","type":"A","content":"192.0.2.10"}' null + marker _externaldns.a-www.devantler.tech; } | reject "listed record that is still declared here" +{ printf '%s\n' "$noop_import"; marker _externaldns.cname-www.devantler.tech; } | reject "listed name, unlisted type" +# A listed record may only be claimed by its own typed marker, never a wider typeless one. +printf '%s\n' '{"externalDnsHandover":[{"type":"A","name":"www.devantler.tech"},{"type":"AAAA","name":"www.devantler.tech"},{"type":"CNAME","name":"www.devantler.tech"}]}' > "$scratch/handover.json" +{ printf '%s\n' "$noop_import"; marker _externaldns.www.devantler.tech; } | reject "typeless marker for listed records" +twenty_one=$(jq -cn '{externalDnsHandover:([{type:"A",name:"www.devantler.tech"}] + [range(20)|{type:"A",name:"h\(.).devantler.tech"}])}') +for list in '{"externalDnsHandover":[{"type":"a","name":"www.devantler.tech"}]}' '{"externalDnsHandover":[{"type":"TXT","name":"devantler.tech"}]}' \ + '{"externalDnsHandover":[{"type":"A","name":"www.devantler.tech","extra":1}]}' '{"other":[]}' \ + '{"externalDnsHandover":[{"type":"A","name":"www.devantler.tech"}],"extra":1}' "$twenty_one" \ + '{"externalDnsHandover":[{"type":"A","name":"www.devantler.tech"},{"type":"CNAME","name":"sig2._domainkey.devantler.tech"}]}'; do + printf '%s\n' "$list" > "$scratch/handover.json" + { printf '%s\n' "$noop_import"; marker _externaldns.a-www.devantler.tech; } | reject "invalid handover list $list" +done +# The repository's own list is valid and admits exactly the www handover it was written for. +cp "$root/.github/dns-handover.json" "$scratch/handover.json" +{ printf '%s\n' "$noop_import"; marker _externaldns.a-www.devantler.tech + change cloudflare_dns_record.marker_aaaa cloudflare_dns_record '["create"]' null '{"name":"_externaldns.aaaa-www.devantler.tech","type":"TXT","content":"x"}' null; } | plan | check +printf '%s\n' '{"externalDnsHandover":[]}' > "$scratch/handover.json" # A record external-dns owns is never written here, and every write has a known # name (OpenTofu writes an unknown value as null). portal='{"name":"portal.devantler.tech","type":"A","content":"192.0.2.2"}'