Skip to content
BETAGuardrails are in beta. The library, the configuration format and the CLI command can still change.

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 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.

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. 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

json
{
  "guardrails": {
      "failOn": "error",
      "comment": true,
      "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 without regard to case. Each one is matched against the name of every variable block. Names only: the collector never carries a default, because a default can be the credential itself.*SECRET*,*TOKEN*,*PASSWORD*,*KEY*,*CREDENTIAL*GUARDRAIL_INPUT_PATTERNS

How to fix

Mark the variable sensitive, and give it no default:

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

Pass the value in from the secret store the pipeline already uses, as an environment variable, rather than from a .tfvars anyone can commit:

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

sensitive = true redacts the value in plans and in CLI output. It does not encrypt it in state, so the backend still has to be one that encrypts. The plan is the exposure that gets missed: a pull request comment quoting a plan publishes whatever the plan printed.

More in terraform

  • 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.
  • 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 its backend encrypted, so the state it writes is not readable from the bucket it sits in.
  • 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