CMD Guide
HomeSystem DesignScalable Systems (Advanced Topics)

What Is Secrets Management, And How Do Environment Variables, KMS, And Vault Compare

Secrets management is the practice of securely storing and handling sensitive information (like passwords, API keys, and tokens) used in software systems to prevent unauthorized access.

Understanding Secrets Management

In modern applications and IT environments, a “secret” refers to any sensitive credential or key that must be kept confidential, examples include database passwords, API tokens, encryption keys, certificates, and SSH keys.

Secrets management encompasses the tools and methods to safeguard these credentials throughout their lifecycle, storing them securely (usually encrypted), controlling who or what can access them, transmitting them safely to applications, and rotating or revoking them when needed.

This practice ensures that only authorized users or services can retrieve secrets, thereby reducing the risk of leaks or breaches.

Why Secrets Management Matters

Without proper secrets management, organizations often suffer from "secret sprawl"; credentials scattered across config files, code, environment variables, and even chat logs.

This sprawl leads to several issues:

By implementing robust secrets management, teams can centralize secrets storage, enforce access controls (like least privilege), and automate credential updates — directly countering the three sprawl failures above (leak surface, no audit trail, manual rotation).

Common Approaches to Secrets Management

Here we’ll compare three common approaches — using environment variables, a Key Management Service (KMS), and a secrets vault like HashiCorp Vault — in rising order of security and operational cost.

Environment Variables

Environment variables are one of the simplest and most widely used methods to supply secrets to applications.

Developers often load sensitive values (database passwords, API keys, etc.) into the application’s environment at runtime (or via a .env file in development), allowing the code to read those values from memory.

For example, you might set an environment variable DB_PASSWORD="supersecret" on a server or in a container, and the application reads DB_PASSWORD from its environment.

This approach is popular because it’s easy and language-agnostic, no additional tools are required, and nearly every platform supports environment configs.

In fact, many tutorials use environment variables for secrets simply because it’s convenient, not because it’s the most secure practice.

However, there are important drawbacks to relying on environment variables for secrets:

Key Management Services (KMS)

A Key Management Service (KMS) is a cloud (or on-premise) service focused on managing cryptographic keys and performing encryption/decryption operations.

Examples include AWS KMS, Google Cloud KMS, or Azure Key Vault (which, despite the name “Key Vault,” functions as a KMS and secrets store).

Using a KMS for secrets management typically means leveraging strong encryption for your secrets: you encrypt sensitive data using keys stored in the KMS, and only decrypt them at runtime with KMS authorization.

For instance, you might store an encrypted form of your API key in an environment variable or configuration file.

Your application at startup calls the KMS (with proper credentials) to decrypt it.

The idea is that the plaintext secret is never stored at rest, only the ciphertext is, and the encryption keys are safely managed by the KMS.

Cloud KMS systems often use Hardware Security Modules (HSMs) under the hood, adding an extra layer of physical security for key material.

Benefits of KMS approach:

Limitations/considerations:

Secrets Vaults (HashiCorp Vault)

HashiCorp Vault (often just called “Vault”) is a popular open-source secrets management tool designed to be a centralized vault for sensitive data.

Vault acts as a secure store where you can keep and tightly control access to secrets like tokens, passwords, certificates, API keys, and more.

Unlike environment variables or a simple KMS, Vault provides a full suite of features purpose-built for secrets management.

Key characteristics of Vault include:

Naturally, Vault’s richness comes with more complexity.

Deploying Vault means running a server (or a cluster of them for high availability).

You need to initialize and “unseal” the Vault (provide master key shares to unlock it) whenever it starts.

Applications must be configured to authenticate to Vault (via tokens, AppRole, cloud IAM, etc.) and to request the secrets they need. This setup is non-trivial, especially compared to using simple env vars or a cloud service.

One caveat specific to Vault is the “secret zero” problem: your app needs some initial secret or trust (a token, AppRole, or cloud IAM role) to authenticate to Vault before it can retrieve any others. Bootstrapping that first credential securely is the crux (Vault Agent and cloud IAM auth exist to solve it).

