Skip to content

Guardrails / terraform

Terraform variables holding secrets are marked sensitive ​

terraform/no-plaintext-secrets@v1

Every Terraform variable named like a credential declares sensitive = true.

Idterraform/no-plaintext-secrets
Versionv1
Categoryterraform
Default severityerror
Interpreterpython3
Timeout30 seconds
Violations tolerated0
Collectsterraform

Collectors ​

This guardrail doesn't gather anything itself. It relies on the collectors below, which the CLI runs once per build before any check, and reads what they found from GUARDRAIL_FACTS. If a collector collects nothing, this guardrail is skipped, not failed.

CollectorGathersInputs it is given
terraformThe 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. Everything is read from the files themselves, so it works on a runner with no Terraform installed. When the terraform CLI is there, the versions an initialised root has already resolved are added.none

The inputs above are this guardrail's own inputs, passed straight through to the collector. Setting one in buildnote.json changes what is collected, and two guardrails configured the same way share a single collection.

Configuration ​

json
{
  "guardrails": {
      "failOn": "error",
      "checks": [
          {
              "use": "terraform/no-plaintext-secrets@v1",
              "severity": "error",
              "with": {
                  "patterns": "*SECRET*,*TOKEN*,*PASSWORD*,*KEY*,*CREDENTIAL*"
              },
              "exemptions": []
          }
      ]
  }
}

Inputs ​

InputDescriptionDefaultEnvironment variable
patternsComma separated glob patterns, matched case-insensitively against the name of every variable block. Only names are checked: the collector never collects defaults, because a default can be the credential itself.*SECRET*,*TOKEN*,*PASSWORD*,*KEY*,*CREDENTIAL*GUARDRAIL_INPUT_PATTERNS

How to fix ​

Mark the variable as sensitive, and don't give it a default:

hcl
variable "db_password" {
  type      = string
  sensitive = true
}

Pass the value in from the secret store your pipeline already uses, as an environment variable, instead of from a .tfvars file anyone can commit:

bash
TF_VAR_db_password="$(aws ssm get-parameter --name /company/db/password --with-decryption --query Parameter.Value --output text)" terraform apply

sensitive = true redacts the value in plans and CLI output. It doesn't encrypt it in state, so the backend still needs to encrypt. Plans are the exposure people miss: a pull request comment that quotes a plan publishes whatever the plan printed.

More in terraform ​

  • terraform/modules-pinned. Every module a Terraform root pulls from a registry or a git URL names a version, so the same commit always resolves to the same module code.
  • 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.
  • terraform/remote-state. Every live Terraform root stores its state in a remote backend, and no state file is left in the checkout.
  • terraform/state-encrypted. Every live Terraform root declares an encrypted backend, so its state isn't stored in the clear in its bucket.
  • terraform/state-locking. Every live Terraform root declares state locking, so two applies cannot write the same state at once.

All 6 terraform guardrails

Buildnote Limited
Registered in England and Wales, Reg: 16140412