A secrets manager from first principles: key hierarchy, secret zero, access, delivery

Four hard problems: the key hierarchy, how a machine first proves who it is, access and audit, and delivery that survives an outage. I'd run an existing one before building one, and build the last mile: a client library with the right defaults.

Read the briefing
About 3 min to read 6 drills

What to remember

  1. Root key out of the database. With envelope encryption, a database dump alone is useless, and rotating the root means re-wrapping, not re-encrypting everything.
  2. Secret zero is the real problem. Everything else protects a secret with another secret. What matters is how a machine first proves who it is.
  3. The hot path never calls the network. Load at start, refresh in the background. A secrets outage must not become a trading outage.
  4. Two valid at once. Rotation without downtime is issue the new one, accept both, then revoke the old. Without overlap, every rotation is an incident.
  5. Signing keys don't come out as bytes. Anything that can be used without being read belongs behind an operation API, so reading the store doesn't hand over the key.

The big picture

"Build a secrets manager from first principles" tests whether you understand why these systems look the way they do. It's also what you'd own on a platform team, because every service reads secrets and most do it badly. Start with the threat and the users, then the four hard problems, and be honest that you'd buy before you build.

The mental model

A locked box inside a locked box inside a vault whose key lives somewhere else. The hard part isn't the boxes. It's deciding who gets to open them when nobody is watching, and what happens when the vault is unreachable.

Diagrams

The key hierarchy
root key (in KMS or an HSM, never in the DB)
  wraps
project key (encrypted, in the DB)
  wraps
data key, one per secret version (encrypted, in the DB)
  encrypts, with AES-256-GCM
the secret (bound to project, path and version)

In depth

The key hierarchy

A root key in KMS or an HSM wraps a key per project, which wraps a data key per secret version. Each secret is encrypted with an AEAD such as AES-256-GCM, with the project, path and version as associated data, so a ciphertext can't be moved to another row. Vault's seal works on the same idea, unsealed by Shamir shares or automatically through a KMS. For reference, Infisical's published components are a REST API, PostgreSQL for storage, Redis for queues and caching, and clients including an agent and a Kubernetes operator.

Secret zero

How does a machine prove who it is the first time? In a cloud, the instance's signed identity document. In Kubernetes, a projected service-account token with an audience and a short life. On bare metal there's no metadata service, so you need TPM attestation or a one-time enrolment token handed over at provisioning, exchanged straight away for short-lived credentials. Never a long-lived key in an environment variable: it ends up in crash dumps, process listings and CI logs.

Access and audit

Paths and environments, with roles plus conditions. Deny by default. Changes to production secrets get approval. Log every read: who, what and when, never the value, and ship the log off the box. An unusual read pattern is one of the best early signals you'll get.

Delivery and availability

An agent on the host fetches and renders secrets to tmpfs or a socket. Services load them at start and refresh in the background. Agents survive a control-plane outage on what they hold: how long they may is a trade-off to state out loud, because a longer life is a longer exposure. Watch the bootstrap loop: the secrets system can't depend on anything that depends on it, including DNS it doesn't control and services that fetch their config from it.

Dynamic and static

Dynamic secrets are created on demand with a short lease, so a leak has a small window and each user has their own credentials, which makes audit meaningful. Static ones, such as a venue's API key, rotate with two valid at once where the other side allows it. When it doesn't, say so: that's a real operational risk to plan around.

Myths and facts

Myth: Encrypting secrets in the database is the hard part.

Fact: AEAD is easy. The hard parts are where the root key lives, how machines authenticate, and what happens during an outage.

Myth: Environment variables are a fine way to deliver secrets.

Fact: They leak into child processes, crash dumps, /proc and CI logs. A file on tmpfs or a socket with narrow permissions is much better.

Myth: Put the signing keys in the secrets manager with everything else.

Fact: A store hands out bytes. A key that can be used through an operation API should never be readable, even by its owner.

Myth: We rotate every 90 days, so we're fine.

Fact: A 90-day rotation leaves up to 90 days of exposure, and if it's manual it's rarely tested. Short leases and rehearsed rotation matter more.

How pros do it

  • Keep the root key out of the database and out of the application's reach.
  • Give machines short-lived credentials, exchanged from an attested or one-time identity.
  • Load secrets at start and refresh in the background; never fetch on a hot path.
  • Log every read without the value, and alert on unusual patterns.
  • Write a Rust client crate with a Secret type, so doing it right is the easiest way.
  • Rehearse losing the control plane, and write down how long the fleet survives.

Questions people ask

Would you actually build this?

Say no first: I'd run an established one, because the failure modes are subtle and it isn't what the company sells. I'd build the last mile: a client library and an agent that fit our hot path. Then show you could build the rest, because that's what they're testing.

What happens if the secrets service is down?

Running services keep working from what they hold. New processes can't start past their cache's life, so deploys pause. Say the number you chose, and that you've rehearsed it.

How do you handle a secret that was logged?

Treat it as leaked: rotate first, then find how it got there, then remove the class with a type that can't be logged. Purging the logs comes after rotating, and doesn't replace it.

Further reading

Drills · 6

Answer in your head, or out loud, before you reveal. Mark each one: "Again" brings it back, "Got it" counts on your desk.

1 Design round

Build me a secrets manager, Infisical or Vault, from first principles. What are the hard problems, and how do you solve them for a multi-region bare-metal fleet?

  • Bare metal: no cloud metadata service
  • Services on a microsecond hot path also need secrets
  • A small team runs it