Environment Vault
Environment Vault

Environment Variables vs KMS vs Vault: Key Differences

Each approach to secrets management strikes a different balance between simplicity, security, and functionality.

Below is a comparison of environment variables, KMS, and Vault across various dimensions:

Worked trace: envelope encryption and blast radius

The prose above compares the three approaches; here is the concrete flow and what actually leaks. Take one secret — DB_PASSWORD = "supersecret" — and store it three ways.

Envelope encryption, step by step (the KMS flow)

KMS never hands you the master key. Instead it uses envelope encryption: a master key wraps a data key, and the data key wraps your secret.

  1. Set-up (once). A Customer Master Key alias/db-key lives inside the KMS/HSM and is non-exportable — its plaintext never leaves the module. You call GenerateDataKey and get back a 256-bit data key (DEK) twice: once in plaintext, once encrypted under the master key (the “EDEK”).
  2. Encrypt (once). You AES-encrypt "supersecret" with the plaintext DEK → ciphertext blob, then discard the plaintext DEK from memory. What you persist in config is only { ciphertext, EDEK } — two wrapped artifacts, no plaintext anywhere.
  3. Decrypt (every boot). The app presents its IAM role and calls KMS Decrypt(EDEK). KMS checks the IAM policy, unwraps the EDEK inside the HSM using the master key, and returns the plaintext DEK. The app AES-decrypts the ciphertext with that DEK → "supersecret", uses it, and discards the DEK.
  4. Audit. Every Decrypt call is logged (who, when, which key) — e.g. CloudTrail.

Chain of custody: master key (in HSM) → wraps → data key → wraps → secret. The leaked-config attacker holds the two right-hand artifacts but not the master key or the IAM permission to unwrap them.

Same secret, three lifecycles — blast radius on one leaked config file

ApproachWhat’s in the leaked fileBlast radiusWhat rotation actually does
Env var / .envDB_PASSWORD=supersecret — plaintextFull standing credential, valid until a human changes it; the attacker has the live password immediately and indefinitely. A proc dump or CI log leaks the same thing.Change the password at the DB and push the new value to every instance’s environment and restart them all; miss one and it either breaks or stays exposed.
KMS envelope{ ciphertext, EDEK } — no plaintextUseless alone. Without the app’s IAM permission to call KMS Decrypt, the attacker cannot unwrap the DEK, so the secret stays sealed; and every real decrypt is audited, so abuse is visible.Re-wrap: generate a new DEK, re-encrypt the secret, replace the ciphertext (or rotate the master key so old EDEKs stop unwrapping). The app just fetches and decrypts the new blob at next boot — no code change to the secret value.
Vault dynamicNothing at rest — the app holds at most a short-lived leaseBounded by the TTL. A leaked credential is a per-app DB user that Vault minted with, say, TTL = 1h; it self-expires in ≤1h, and you can revoke the lease instantly on incident.Largely automatic: dynamic secrets are minted per request and auto-revoked at TTL, so there is often nothing to rotate by hand; static KV secrets are versioned and rotated centrally, propagating on next fetch.

Ranking by blast radius on that single leaked file: env var (full, forever) ≫ KMS envelope (nothing without the IAM decrypt right) ≈ Vault dynamic (≤ the lease TTL). That is the whole argument for moving off plaintext env secrets: you are shrinking what a single leaked artifact is worth.

🤖 Don't fully get this? Learn it with Claude

Stuck on What Is Secrets Management, And How Do Environment Variables, KMS, And Vault Compare? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **What Is Secrets Management, And How Do Environment Variables, KMS, And Vault Compare** (System Design) and want to truly understand it. Explain What Is Secrets Management, And How Do Environment Variables, KMS, And Vault Compare from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **What Is Secrets Management, And How Do Environment Variables, KMS, And Vault Compare** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **What Is Secrets Management, And How Do Environment Variables, KMS, And Vault Compare** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **What Is Secrets Management, And How Do Environment Variables, KMS, And Vault Compare** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes