Course map

Topic 165 / Core

Key and secrets management: rotation

Chapter 10.3 · Protecting Data

What and why

Topic 164 assumed keys and credentials already exist safely; this topic is where they actually live and how they're kept safe over time. A secret is any credential, key, or token a service holds — a database password, an API credential, a signing key. The two failure modes that matter most are secrets kept somewhere they shouldn't be (code, committed config) and secrets that never change, so a single leak stays useful indefinitely. Rotation — replacing a secret on a schedule, and immediately on suspected compromise — bounds how long any one leak matters. Builds on: → 164 (encryption at rest and in transit).

Diagram

Secret type Anti-pattern Correct home
Database credential Hardcoded in source or committed config A secrets vault, injected at runtime (→ 188)
TLS/signing private key Checked into a repository KMS or HSM, never exported as a file
API credential A static, long-lived shared value Short-lived, automatically rotated

How it works

  1. Never place a secret in source code or any file tracked by version control — including history: a secret removed from the latest commit still exists in every earlier one.
  2. Store secrets in a dedicated secrets store, with access controlled separately from the application code that uses them.
  3. Separate duties around keys: who can use a key to encrypt/decrypt is not the same permission as who can manage (create, rotate, destroy) it.
  4. Rotate on a fixed schedule, and immediately — out of schedule — the moment compromise is suspected.
  5. Prefer short-lived, automatically rotated credentials over static ones wherever the infrastructure supports it; a credential that expires in hours bounds a leak's usefulness far tighter than a scheduled rotation ever can.
  6. Inject secrets at runtime/deploy time (→ 188) rather than baking them into build artifacts, container images, or logs.

Practical example

(illustrative). A database credential rotated automatically every 90 days has, at most, a 90-day exposure window if leaked. A static credential that was never rotated remains valid — and useful to whoever leaked or stole it — for the entire life of the service, however many years that turns out to be. The difference is entirely the rotation cadence, not the strength of the credential itself.

When to use it

  • Use a managed secrets store and a rotation schedule for every credential, key, and token a service holds — there is no "too small a project" exception in production.
  • Avoid storing secrets in any environment file that version control tracks, or logging them anywhere, even at debug level.
  • Failure scenarios: a secret committed to git history, requiring rotation even after it's deleted from the current commit, because the history still holds it; one long-lived credential shared across many services, so a single leak compromises all of them at once; a rotated-out key never actually revoked, leaving two valid keys when only the new one should work.
  • Simpler alternative: a local, untracked environment file is fine for a single developer's local machine; production always needs a managed secrets store with rotation, with no equivalent shortcut.

Practice

(Process kind — fill the template on a small real system; 20 min, lab: Postgres). Using the lab's own Postgres credential as the example, write a rotation runbook: who approves a rotation, the exact steps to issue a new credential, how the old one is invalidated, and how every service using it picks up the new value without downtime. Done when the runbook accounts for the moment both credentials are briefly valid and states which step ends that window.

Shared lab setup

Key takeaway

A secret's safety depends more on how often it rotates than on how strong it is; rotation bounds the damage of a leak that will eventually happen. Done when you can: state, for any secret in a system, where it's stored, who can use versus manage it, and when it last rotated. Used next in → 188 (runtime injection of secrets into a running service).

References

None — this topic makes no external claims.

Source directory

Search the guide