diff --git a/.github/dns-retirements.json b/.github/dns-retirements.json new file mode 100644 index 0000000..df0f8be --- /dev/null +++ b/.github/dns-retirements.json @@ -0,0 +1,3 @@ +{ + "retire": [] +} diff --git a/AGENTS.md b/AGENTS.md index 9654708..4063eb5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -43,6 +43,13 @@ the clean and disposable failure fixtures locally. replace a DNS record only after the import migration is complete; zone or bucket destruction requires a separate, explicit change to that lifecycle boundary. +- The guarded DNS apply deletes a DNS record only through + `.github/dns-retirements.json`: one reviewed entry per record, naming its + `address`, `name`, `type` and current `content`, in the change that removes + the record from `dns.tf`. `scripts/assert-no-destructive-plan.sh` and + `scripts/check-dns-plan.jq` admit only a pure delete that matches its entry; + replacements, unlisted deletes and deletes of anything but a DNS record stay + refused. Remove an entry once its deletion is applied. - Run `scripts/test.sh` before every PR. - The confirmed main-only state-bootstrap workflow is a programmed data writer. It owns `recovery/cloudflare-state-` branches and immutable diff --git a/README.md b/README.md index fca1bc4..011e935 100644 --- a/README.md +++ b/README.md @@ -68,9 +68,10 @@ The run creates a `.terraform/` directory in the repository, which Git ignores and which you can delete, and a few `cloudflare-plan-*.out` files in `/tmp`. This check never contacts the Cloudflare API and never reads state. It shows -that the configuration is well-formed and that the guard rejects deletes and -replacements. It does not show what a plan against the live account would -change. Do not set `CLOUDFLARE_API_TOKEN` or backend credentials to run it. +that the configuration is well-formed and that the guard rejects replacements +and every delete that the reviewed retirement list does not name. It does not +show what a plan against the live account would change. Do not set +`CLOUDFLARE_API_TOKEN` or backend credentials to run it. [CI](.github/workflows/ci.yaml) runs the same script on every pull request and on every push to `main`, on a GitHub-hosted runner with no production credentials. @@ -91,8 +92,11 @@ DNS records are declared in [`dns.tf`](dns.tf), and the zone settings that would rewrite proxied pages in [`zone.tf`](zone.tf). Both change only through the manual **DNS plan and guarded apply** workflow: plan first, then apply the same reviewed revision. It imports declared records that already exist, never -deletes or replaces anything, and keeps an encrypted state copy from before and -after every apply. Other resources still have no general apply path. +replaces anything, and keeps an encrypted state copy from before and after +every apply. It deletes a DNS record only when +[`.github/dns-retirements.json`](.github/dns-retirements.json) names it: one +reviewed entry per record, removed again once the deletion is applied. Other +resources still have no general apply path. The ownership decision and dependency boundary are recorded in [`docs/adr/0001-cluster-independent-cloudflare-owner.md`](docs/adr/0001-cluster-independent-cloudflare-owner.md). diff --git a/docs/runbooks/bootstrap-and-recovery.md b/docs/runbooks/bootstrap-and-recovery.md index d3bef8d..308dc4e 100644 --- a/docs/runbooks/bootstrap-and-recovery.md +++ b/docs/runbooks/bootstrap-and-recovery.md @@ -369,7 +369,8 @@ may change. Those settings, the bot management configuration and the listed zone settings, always exist, so they are imported by zone, never created or forgotten, and may be updated by the change that imports them. Imports must leave their record exactly as it is. Creates, updates and `removed` blocks with -`destroy = false` are admitted; deletes and replacements are not. A created or +`destroy = false` are admitted; replacements are not. A delete is admitted only +as a reviewed retirement of a DNS record, described below. 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. Each record it claims must be one the same plan stops managing, or one nothing here @@ -406,8 +407,35 @@ record and replace the record's resource with a `removed` block that keeps the record (`lifecycle { destroy = false }`), in the same change. Forget the ownership record in the next change, because external-dns then owns it. To undo an apply, revert its pull request and run the same two dispatches; a revert that -would delete a record is refused, so restore deleted values by declaring them -again. +would delete a record is refused unless that record gets a retirement entry, as +below. Restore deleted values by declaring them again. + +To delete a record this repository manages, remove its resource from `dns.tf` +and, in the same pull request, add one entry for it to +`.github/dns-retirements.json`: + +```json +{ + "retire": [ + { + "address": "cloudflare_dns_record.", + "name": "", + "type": "MX", + "content": "" + } + ] +} +``` + +`type` is one of `A`, `AAAA`, `CNAME`, `MX` and `TXT`. Both +`scripts/assert-no-destructive-plan.sh` and `scripts/check-dns-plan.jq` admit +the plan's delete only when its actions are exactly `delete`, the resource is a +DNS record at the listed address, and its name (in full, lower-case form), type +and content before the change equal the entry. The plan check also refuses a +record that external-dns owns. A listed record is still never replaced, and +nothing but a DNS record is ever deleted. The list holds at most 20 entries +with unique addresses and nothing else; a malformed list refuses every run. +Once the deletion is applied, remove its entry in a later change. ## Recovery diff --git a/scripts/apply-dns.sh b/scripts/apply-dns.sh index b702c33..7936bcd 100755 --- a/scripts/apply-dns.sh +++ b/scripts/apply-dns.sh @@ -1,7 +1,8 @@ #!/usr/bin/env bash # Reviewed DNS changes against the authoritative R2 state. The change phase -# imports declared records that already exist, refuses deletes, replacements -# and changing imports, and in apply mode captures encrypted state before and +# imports declared records that already exist, refuses replacements, changing +# imports and every delete that .github/dns-retirements.json does not name, +# and in apply mode captures encrypted state before and # after the apply. The recover phase plans from a fresh runner and must find # nothing to do. Only fixed verdict lines reach the log; values stay encrypted. set +x @@ -214,8 +215,14 @@ stage=boundary 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" --slurpfile handover "$handover" -f "$root/scripts/check-dns-plan.jq" "$scratch/evidence/change.json" >/dev/null 2>&1 || stop +# The reviewed list of DNS records this change may delete: one entry per +# record, by address, name, type and content. Every other delete is refused. +retirements="$root/.github/dns-retirements.json" +[[ -f $retirements && ! -L $retirements && $(wc -c < "$retirements") -le 65536 ]] || stop +state_unique_json "$retirements" || stop +bash "$root/scripts/assert-no-destructive-plan.sh" "$scratch/evidence/change.json" "$retirements" > "$scratch/evidence/destructive.log" 2>&1 || stop +jq -e --slurpfile imports "$scratch/evidence/imports.json" --slurpfile snapshot "$scratch/evidence/snapshot.json" --slurpfile handover "$handover" \ + --slurpfile retirements "$retirements" -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/assert-no-destructive-plan.sh b/scripts/assert-no-destructive-plan.sh index 39ed876..80e4c53 100755 --- a/scripts/assert-no-destructive-plan.sh +++ b/scripts/assert-no-destructive-plan.sh @@ -1,8 +1,16 @@ #!/usr/bin/env sh +# Refuses every delete and replacement in a saved OpenTofu plan (JSON). +# +# The optional second argument is the reviewed retirement list +# (.github/dns-retirements.json). With it, a pure delete (actions exactly +# ["delete"]) of a managed DNS record is admitted when the list names its +# address, and its name, type and content before the change equal the listed +# entry. Every other delete, every replacement, and every delete of a resource +# that is not a DNS record stays refused. A malformed list refuses the plan. set -eu -if [ "$#" -ne 1 ]; then - echo "usage: $0 " >&2 +if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then + echo "usage: $0 [dns-retirements.json]" >&2 exit 2 fi @@ -20,12 +28,50 @@ if ! jq -e '.resource_changes | type == "array"' "$plan_json" >/dev/null 2>&1; t exit 2 fi +# The list shape and name normalisation match scripts/check-dns-plan.jq. +# shellcheck disable=SC2016 # jq variables, expanded by jq. +filters=' +def fqdn: ascii_downcase | rtrimstr(".") | + if .=="@" then "devantler.tech" + elif .=="devantler.tech" or endswith(".devantler.tech") then . + else .+".devantler.tech" end; +def valid: length==1 and (.[0] | type=="object" and keys==["retire"] and + (.retire | type=="array" and length<=20 and + all(.[]; type=="object" and keys==["address","content","name","type"] and + (.address|type=="string" and test("\\Acloudflare_dns_record\\.[A-Za-z_][A-Za-z0-9_-]*\\z")) and + (.name|type=="string" and test("\\A[A-Za-z0-9_.*@-]+\\z")) and + (.type|IN("A","AAAA","CNAME","MX","TXT")) and + (.content|type=="string" and length>0)) and + (map(.address)|length)==(map(.address)|unique|length))); +def retired($retire): + .change.before as $before | .address as $address | + .change.actions==["delete"] and .mode=="managed" and .type=="cloudflare_dns_record" and + ($before|type=="object") and ($before.name|type=="string") and + ($before.type|type=="string") and ($before.content|type=="string") and + any($retire[]; .address==$address and (.name|fqdn)==($before.name|fqdn) and + .type==($before.type|ascii_upcase) and .content==$before.content); +' + +if [ "$#" -eq 2 ]; then + retirements=$2 + if [ ! -f "$retirements" ] || + ! jq -n -e --slurpfile list "$retirements" "$filters"'$list | valid' >/dev/null 2>&1; then + echo "ERROR: the DNS retirement list is invalid: $retirements" >&2 + exit 2 + fi + set -- --slurpfile list "$retirements" +else + retirements='' + set -- --argjson list '[{"retire":[]}]' +fi + tmp_file=${TMPDIR:-/tmp}/cloudflare-destructive-plan.$$ trap 'rm -f "$tmp_file"' EXIT HUP INT TERM -jq -r ' +jq -r "$@" "$filters"' .resource_changes[] | select(any(.change.actions[]; . == "delete")) + | select(retired($list[0].retire) | not) | "\(.address): \(.change.actions | join(","))" ' "$plan_json" >"$tmp_file" @@ -35,4 +81,9 @@ if [ -s "$tmp_file" ]; then exit 1 fi -echo "OpenTofu plan contains 0 destructive resource changes" +if [ -z "$retirements" ]; then + echo "OpenTofu plan contains 0 destructive resource changes" + exit 0 +fi +count=$(jq "$@" "$filters"'[.resource_changes[] | select(retired($list[0].retire))] | length' "$plan_json") +echo "OpenTofu plan contains 0 destructive resource changes besides $count reviewed DNS record retirement(s)" diff --git a/scripts/check-dns-plan.jq b/scripts/check-dns-plan.jq index 8c71451..1894f6d 100644 --- a/scripts/check-dns-plan.jq +++ b/scripts/check-dns-plan.jq @@ -1,7 +1,11 @@ # Input: the saved plan JSON. $imports[0]: the resolved import list. # $snapshot[0]: the live zone. Names are compared in their full, lower-case # form, so a relative or differently written name cannot slip by. -# Only DNS records may change. Nothing is deleted or replaced. Every import is +# Only DNS records may change, and nothing is replaced. A DNS record is deleted +# only as a reviewed retirement: a pure delete of a record external-dns does +# not own, whose address the reviewed $retirements[0] list +# (.github/dns-retirements.json) names together with the record's name, type +# and content before the change. Nothing else is ever deleted. Every import is # 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 @@ -35,12 +39,31 @@ def claims: [$rest|capture("^(?[a-z0-9]+)-(?.+)$")|{name, type}]; 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"]; +# $retire: the validated retirement entries, names in their full form. +def retired($retire; $live): + .change.before as $before | .address as $address | + .change.actions==["delete"] and .type=="cloudflare_dns_record" and + ($before|type=="object") and ($before.name|type=="string") and + ($before.type|type=="string") and ($before.content|type=="string") and + any($retire[]; .address==$address and .name==($before.name|fqdn) and + .type==($before.type|ascii_upcase) and .content==$before.content) and + ($before|owned($live)|not); . 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 | +($retirements | if length==1 and (.[0] | type=="object" and keys==["retire"] and + (.retire | type=="array" and length<=20 and + all(.[]; type=="object" and keys==["address","content","name","type"] and + (.address|type=="string" and test("\\Acloudflare_dns_record\\.[A-Za-z_][A-Za-z0-9_-]*\\z")) and + (.name|type=="string" and test("\\A[A-Za-z0-9_.*@-]+\\z")) and + (.type|IN("A","AAAA","CNAME","MX","TXT")) and + (.content|type=="string" and length>0)) and + (map(.address)|length)==(map(.address)|unique|length))) + then [.[0].retire[] | .name |= fqdn] + else error("dns-retirements: invalid list") end) as $retire | [.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 | @@ -57,7 +80,7 @@ all($wanted[]; all(.resource_changes[]; . as $resource | .mode=="managed" and - (.change.actions|IN(["no-op"],["create"],["update"],["forget"])) and + (.change.actions|IN(["no-op"],["create"],["update"],["forget"],["delete"])) and (if .change.importing then any($wanted[]; .address==$resource.address) and (if setting then .change.actions|IN(["no-op"],["update"]) else .change.actions==["no-op"] and (changed|not) end) @@ -81,4 +104,5 @@ all(.resource_changes[]; all($claims[]; . as $claim | all($kept[]; .name!=$claim.name or ($claim.type!=null and .type!=$claim.type))) else ($after|owned($live)|not) end) + elif .change.actions==["delete"] then retired($retire; $live) else true end)) diff --git a/scripts/tests/fixtures/delete-zone.json b/scripts/tests/fixtures/delete-zone.json new file mode 100644 index 0000000..bbd6c67 --- /dev/null +++ b/scripts/tests/fixtures/delete-zone.json @@ -0,0 +1,15 @@ +{ + "format_version": "1.2", + "resource_changes": [ + { + "address": "cloudflare_zone.primary", + "mode": "managed", + "type": "cloudflare_zone", + "change": { + "actions": ["delete"], + "before": { "name": "devantler.tech" }, + "after": null + } + } + ] +} diff --git a/scripts/tests/fixtures/retire-mx02.json b/scripts/tests/fixtures/retire-mx02.json new file mode 100644 index 0000000..01e8c43 --- /dev/null +++ b/scripts/tests/fixtures/retire-mx02.json @@ -0,0 +1,25 @@ +{ + "format_version": "1.2", + "resource_changes": [ + { + "address": "cloudflare_dns_record.mail_mx_01", + "mode": "managed", + "type": "cloudflare_dns_record", + "change": { + "actions": ["update"], + "before": { "name": "devantler.tech", "type": "MX", "content": "mx01.mail.icloud.com", "priority": 10 }, + "after": { "name": "devantler.tech", "type": "MX", "content": "mail.devantler.tech", "priority": 10 } + } + }, + { + "address": "cloudflare_dns_record.mail_mx_02", + "mode": "managed", + "type": "cloudflare_dns_record", + "change": { + "actions": ["delete"], + "before": { "name": "devantler.tech", "type": "MX", "content": "mx02.mail.icloud.com", "priority": 10 }, + "after": null + } + } + ] +} diff --git a/scripts/tests/fixtures/retirements.json b/scripts/tests/fixtures/retirements.json new file mode 100644 index 0000000..7463ac5 --- /dev/null +++ b/scripts/tests/fixtures/retirements.json @@ -0,0 +1,10 @@ +{ + "retire": [ + { + "address": "cloudflare_dns_record.mail_mx_02", + "name": "devantler.tech", + "type": "MX", + "content": "mx02.mail.icloud.com" + } + ] +} diff --git a/scripts/tests/test-dns-apply.sh b/scripts/tests/test-dns-apply.sh index 5ca9f33..e1301a8 100755 --- a/scripts/tests/test-dns-apply.sh +++ b/scripts/tests/test-dns-apply.sh @@ -89,7 +89,8 @@ result=$(create cloudflare_dns_record.lower devantler.tech txt '"apple-domain=x" jq -e --arg zone "$zone" '.refused==[] and .imports==[{address:"cloudflare_dns_record.lower",id:($zone+"/00000000000000000000000000000003")}]' <<<"$result" >/dev/null printf '%s\n' 'PASS: declared records import only from one exact unowned live record' -# 3. Plan boundary: imports must be unchanged, nothing is deleted or replaced, +# 3. Plan boundary: imports must be unchanged, nothing is replaced, nothing is +# deleted while the retirement list is empty (3c covers listed retirements), # and an ownership record may only claim a record this plan stops managing. import_id="$zone/00000000000000000000000000000001" printf '[{"address":"cloudflare_dns_record.mail_mx_01","id":"%s"}]\n' "$import_id" > "$scratch/imports.json" @@ -100,7 +101,9 @@ change() { jq -n --arg address "$1" --arg type "$2" --argjson actions "$3" --arg '{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:.}'; } 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; } +printf '%s\n' '{"retire":[]}' > "$scratch/retirements.json" +check() { jq -e --slurpfile imports "$scratch/imports.json" --slurpfile snapshot "$scratch/snapshot.json" --slurpfile handover "$scratch/handover.json" \ + --slurpfile retirements "$scratch/retirements.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) @@ -200,6 +203,80 @@ printf '%s\n' "$bot_import" | reject "missing zone-setting import" printf '%s\n' "[{\"address\":\"cloudflare_dns_record.mail_mx_01\",\"id\":\"$import_id\"}]" > "$scratch/imports.json" printf '%s\n' 'PASS: zone settings are imported, may be updated, and never created, forgotten or widened' +# 3c. A DNS record is deleted only as a reviewed retirement: a pure delete whose +# address, name, type and content .github/dns-retirements.json names, of a +# record external-dns does not own. Nothing else is ever deleted or replaced. +mx02='{"name":"devantler.tech","type":"MX","content":"mx02.mail.icloud.com","ttl":3600}' +retire_mx02=$(change cloudflare_dns_record.mail_mx_02 cloudflare_dns_record '["delete"]' "$mx02" null null) +entry='{"address":"cloudflare_dns_record.mail_mx_02","name":"devantler.tech","type":"MX","content":"mx02.mail.icloud.com"}' +listed() { jq -cn --argjson entry "$entry" "{retire:[\$entry | $1]}" > "$scratch/retirements.json"; } +printf '%s\n' "$noop_import" "$retire_mx02" | reject "unlisted retirement" +listed . +printf '%s\n' "$noop_import" "$retire_mx02" | plan | check +# The company Email cutover: mx01 moves in place and mx02 is retired. +printf '%s\n' '[]' > "$scratch/imports.json" +{ change cloudflare_dns_record.mail_mx_01 cloudflare_dns_record '["update"]' "$record" '{"name":"devantler.tech","type":"MX","content":"mail.devantler.tech","ttl":3600}' null + printf '%s\n' "$retire_mx02"; } | plan | check +printf '%s\n' "[{\"address\":\"cloudflare_dns_record.mail_mx_01\",\"id\":\"$import_id\"}]" > "$scratch/imports.json" +for name in Devantler.Tech. @; do + listed ".name=\"$name\"" + printf '%s\n' "$noop_import" "$retire_mx02" | plan | check +done +for edit in '.content="mx01.mail.icloud.com"' '.name="www.devantler.tech"' '.type="TXT"' '.address="cloudflare_dns_record.mail_mx_03"'; do + listed "$edit" + printf '%s\n' "$noop_import" "$retire_mx02" | reject "retirement that differs from its entry ($edit)" +done +listed . +for actions in '["delete","create"]' '["create","delete"]'; do + { printf '%s\n' "$noop_import"; change cloudflare_dns_record.mail_mx_02 cloudflare_dns_record "$actions" "$mx02" "$mx02" null; } | reject "listed replacement $actions" +done +{ printf '%s\n' "$noop_import"; change cloudflare_dns_record.mail_mx_02 cloudflare_dns_record '["delete"]' null null null; } | reject "retirement without its prior record" +{ printf '%s\n' "$noop_import"; change cloudflare_dns_record.mail_mx_02 cloudflare_zone '["delete"]' "$mx02" null null; } | reject "listed address of a resource that is not a DNS record" +{ printf '%s\n' "$noop_import"; change cloudflare_zone.primary cloudflare_zone '["delete"]' '{"name":"devantler.tech"}' null null; } | reject "zone delete" +{ printf '%s\n' "$noop_import"; change 'cloudflare_r2_bucket.buckets["state"]' cloudflare_r2_bucket '["delete"]' '{"name":"state"}' null null; } | reject "bucket delete" +{ printf '%s\n' "$noop_import"; change cloudflare_zone_setting.nel cloudflare_zone_setting '["delete"]' '{"setting_id":"nel","value":"on"}' null null; } | reject "zone setting delete" +# A listed record that external-dns owns is never deleted here; one it does not own is. +printf '%s\n' '{"retire":[{"address":"cloudflare_dns_record.www","name":"www.devantler.tech","type":"CNAME","content":"pages.example.test"}, + {"address":"cloudflare_dns_record.portal","name":"portal.devantler.tech","type":"A","content":"192.0.2.1"}]}' > "$scratch/retirements.json" +{ printf '%s\n' "$noop_import"; change cloudflare_dns_record.www cloudflare_dns_record '["delete"]' "$www" null null; } | plan | check +{ printf '%s\n' "$noop_import"; change cloudflare_dns_record.portal cloudflare_dns_record '["delete"]' '{"name":"portal.devantler.tech","type":"A","content":"192.0.2.1"}' null null; } | reject "retirement of an external-dns record" +# A malformed list refuses every plan, like the handover list. +reject_list() { + if printf '%s\n' "$noop_import" | plan | check 2> "$scratch/list-error"; then printf 'FAIL: retirement list %s was admitted\n' "$1" >&2; exit 1; fi + grep -Fq 'dns-retirements: invalid list' "$scratch/list-error" +} +twenty=$(jq -cn --argjson entry "$entry" '{retire:([$entry] + [range(19)|{address:"cloudflare_dns_record.r\(.)",name:"r\(.).devantler.tech",type:"TXT",content:"x"}])}') +printf '%s\n' "$twenty" > "$scratch/retirements.json" +printf '%s\n' "$noop_import" "$retire_mx02" | plan | check +twenty_one=$(jq -c '.retire += [{address:"cloudflare_dns_record.r19",name:"r19.devantler.tech",type:"TXT",content:"x"}]' <<<"$twenty") +for list in "$twenty_one" '{"retire":[{"address":"cloudflare_dns_record.a","name":"devantler.tech","type":"mx","content":"x"}]}' \ + '{"retire":[{"address":"cloudflare_dns_record.a","name":"devantler.tech","type":"SRV","content":"x"}]}' \ + '{"retire":[{"address":"cloudflare_dns_record.a","name":"devantler.tech","type":"MX","content":""}]}' \ + '{"retire":[{"address":"cloudflare_dns_record.a","name":"devantler.tech","type":"MX","content":10}]}' \ + '{"retire":[{"address":"cloudflare_dns_record.a","name":"devantler.tech","type":"MX"}]}' \ + '{"retire":[{"address":"cloudflare_dns_record.a","name":"devantler.tech","type":"MX","content":"x","extra":1}]}' \ + '{"retire":[{"address":"cloudflare_zone.primary","name":"devantler.tech","type":"MX","content":"x"}]}' \ + '{"retire":[{"address":"cloudflare_dns_record.records[\"x\"]","name":"devantler.tech","type":"MX","content":"x"}]}' \ + '{"retire":[{"address":"cloudflare_dns_record.a","name":"devantler tech","type":"MX","content":"x"}]}' \ + '{"retire":[{"address":"cloudflare_dns_record.a","name":"","type":"MX","content":"x"}]}' \ + '{"retire":[{"address":"cloudflare_dns_record.a","name":"a.devantler.tech","type":"A","content":"x"},{"address":"cloudflare_dns_record.a","name":"b.devantler.tech","type":"A","content":"y"}]}' \ + '{"retire":["cloudflare_dns_record.a"]}' '{"retire":{}}' '{"retire":[],"extra":1}' '{"other":[]}' '[]' '{"retire":[]} {"retire":[]}'; do + printf '%s\n' "$list" > "$scratch/retirements.json" + reject_list "$list" +done +printf '%s\n' 'not json' > "$scratch/retirements.json" +if printf '%s\n' "$noop_import" | plan | check 2>/dev/null; then printf 'FAIL: unparsable retirement list was admitted\n' >&2; exit 1; fi +# The repository's own list is valid, so a typo in it cannot stop every DNS run, +# and the apply passes it to both guards. +cp "$root/.github/dns-retirements.json" "$scratch/retirements.json" +printf '%s\n' "$noop_import" | plan | check +# shellcheck disable=SC2016 # The patterns are literal apply-dns.sh text. +{ grep -Fq 'retirements="$root/.github/dns-retirements.json"' "$root/scripts/apply-dns.sh" + grep -Fq 'assert-no-destructive-plan.sh" "$scratch/evidence/change.json" "$retirements"' "$root/scripts/apply-dns.sh" + grep -Fq -- '--slurpfile retirements "$retirements"' "$root/scripts/apply-dns.sh"; } +printf '%s\n' '{"retire":[]}' > "$scratch/retirements.json" +printf '%s\n' 'PASS: DNS records are deleted only as reviewed retirements, never replaced' + # 4. The declared mail records are the ones the company Email depends on. grep -q 'content = "mx01.mail.icloud.com"' "$root/dns.tf" grep -q 'content = "mx02.mail.icloud.com"' "$root/dns.tf" diff --git a/scripts/tests/test-no-destructive-plan.sh b/scripts/tests/test-no-destructive-plan.sh index 9029ca7..31516e6 100755 --- a/scripts/tests/test-no-destructive-plan.sh +++ b/scripts/tests/test-no-destructive-plan.sh @@ -4,6 +4,8 @@ set -eu repo_root=$(CDPATH='' cd -- "$(dirname -- "$0")/../.." && pwd) guard="$repo_root/scripts/assert-no-destructive-plan.sh" fixtures="$repo_root/scripts/tests/fixtures" +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT HUP INT TERM fail() { echo "FAIL: $*" >&2 @@ -12,7 +14,7 @@ fail() { "$guard" "$fixtures/create-update.json" >/tmp/cloudflare-plan-safe.out 2>&1 || fail "create/update plan should be accepted" -grep -Fq "0 destructive resource changes" /tmp/cloudflare-plan-safe.out || +grep -Fxq "OpenTofu plan contains 0 destructive resource changes" /tmp/cloudflare-plan-safe.out || fail "safe-plan confirmation is missing" if "$guard" "$fixtures/delete.json" >/tmp/cloudflare-plan-delete.out 2>&1; then @@ -28,3 +30,112 @@ grep -Fq 'cloudflare_r2_bucket.backups["velero"]: delete,create' /tmp/cloudflare fail "replacement rejection should name the resource and action sequence" echo "PASS: destructive OpenTofu plans fail closed" + +# Reviewed DNS record retirements: the optional second argument is the +# reviewed list (.github/dns-retirements.json). Without it, the reviewed +# retirement of mail_mx_02 is refused like every other delete. +retire="$fixtures/retire-mx02.json" +list="$fixtures/retirements.json" +if "$guard" "$retire" >"$work/unlisted.out" 2>&1; then + fail "a delete without a retirement list should be rejected" +fi +grep -Fq 'cloudflare_dns_record.mail_mx_02: delete' "$work/unlisted.out" || + fail "a delete without a retirement list should name the resource" + +"$guard" "$retire" "$list" >"$work/listed.out" 2>&1 || + fail "a listed pure DNS record delete should be accepted" +grep -Fxq 'OpenTofu plan contains 0 destructive resource changes besides 1 reviewed DNS record retirement(s)' "$work/listed.out" || + fail "an accepted retirement should be counted" +"$guard" "$fixtures/create-update.json" "$list" >"$work/safe-listed.out" 2>&1 || + fail "a plan without deletes should be accepted with a list" +grep -Fxq 'OpenTofu plan contains 0 destructive resource changes besides 0 reviewed DNS record retirement(s)' "$work/safe-listed.out" || + fail "a plan without retirements should count none" + +# reject +reject() { + if "$guard" "$1" "$2" >"$work/reject.out" 2>&1; then + fail "$4 should be rejected" + fi + grep -Fq "$3" "$work/reject.out" || fail "$4 should be reported as: $3" +} +reject "$fixtures/delete.json" "$list" 'cloudflare_dns_record.legacy: delete' "an unlisted delete" +reject "$fixtures/delete-zone.json" "$list" 'cloudflare_zone.primary: delete' "a zone delete" +reject "$fixtures/replace.json" "$list" 'cloudflare_r2_bucket.backups["velero"]: delete,create' "a bucket replacement" + +# A listed record is still never replaced. +for actions in delete,create create,delete; do + jq --arg actions "$actions" '(.resource_changes[] | select(.address == "cloudflare_dns_record.mail_mx_02") | .change.actions) = ($actions | split(","))' \ + "$retire" >"$work/plan.json" + reject "$work/plan.json" "$list" "cloudflare_dns_record.mail_mx_02: $actions" "a listed replacement ($actions)" +done + +# The plan's record must be the listed one: a DNS record, managed here, with +# the listed name, type and content before the change. +for edit in '.type = "cloudflare_r2_bucket"' '.mode = "data"' '.change.before = null' '.change.before.content = "mx01.mail.icloud.com"'; do + jq "(.resource_changes[] | select(.address == \"cloudflare_dns_record.mail_mx_02\")) |= ($edit)" "$retire" >"$work/plan.json" + reject "$work/plan.json" "$list" 'cloudflare_dns_record.mail_mx_02: delete' "a retirement whose plan differs ($edit)" +done +for edit in '.content = "mx01.mail.icloud.com"' '.name = "www.devantler.tech"' '.type = "TXT"' '.address = "cloudflare_dns_record.mail_mx_03"'; do + jq ".retire[0] |= ($edit)" "$list" >"$work/list.json" + reject "$retire" "$work/list.json" 'cloudflare_dns_record.mail_mx_02: delete' "a retirement whose list entry differs ($edit)" +done + +# Names compare in their full, lower-case form. +for name in Devantler.Tech. @; do + jq --arg name "$name" '.retire[0].name = $name' "$list" >"$work/list.json" + "$guard" "$retire" "$work/list.json" >"$work/name.out" 2>&1 || + fail "the listed name $name should match devantler.tech" +done + +# At most 20 entries, each exactly {address, name, type, content}, with unique +# addresses of DNS records declared here. Anything else is refused before the +# plan is read, even when the plan deletes nothing. +jq '.retire += [range(19) | {address: "cloudflare_dns_record.r\(.)", name: "r\(.).devantler.tech", type: "TXT", content: "x"}]' \ + "$list" >"$work/list.json" +"$guard" "$retire" "$work/list.json" >"$work/twenty.out" 2>&1 || + fail "a list of 20 entries should be accepted" +cat >"$work/malformed" <<'EOF' +{"retire":[{"address":"cloudflare_dns_record.mail_mx_02","name":"devantler.tech","type":"mx","content":"mx02.mail.icloud.com"}]} +{"retire":[{"address":"cloudflare_dns_record.mail_mx_02","name":"devantler.tech","type":"SRV","content":"mx02.mail.icloud.com"}]} +{"retire":[{"address":"cloudflare_dns_record.mail_mx_02","name":"devantler.tech","type":"MX","content":""}]} +{"retire":[{"address":"cloudflare_dns_record.mail_mx_02","name":"devantler.tech","type":"MX","content":10}]} +{"retire":[{"address":"cloudflare_dns_record.mail_mx_02","name":"devantler.tech","type":"MX"}]} +{"retire":[{"address":"cloudflare_dns_record.mail_mx_02","name":"devantler.tech","type":"MX","content":"mx02.mail.icloud.com","extra":1}]} +{"retire":[{"address":"cloudflare_zone.primary","name":"devantler.tech","type":"MX","content":"mx02.mail.icloud.com"}]} +{"retire":[{"address":"cloudflare_r2_bucket.backups","name":"devantler.tech","type":"MX","content":"mx02.mail.icloud.com"}]} +{"retire":[{"address":"cloudflare_dns_record.records[\"x\"]","name":"devantler.tech","type":"MX","content":"mx02.mail.icloud.com"}]} +{"retire":[{"address":"cloudflare_dns_record.mail_mx_02","name":"devantler tech","type":"MX","content":"mx02.mail.icloud.com"}]} +{"retire":[{"address":"cloudflare_dns_record.mail_mx_02","name":"","type":"MX","content":"mx02.mail.icloud.com"}]} +{"retire":[{"address":"cloudflare_dns_record.mail_mx_02","name":"devantler.tech","type":"MX","content":"mx02.mail.icloud.com"},{"address":"cloudflare_dns_record.mail_mx_02","name":"devantler.tech","type":"MX","content":"mx02.mail.icloud.com"}]} +{"retire":["cloudflare_dns_record.mail_mx_02"]} +{"retire":{}} +{"retire":[],"extra":1} +{"other":[]} +{"retire":[]} {"retire":[]} +[] +not json +EOF +jq -c '.retire += [range(20) | {address: "cloudflare_dns_record.r\(.)", name: "r\(.).devantler.tech", type: "TXT", content: "x"}]' \ + "$list" >>"$work/malformed" +while IFS= read -r malformed; do + printf '%s\n' "$malformed" >"$work/list.json" + for plan in "$retire" "$fixtures/create-update.json"; do + if "$guard" "$plan" "$work/list.json" >"$work/malformed.out" 2>&1; then + fail "a malformed retirement list was accepted: $malformed" + fi + grep -Fq 'retirement list is invalid' "$work/malformed.out" || + fail "a malformed retirement list should be reported: $malformed" + done +done <"$work/malformed" +if "$guard" "$fixtures/create-update.json" "$work/missing.json" >"$work/missing.out" 2>&1; then + fail "a missing retirement list should be rejected" +fi +if "$guard" "$fixtures/create-update.json" "$list" "$list" >"$work/arguments.out" 2>&1; then + fail "a third argument should be rejected" +fi + +# The repository's own list is valid, so a typo in it cannot stop every DNS run. +"$guard" "$fixtures/create-update.json" "$repo_root/.github/dns-retirements.json" >"$work/repository.out" 2>&1 || + fail "the repository's retirement list should be valid" + +echo "PASS: DNS records are deleted only through the reviewed retirement list"