Terraform state is encrypted at rest
iac/terraform-state-encrypted@v1
Every live Terraform root declares its backend encrypted, so the state it writes is not readable from the bucket it sits in.
| Id | iac/terraform-state-encrypted |
| Version | v1 |
| Category | iac |
| Default severity | error |
| Interpreter | python3 |
| Timeout | 30 seconds |
| Violations tolerated | 0 |
| Collects | terraform |
Collectors
This guardrail gathers nothing itself. It depends on the collectors below, which the CLI runs once per build before any check, and reads what they found out of GUARDRAIL_FACTS. A collector that collects nothing skips this guardrail rather than failing it.
| Collector | Gathers | Inputs it is given |
|---|---|---|
terraform | The Terraform configuration in the checkout: every root directory holding .tf files, the backend it stores state in, the providers and modules it takes on and how tightly they are pinned, the resources it declares, the variables it takes, the lock file that resolves it, and any state file left in the tree. Read out of the files themselves, so it works on a runner with no Terraform installed, and enriched with the versions an initialised root has already resolved when the terraform CLI is there. | none |
The inputs above are this guardrail's own, passed straight through. Configuring one in buildnote.json changes what is collected, and two guardrails configured the same way share the one collection.
Configuration
{
"guardrails": {
"failOn": "error",
"comment": true,
"checks": [
{
"use": "iac/terraform-state-encrypted@v1",
"severity": "error",
"with": {
"modulePaths": "modules/*,*/modules/*"
},
"exemptions": []
}
]
}
}Inputs
| Input | Description | Default | Environment variable |
|---|---|---|---|
modulePaths | Comma separated globs naming directories that hold reusable modules rather than live roots. A module declares no backend and pins nothing, so it is not judged here. A directory another root takes on with a local source is recognised as a module whether or not it matches. | modules/*,*/modules/* | GUARDRAIL_INPUT_MODULEPATHS |
How to fix
Declare encryption on the backend:
terraform {
backend "s3" {
bucket = "acme-terraform-state"
key = "live/main/terraform.tfstate"
region = "eu-west-1"
encrypt = true
kms_key_id = "arn:aws:kms:eu-west-1:111122223333:key/acme-terraform"
}
}State is not a summary of the infrastructure, it is a copy of it: every attribute of every resource, including the database password the provider generated and the private key it holds. An unencrypted state file is that content sitting in object storage in the clear.
More in iac
iac/dockerfile-base-pinned. Every image a Dockerfile builds from is pinned to a digest, so a rebuild of the same commit starts from the same bytes.iac/dockerfile-healthcheck. Every Dockerfile declares aHEALTHCHECK, so the orchestrator can tell a container that is running from one that is working.iac/dockerfile-lint-clean. Every finding the Dockerfile linter reported sits below the severity the team gates on.iac/dockerfile-no-build-secrets. NoARGorENVa Dockerfile declares is named like a credential.iac/dockerfile-nonroot-user. Every Dockerfile ends on aUSERthat is not root, so the container runs its process unprivileged.iac/terraform-modules-pinned. Every module a Terraform root takes from a registry or a git URL names the version it takes, so the same commit resolves to the same module twice.iac/terraform-no-plaintext-secrets. Every Terraform variable named like a credential declaressensitive = true.iac/terraform-providers-pinned. Every provider a live Terraform root requires is pinned to one exact version, and the lock file that resolves them is committed.