Course contents

12. Security and Identity Lesson 8 of 8

Secrets Management

Keep secrets out of code and images: a vault with policy and audit, workload identity instead of secret zero, dynamic short-lived credentials, rotation.

15 minIntermediate9 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • A committed or baked-in secret lives in history, clones and image layers: rotation is the only fix.
  • A secrets manager stores secrets encrypted, releases them by identity-based policy, versions them and audits every read.
  • Workload identity (service-account tokens, cloud roles, SPIFFE) removes the need for a bootstrap secret.
  • Dynamic credentials are unique per workload and expire with their lease; static secrets rotate with an overlap window.
  • Fetch once and renew in the background: the lease TTL is how long you survive a vault outage.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Secrets management is how a system stores, distributes, uses, rotates and revokes credentials: database passwords, API keys, signing keys, TLS private keys, OAuth client secrets. In practice it combines a secrets manager (HashiCorp Vault or its fork OpenBao, AWS Secrets Manager, Azure Key Vault, Google Secret Manager, CyberArk Conjur), workload identity so software can authenticate to it without a password, and practices that keep secrets out of artefacts.

Why it exists

The default is to put credentials where the code is: a config file, an environment file, a Dockerfile ENV, a CI variable echoed into logs. Every copy of the artefact is then a copy of the secret: repository history, forks, developer laptops, image layers in every registry the image was pushed to, crash dumps and logs. Credential leaks are among the most common initial access paths in breach reports, and bots scan public repositories for keys continuously. Static shared credentials also make incidents worse: nobody knows which service used them, rotating them breaks everything at once, so they are rarely rotated.

How it works

Central storage. Secrets live in the manager, encrypted at rest (often envelope encryption with a key held in a KMS or HSM). Applications reference secrets by name; artefacts contain no values.

Identity-based access. Each workload authenticates with an identity the platform issues: a projected Kubernetes service-account token (short-lived JWT with an audience), a cloud workload identity (instance or pod role), a SPIFFE certificate, or an OIDC token from the CI system. The manager verifies it and applies a policy (orders may read database/creds/orders). This solves secret zero: there is no bootstrap password to ship.

Dynamic credentials. For systems the manager can administer (databases, cloud IAM, message brokers), it creates a credential on request with only the needed grants and a lease (for example one hour), and deletes it when the lease ends or is revoked. Each workload instance gets its own credential.

Leases, renewal and revocation. Clients renew leases while they need them, up to a maximum TTL. Revocation removes a credential immediately: on pod shutdown, or for every credential of a compromised service.

Rotation of static secrets. Third-party API keys and similar secrets rotate with an overlap: create the new version, roll it out, confirm use, revoke the old one. Applications reload secrets at runtime.

Delivery. Common patterns: the app calls the manager's API with an SDK; a sidecar or agent (Vault Agent, the Secrets Store CSI driver) writes secrets to an in-memory volume and renews them; or an operator (External Secrets Operator) syncs them into Kubernetes Secrets, which then need encryption at rest and tight RBAC.

When to use it

Always, for production credentials. Even a small system benefits from a cloud provider's secrets manager plus workload identity, which removes static cloud keys entirely. Dynamic database credentials are worth it once several services share a database or audits ask who ran a query.

When not to

Do not use a secrets manager as a general configuration store: non-secret settings belong in config, where they can be reviewed. Avoid calling the manager on every request (see Failure modes). For local development, use separate, low-privilege credentials rather than production secrets.

Trade-offs

Choice You gain You pay
Dynamic credentials Unique, expiring, revocable per workload The manager must administer the target; more moving parts
Short leases Smaller exposure window More renewals; a vault outage bites sooner
SDK calls from the app Fine control, no extra containers Vault-specific code in every service
Sidecar or CSI agent Apps read files; renewal handled for them An extra component per pod
Syncing into Kubernetes Secrets Works with any app unchanged Secrets now also live in etcd; RBAC and encryption at rest are essential

Alternatives

  • Workload identity with no secret at all: for cloud APIs, a pod role or federated identity replaces API keys entirely. Prefer this where available.
  • Encrypted secrets in git (SOPS, Sealed Secrets): GitOps-friendly; still static secrets that must be rotated by hand, and access to decrypt is the new secret.
  • Platform environment variables: simple, but visible to anything that can read the process environment, and easily logged.
  • Cloud KMS for keys: for signing and encryption keys, keep the key in a KMS or HSM and send data to it, so the key never leaves.