In short

Keep the root key out of the database, prove machines by attestation or one-time enrolment, log every read, and deliver through an agent so the hot path never calls the network.

A strong answer covers
  • Says build or buy, and what they'd build
  • A key hierarchy with the root outside the database
  • Secret zero on bare metal: attestation or one-time enrolment
  • Short-lived credentials rather than long-lived keys
  • Delivery through an agent, with the hot path never calling the network
  • Behaviour during a control-plane outage, with a chosen number
  • Audit of every read, off the box
What a strong answer does
  • Finds the hard problems: Hierarchy, secret zero, access and audit, delivery and outages, each with a decision.
  • Fits this environment: TPM or one-time enrolment, agent delivery, and the hot path never touches the network.
  • Judgement: Says what they'd run, what they'd build and why, and states the trade-off on outage survival.
  • communication: Names the four problems first, then walks them.
Why

Encryption is the easy part, and candidates who spend ten minutes on AES have missed it. The signal is going to secret zero and outage behaviour without being led there, and being honest that you'd run an existing system.

The common mistake

Spending the whole answer on encryption, or designing a service every process calls at runtime.

2 Pick the strongest answer

A teammate wants to keep the hot trading key in the secrets manager, so services can fetch it at start. What's your objection?

  1. It's fine as long as the store encrypts it at rest. The service still receives plaintext, and so does anyone with its permissions.
  2. Secrets managers are too slow to fetch from at start. Start-up fetches are fine. The problem is the key existing as bytes in many places.
  3. It would need rotating every 90 days. Rotating a stolen key later doesn't undo what it signed. And Solana accounts name their signer.
  4. A store hands out bytes, so one read gives away the key for good, silently. A signing key should sit behind an operation that's limited and logged. It turns one over-broad read permission into permanent, invisible key theft.
In short

A store hands out bytes, so anyone who can read it has the key; a signing key should be used through an operation, never read.

Why

Separate secrets you must read, like an API token for a venue, from keys you only need to use. The second kind goes behind a signer or HSM, where the operation can be limited, checked and logged.

3 Scenario: what do you do next?

Your secrets control plane has been down for 40 minutes. Trading is fine, but a deploy is queued, and someone wants to disable the agents' cache limit to be safe.

Step 1. What do you do about the queued deploy?

  1. Pause it. New processes can't fetch secrets past their cache, so a deploy now could turn a quiet outage into a trading one. Stable and degraded beats unstable. The hot path is working.
  2. Deploy anyway. The agents should handle it. You'd find out whether they handle it during an outage, with real money.

Step 2. The cache limit is 24 hours and you're at 40 minutes. Do you extend it?

  1. Not yet. You have plenty of margin, so work the cause. Decide about extending only if it gets close, and write down who decided. The limit is a security decision. Changing it under stress without need removes a control for nothing.
  2. Yes, remove it now to be safe. That widens exposure right away for a risk that's hours off.

Step 3. It's back. What do you change?

  1. Alert on cache age across the fleet, and rehearse this: what expires first, and when deploys must pause. Now you'll know the margin before it's urgent.
  2. Note it in the postmortem and move on. A note doesn't remove the class of problem.
In short

Keep trading on what's cached, pause deploys rather than widen the rules, and fix the control plane; change the limit only by a decision you've written down.

Why

The scoring is on keeping a stable system stable, treating the cache limit as a deliberate security trade-off, and rehearsing it.

4 Answer out loud

A new bare-metal box comes up in a data centre. There's no cloud metadata service. How does it get its first secret?

In short

It proves itself with TPM attestation or a one-time enrolment token, then swaps that for short-lived credentials straight away.

What a strong answer does
  • Mechanism: TPM attestation or a bound one-time token, exchanged for short-lived credentials that renew by themselves.
  • Honesty about trust: Names what's still trusted: physical access, firmware, whoever provisions.
  • communication: A clear sequence, from power-on to first secret.
Why

This is the secret-zero question. Good answers separate proving what the machine is from what it's allowed to do, and say what you still trust: the provider's physical access, and whoever handed over the enrolment token.

The common mistake

Baking a long-lived credential into the image. It's in every copy of that image, forever.

5 Quick recall

A data key encrypts the secret, and a higher key encrypts the data key. What's this pattern called?

In short

Envelope encryption: data keys encrypt the data, and a root key wraps the data keys.

Accepted

envelope encryption · envelope · key wrapping

Why

Envelope encryption means the root key is rarely used and never leaves its home, and rotating it only means re-wrapping the keys below it.

6 Pick the strongest answer

How do you rotate a database password used by forty services without an outage?

  1. Change it, and let services fail and reconnect with the new one. That makes every rotation a small outage, on purpose.
  2. Move it to an environment variable, so changing it is easier. It's still a restart, and now it's in more places.
  3. Change it during a maintenance window and restart all forty services together. It works once. People then avoid rotating because it's an event.
  4. Make two valid at once: add the new credential, roll services onto it, check nothing uses the old one, then revoke it. Overlap turns rotation into a routine step, and you can stop and reverse at any point.
In short

Make two valid at once: add the new one, roll services over, confirm nothing uses the old one, then revoke it.

Why

Check which credential is in use before revoking. Without that step, you don't know whether the forty-first service exists until it fails. Dynamic per-service credentials make this go away altogether.

Next lesson PKI and workload identity: mTLS, short-lived certificates and rotation without heroics
Drill · A secrets manager from first principles
0:00

Loading…