Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 7Name in the course: Objective 4h: sensitive data and secrets management with Vault

Handle sensitive data and secrets

Infrastructure code touches passwords, keys and tokens. Terraform can hide them in its output, but they may still be in the state. This chapter shows the safe patterns: sensitive values, secrets from Vault or Key Vault, protected state, and scanning the code.

  • 5 Lessons
  • 3 Commands

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • Sensitive data and secrets with Vault

7.1

Mark values as sensitive

sensitive = true hides a value in plan and apply output. It does not encrypt it in the state.

secrets.tf
variable "db_admin_password" {
  type      = string
  sensitive = true
}
 
output "connection_string" {
  value     = "postgres://admin:${var.db_admin_password}@${azurerm_postgresql_flexible_server.db.fqdn}/shop"
  sensitive = true
}
terraform plan
  + administrator_password = (sensitive value)
terraform output -raw connection_string

7.2

Keep secrets out of code

A secret must never be in a .tf or committed .tfvars file.

Good placeHow
Environment variableexport TF_VAR_db_admin_password=… (CI secret store)
HashiCorp Vaultvault_kv_secret_v2 data source, or dynamic secrets
Azure Key Vaultazurerm_key_vault_secret data source
Generated by Terraformrandom_password → write it into Key Vault
HCP TerraformSensitive workspace variables
keyvault.tf
resource "random_password" "db" {
  length  = 24
  special = true
}
 
resource "azurerm_key_vault_secret" "db" {
  name         = "db-admin-password"
  value        = random_password.db.result
  key_vault_id = azurerm_key_vault.main.id
}

7.3

Read secrets from Vault

Vault stores secrets centrally, with access policies and audit logs. Terraform reads them with the Vault provider; dynamic secrets are created on demand and expire.

vault.tf
provider "vault" {
  address = "https://vault.example.com:8200"
}
 
data "vault_kv_secret_v2" "db" {
  mount = "secret"
  name  = "shop/db"
}
 
resource "azurerm_postgresql_flexible_server" "db" {
  # ...
  administrator_password = data.vault_kv_secret_v2.db.data["password"]
}

At work

Newer Terraform versions add ephemeral values and resources: secrets that are used during the run but never written to plan or state. Use them where your providers support them.

7.4

Protect the state

Because state can hold secrets, treat the state file like a password store.

  • Use a remote backend with encryption at rest and access control (Azure Storage with RBAC, HCP Terraform).
  • Turn on versioning / soft delete for the state container.
  • Give write access to the state only to the pipeline identity.
  • Never commit *.tfstate to Git: add it to .gitignore.
.gitignore
.terraform/
*.tfstate
*.tfstate.*
*.tfvars
!example.tfvars
crash.log

7.5

Scan the code for security problems

Static scanners find insecure settings (public storage, open NSGs, no encryption) before apply.

trivy config .
checkov -d .
tflint --init && tflint
ToolChecks
Trivy (config)Misconfigurations in Terraform, Kubernetes, Dockerfiles
CheckovHundreds of cloud security policies
TFLintProvider-specific errors and best practices

Remember

Put the scan in the pipeline before plan. A failed scan stops the pull request.

Commands in this lesson

All commands in this chapter