Skip to content

fix(terraform): drift reads state through tofu show -json, so encrypted state and module resources are no longer misreported #1437

Closed

Description

@dan-petty

Problem

Lines cited at 05e9388 (release/v0.2.29).

  • The state file is parsed directly. load_state (tf/analysis.py:280-313) reads the state file as JSON and projects it (_project_state_resource, :258-277). The state file is not a public interface of OpenTofu or Terraform; tofu show -json is.
  • Encrypted state reads as empty. OpenTofu's client-side state encryption writes a state without a resources key. load_state then returns an empty state, and detect_drift (:362-391) reports every declared resource as missing from state.
  • Module resources are reported as orphaned. parse_hcl_directory reads only the root module's files (_hcl_files, :172-178). State addresses resources inside modules as module.<name>.<type>.<name> (:270), so none of them match a declaration.
  • The provider is guessed from the resource type's prefix (:148), so aliased providers (provider = aws.west) are misreported.
  • "Drift" here is structural only. It compares declared addresses with recorded ones. Drift against live infrastructure needs a refresh.

Proposed remedy

  • When the directory is initialised:
    • state comes from tofu show -json (resource addresses, modules and providers);
    • drift comes from tofu plan -refresh-only -json.
    • Terraform is used only when OpenTofu is absent, as _resolve_tf_binary (commands/tf.py:51-61) already orders them.
  • When it is not initialised, the commands report the state as unavailable and name tofu init, instead of guessing.
  • Delete load_state, _project_state_resource, resolve_state_file and the prefix-based provider guess.
  • Keep the python-hcl2 parse of the configuration for the dependency graph and blast radius.

Key questions

Acceptance criteria

  • Recorded tofu show -json output for a configuration with a module reports the module's resources as in sync, not orphaned.
  • Recorded output for an encrypted-state backend reports the resources, not an empty state.
  • Recorded tofu plan -refresh-only -json output with a changed attribute reports drift on that resource.
  • An uninitialised directory reports the state as unavailable and names tofu init.
  • Tests use the process fake replaying the recorded output.
  • Tests run offline with fakes only at process and HTTP edges; each new test runs under 1 s.
  • uv run devops ci passes.

Related: #310, #775 (process runner), #1031.

Found by the 2026-10-08 client-library audit.

Opened as a borrow (#1153, budget/borrowed): the agent filing quota is spent for this cycle, and this is a defect or security finding of the owner-requested 2026-10-08 client-library audit.

Update 2026-10-08: owner decision

The key question is answered as proposed: reverse #310's no-binary design for state and drift only.

  • When the directory is initialised, state comes from tofu show -json and drift from tofu plan -refresh-only -json.
  • An uninitialised directory reports "state unavailable (run tofu init)" instead of a guess.
  • The in-process HCL parse stays for the dependency graph and blast radius.

Activity

  1. added
    type/bug
    scope/terraform
    source/agent
    on Oct 8, 2026
  2. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. Drift reports are wrong for encrypted-state and module configurations, but the tool does not crash or leak data; the owner has already approved the reversal, making this a planned correctness fix rather than an urgent outage.
    • Value: High. Users with encrypted state or module-based configurations receive completely incorrect drift reports (all resources missing or orphaned), undermining trust in a core IaC inspection feature.
    • Effort: Medium. Requires replacing in-process state parsing with subprocess calls to 'tofu show -json' and 'tofu plan -refresh-only -json', handling the uninitialised-directory case, deleting four functions, and writing process-fake tests, but the scope is
  3. added 2 commits that reference this issue on Oct 26, 2026
    6504f9d
    169859a
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

    scope/terraformsource/agenttype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions