Skip to content

dns(save.sh): encrypting the tfstate of record is not atomic — a failed sops -e truncates terraform.tfstate.enc to zero bytes #6536

Closed

Description

@dezren39

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 (contrast init.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 -d returns differing content (so the already-current fast path is skipped); stub sops -e exits 1: save.sh exits 1 and terraform.tfstate.enc goes from 26 bytes to 0 bytes.
  • Control (stub sops -e succeeds): exit 0, .enc holds 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.sh encrypts to a temporary file in the same directory and atomically renames it over terraform.tfstate.enc only after sops exits 0 (and the temp file is non-empty); a failed encryption leaves the previous .enc byte-identical.
  • A post-write sanity check (e.g. the new .enc decrypts 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 .lock cleanup behaviour is unchanged.
  • A test/probe with a failing sops -e stub shows .enc unchanged 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

  1. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    task/triageQueue packet kind: verify, reproduce, and route this item
    source/creatorFiled by the new-issue-creator job
    priority/normalOrdinary priority (the default)
    on Nov 6, 2026
  2. developing-today commented on Nov 6, 2026

    @developing-today
    Contributor
  3. added
    area/infraInfrastructure, deployment, and operations
    app/dnsinfra/dns terraform state workflow
    on Nov 6, 2026
  4. developing-today commented on Nov 6, 2026

    @developing-today
    Contributor

    🤖 Agent note · state: filed

  5. dezren39 commented on Nov 6, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · handed off

  6. added
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    status/parkedDeliberately paused with a recorded resume condition
    work/assignedQueue worker lifecycle state
    and removed on Nov 6, 2026
  7. 17 remaining items

  8. removed
    status/parkedDeliberately paused with a recorded resume condition
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    on Nov 9, 2026
  9. dezren39 commented on Nov 9, 2026

    @dezren39
    MemberAuthor

    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

  10. dezren39 commented on Nov 9, 2026

    @dezren39
    MemberAuthor

    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

  11. added
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    status/parkedDeliberately paused with a recorded resume condition
    and removed
    queue/claimedQueue worker lifecycle state
    on Nov 9, 2026
  12. added a commit that references this issue on Nov 9, 2026
  13. removed
    status/parkedDeliberately paused with a recorded resume condition
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    work/assignedQueue worker lifecycle state
    on Nov 9, 2026
  14. dezren39 commented on Nov 9, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done — PR #8021 merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    app/dnsinfra/dns terraform state workflowarea/infraInfrastructure, deployment, and operationskind/bugSomething isn't working (prefixed form of the classic bug label)priority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobstatus:donetask/triageQueue packet kind: verify, reproduce, and route this item

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions