Terraform and OpenTofu state secret review
Brief
Terraform and OpenTofu keep a state file, their record of everything they built, and it stores nearly every resource argument in plain text, marked sensitive or not, so whoever reads the state reads the passwords and keys the code set. The review lists those values by name and length, checks saved plan files, and checks whether the state is encrypted.
On this page
- Situation
- A copy of the infrastructure code and read access to its state, granted for the review: the state bucket or storage container, or the working directory on a build runner. Every step reads; nothing is applied or pushed, and no value is printed beyond its length or its first four characters. The page runs
tofu;terraformtakes the same commands, except for encryption, which only OpenTofu has. - Goal
- A list of state and plan files with who can read each, and for each the secrets it holds, by name and length.
- Outputs
- Captured on 5 Nov 2026 in the Atarus Bench review lab, edition
review-lab-0.1: a Debian 13 container with OpenTofu 1.13.2, the bench's pinned version, and a fabricated configuration that uses only the built-interraform_dataresource, so no provider, network or cloud account was involved. The lab's backend is local; on an engagement it is usually a bucket.
Check first
- Where each configuration keeps its state: the
backendblock, or the workspace settings when Terraform Cloud holds it. - Who can read that backend: the bucket or container policy, the roles and service accounts that hold read on it, the file permissions on build runners, and every pipeline job that runs
plan. - Whether plan files (
*.tfplan) or old state copies are kept as build artifacts or in object versions. - The tool and version, since client-side state encryption exists only in OpenTofu 1.7 and later.
- The client's agreement that values are counted and masked, never printed beyond their first four characters or copied out.
Steps
Where is the state kept, and who can read it?
grep -n -A2 'backend' main.tf
stat -c '%A %U %n' terraform.tfstate*
Expected output
6: backend "local" {
7- path = "terraform.tfstate"
8- }
-rw-r--r-- root terraform.tfstate # <- readable by every account on this machine
The backend block names where the state lives. Here it is a local file, readable by every account on the machine, which is how state sits on many build runners and laptops. On an engagement the block more often names an S3 bucket, an Azure storage container or a Google Cloud Storage bucket. The question then becomes who can read that object: the bucket policy, every role with read on it, and the pipeline identities (CI pipeline trust, resource policies). Old versions of the object count too, since a versioned bucket keeps every past state.
Negative result. The file is -rw------- and owned by the runner's own account, or the bucket is readable only by the deploy role and named administrators.
Fix. Limit read on the backend to the identity that applies the code and a named break-glass role, and keep the same limit on old versions. Delete state files left on runners and laptops.
Does sensitive = true keep the value out of the state?
tofu output -no-color
Expected output
db_endpoint = "db01.corp.example.com:5432"
db_password = <sensitive> # <- hidden from the command's output only
The output command hides the password, which reads like protection. The state file says otherwise:
jq -r '.outputs | to_entries[] | "output \(.key) sensitive=\(.value.sensitive) length=\(.value.value | tostring | length)"' terraform.tfstate
jq -r '.resources[] | "\(.type).\(.name) inputs: \(.instances[0].attributes.input.value | keys | join(" "))"' terraform.tfstate
jq -r '.resources[] | select(.name == "payments_db") | .instances[0].attributes.input.value.password[0:4] + "..."' terraform.tfstate
Expected output
output db_endpoint sensitive=null length=26 # <- null: not marked sensitive
output db_password sensitive=true length=25 # <- marked sensitive, stored whole
terraform_data.payments_app inputs: api_key image
terraform_data.payments_db inputs: engine host password username
lab-... # <- the first four characters: plain text, not a hash
The first command lists each output with its flag and the length of its value; the second lists the inputs each resource stored; the third prints the first four characters of one password, enough to show the state holds the value itself and nothing more. The second command reads input because the lab's terraform_data resources store their arguments there. A real resource such as aws_db_instance stores them directly under attributes, so list .attributes | keys and look for names like password, secret or private_key. sensitive only stops the tool printing a value; HashiCorp's documentation says state holds sensitive values in plain text, and every resource argument a provider stores, such as a database's master password, lands there too. The report lists each secret by resource, name and length, and the client rotates them.
Negative result. No secret appears among the outputs, resource attributes or data sources: the service fetches its secret itself at start, the provider keeps it (as manage_master_user_password does), the code uses ephemeral resources and write-only arguments, or the state is encrypted (below). A secret read with an ordinary data source, such as aws_secretsmanager_secret_version, lands in the state like any other value.
Fix. Let a secrets manager generate and hold the secret and pass the code only its name. For an AWS RDS database, manage_master_user_password = true has AWS create and keep the password in Secrets Manager, so it never reaches the state. Elsewhere, use a write-only argument such as password_wo (Terraform 1.11 and later) or an ephemeral value where the provider offers one.
Do saved plan files hold the same values?
tofu plan -input=false -no-color -var-file=lab.tfvars -out=review.tfplan >/dev/null
tofu show -json review.tfplan | jq -r '.variables | to_entries[] | "variable \(.key) length=\(.value.value | length)"'
rm review.tfplan
Expected output
variable db_password length=25 # <- a saved plan carries every variable's value
variable payments_api_key length=25
A saved plan carries the value of every variable, sensitive or not, so a pipeline that keeps plan files as artifacts for a later apply step keeps the secrets with them. In the lab we make one plan, read the variable names and lengths, and delete it straight away. On an engagement we do not run plan against the client's backend, since it takes the state lock and calls the cloud with real credentials; we run the same tofu show -json and jq on plan files the pipeline already keeps, and the question is where those go: build artifacts, a cache, a review comment.
Negative result. No plan file is kept beyond the job that made it, or plans are encrypted.
Fix. Apply in the same job that plans, or keep plan files only in storage as tight as the state and expire them within the day; with OpenTofu, encrypt plans as well as state.
Is the state encrypted by the tool?
cd ../payments-encrypted
grep -nE 'encryption \{|key_provider "|passphrase|method "' main.tf
jq -r 'keys[]' terraform.tfstate
grep -c 'lab-only' terraform.tfstate || true
Expected output
2:# encryption on; the passphrase comes from a variable, not from this file.
12: encryption {
13: key_provider "pbkdf2" "state" {
14: passphrase = var.state_passphrase
16: method "aes_gcm" "state" {
28:variable "state_passphrase" {
encrypted_data # <- the resources and outputs are now one encrypted blob
encryption_version
lineage
meta
serial
0 # <- every lab value contains lab-only; none appears here
The second lab configuration is the same code with OpenTofu's client-side encryption on: a key derived from a passphrase that comes from a variable, AES-GCM, for both state and plans. Its state file keeps only bookkeeping in the clear, and the lab values appear nowhere in it. Encryption is only as strong as where the passphrase lives: in the lab it sits in lab.tfvars beside the state, readable by anyone who can read the state, so on an engagement we record where state_passphrase or the key provider's credentials come from. A plain state file instead shows top-level keys such as version, terraform_version, outputs and resources. Terraform has no client-side state encryption: there, encryption is the bucket's job. Default server-side encryption protects against stolen disks, not against anyone who may read the bucket; a customer-managed key whose policy leaves that reader out does (below).
Negative result. Plain top-level keys, outputs and resources, mean the state is not encrypted by the tool.
Fix. On OpenTofu, turn on state and plan encryption with a key provider backed by a key service (AWS KMS, Google Cloud KMS or OpenBao), not a passphrase in a file. On Terraform, use server-side encryption with a customer-managed key whose key policy admits only the deploy role, so reading the bucket is not enough to read the state.
The defender sees
On a bucket backend, each read of the state is an object read, and each cloud logs it only when asked. AWS CloudTrail needs S3 data events on for that bucket, Azure needs storage resource logs, and Google Cloud needs Data Access audit logs; all three are off by default. A read by any identity other than the deploy role is worth an alert. On a runner or laptop, reading a local state file leaves nothing. If the state bucket's reads are not logged, that goes in the report beside the finding.
Done when
Each state backend is named with its readers, every output and resource input holding a secret is listed by name and length, saved plan files are found, and state encryption is recorded as on or off.
Not covered
Reviewing a remote backend's bucket policy, versioning and logging in depth (see resource policies and public buckets), Terraform Cloud and HCP workspaces, editing or pushing state, and the provider credentials the runner itself holds.