Operations and security 4 min read Updated
Keeping secrets out of your code and logs
In short
Keep secrets in a store outside the code, give each one the smallest access it needs, and rotate it on schedule and after any leak.
A secret in the wrong place is a common route into a system. Passwords, API keys and database credentials end up in source files, container build settings, chat messages and backups, and a copy nobody remembers can outlive the team that made it. Start by deciding what counts as a secret, because the list is broader than most teams expect.
A secret is more than a password
OWASP treats a broad range of credentials as secrets. Its list includes API keys, database credentials and identity and access management permissions, and a later section also covers session tokens, two-factor keys and passwords. Treat anything that lets a caller act as your system as a secret, even when it looks harmless on its own.
Not every setting is a secret, and treating everything as one makes the real secrets harder to protect. The Twelve-Factor App defines configuration as everything that is likely to vary between deploys, such as a database address or the hostname of the current deploy. Internal settings that stay the same in every deploy belong in the code.
Keep configuration apart from code
The Twelve-Factor App stores configuration in environment variables. They can change between deploys without touching the code, they are unlikely to be committed by accident, and they work across languages and operating systems. Its litmus test is that the codebase could be released as open source at any time without compromising any credentials.
OWASP is more cautious about the same mechanism. It generally discourages environment variables for secrets, because every process can see them and they can end up in logs or dumps. Secrets should also never be hard-coded in container build settings such as ENV or ARG. The preferred approach is a central secrets management system, with values mounted into the application or fetched at runtime. OWASP does not forbid Git outright, but it describes committing encrypted secrets, such as Sealed Secrets in a GitOps workflow.
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Limit access and keep an audit trail
Least privilege applies to secrets as much as to people. OWASP advises that engineers should not have access to all secrets in the secrets management system, and that access policies should be fine-grained, one secret at a time. It also suggests separate stores for production and development.
Audit logs should record at least the following:
- who requested a secret, and why;
- whether the request was approved or denied;
- when the secret was used, and attempts to reuse an expired one;
- every update and administrative action.
OWASP asks that audit logs resist tampering and deletion, that timestamps are accurate, and that unusual access is monitored. Its example is an alert when CI/CD credentials are used from an unexpected IP address. Log design has its own rules, covered in logging: what to record and how long.
Rotate on a schedule, and after a leak
OWASP says secrets should be rotated regularly, so that stolen credentials work only for a short time. Automate rotation where you can, and prefer dynamic secrets with short validity periods. Two details are easy to miss. Restarting an application does not revoke a stolen credential, which stays usable until the backing service expires or revokes it. User passwords are excluded from routine rotation; they should be changed when there is evidence or suspicion of compromise.
When a secret is exposed, the order matters. OWASP recommends revoking it first, then rotating it, preferably through an automated process, and then removing it from every place where it was exposed, including code and logs. Removing a secret from logs must not compromise their integrity. The response team also needs to know who had access, when the secret was used and when it was last rotated. If the leak becomes an incident, follow incident response for a small team.
Checklist for the next release
- Search the repository and recent build logs for keys and passwords, and remove any you find.
- Move each secret to a store or runtime source outside the code, and write down where it lives.
- Give each service only the secrets it uses, and review who can read the store.
- Turn on audit logging and alert on unusual access.
- Write down how to revoke and rotate each secret, and who to call.
- Test one rotation before you need it.
The checklist and the separation of duties are general engineering practice. The rules attributed to OWASP and the Twelve-Factor App are the ones those pages state.
Common questions
- Are environment variables safe for secrets?
- They suit configuration well, but OWASP generally discourages them for secrets, because every process can see them and they can end up in logs or dumps. Use them only when other methods are not possible.
- Should user passwords be rotated on a schedule?
- No. OWASP excludes user credentials from routine rotation and says they should be changed when there is evidence or suspicion of compromise.
- Does restarting the application revoke a leaked key?
- No. A stolen credential stays usable until the backing service expires or revokes it, so the key must be revoked explicitly.
Sources
- Secrets Management Cheat Sheet, OWASP Cheat Sheet Series. Accessed 24 November 2026
- III. Config, The Twelve-Factor App. Accessed 24 November 2026
Further reading
-
Two-factor sign-in for admin accounts
Require a second factor for every admin account, prefer phishing-resistant methods, and keep recovery from becoming a way around MFA.
-
What to record in logs and how long to keep them
Log the security events that help you answer what happened, keep passwords, tokens and personal data out of logs, and protect the logs you keep.
-
Incident response for a small team
A small team handles an incident well when roles, severity levels and the first hour are agreed before anything breaks.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea