Managing secrets for software
Software has nobody to type a password, so it carries its proof with it, and anyone who copies that proof can act as the software. This lesson follows Cedar Inc.'s engineers through the service accounts on Quinn's list: keeping their secrets in a vault, making credentials that expire by themselves, finding the copies that escaped, changing a secret without an outage, and removing the stored secret altogether.
Secrets that software holds
A person proves who they are by typing a password or touching a key. Software has nobody to do that, so it carries its proof with it: an APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. key for the payroll provider, a client secretA shared secret that a confidential client uses to prove itself to the authorization server, much like a password for an application. It belongs on a backend, never in code sent to browsers or phones. for Cedar's identity providerA service that authenticates users and tells other applications about the sign-in, so those applications never handle the users' passwords. In OpenID Connect it is called an OpenID Provider. at login.cedar.example, a password for the finance database, a private keyThe half of a key pair that its owner keeps to themselves and uses to create signatures or decrypt data. Anyone who obtains it can act as its owner. for signing. These are secrets, and anyone who copies one can act as the software that owns it, from anywhere, for as long as the secret lasts.
Service accounts were the fourth kind of privilege on Quinn's list in Privileged access management, the one its table left out. Cedar's engineers took stock of the secrets behind them one afternoon, starting with the nightly report job that reads the finance database, and found the same secrets in more places than they expected. The database password was in the configuration file of each service, in an environment variable on each server, in the deployment pipeline's settings, in a chat message from two years earlier when someone needed it in a hurry, and in an old commit of the repository, where it had been removed from the file but not from the history. Each copy was as powerful as the original. Nobody could say which systems would break if the password changed, so it never had.
Secrets management is the work of keeping that from happening: storing secrets in one place, handing them only to the software that needs them, making them short-lived where possible, finding copies that escaped, and changing them without an outage. Better than any of these is to stop needing a stored secret at all.
Keeping secrets in a vault
A secrets vault, or secret manager, is a service that stores secrets encrypted and gives each one out only to identities its policy allows. Cedar's payroll export may read the payroll provider key; the report job may read the finance database password; neither may read the other's. Every read is recorded with the identity that asked, so the question "who used this secret last week?" finally has an answer. Secrets are versioned, so a change can be rolled back, and the vault rather than a file is the one place to change them. It is the same idea as the credential vault Cedar uses for people's root passwords, applied to software.
The vault has a puzzle at its centre. To read the database password, the report job must authenticate to the vault, and if that needs another stored secret, the problem has moved rather than gone away. The answer is to authenticate the job by something its platform can vouch for: the cloud machine it runs on, the account the platform started it under, or a signed tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. from the deployment system. When software needs its own identity and the lessons after it explain how platforms vouch for workloads. With that in place, the only secret the job still holds is the one it fetched a moment ago, in memory.
Dynamic and short-lived secrets
A vault can do more than store a password someone typed in. With dynamic secrets, it creates a credential when it is asked for one. When the report job starts, it asks the vault for database access and receives a username and password that did not exist a second earlier, together with a lease:
{
"lease_id": "database/creds/report-job/3f9c21",
"lease_duration": 3600,
"renewable": true,
"data": {
"username": "v-report-job-7Hq2Lm",
"password": "demo-generated-password-not-real"
}
}All names, hosts and values in these examples are fictional.
The vault created that database user with exactly the rights the report job needs, and it will drop the user when the lease ends in an hour, unless the job renews it while it is still running. Every running copy of the job gets its own user, so the database's own logs say which copy ran which query, and a credential copied out of a crash report stops working by itself within the hour. Changing a credential is no longer a project, because every credential is replaced all the time.
Short-lived access tokens work the same way at a smaller scale. A service that authenticates once and receives a token valid for fifteen minutes holds something far less valuable than a static API key valid until someone remembers to change it.
Scanning for leaked secrets
Secrets escape anyway, most often into source code. Secret scanning looks for them where they land. A scanner recognizes secrets in two ways: by known shapes, such as a provider's key prefix or a private key's BEGIN PRIVATE KEY line, and by long strings of random-looking characters assigned to names like password or secret. Some scanners go on to test whether a found key is still valid, which separates a live leak from an old example.
Where the scanner runs decides what it can prevent. A check on the developer's machine before each commit stops a secret before it is shared. Push protection on the code host refuses a push that contains one. A scan of the whole history, including old commits and forks, finds the copies that already escaped, like Cedar's database password, which deleting the line from the current file had not removed.
Before each commit
- Runs on
- The developer's machine
- The secret
- Never committed
- Who may have it
- Only the developer
Stopped before it is shared
Push protection
- Runs on
- The code host
- The secret
- Push refused
- Who may have it
- Only the developer
Never reaches the shared repository
History scan
- Runs on
- Old commits and forks
- The secret
- Already escaped
- Who may have it
- Anyone with a copy
Treat it as stolen: replace it first
A found secret must be treated as stolen, because there is no way to know who else saw it.
A found secret must be treated as stolen, because there is no way to know who else saw it. The first step is to revokeCancelling a credential, token, certificate, or authenticator before it would expire, so that it is no longer accepted. Whether every copy stops working at once depends on how services check it. or replace it, before investigating, and only then to remove it from the history and look for misuse. Rotating credentials and keys after exposure follows that response further.
Rotating without an outage
Rotation means replacing a secret with a new one. Done carelessly, it is an outage: the old password stops working before every service has the new one. The way to avoid that is an overlap, in which both secrets are valid for a while:
- Create the new secret alongside the old one, so both work.
- Put the new secret in the vault, and let each service pick it up.
- Watch until nothing uses the old secret any more.
- Revoke the old secret.
Rotating client credentials showed the same sequence for an OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. client's secret and keys. Rotation that is automated and frequent becomes boring, and boring is the goal: a secret that is changed every month will be changed smoothly on the day it leaks, while one that has never been changed tends to be the one nobody dares touch.
Removing the stored secret
The strongest move is to stop needing the secret. Two changes take Cedar most of the way.
The first replaces shared secrets with key pairs. When the report job authenticates to Cedar's identity provider with a signed assertion, as Secrets and signed assertions described, the identity provider holds only the job's public keyThe half of a key pair that can be shared freely. It checks signatures made with the matching private key and can encrypt data that only that private key can decrypt., so nothing it stores can be used to impersonate the job. The private key still has to be kept somewhere, ideally in hardware or a key service that signs without ever releasing the key.
The second removes the stored credential altogether. With workload identity federation, the platform a workload runs on issues it a short-lived signed token that says which workload it is, such as a deployment pipeline's token naming the repository, branch and job. The service it calls is configured to trust that platform's tokens under exact conditions, and exchanges each one for a short-lived credential of its own. Cedar's deployment pipeline used to hold a cloud access key in its settings; now each run proves which job it is with a token minted for that run, and there is no key to leak, scan for or rotate. Workload identity federation follows how the trust conditions are written and why they must be exact.
| Secret | Before | After |
|---|---|---|
| Finance database password | One password in five places, never changed | A dynamic user per running copy, leased for an hour |
| Report job's client secret | Shared secret in configuration | A key pair; the identity provider holds only the public key |
| Cloud deployment key | A static key in the pipeline's settings | Federation: each run exchanges a token minted for that run |
| Payroll provider API key | In configuration and an old commit | Rotated, kept in the vault, readable only by the payroll export |
The payroll provider accepts nothing but an API key, so one static secret remains. It lives in the vault, only one identity may read it, its reads are recorded, and it is rotated on a schedule, which is the most care a secret the software cannot avoid can be given.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Scan a workspace for a secret, then move the client to a key the server never holdsLeave a client secret in a configuration file, find it with a secret scan, see that the copy works from anywhere, then switch the client to a key pair so the leaked secret is refused.