Keep passwords, API keys and certificates out of code, and hand them out safely.
A secret is any value that gives access if someone else learns it: a database password, an API key, a private key, a token, a certificate. Secrets management is how you store, hand out, rotate and watch those values safely.
Your app needs secrets to do its job: to log in to the database, call a payment service, sign tokens or open a TLS connection. The easy way is to type the value into the code or a config file. That is also the dangerous way, because code is copied everywhere: to every developer laptop, to the build server, to the container image and to the git history, where it stays even after you delete the line.
A secret manager (for example HashiCorp Vault, or the managed services of the big clouds) solves this. The secret lives in one protected store. When a service starts, it proves who it is (using an identity such as a cloud role or a Kubernetes service account) and asks for the secrets it is allowed to have. The vault checks the rules, hands the secret over and writes a line in the audit log.
Better still are dynamic secrets. Instead of one shared password that lives for a year, the vault creates a new database user for each app instance, with a lease that expires in hours. If that credential leaks, it is unique, narrow and short-lived, and the vault can revoke it at once.
| Place | How it is used | Main risk |
|---|---|---|
| In the source code | Typed into a file in the repo | Copied everywhere and kept forever in git history. Never do this. |
| In a config or .env file in the repo | Loaded at start | Same as code if it is committed. Local files must stay out of git. |
| In the container image | Baked in at build time | Anyone who can pull the image can read it, and old layers keep it. |
| Environment variables set by the platform | Injected at deploy | Better, but often long-lived, shared, and visible in dumps and crash reports. |
| Secret manager, fetched by the app | Fetched with the service identity | Central, controlled and logged. The app still holds it in memory. |
| Dynamic, short-lived credentials | Created per instance with a lease | Best. A leak expires soon and is narrow. Needs a vault that can create them. |
Copied to every clone, build and laptop.
A teaching model with illustrative numbers (a vault secret rotated every 90 days, a dynamic credential living about 1 day). Real risk depends on your setup. The pattern holds: keep secrets out of code and images, give each service only what it needs, make them short-lived, and watch who reads them.
| Term | Meaning in easy words |
|---|---|
| Secret | A value that gives access if someone learns it: passwords, API keys, tokens, private keys |
| Secret manager / vault | A service that stores secrets, controls access to them and logs every read |
| Least privilege | Give each service and secret only the access it needs, and no more |
| Dynamic secret | A credential the vault creates on demand for one client, with a short life |
| Lease / TTL | The time a secret or token stays valid before it expires |
| Rotation | Replacing a secret with a new one and retiring the old one |
| Revocation | Cancelling a secret right now, before it would expire |
| Service identity | How a workload proves who it is, such as a cloud role or service account |
| Secret scanning | A tool that searches code and history for accidentally committed secrets |
| Audit log | A record of who read or changed which secret, and when |
| Secret sprawl | Secrets scattered across code, tickets, chats and machines with no owner |
| Choice | We gain | We lose |
|---|---|---|
| Environment variables | Simple, works everywhere | Long-lived, shared, little control or audit |
| Secret manager | Central control, audit and rotation | A new dependency and some set-up |
| Dynamic credentials | Small blast radius, auto-expiry | More moving parts, and the target system must support it |
| Short lease | A leak dies soon | More renewals, and failures if the vault is unreachable |
| Long lease | Fewer calls to the vault | A leak stays useful for longer |
| Cache secrets in memory | Fast, survives short vault outages | Stale values after a rotation, and exposure in memory |