Secrets management
Keep keys out of code and out of trouble
What a secret is, how secrets leak, how vaults and short-lived credentials help, and what to do in the first hour after a leak.
beginner level, 18 minutes, 18 slides.
Secrets management
Welcome. What a secret is, how secrets leak, how vaults and short-lived credentials help, and what to do in the first hour after a leak. This talk runs about 18 minutes.
A secret is a key to something
Anyone holding the secret can act as you. So the goal is simple: fewer secrets, shorter lives, and nowhere they can leak.
A secret is anything that proves you are allowed in. Whoever holds it can act as you, and the system cannot tell the difference. This talk is about holding fewer secrets, for less time, in safer places, and knowing what to do when one gets out.
What counts as a secret
- API keys: a password a program uses to call a service.
- Database credentials.
- Cloud access permissions (IAM) and tokens.
- SSH keys: a key pair used to log in to servers.
- Certificates.
Why it matters: OWASP notes that many organizations hardcode secrets in source code and scatter them through configuration files.
Secrets come in many shapes: API keys, database passwords, cloud permissions, SSH keys, and certificates. OWASP points out that many organizations have them hardcoded in source code and scattered through configuration files. That is the problem we are going to fix.
How secrets leak
- Hardcoded in source code.
- Written into config files and committed.
- Set in Docker ENV or ARG lines.
- Environment variables seen by every process.
- Written into logs or system dumps.
Environment variables: OWASP says they may be visible to all processes and end up in logs or dumps, so avoid them unless nothing else works.
Secrets leak in boring ways. They get pasted into code, saved in config files, baked into container build lines, or exposed through environment variables. OWASP warns that environment variables are generally accessible to all processes and may land in logs or system dumps, so it does not recommend them unless the other methods are not possible.
Git never forgets
Git keeps every past version of every file. A later commit that deletes the secret does not erase the older commit that holds it. Removing it from history takes a rewrite, and forks, clones and caches can still hold a copy.
First step, always: If it is a secret, revoke or rotate it first. Once it no longer works, rewriting history may not be needed.
Git stores history. If you commit a password and delete it in the next commit, the password is still in the old commit. GitHub's own guidance says that when the sensitive data is a secret, your first step is to revoke or rotate it. Once it no longer works, the extra work of rewriting history may not even be warranted.
Env file versus secrets manager
.env file in the project
- Plain text on disk
- Easy to commit by mistake
- No record of who read it
- Hard to rotate everywhere
Secrets manager
- Central, access controlled
- Access can be audited
- Rotation can be automated
- Can hand out short-lived secrets
Define: secrets manager: A service that stores secrets, controls who can read them and keeps an audit trail.
Compare a plain environment file to a secrets manager. The file is easy but sits in plain text, can be committed by mistake, and tells you nothing about who used it. A secrets manager centralizes storage, controls access, keeps an audit trail, and can automate rotation. OWASP describes the goal of centralizing storage, provisioning, auditing, rotation, and management of secrets.
How a vault is used
The app never needs the secret baked into its code.
- App starts: Proves who it is
- Secrets manager: Checks policy, writes audit log
- Credential issued: Stored value or freshly made
- App uses it: Calls database or API
Vault secrets engines can store data, or connect to other services and generate credentials on demand.
Here is the basic flow. The app starts and proves who it is to the secrets manager. The manager checks its policy and writes an audit entry. It hands back a credential, which might be a stored value or one it just created. In HashiCorp Vault, secrets engines can simply store and read data, or connect to other services and generate dynamic credentials on demand.
Least privilege and auditing
- Engineers should not see every secret.
- Give each app access to only what it needs.
- Audit access, and protect the audit logs.
- Keep accurate timestamps on audit records.
Define: least privilege: Give an account only the access it needs, and no more.
A vault with open doors is not much better than an env file. Apply least privilege, so people and apps only reach the secrets they need. Audit all access, and protect the audit logs from tampering. OWASP also stresses correct timestamps, so keep your clocks in sync.
Short-lived credentials
Dynamic secrets are made fresh when an app asks, and expire on their own.
- Each session gets new credentials.
- Set a short lease, the time before expiry.
- Revoke credentials when no longer needed.
- A stolen one stops working soon.
Restart is not revoke: Restarting an app does not revoke stolen credentials. They work until they expire or are revoked.
The best secret is one that expires before an attacker can use it. OWASP recommends dynamic secrets where possible: the application asks at startup and gets new credentials for that session. Configure a short lease and revoke when finished. Remember that simply restarting the app does not cancel a stolen credential.
Rotation
Rotation means replacing a secret with a new one on a schedule.
- Create. Make the new secret.
- Set. Install it in the service.
- Test. Check that it works.
- Finish. Complete the switch to the new secret.
OWASP: automate rotation. Manual rotation invites mistakes.
Rotation limits how long a stolen secret is useful. OWASP describes cloud secret managers rotating in steps: create the new secret, set it, test it, and finish the rotation. OWASP says it is better to automate rotation, because doing it by hand is hard and leads to mistakes.
Scan for secrets
- Scan repositories and files for secrets.
- Run scans before commit, on a developer's machine.
- Use more than one tool and compare results.
- Make secrets part of your threat model.
Example tool: Gitleaks detects secrets such as passwords, API keys and tokens in git repos and files. Its README says it is feature complete and gets security patches only.
You cannot fix leaks you cannot see. OWASP suggests detecting secrets early, even before commit with a pre-commit hook, treating secrets as part of the threat model, and considering more than one detection tool. Gitleaks is one such scanner. It scans code, past or present. Its README currently says the project is feature complete, with future releases being security patches only, so check the README for the maintainer's latest advice.
Block secrets before commit
.pre-commit-config.yaml as shown in the Gitleaks README (rev is the version it shows)
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.24.2
hooks:
- id: gitleaksThen, in the repository
pre-commit autoupdate
pre-commit installHooks can be skipped: A developer can bypass the hook, so also scan on the server side.
A pre-commit hook runs a check before a commit is saved. The Gitleaks README shows this small config. Run pre-commit autoupdate to get the latest version, then pre-commit install to turn it on. The README also notes you can skip the hook by putting SKIP=gitleaks before the commit command, so a hook is a helpful first layer, not the only one.
When a secret leaks
- Revoke. Cut off the exposed key immediately.
- Rotate. Issue a new one, ideally automated.
- Delete. Remove it from code, logs and systems.
- Investigate. Who had access? When used? Last rotated?
Goal: OWASP: the primary goal is rapid response and containment.
If a secret leaks, speed matters. OWASP's order is revoke, rotate, delete, and log. Revoke the exposed key right away. Create and deploy a new one, ideally automatically. Remove the old one from code and logs. Then use your logs to answer who had access, when they used it, and when it was last rotated.
Layers that protect secrets
No single layer is enough.
- Prevent: Pre-commit scanning
- Detect: Repository scans
- Limit: Short-lived, least privilege
- Respond: Revoke, rotate, investigate
- The secret: What we protect
Put it together as layers. Prevent leaks with pre-commit checks. Detect what slips through with scans. Limit the damage with short lifetimes and least privilege. And respond fast with revoke and rotate. If one layer fails, the next one catches it.
Check your understanding
Check yourself: You committed a password, then deleted it in the next commit. What is the first step?
Answer: Revoke or rotate the password. Git history still has it. Revoke or rotate first, then decide whether history needs rewriting.
Check yourself: Which is better for stolen-credential risk?
Answer: A credential with a short lease. Short-lived credentials stop working soon, which limits what a thief can do.
Two questions. Pause the video, answer, then reveal.
Recap
- A secret lets anyone act as you.
- Keep secrets out of code, history and logs.
- Use a manager, least privilege and audit logs.
- Prefer short-lived credentials and automate rotation.
- On a leak: revoke, rotate, delete, investigate.
To recap: fewer secrets, shorter lives, and safer places. Scan for leaks before and after commit, and when one happens, revoke first. The labs let you try scanning with Gitleaks and running Vault on your own machine.
Practise this
- Find hardcoded secrets in your code and git history with Gitleaks
- Manage secrets with HashiCorp Vault on your own machine
- Issue dynamic, short-lived database credentials with Vault
- Rebuild Git objects by hand, and see why a deleted secret stays in history
Slides only get you so far. Pick one of these hands-on labs and do it today while it is fresh. Every lab is free.
Sources
- OWASP Secrets Management Cheat Sheet
- GitHub Docs: Removing sensitive data from a repository
- Vault docs: Secrets engines
- Gitleaks README
- Git documentation: githooks (pre-commit)
Every claim in this deck came from these pages, fetched and checked for this release. Read the originals.
Hands-on labs for this deck
- Find hardcoded secrets in your code and git history with Gitleaks
- Manage secrets with HashiCorp Vault on your own machine
- Issue dynamic, short-lived database credentials with Vault
- Rebuild Git objects by hand, and see why a deleted secret stays in history
Open the interactive deck or see all slide decks.