Secrets management for infrastructure

Advanced · 29 min

Keep secrets out of plain text on the infrastructure side: secret stores, envelope encryption with KMS, SOPS for GitOps, and short-lived dynamic credentials.

Before thisCloud identity and least privilegeTerraform and OpenTofu: plan, apply and stateGitOps with Argo CD and Flux

TechWorld with Nana · 29 min

Secrets management for infrastructure

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by TechWorld with NanaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Secret sprawl is the real problem: copies of the same password in CI settings, Terraform state, wiki pages and laptops, with no record of who read them and no easy way to rotate.
  • A secret store such as Vault, AWS Secrets Manager, Azure Key Vault or Google Secret Manager keeps secrets in one place, behind IAM, with an audit log and versioning.
  • Envelope encryption encrypts data with a data key and encrypts that key with a master key held in a KMS. The master key never leaves the KMS, and rotating it only means re-wrapping small data keys.
  • SOPS applies envelope encryption to values in YAML or JSON files, so encrypted secrets can live in Git for GitOps while the keys stay in a KMS or age.
  • The best secret is one that expires on its own. Dynamic secrets are created on request with a short lease, so a leaked one stops working soon.

The problem: secrets everywhere

An application needs a database password, an API key for the payment provider and a TLS private key. A year in, the database password exists in the CI system’s settings, a .env file on three laptops, Terraform state, a Slack message from the day it was set up, and a wiki page titled “prod access”. Nobody knows who has read it. Rotating it means finding every copy, so nobody rotates it.

That is secret sprawl. On the code side, the backend roadmap covers handling secrets in an application, and the git-github roadmap covers secret scanning and cleaning a leak out of history. System design explains secrets and config in concept. This lesson is the infrastructure side: where secrets are stored, how they are encrypted, and how they reach machines and pipelines.

A secret store

The first step is one place for secrets, with access control and an audit log:

Store Master keys
AWS Secrets Manager, SSM Parameter Store KMS
Azure Key Vault Key Vault keys and managed HSM
Google Cloud Secret Manager Cloud KMS
Any HashiCorp Vault, OpenBao Vault’s own seal, or a cloud KMS