Failure modes

  • Secret committed or baked into an image: rotate first; history rewriting is secondary.
  • Vault on the request path: every request depends on the manager's latency and availability (step 7).
  • Lease longer than you can afford to lose, or shorter than an outage you must survive: pick TTLs deliberately and alert on renewal failures.
  • Broad policies: one identity that can read every secret makes the vault a single point of compromise.
  • Secrets in logs and error messages: request dumps, debug output and crash reports. Scrub and scan.
  • Root or unseal keys handled casually: protect them with auto-unseal via KMS and split access.
  • Apps that read secrets only at startup: rotation forces restarts, so teams avoid rotating.

In production

Run the secrets manager highly available across zones, with backups and tested restores. Use workload identity for every service and CI pipeline; ban long-lived cloud access keys. Turn on secret scanning in repositories and CI (pre-commit hooks, push protection). Start with dynamic database credentials for the most sensitive stores. Ship audit logs to your SIEM and alert on unusual reads. Practise rotating every static secret, so a real leak is a routine operation. Track lease renewal failures as a key health signal.

Common misconceptions

  • "Private repositories are a safe place for secrets." Access to the repository is broad, clones live on laptops and CI, and repositories leak.
  • "Deleting the commit removes the secret." History, forks, caches and image layers keep it. Rotate.
  • "Environment variables are secure." They are visible to the process, its children, debug endpoints and often logs; in images they are plain metadata.
  • "Kubernetes Secrets are encrypted." By default they are base64-encoded in etcd. Encryption at rest must be enabled, and RBAC still decides who can read them.
  • "Using a vault means fetching the secret every time." Fetch once, cache in memory, renew in the background.

Interview angle

Expect to be asked how a service gets its database password. A strong answer: workload identity to authenticate to a secrets manager, a dynamic per-instance credential with a lease, delivered by an agent or SDK, renewed in the background, revoked on shutdown; static secrets rotated with an overlap; nothing in code, images or logs; and a highly available vault, with the lease TTL as the outage budget. Mention secret scanning and what you do when a key leaks.

Model assumptions

Steps 1–6 are a scripted illustration, not a simulation: commits, image builds, identity tokens, credential issuance, leases and rotation are drawn as hand-placed packets with illustrative durations (8 ms per hop). The engine has no notion of secrets or credentials. Steps 7–9 use the simulation: Poisson arrivals at 200 requests per second, an orders pod with 32 workers and 5 ms mean service time (lognormal, cv 0.3), a database with 24 connections and 3 ms reads 1 ms away, and in step 7 a vault with 16 workers and 3 ms per read, 3 ms away. A vault that is down refuses connections and the pod fails the request. That gives about 39 ms per request with the vault on the path and about 30 ms without it. Lease expiry is not modelled: in step 8 the credential is assumed to stay valid for the whole run. Numbers are illustrative, not benchmarks of any product.

