Secrets Management
Store and manage secrets securely with vaults, KMS, and environment-based injection.
What you'll learn
- Why hardcoded secrets are dangerous
- Secret management tools: Vault, AWS Secrets Manager
- Injecting secrets into applications safely
- Rotating and auditing secrets
Concept
The Problem with Hardcoded Secrets
Every application needs secrets — database passwords, API keys, TLS certificates. The worst approach is hardcoding them in source code or config files committed to git. Secrets in git are effectively public — even in a private repo, they end up in CI logs, Docker images, and developer machines. Once a secret is committed, it must be considered compromised and rotated. Never commit secrets to version control.
Secret Management Tools
HashiCorp Vault is a popular open-source tool that centralizes secret storage, with encryption, access policies, and dynamic secrets (it can generate temporary database credentials). AWS Secrets Manager and Parameter Store are cloud-native options for AWS. Azure Key Vault and Google Secret Manager are their equivalents. Doppler and Infisical are newer developer-friendly options. These tools encrypt secrets at rest, control access with policies, and audit every access.
Injecting Secrets
Applications should receive secrets at runtime, not have them baked in. In Docker, pass secrets with -e or --env-file. In Kubernetes, use Secrets and mount them as files or environment variables. In cloud platforms, use the SDK to fetch secrets at startup. For local development, use .env files that are gitignored. The key principle is that secrets live outside the code and are injected at the moment they're needed.
Rotation and Auditing
Secrets should be rotated regularly — even if they haven't been leaked. Set a rotation schedule (e.g., every 90 days) and automate it where possible. Dynamic secrets (short-lived credentials generated on demand) are even better. Audit who accessed which secret and when — all secret management tools provide audit logs. Have an incident response plan for secret leaks: rotate immediately, investigate how it leaked, and fix the root cause.
Secret Scanning
Integrate secret scanning into your CI pipeline to catch leaked secrets before they reach production. Tools like GitGuardian, TruffleHog, and GitHub's built-in secret scanning scan commits for known secret patterns (AWS keys, database URLs, API tokens). If a secret is detected, the pipeline fails and the team is notified. Enable push protection to block commits containing secrets at the Git level. This automated detection layer complements your manual practices and provides a safety net for human error.
Example
# Never do this — hardcoded secrets in code:
# const DB_PASSWORD = "supersecret123"; // BAD!
# Instead, read from environment or a secret manager:
# const password = process.env.DB_PASSWORD;
# HashiCorp Vault: store and retrieve a secret
vault kv put secret/myapp/db password="s3cur3p@ss" user="admin"
vault kv get secret/myapp/db # retrieve the secret
# AWS Secrets Manager (CLI)
aws secretsmanager create-secret \
--name "myapp/db-password" \
--secret-string '{"password":"s3cur3p@ss"}'
aws secretsmanager get-secret-value \
--secret-id "myapp/db-password" \
--query SecretString --output text
# Kubernetes: create a secret from literals
kubectl create secret generic db-secret \
--from-literal=password=s3cur3p@ss \
--from-literal=user=admin
Vault, AWS Secrets Manager, and Kubernetes Secrets all provide secure storage. Secrets are injected at runtime via environment variables or mounted files, never hardcoded.
Try it
- Password Generator
Generate strong random passwords for secrets.
- Password Strength Analyser
Analyse secret strength and crack time.
- SSH Key Generator
Generate SSH keys for secure access.
Common mistakes
The mistake
Storing secrets in environment variables in production
The fix
Environment variables can be leaked via process dumps, error reports, and child process inheritance. Use a secret manager that injects secrets at runtime or mounts them as files.
The mistake
Not rotating secrets regularly
The fix
Set an automated rotation schedule. Stale secrets are a security risk — if one leaks, the longer it goes unrotated, the more damage an attacker can do.
Practice Secrets Management
1 exercise
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.