Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/dns-retirements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"retire": []
}
7 changes: 7 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<run>` branches and immutable
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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).
Expand Down
34 changes: 31 additions & 3 deletions docs/runbooks/bootstrap-and-recovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>",
"name": "<record name>",
"type": "MX",
"content": "<current 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

Expand Down
15 changes: 11 additions & 4 deletions scripts/apply-dns.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
59 changes: 55 additions & 4 deletions scripts/assert-no-destructive-plan.sh
Original file line number Diff line number Diff line change
@@ -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 <tofu-plan.json>" >&2
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "usage: $0 <tofu-plan.json> [dns-retirements.json]" >&2
exit 2
fi

Expand All @@ -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"

Expand All @@ -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)"
28 changes: 26 additions & 2 deletions scripts/check-dns-plan.jq
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,12 +39,31 @@ def claims:
[$rest|capture("^(?<type>[a-z0-9]+)-(?<name>.+)$")|{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 |
Expand All @@ -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)
Expand All @@ -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))
15 changes: 15 additions & 0 deletions scripts/tests/fixtures/delete-zone.json
Original file line number Diff line number Diff line change
@@ -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
}
}
]
}
25 changes: 25 additions & 0 deletions scripts/tests/fixtures/retire-mx02.json
Original file line number Diff line number Diff line change
@@ -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
}
}
]
}
10 changes: 10 additions & 0 deletions scripts/tests/fixtures/retirements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"retire": [
{
"address": "cloudflare_dns_record.mail_mx_02",
"name": "devantler.tech",
"type": "MX",
"content": "mx02.mail.icloud.com"
}
]
}
Loading
Loading