Skip to content

Security · 4 of 6

Secrets out of code

Step 1 · The pattern

The key sits in a config file, and it works

The app needs a cloud storage key, so an engineer puts it in config.py and commits it. The app reads and writes storage with it. It is the quickest way to ship. (All numbers are illustrative.)

  • the app
  • storage read

In short

Secrets out of code: how it works and how it breaks

The quickest way to give an app a database password or a cloud key is to put it in the code or a config file next to it. From then on the key is copied to every clone, every build log, every fork and every person who can sign in to the code host, and it never expires. 'Private' repositories are only as private as the weakest account that can read them: a reused password without multi-factor authentication is enough. Keep keys out of code entirely. The app gets credentials at run time, ideally short-lived ones from the platform's identity (a cloud role) or a secrets manager, scoped to what it needs. Scan commits and history for keys and block pushes that contain them, require multi-factor authentication on the code host, and when a key does leak, rotate it at once: deleting the commit does not delete the copies.

What this simulation simplifies

  • The intruder's sign-in is instant; real ones search many repositories over weeks.
  • Build logs, container images and chat messages leak keys too; only the repository is shown.

How this comes up in interviews

"How does your service get its database password and cloud credentials?"

Then they ask

  1. What happens when one leaks?
  2. How do you rotate without downtime?
  3. How do you stop keys reaching the repository?

A strong answer covers

  • Never in code or images; fetched at run time from a secrets manager.
  • Prefer short-lived credentials from the platform identity (cloud roles).
  • Least privilege per service; separate keys per environment.
  • Rotate on a schedule and immediately on any leak; support two keys during rotation.
  • Secret scanning on push, and MFA on the code host.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last reviewed: Official documentation

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.