Environment variable best practices
Practical rules for handling secrets with Keel, from keeping them out of Git to rotating compromised credentials and restricting access.
Last updated
Keel gives you a safer place to keep secrets. These practices keep them safe once they leave it.
Never commit real secrets to Git#
- Add
.env,.env.*and.keel.jsonto.gitignore..keel.jsonholds no secrets but is personal to you. - Commit a
.env.examplewith names and fake values so teammates know what to set. - Add a secret scanner to your repository or CI. Keel's own tokens are prefixed
keel_at_andkeel_rt_to be easy to detect. - If a secret reaches Git history, treat it as compromised even after you delete the commit. Rotate it.
.env
.env.*
!.env.example
.keel.jsonUse separate credentials per environment#
Create distinct credentials for development, staging and production at their source, such as a separate database user or API key for each. Store each in the matching Keel environment. A leak of a development key then cannot touch production.
Restrict access#
- Give people the lowest role that lets them work. Most developers need
member, notadmin. - Grant only the environments they use. Production is denied by default for members and viewers. Keep it that way.
- Remember that
viewercan still read values in granted environments. - Review the member list and the access matrix regularly, and remove people who have moved on.
Keep secrets out of logs and errors#
- Never print
process.envor whole config objects. - Log whether a variable is set, not its value.
- Check that your error reporter and request logger scrub values and headers.
- Do not echo secrets in CI output. Mask them in your CI provider.
Handle .env files carefully#
- Prefer
keel runto exported files. See Runtime secret injection. - If you must export, write the file outside the repository, restrict its permissions, and delete it afterwards.
- Do not attach
.envfiles to tickets or messages. - Remember that an export is audited as
secrets.exported.
Rotate compromised credentials#
- Revoke or rotate the credential at its source first, so the leaked value stops working.
- Update the value in Keel. This creates a new version.
- Redeploy or restart applications so they read the new value. With Vercel, a new deployment is required.
- Check the audit log for
secret.revealedandsecrets.exportedevents to see who could have had it. - Do not use rollback to return to a compromised value.
Manage deployment credentials safely#
- Keep the Vercel access token scoped to the team and projects you need, and rotate it periodically.
- Use a deploy hook for one branch only.
- Treat the Keel server's
SECRETS_ENCRYPTION_KEY,CLERK_SECRET_KEYandMONGODB_URIas your most sensitive values. Keep them in your hosting provider's secret store, not in the repository. - Back up
SECRETS_ENCRYPTION_KEYseparately from the database backups. Without it the data cannot be decrypted.
Next steps#
Fix problems with Troubleshooting.