Secrets management, and what to do when one leaks
Where secrets should live, .gitignore and pre-commit scanning, SSM and Secrets Manager, least-privilege IAM, and a step-by-step leaked-secret response using real findings from your repos.
What counts as a secret
DB passwords, JWT signing keys, AES keys, API keys (Stripe, Paystack, Cloudinary, SES), OAuth client secrets, VAPID private keys, Apple .p8 keys, Firebase service-account JSON, CloudFront private keys, tunnel tokens.
Where secrets should live
| Environment | Store |
|---|---|
| Local dev | .env (git-ignored) + .env.example with placeholder values |
| CI | GitHub Actions encrypted secrets, or better, OIDC with no long-lived keys at all |
| Production | AWS SSM Parameter Store (SecureString) / Secrets Manager / Vault / Render environment variables |
In your projects (findings)
src/main/resources/application.yaml⚠️ A hard-coded DB username and password in a public repo. Store PR #1 moved them to DB_URL, DB_USERNAME and DB_PASSWORD, but the old value is still in git history, so treat it as leaked and never reuse it.
backend/.env · frontend/.env · frontend/.env.production⚠️ These files are tracked in git. .env.staging.example is the only file meant to be committed.
*.p8 · backend/secrets/*.pem · firebase-service-account.json · backend/.env⚠️ Key material sits in the working tree (Apple .p8 keys, a CloudFront private key, a Firebase service account). Make sure all of it is git-ignored, or better, keep it in SSM only. Production does the right thing: fetch-env.sh pulls from SSM under /kontentt/(env)/... using the instance’s IAM role.
infrastructure-stack.ts · deploy.ymlBest practice:
- The RDS password comes from
Credentials.fromGeneratedSecret(in Secrets Manager; no human ever sees it). - App secrets are injected with
ecs.Secret.fromSsmParameter. - GitHub Actions uses OIDC to assume
Hostel-Github-Actions, so there are no AWS access keys stored in GitHub.
ProductionSecretsCheck · .env.exampleThe app fails fast if prod secrets are blank or dev defaults. A real .env exists locally, so confirm it’s ignored.
Leaked-secret response playbook
- Rotate first. Generate a new secret at the provider or DB and deploy it. Deleting the file doesn’t un-leak anything, and bots scrape GitHub for keys within minutes.
- Revoke the old secret.
- Check the logs for misuse (Stripe dashboard, AWS CloudTrail, DB logs).
- Remove it from git history with
git filter-repo --path backend/.env --invert-paths, then force-push and have collaborators re-clone. This is cosmetic once rotated, but it reduces noise. - Prevent it next time:
.gitignore:.env,*.pem,*.p8,secrets/.- Pre-commit scanning with gitleaks or trufflehog (you already use Husky in Hostel’s frontend, so add a hook).
- Turn on GitHub secret scanning + push protection.
# quick audit
git ls-files | grep -Ei '\.env$|\.pem$|\.p8$|secret|credential'
gitleaks detect --source . -v
Related concepts you must know
- Least privilege. The IAM role reads only
/kontentt/prod/*, and the DB user can’tDROP. - Short-lived credentials (OIDC, IAM roles, Vault dynamic DB users) beat long-lived keys.
- Don’t log secrets. Sentry PII scrubbing (RM and IR3D
BeforeSend), and maskAuthorizationheaders in request logs. - Separate secrets per environment. A leaked staging key must not unlock production.