The lesson as textEvery step of the simulation, written out.
  1. 1. A password in the code travels everywhere the code goes

    The database password sits in config.py. The developer commits it; CI builds an image with it baked into a layer; the image is pushed to the registry and deployed to the pod. The secret is now in the repository history, in every clone, in every image layer, often in CI logs too.

    Then the repository is exposed: a public fork, a leaked laptop, a compromised CI token. Automated scanners find credentials in public repositories within minutes. The attacker connects straight to the database with the password. Deleting the line in a new commit does not help: history and old images keep it. The only fix is rotating the password. This is a scripted illustration.

    Scene: The developer commits a password, the build bakes it into an image and the pod uses it. Later the repository leaks: an attacker copies it and logs in to the database with the same password.

    Key idea: Secrets in code, images or config files leak with them and stay leaked: you must rotate, not delete.

  2. 2. One place for secrets: a secrets manager

    Secrets move into a secrets manager (HashiCorp Vault, OpenBao, AWS Secrets Manager, Azure Key Vault, Google Secret Manager). It stores them encrypted, hands each one out only to identities whose policy allows it, keeps versions, and writes an audit log of every read. Code, images and repositories contain only the secret’s *name*.

    The pod now asks the vault for the database password at startup. But to ask, the pod must prove who it is, and if that proof is another password in the image, nothing has improved. This is the secret zero problem.

    Scene: The pod requests the database secret from the secrets vault at startup and receives it. The open question is how the pod proves its identity.

    Key idea: Store secrets centrally with policy, versions and audit; ship references, never values.

  3. 3. Secret zero: let the platform vouch for the pod

    The platform already knows which workload is running. Kubernetes mounts a projected service-account token into the pod: a short-lived signed JWT naming its namespace and service account, with the vault as audience. The pod logs in to the vault with it; the vault verifies the token with Kubernetes (TokenReview, or offline via the cluster’s OIDC keys) and maps orders to a policy that may read only the orders database credentials.

    The same idea exists everywhere: cloud instance and pod identities (IAM roles), SPIFFE certificates, and OIDC tokens from CI systems. No long-lived secret is needed to get the first secret.

    Scene: Kubernetes gives the pod a short-lived service-account token. The pod logs in to the vault with it; the vault checks the token with Kubernetes and returns a vault token tied to the orders policy.

    Key idea: Authenticate workloads with platform-issued identity, so no bootstrap secret is shipped.

  4. 4. Dynamic credentials: a fresh database user per pod

    Instead of handing out one shared password, the vault creates a database user on demand: it connects to the database with its own admin credential, runs CREATE ROLE v-orders-7f3k … VALID UNTIL now + 1h with only the grants the orders service needs, and returns the username and password with a lease of one hour.

    Every pod has its own credential, so the database audit log says *which* pod did what, and revoking one pod does not touch the others. The pod then connects to the database with it.

    Scene: The pod asks the vault for database credentials. The vault creates a dedicated database user, which the database acknowledges, and returns it to the pod with a one-hour lease. The pod then queries the database with it.

    Key idea: Issue unique, short-lived credentials per workload instead of sharing a static password.

  5. 5. Renew while needed, revoke when not

    Before the lease runs out, the pod (or a sidecar agent next to it) renews it. When the pod stops, or when a leak is suspected, the vault revokes the lease: it drops the database user, and the credential is dead within seconds instead of living until someone remembers to rotate it.

    A leaked dynamic credential is worth little: it expires on its own within the lease, it names one pod, and it can be revoked alone. Leases also need a maximum TTL, so even a pod that keeps renewing must come back for a fresh credential eventually.

    Scene: The pod renews its lease with the vault and gets an acknowledgement. Later the vault revokes the lease and the database drops the pod’s user.

    Key idea: Leases make credentials expire by default; revocation kills one credential without touching others.

  6. 6. Rotating a secret you cannot make dynamic

    Some secrets are static: a payment provider’s API key, a webhook signing secret. They still rotate, on a schedule and immediately after any suspected leak, without downtime: the vault creates key v2 at the provider while v1 stays valid, publishes v2 as the new version, the pods pick it up and use it, and only then is v1 revoked.

    The overlap window is what makes rotation safe. Apps must reload secrets at runtime (watch the mounted file, or re-read on a schedule) instead of reading them once at boot.

    Scene: The vault creates a second API key at the payment provider, publishes it to the pod, the pod calls the provider with the new key, and only then does the vault revoke the old key.

    Key idea: Rotate with an overlap: create the new version, roll it out, then revoke the old one.

  7. 7. Reading the secret on every request

    Now the simulation runs: 200 requests per second. This version of the service reads the credential from the vault on every request, then queries the database. Each request pays an extra round trip: about 39 ms instead of 30 ms here, and the vault serves every request the system receives.

    At 1.5 s the vault goes down, and every request fails. The secrets manager has become a hard dependency of every request, with the availability of the weakest component in the chain.

    Scene: Users send 200 requests per second. The orders pod reads its credential from the vault, then queries the database, on every request, at about 39 ms. After 1.5 seconds the vault goes down and every request fails.

    Key idea: A vault on the request path adds latency and turns a vault outage into a full outage.

  8. 8. Hold the lease, renew in the background

    The pod fetches its credential once, keeps it in memory (or a sidecar agent writes it to a shared in-memory volume), and renews the lease in the background. The request path is pod → database only: back to about 30 ms.

    The vault fails again at 1.5 s, and nothing happens: the pod keeps using its credential, which is valid until its lease expires. The lease length is now also your outage budget: a vault outage longer than the remaining TTL still takes the service down, so run the vault highly available and alert on failed renewals long before expiry.

    Scene: The vault renews the pod’s lease in the background. Requests go from the pod straight to the database at about 30 ms. The vault goes down after 1.5 seconds and no request fails.

    Key idea: Fetch once, cache in memory, renew in the background: the vault stays off the request path.

  9. 9. Secrets that expire, identities that do not leak

    Keep secrets out of code, images, environment files and logs; store them in a secrets manager with policy, versions and audit. Let the platform vouch for workloads so there is no secret zero. Prefer dynamic, per-workload credentials with leases; rotate static secrets with an overlap window.

    And keep the vault off the request path: fetch once, renew in the background, and run the vault highly available, because the lease TTL is how long you can survive without it.

    Scene: Users send 200 requests per second to the orders pod, which uses its leased credential for the database. The vault is up and renews leases in the background.

    Key idea: No secrets in artefacts, identity instead of bootstrap passwords, short leases, vault off the hot path.