Secrets management
Move from a laptop shell to a safe, observable delivery pipeline: version control, Linux processes and permissions, container images, networking, configuration and secrets, deployment, health checks, observability, rollback, reproducibility and security hardening.
What you will be able to do
#- Describe the threat model for secrets in source, images and logs.
- Inject secrets at runtime from a managed store instead of committing them.
- Rotate a secret and verify the old value no longer works.
Overview
#A secret is any value that grants access: passwords, API keys, tokens, private keys. Configuration can be public; secrets must never be. The goal is to keep secrets out of source control, images, logs and error messages, and to be able to rotate them without a full redeploy.
Where secrets leak
- Committed to Git, where history preserves them even after deletion.
- Baked into a container image layer, where anyone who pulls the image can read them.
- Printed in logs, stack traces or crash reports.
- Passed as command-line arguments, which are visible in the process list.
Injection at runtime
The application should read secrets from the environment or a mounted file supplied by the platform at run time, not from the repository. A managed store issues short-lived credentials and audits access.
# the platform injects the value; the app only reads it
DATABASE_PASSWORD="$(secret-tool lookup service db)" ./start.sh
Rotation
Rotation replaces a secret with a new value and retires the old one. To rotate without downtime you need two valid credentials at once, so the system should accept the new value before the old is revoked. After rotation, verify the old value is rejected.
Common pitfalls
- Assuming deleting a committed secret removes it; history and forks keep it, so rotate immediately.
- Logging the whole request or config object, which includes credentials.
- Using one long-lived credential for every service, so a single leak compromises everything.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Explain
Explain why a secret committed to Git must be rotated even after it is removed in a later commit.
starter.text// Describe Git history and why removal is not enough.Reveal the reference answer
reference.textGit keeps every commit, so the secret remains in history and in every clone, fork and backup. Anyone with repository access can recover it. Because you cannot prove the value was never read, the only safe response is to treat it as compromised and rotate it, then purge history if appropriate.Authored expected output: An explanation that history preserves the value and rotation is the only safe remedy.
Removal changes the working tree, not the object database. Rotation invalidates the leaked value; history rewriting is cleanup, not security.
Exercise 2Implement
Write a small module that reads a database password from a file path given by DB_PASSWORD_FILE, and throws if the file is missing, without ever logging the value.
starter.javascript// read secret from file, fail fast, never log itReveal the reference answer
reference.javascriptconst fs = require('fs'); function readSecret(env = process.env) { const path = env.DB_PASSWORD_FILE; if (!path) throw new Error('DB_PASSWORD_FILE is not set'); let value; try { value = fs.readFileSync(path, 'utf8').trim(); } catch { throw new Error('Unable to read DB_PASSWORD_FILE'); } if (!value) throw new Error('DB_PASSWORD_FILE is empty'); return value; }Authored expected output: The password string is returned, or an Error is thrown that names the variable but never the value.
Reading from a mounted file is a common platform pattern. The error messages reference the variable and path, never the secret itself, so logs stay safe.
Exercise 3Debug
A crash handler logs the entire config object on error, and a screenshot of the log is shared in a public issue. What happened and what should change?
starter.textcatch (err) { logger.error({ config }, err.message); }Reveal the reference answer
reference.textThe config object contained secrets, so the log leaked them. Redact sensitive fields before logging, log only safe identifiers, and treat the leaked values as compromised and rotate them.Authored expected output: Logs no longer contain secret values; leaked credentials are rotated.
Structured logging makes it easy to log whole objects, which is exactly how secrets end up in log stores. Maintain an allow-list of loggable fields and redact everything else.
Common misconceptions
#- Deleting a secret from the latest commit removes it: it stays in history and must be rotated.
- Environment variables are always safe: they can leak through crash dumps or child processes.
Follow-up practice
#- devops-deployment
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.