Access goes through the IAM you already set up. The workload’s role may read prod/shop-web/* and nothing else. Every read is logged. Secrets are versioned, so you can rotate and roll back.

The workload fetches the secret at start-up, or a sidecar or CSI driver mounts it as a file. It is never baked into an image or committed to a repository.

Envelope encryption

Under the hood, cloud secret stores, SOPS and Vault’s transit engine use the same pattern: envelope encryption. Think of a hotel. Each guest’s valuables go in a room safe with its own code (the data key), and the codes are kept in the manager’s vault (the key-encryption key, or KEK). Changing the vault lock doesn’t mean reopening every room safe.

  1. A KMS holds the KEK. It never leaves the KMS, and you ask the KMS to encrypt or decrypt with it.
  2. For each secret or file, generate a fresh random data key and encrypt the data with it locally.
  3. Send the data key to the KMS to be encrypted (“wrapped”) with the KEK. Store the wrapped key next to the ciphertext. Discard the plaintext data key.
  4. To decrypt, send the wrapped key to the KMS. If IAM allows it, the KMS returns the data key, and you decrypt locally.

A worked example: envelope encryption with openssl

I have no KMS, so I played its part with a key in a local file and used OpenSSL 4.1.0 for the encryption. The script generates a KEK and a data key, encrypts a one-line prod.env with the data key, wraps the data key with the KEK, and deletes both plaintexts:

$ ./envelope.sh
dek.wrapped
envelope.sh
ivs
kek.hex
prod.env.enc
--- what sits in git:
00000000: 9a77 ad20 bf10 8679 3c1e 6d40 07e7 c8bb  .w. ...y<.m@....
00000010: 5478 66ab 3ebe d03c 1020 aea0 06c8 2808  Txf.>..<. ....(.

The deploy step unwraps the data key and decrypts to standard output, without writing plaintext to disk. Then I rotated the KEK, which re-wraps only the data key, and decrypted again. The hash of the encrypted file shows it was not touched:

$ ./decrypt.sh; shasum -a 256 prod.env.enc | cut -c1-16
DB_PASSWORD=s3cr3t-prod-pa55
3e136e0dd20311f7
$ ./rotate_kek.sh; ./decrypt.sh; shasum -a 256 prod.env.enc | cut -c1-16
DB_PASSWORD=s3cr3t-prod-pa55
3e136e0dd20311f7

With a different KEK, decryption fails:

$ ./decrypt.sh
bad decrypt
...:error:1C800064:Provider routines:ossl_cipher_unpadblock:bad decrypt:...

One caveat on my demo. I used AES-CBC because the openssl enc command doesn’t support GCM. CBC has no integrity check. The “bad decrypt” above comes from a padding check, which a wrong key can occasionally pass. Real tools use authenticated encryption such as AES-GCM, which detects both a wrong key and tampering. Don’t build your own. Use the KMS and the tools below.

Secrets in Git: SOPS

GitOps wants everything in Git, secrets included. SOPS (v3.13.3 when I checked) applies envelope encryption to the values of YAML, JSON, ENV and INI files, using a cloud KMS, Vault or age keys. Keys and structure stay readable, so a diff shows which setting changed. A .sops.yaml file says which keys encrypt which paths. I wrote this from the SOPS README. I did not run it, because neither SOPS nor age is installed here:

creation_rules:
  - path_regex: envs/prod/.*\.yaml$
    kms: arn:aws:kms:eu-west-1:111122223333:key/<key-id>
  - path_regex: envs/dev/.*\.yaml$
    age: age1...   # your age public key, from age-keygen

sops --encrypt --in-place envs/prod/db.yaml encrypts the values. Flux can decrypt SOPS files natively at deploy time, and Argo CD does it through plugins. Sealed Secrets and the External Secrets Operator are the other common answers in GitOps.

Dynamic secrets

A static password, however well stored, is valid until someone rotates it. Dynamic secrets go further. Vault’s database engine, for example, creates a new database user when a client asks, with a lease of perhaps an hour, and drops the user when the lease ends. A leaked credential is soon worthless, and each user maps to one client in the audit log. Cloud IAM roles and OIDC federation apply the same idea to cloud access. The rule to aim for: prefer credentials that expire by themselves.

About the video

“HashiCorp Vault Tutorial for Beginners - What, Why and How” is from TechWorld with Nana, published January 2025, 29 minutes 10 seconds. Its chapters cover what secrets are, the challenges of storing them, two problems with secret sprawl, what secret management tools do, dynamic secrets, encryption as a service and Vault’s architecture. It is Vault-centred and does not cover SOPS or the cloud providers’ stores. English is judged from the title, description and channel. Cybr’s three-minute Vault overview and Mircea Anton’s SOPS-and-age walkthrough are linked below.

Where this fits

Secrets protect what runs. The next lesson protects what gets built and shipped: securing the pipeline and the supply chain.

Check yourself

  1. 01Why is Terraform state a secrets risk?

    Terraform stores resource attributes in state, including generated passwords and keys, in plain text unless the backend encrypts it. The state backend needs encryption and tight access, and secrets should come from a store at apply time where possible.

  2. 02With envelope encryption, what has to change when you rotate the master key?

    Only the wrapped data keys are decrypted with the old master key and re-encrypted with the new one. The encrypted data itself does not need to be touched.

  3. 03What does SOPS leave unencrypted in a YAML file, and why is that useful?

    The keys and structure. Only the values are encrypted, so diffs in Git show which settings changed and reviews stay readable.

  4. 04What is a dynamic secret?

    A credential the secret store creates on demand for one client, such as a database user, with a lease after which it is revoked automatically.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark