5 min read

Security and Governance in Infrastructure - Secrets Management in Practice

1. A New Series: Security and Governance in Infrastructure

Provisioning infrastructure as code, versioning everything in Git and automating deploys solves a good part of a team’s operational problems — but it also creates a new attack surface. A wrong terraform apply can expose a bucket to the world; a secret committed by mistake stays in the repository history forever; an over-permissioned role is just one stolen credential away from a serious incident. In this new series, we’ll cover three security and governance pillars that every infrastructure automation needs to take into account: secrets management, policy as code and least privilege in IAM.

We start with the most basic problem and, even so, one of the most common in security audits: how to store and distribute secrets without leaving them in plain text anywhere.

2. The Problem of Storing Secrets in Plain Text

It’s extremely common to find, in real projects, secrets leaking through seemingly harmless channels:

  • A terraform.tfvars with db_password = "supersecret123" committed “just to test locally” — and never removed from the Git history.
  • Environment variables with cloud credentials defined directly in the CI configuration file (visible in plain text to anyone with read access to the pipeline).
  • Terraform’s own state file, which stores in plain text every attribute of the resources it manages — including database passwords created via random_password or full connection strings.

The problem isn’t just the initial leak. A secret committed to Git remains accessible in the history even after it’s removed from the current file — you have to rewrite history (git filter-repo, BFG) and still treat the secret as compromised, because any earlier clone already has it. The right practice is to never let the secret reach the repository: it should live in a dedicated system, with access control, auditing and rotation.

3. Introduction to Vault

HashiCorp Vault is the most widely used secrets management system in the IaC ecosystem. Its core concepts are:

  • Secrets engines: pluggable modules that define how secrets are generated and stored. The simplest one is KV (key-value), which stores static key-value pairs, encrypted at rest. Others, like the AWS engine, generate dynamic secrets: IAM credentials created on demand, with a limited lifetime.
  • Leases: every dynamic secret has a lease (a validity contract). When the lease expires, Vault revokes the credential automatically — without relying on anyone remembering to rotate it by hand.
  • Access policies: define who can read/write which Vault paths, integrated with authentication methods (tokens, AppRole, Kubernetes service accounts, OIDC, etc.).

The most common alternative outside the HashiCorp universe is AWS Secrets Manager (or its Azure/GCP equivalent), which solves the same problem more simply when the team is already 100% inside a single cloud, trading multi-cloud flexibility for native integration and less self-managed operation. The examples below use Vault, but the reasoning of “never commit a secret, always fetch it at runtime” is the same for both.

4. Running a Local Vault to Experiment

For study purposes, Vault has a development mode that runs everything in memory, with no real persistence:

vault server -dev -dev-root-token-id="root-token-local"

In another terminal, pointing the client at the local server:

export VAULT_ADDR="http://127.0.0.1:8200"
export VAULT_TOKEN="root-token-local"

vault status

5. Storing and Reading Secrets in KV

# Enable the KV version 2 secrets engine at the "secret/" path
vault secrets enable -path=secret kv-v2

# Store a secret
vault kv put secret/app/database \
  username="app_user" \
  password="a-really-strong-password"

# Read the secret back
vault kv get secret/app/database

KV v2 keeps versions of each secret, which lets you audit when a value changed and roll back if needed — something an environment variable thrown into a .env never offers.

6. Integrating Vault with Terraform

The official Vault provider for Terraform lets you read secrets at plan/apply time, without them appearing in any versioned .tf file:

provider "vault" {
  address = "http://127.0.0.1:8200"
}

data "vault_kv_secret_v2" "database" {
  mount = "secret"
  name  = "app/database"
}

resource "aws_db_instance" "app" {
  identifier     = "app-db"
  engine         = "postgres"
  instance_class = "db.t3.micro"

  username = data.vault_kv_secret_v2.database.data["username"]
  password = data.vault_kv_secret_v2.database.data["password"]
}

An important caveat: the value read still ends up in the state file, in plain text, because that’s how Terraform tracks resource attributes. This doesn’t remove the need for an encrypted state backend (S3 with SSE, or OpenTofu’s native encryption seen in the previous series) — Vault solves the problem of “where the secret comes from”, not “where the end result is stored afterwards”.

7. Dynamic Secrets: Going Beyond Static KV

Vault’s real security gain shows up with dynamic secrets engines. Instead of storing a fixed AWS credential, Vault can generate a new one on every request, with automatic expiration:

# Enable the AWS secrets engine
vault secrets enable aws

vault write aws/config/root \
  access_key="AKIA..." \
  secret_key="..." \
  region="us-east-1"

# Define a role that generates credentials with a specific IAM policy
vault write aws/roles/deploy-readonly \
  credential_type="iam_user" \
  policy_document=-<<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    { "Effect": "Allow", "Action": "s3:GetObject", "Resource": "*" }
  ]
}
EOF

# Generate a temporary credential, valid for a limited time
vault read aws/creds/deploy-readonly

Each vault read above generates a new key pair, with its own lease. If a CI pipeline uses this flow, a credential leaked in a specific log expires on its own in minutes or hours, instead of staying valid indefinitely like a traditional IAM user key.

8. Conclusion

Plain-text secrets in the repository — or in uncontrolled CI environment variables — are one of the most common and easiest-to-avoid incident vectors in infrastructure automation. A dedicated secrets manager (Vault, AWS Secrets Manager or equivalent) solves this with encrypted storage, granular access control and, in the best case, short-lived dynamic credentials. In the next article of this series, we’ll look at another kind of risk: insecure resources that get through apply because no automated rule blocked them beforehand — and how Policy as Code solves this with OPA.


References:

  1. HashiCorp Vault — Documentation
  2. Terraform Provider: Vault
  3. AWS Secrets Manager — User Guide