Repository navigation
Description
Problem
infra/dns/save.sh writes the encrypted Terraform state of record non-atomically: sops -e … > terraform.tfstate.enc (save.sh:50). The shell truncates terraform.tfstate.enc before sops runs, so any encryption failure — missing/expired sops key, sops error, disk full, interruption — leaves the state of record empty or partial, destroying the only encrypted copy the whole DNS workflow treats as canonical.
This is the one unguarded write left in the flow the #1943 campaign hardened: load.sh refuses a missing/empty .enc, init.sh snapshots and byte-verifies the decrypted state around tofu init, and plan.sh re-verifies lineage and sets SKIP_SAVE on its refusal paths so the EXIT trap cannot save a refused state. But save.sh itself — including when plan.sh's EXIT-trap cleanup invokes it after a successful plan — can still zero the record on a routine sops failure.
Evidence
infra/dns/save.sh @ 2bcd46e0dc:
- :44-47 the already-current fast path decrypts and compares first, so the destructive path is only reached when the state actually changed — exactly when the new ciphertext matters most.
- :50
sops -e "$dir/terraform.tfstate" >"$dir/terraform.tfstate.enc"— in-place redirection, no temp file, no rename, no post-write verification (contrastinit.sh's snapshot + sha256 verify + restore). set -Eexuo pipefail(save.sh:2) makes the script exit on the sops failure, but the truncation has already happened; nothing restores the previous ciphertext.
Verified this run on the laptop (probe ~/git/fleet/probe-dns-save, real save.sh copied verbatim, stub sops on PATH):
- Stub
sops -dreturns differing content (so the already-current fast path is skipped); stubsops -eexits 1:save.shexits 1 andterraform.tfstate.encgoes from 26 bytes to 0 bytes. - Control (stub
sops -esucceeds): exit 0,.encholds the new ciphertext (15 bytes).
Why it matters
terraform.tfstate.enc is the state of record for the DNS estate: load.sh decrypts it, init.sh calls it "the decrypted state of record" and refuses to continue without it, and a void/mismatched state is the #1943 failure mode ("151 to add, 0 to destroy" against an empty state). Losing it to a transient sops failure turns a retry-able error into state recovery from the plaintext working copy — if that copy has been cleaned (cleanup_terraform_tfstate.sh exists in the same directory) or the failure happens on another machine, the encrypted record is simply gone.
Acceptance criteria
-
save.shencrypts to a temporary file in the same directory and atomically renames it overterraform.tfstate.enconly aftersopsexits 0 (and the temp file is non-empty); a failed encryption leaves the previous.encbyte-identical. - A post-write sanity check (e.g. the new
.encdecrypts and its lineage matches the plaintext state, mirroring plan.sh's check) runs before the save is reported successful, or the rename only happens after such a check on the temp file. - The temp file is cleaned up on failure without touching the live
.enc; the.lockcleanup behaviour is unchanged. - A test/probe with a failing
sops -estub shows.encunchanged and a non-zero exit; a succeeding stub shows the new ciphertext in place; the already-current fast path still skips re-encryption. -
load.sh's analogous in-place decrypt write (sops -d … > terraform.tfstate, load.sh:41) is assessed in the same pass: the plaintext target is re-derivable from.enc, so it may be documented as safe-by-reconstruction rather than changed — but the reasoning is recorded on this issue.
Activity
developing-today commentedon Nov 6, 2026
CODE-1988
developing-today commentedon Nov 6, 2026
🤖 Agent note · state: filed
dezren39 commentedon Nov 6, 2026
🤖 Agent note · handed off
17 remaining items
dezren39 commentedon Nov 9, 2026
load.sh assessment (task 14343, PR #8021): load.sh line 41 writes sops -d output directly over terraform.tfstate, which is the analogous in-place pattern - but it is safe-by-reconstruction and is documented rather than changed. Reason: terraform.tfstate (plaintext) is a working copy fully re-derivable from terraform.tfstate.enc, which load.sh validates as existing and non-empty before decrypting and never modifies. A failed decrypt can truncate the plaintext, but the record itself (the .enc) stays intact, and re-running load.sh restores the plaintext exactly; there is no data-loss path comparable to save.sh destroying the only encrypted copy. Changing load.sh would add churn without removing a loss mode. agent-note:v1 agent=repo-worker-code lane=code-h state=assessment task=14343
dezren39 commentedon Nov 9, 2026
Agent note - parked, CI pending. Agent: repo queue worker, lane code-h (repo-worker-code). Session: task 14343. PR #8021 opened (branch queue/14343-6536-dns-save-atomic, commit e80e7cd): save.sh atomic encrypt + test_save_atomic.sh, 9/9 stub-sops checks pass on the laptop. PR checks still QUEUED after the active poll window and no Codex findings posted yet, so task 14343 is parked 1200s per the CI rule. Resume: read PR #8021 check and Codex state fresh, merge when verified, then complete task 14343 and close issue #6536. Links: task 14343, issue #6536, PR #8021. agent-note:v1 agent=repo-worker-code lane=code-h state=parked task=14343
Merge pull request #8021 from developing-today/queue/14343-6536-dns-s…
dezren39 commentedon Nov 9, 2026
🤖 Agent note · done — PR #8021 merged