Skip to content

Commit 4eeb50e

Browse files
devantlerclaude
andauthored
feat(dns): retire DNS records only through a reviewed list (#91)
The guarded DNS apply refused every delete, so a reviewed change could not remove a DNS record, such as the second iCloud MX record the company Email cutover retires (devantler-tech/platform#5481). Add .github/dns-retirements.json, committed empty. Each entry names one record by address, name, type and current content; at most 20 entries with unique addresses, and a malformed list refuses every run, like the handover list. scripts/assert-no-destructive-plan.sh takes the list as an optional second argument and then admits only a pure delete of a managed DNS record that matches its entry, reporting how many it admitted. Without the argument it behaves exactly as before. scripts/check-dns-plan.jq takes the list as $retirements and admits ["delete"] only for a matching record that external-dns does not own. Replacements, unlisted deletes and deletes of anything but a DNS record stay refused. scripts/apply-dns.sh checks the file like the handover list and passes it to both guards. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5d065fd commit 4eeb50e

12 files changed

Lines changed: 383 additions & 21 deletions

‎.github/dns-retirements.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"retire": []
3+
}

‎AGENTS.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ the clean and disposable failure fixtures locally.
4343
replace a DNS record only after the import migration is complete; zone or
4444
bucket destruction requires a separate, explicit change to that lifecycle
4545
boundary.
46+
- The guarded DNS apply deletes a DNS record only through
47+
`.github/dns-retirements.json`: one reviewed entry per record, naming its
48+
`address`, `name`, `type` and current `content`, in the change that removes
49+
the record from `dns.tf`. `scripts/assert-no-destructive-plan.sh` and
50+
`scripts/check-dns-plan.jq` admit only a pure delete that matches its entry;
51+
replacements, unlisted deletes and deletes of anything but a DNS record stay
52+
refused. Remove an entry once its deletion is applied.
4653
- Run `scripts/test.sh` before every PR.
4754
- The confirmed main-only state-bootstrap workflow is a programmed data writer.
4855
It owns `recovery/cloudflare-state-<run>` branches and immutable

‎README.md‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ The run creates a `.terraform/` directory in the repository, which Git ignores
6868
and which you can delete, and a few `cloudflare-plan-*.out` files in `/tmp`.
6969

7070
This check never contacts the Cloudflare API and never reads state. It shows
71-
that the configuration is well-formed and that the guard rejects deletes and
72-
replacements. It does not show what a plan against the live account would
73-
change. Do not set `CLOUDFLARE_API_TOKEN` or backend credentials to run it.
71+
that the configuration is well-formed and that the guard rejects replacements
72+
and every delete that the reviewed retirement list does not name. It does not
73+
show what a plan against the live account would change. Do not set
74+
`CLOUDFLARE_API_TOKEN` or backend credentials to run it.
7475

7576
[CI](.github/workflows/ci.yaml) runs the same script on every pull request
7677
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
9192
rewrite proxied pages in [`zone.tf`](zone.tf). Both change only through the
9293
manual **DNS plan and guarded apply** workflow: plan first, then apply the same
9394
reviewed revision. It imports declared records that already exist, never
94-
deletes or replaces anything, and keeps an encrypted state copy from before and
95-
after every apply. Other resources still have no general apply path.
95+
replaces anything, and keeps an encrypted state copy from before and after
96+
every apply. It deletes a DNS record only when
97+
[`.github/dns-retirements.json`](.github/dns-retirements.json) names it: one
98+
reviewed entry per record, removed again once the deletion is applied. Other
99+
resources still have no general apply path.
96100

97101
The ownership decision and dependency boundary are recorded in
98102
[`docs/adr/0001-cluster-independent-cloudflare-owner.md`](docs/adr/0001-cluster-independent-cloudflare-owner.md).

‎docs/runbooks/bootstrap-and-recovery.md‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ may change. Those settings, the bot management configuration and the listed
369369
zone settings, always exist, so they are imported by zone, never created or
370370
forgotten, and may be updated by the change that imports them. Imports must leave
371371
their record exactly as it is. Creates, updates and `removed` blocks with
372-
`destroy = false` are admitted; deletes and replacements are not. A created or
372+
`destroy = false` are admitted; replacements are not. A delete is admitted only
373+
as a reviewed retirement of a DNS record, described below. A created or
373374
updated record must have a known name and must not be owned by external-dns.
374375
An `_externaldns.` ownership record must claim at least one live record. Each
375376
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
406407
record (`lifecycle { destroy = false }`), in the same change. Forget the
407408
ownership record in the next change, because external-dns then owns it. To undo an
408409
apply, revert its pull request and run the same two dispatches; a revert that
409-
would delete a record is refused, so restore deleted values by declaring them
410-
again.
410+
would delete a record is refused unless that record gets a retirement entry, as
411+
below. Restore deleted values by declaring them again.
412+
413+
To delete a record this repository manages, remove its resource from `dns.tf`
414+
and, in the same pull request, add one entry for it to
415+
`.github/dns-retirements.json`:
416+
417+
```json
418+
{
419+
"retire": [
420+
{
421+
"address": "cloudflare_dns_record.<name>",
422+
"name": "<record name>",
423+
"type": "MX",
424+
"content": "<current content>"
425+
}
426+
]
427+
}
428+
```
429+
430+
`type` is one of `A`, `AAAA`, `CNAME`, `MX` and `TXT`. Both
431+
`scripts/assert-no-destructive-plan.sh` and `scripts/check-dns-plan.jq` admit
432+
the plan's delete only when its actions are exactly `delete`, the resource is a
433+
DNS record at the listed address, and its name (in full, lower-case form), type
434+
and content before the change equal the entry. The plan check also refuses a
435+
record that external-dns owns. A listed record is still never replaced, and
436+
nothing but a DNS record is ever deleted. The list holds at most 20 entries
437+
with unique addresses and nothing else; a malformed list refuses every run.
438+
Once the deletion is applied, remove its entry in a later change.
411439

412440
## Recovery
413441

‎scripts/apply-dns.sh‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env bash
22
# Reviewed DNS changes against the authoritative R2 state. The change phase
3-
# imports declared records that already exist, refuses deletes, replacements
4-
# and changing imports, and in apply mode captures encrypted state before and
3+
# imports declared records that already exist, refuses replacements, changing
4+
# imports and every delete that .github/dns-retirements.json does not name,
5+
# and in apply mode captures encrypted state before and
56
# after the apply. The recover phase plans from a fresh runner and must find
67
# nothing to do. Only fixed verdict lines reach the log; values stay encrypted.
78
set +x
@@ -214,8 +215,14 @@ stage=boundary
214215
handover="$root/.github/dns-handover.json"
215216
[[ -f $handover && ! -L $handover && $(wc -c < "$handover") -le 65536 ]] || stop
216217
state_unique_json "$handover" || stop
217-
bash "$root/scripts/assert-no-destructive-plan.sh" "$scratch/evidence/change.json" > "$scratch/evidence/destructive.log" 2>&1 || stop
218-
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
218+
# The reviewed list of DNS records this change may delete: one entry per
219+
# record, by address, name, type and content. Every other delete is refused.
220+
retirements="$root/.github/dns-retirements.json"
221+
[[ -f $retirements && ! -L $retirements && $(wc -c < "$retirements") -le 65536 ]] || stop
222+
state_unique_json "$retirements" || stop
223+
bash "$root/scripts/assert-no-destructive-plan.sh" "$scratch/evidence/change.json" "$retirements" > "$scratch/evidence/destructive.log" 2>&1 || stop
224+
jq -e --slurpfile imports "$scratch/evidence/imports.json" --slurpfile snapshot "$scratch/evidence/snapshot.json" --slurpfile handover "$handover" \
225+
--slurpfile retirements "$retirements" -f "$root/scripts/check-dns-plan.jq" "$scratch/evidence/change.json" >/dev/null 2>&1 || stop
219226
if [[ $DNS_MODE == plan ]]; then unset root_token; exit 0; fi
220227
stage=before-capture
221228
capture before || stop

‎scripts/assert-no-destructive-plan.sh‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
#!/usr/bin/env sh
2+
# Refuses every delete and replacement in a saved OpenTofu plan (JSON).
3+
#
4+
# The optional second argument is the reviewed retirement list
5+
# (.github/dns-retirements.json). With it, a pure delete (actions exactly
6+
# ["delete"]) of a managed DNS record is admitted when the list names its
7+
# address, and its name, type and content before the change equal the listed
8+
# entry. Every other delete, every replacement, and every delete of a resource
9+
# that is not a DNS record stays refused. A malformed list refuses the plan.
210
set -eu
311

4-
if [ "$#" -ne 1 ]; then
5-
echo "usage: $0 <tofu-plan.json>" >&2
12+
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
13+
echo "usage: $0 <tofu-plan.json> [dns-retirements.json]" >&2
614
exit 2
715
fi
816

@@ -20,12 +28,50 @@ if ! jq -e '.resource_changes | type == "array"' "$plan_json" >/dev/null 2>&1; t
2028
exit 2
2129
fi
2230

31+
# The list shape and name normalisation match scripts/check-dns-plan.jq.
32+
# shellcheck disable=SC2016 # jq variables, expanded by jq.
33+
filters='
34+
def fqdn: ascii_downcase | rtrimstr(".") |
35+
if .=="@" then "devantler.tech"
36+
elif .=="devantler.tech" or endswith(".devantler.tech") then .
37+
else .+".devantler.tech" end;
38+
def valid: length==1 and (.[0] | type=="object" and keys==["retire"] and
39+
(.retire | type=="array" and length<=20 and
40+
all(.[]; type=="object" and keys==["address","content","name","type"] and
41+
(.address|type=="string" and test("\\Acloudflare_dns_record\\.[A-Za-z_][A-Za-z0-9_-]*\\z")) and
42+
(.name|type=="string" and test("\\A[A-Za-z0-9_.*@-]+\\z")) and
43+
(.type|IN("A","AAAA","CNAME","MX","TXT")) and
44+
(.content|type=="string" and length>0)) and
45+
(map(.address)|length)==(map(.address)|unique|length)));
46+
def retired($retire):
47+
.change.before as $before | .address as $address |
48+
.change.actions==["delete"] and .mode=="managed" and .type=="cloudflare_dns_record" and
49+
($before|type=="object") and ($before.name|type=="string") and
50+
($before.type|type=="string") and ($before.content|type=="string") and
51+
any($retire[]; .address==$address and (.name|fqdn)==($before.name|fqdn) and
52+
.type==($before.type|ascii_upcase) and .content==$before.content);
53+
'
54+
55+
if [ "$#" -eq 2 ]; then
56+
retirements=$2
57+
if [ ! -f "$retirements" ] ||
58+
! jq -n -e --slurpfile list "$retirements" "$filters"'$list | valid' >/dev/null 2>&1; then
59+
echo "ERROR: the DNS retirement list is invalid: $retirements" >&2
60+
exit 2
61+
fi
62+
set -- --slurpfile list "$retirements"
63+
else
64+
retirements=''
65+
set -- --argjson list '[{"retire":[]}]'
66+
fi
67+
2368
tmp_file=${TMPDIR:-/tmp}/cloudflare-destructive-plan.$$
2469
trap 'rm -f "$tmp_file"' EXIT HUP INT TERM
2570

26-
jq -r '
71+
jq -r "$@" "$filters"'
2772
.resource_changes[]
2873
| select(any(.change.actions[]; . == "delete"))
74+
| select(retired($list[0].retire) | not)
2975
| "\(.address): \(.change.actions | join(","))"
3076
' "$plan_json" >"$tmp_file"
3177

@@ -35,4 +81,9 @@ if [ -s "$tmp_file" ]; then
3581
exit 1
3682
fi
3783

38-
echo "OpenTofu plan contains 0 destructive resource changes"
84+
if [ -z "$retirements" ]; then
85+
echo "OpenTofu plan contains 0 destructive resource changes"
86+
exit 0
87+
fi
88+
count=$(jq "$@" "$filters"'[.resource_changes[] | select(retired($list[0].retire))] | length' "$plan_json")
89+
echo "OpenTofu plan contains 0 destructive resource changes besides $count reviewed DNS record retirement(s)"

‎scripts/check-dns-plan.jq‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Input: the saved plan JSON. $imports[0]: the resolved import list.
22
# $snapshot[0]: the live zone. Names are compared in their full, lower-case
33
# form, so a relative or differently written name cannot slip by.
4-
# Only DNS records may change. Nothing is deleted or replaced. Every import is
4+
# Only DNS records may change, and nothing is replaced. A DNS record is deleted
5+
# only as a reviewed retirement: a pure delete of a record external-dns does
6+
# not own, whose address the reviewed $retirements[0] list
7+
# (.github/dns-retirements.json) names together with the record's name, type
8+
# and content before the change. Nothing else is ever deleted. Every import is
59
# known and leaves its record exactly as it is. A created or updated record
610
# has a known name and an upper-case type, and external-dns owns it neither
711
# before nor after the change. Its name uses only hostname characters. An
@@ -35,12 +39,31 @@ def claims:
3539
[$rest|capture("^(?<type>[a-z0-9]+)-(?<name>.+)$")|{name, type}];
3640
def hits($claim): (.name|fqdn)==$claim.name and ($claim.type==null or (.type|ascii_downcase)==$claim.type);
3741
def writes: .change.actions==["create"] or .change.actions==["update"];
42+
# $retire: the validated retirement entries, names in their full form.
43+
def retired($retire; $live):
44+
.change.before as $before | .address as $address |
45+
.change.actions==["delete"] and .type=="cloudflare_dns_record" and
46+
($before|type=="object") and ($before.name|type=="string") and
47+
($before.type|type=="string") and ($before.content|type=="string") and
48+
any($retire[]; .address==$address and .name==($before.name|fqdn) and
49+
.type==($before.type|ascii_upcase) and .content==$before.content) and
50+
($before|owned($live)|not);
3851
. as $plan | $imports[0] as $wanted | $snapshot[0].records as $live |
3952
($handover[0] | if type=="object" and keys==["externalDnsHandover"] and (.externalDnsHandover|type=="array" and length<=20) and
4053
all(.externalDnsHandover[]; type=="object" and keys==["name","type"] and (.type|IN("A","AAAA","CNAME")) and
4154
(.name|type=="string" and test("\\A[A-Za-z0-9.-]+\\z")))
4255
then [.externalDnsHandover[] | {name:(.name|fqdn), type:(.type|ascii_downcase)}]
4356
else error("dns-handover: invalid list") end) as $listed |
57+
($retirements | if length==1 and (.[0] | type=="object" and keys==["retire"] and
58+
(.retire | type=="array" and length<=20 and
59+
all(.[]; type=="object" and keys==["address","content","name","type"] and
60+
(.address|type=="string" and test("\\Acloudflare_dns_record\\.[A-Za-z_][A-Za-z0-9_-]*\\z")) and
61+
(.name|type=="string" and test("\\A[A-Za-z0-9_.*@-]+\\z")) and
62+
(.type|IN("A","AAAA","CNAME","MX","TXT")) and
63+
(.content|type=="string" and length>0)) and
64+
(map(.address)|length)==(map(.address)|unique|length)))
65+
then [.[0].retire[] | .name |= fqdn]
66+
else error("dns-retirements: invalid list") end) as $retire |
4467
[.resource_changes[] | select(.type=="cloudflare_dns_record" and .change.actions!=["forget"]) |
4568
.change.after // .change.before | select(type=="object") |
4669
{name:(.name // "" | fqdn), type:(.type // "" | ascii_downcase)}] as $kept |
@@ -57,7 +80,7 @@ all($wanted[];
5780
all(.resource_changes[];
5881
. as $resource |
5982
.mode=="managed" and
60-
(.change.actions|IN(["no-op"],["create"],["update"],["forget"])) and
83+
(.change.actions|IN(["no-op"],["create"],["update"],["forget"],["delete"])) and
6184
(if .change.importing then
6285
any($wanted[]; .address==$resource.address) and
6386
(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[];
81104
all($claims[]; . as $claim |
82105
all($kept[]; .name!=$claim.name or ($claim.type!=null and .type!=$claim.type)))
83106
else ($after|owned($live)|not) end)
107+
elif .change.actions==["delete"] then retired($retire; $live)
84108
else true end))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"format_version": "1.2",
3+
"resource_changes": [
4+
{
5+
"address": "cloudflare_zone.primary",
6+
"mode": "managed",
7+
"type": "cloudflare_zone",
8+
"change": {
9+
"actions": ["delete"],
10+
"before": { "name": "devantler.tech" },
11+
"after": null
12+
}
13+
}
14+
]
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"format_version": "1.2",
3+
"resource_changes": [
4+
{
5+
"address": "cloudflare_dns_record.mail_mx_01",
6+
"mode": "managed",
7+
"type": "cloudflare_dns_record",
8+
"change": {
9+
"actions": ["update"],
10+
"before": { "name": "devantler.tech", "type": "MX", "content": "mx01.mail.icloud.com", "priority": 10 },
11+
"after": { "name": "devantler.tech", "type": "MX", "content": "mail.devantler.tech", "priority": 10 }
12+
}
13+
},
14+
{
15+
"address": "cloudflare_dns_record.mail_mx_02",
16+
"mode": "managed",
17+
"type": "cloudflare_dns_record",
18+
"change": {
19+
"actions": ["delete"],
20+
"before": { "name": "devantler.tech", "type": "MX", "content": "mx02.mail.icloud.com", "priority": 10 },
21+
"after": null
22+
}
23+
}
24+
]
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"retire": [
3+
{
4+
"address": "cloudflare_dns_record.mail_mx_02",
5+
"name": "devantler.tech",
6+
"type": "MX",
7+
"content": "mx02.mail.icloud.com"
8+
}
9+
]
10+
}

0 commit comments

Comments
 (0)