A Secret in Git Is a Secret Forever
The file everyone has
Almost every codebase I’ve inherited had the same file at its root: .env. One I took on had two of them, one for development and one for production, both committed to git and both copied into most of the Docker images. Every image carried every credential, whether that service needed it or not.
Nobody had done this out of carelessness. It’s how the project started, when there was one app, one server and three people. The file was convenient, so it grew. By the time I read it line by line, it held more than eighty keys.
That file is the subject of this lesson, because the habit it represents is about to get a lot more expensive.
A secret in git is a secret forever
The first instinct, when someone spots a password in the repo, is to delete it and commit. That fixes the file. It doesn’t fix the leak.
The value is still in the history. It’s in every clone on every laptop, in the CI runner’s cache, in images sitting in a registry, in the fork someone made two years ago. Rewriting history is slow, breaks everyone’s checkout and still misses the copies you don’t control.
So the rule I work by is short: only rotation un-leaks a secret. Moving a leaked key into a vault without changing it protects nothing. It just gives the old value a nicer home. When I planned the migration, rotating each credential as it moved was its own step, and scrubbing git history was explicitly out of scope. Rotation is the fix. Everything else is tidying.
Most of your .env isn’t secret
Before moving anything, I classified every key. What does it hold, who reads it, and would it hurt if a stranger saw it?
The answer surprised me. Fewer than a third of the keys were actually secrets. The rest were settings: hostnames, feature switches, timeouts, a few dead keys nothing read anymore, and a handful that had quietly drifted between the dev and production files.
That changed the size of the problem. Settings go into ordinary configuration, where they can be read, reviewed and diffed. Only the real secrets go into the vault, and a short list of real secrets is far easier to protect, audit and rotate than a long file of everything.
The same pass turned up the finding I now look for first: most of the development secrets were byte-for-byte identical to production’s. A leaked dev laptop was a leaked production system. The fix is giving each environment its own credentials. The database login went first: development now has its own user, one that can’t reach anything production owns.
A vault you don’t read from is decoration
Once I had access to the infrastructure clusters, I found a vault already running there. A highly available install, healthy, on both clusters. It had been up for over four months.
It held nothing. Someone had stood it up, and no application had ever been changed to read from it.
That’s the trap with security tooling. Installing it feels like progress, and the dashboard looks good. But a vault only protects you once the code stops reading secrets from anywhere else, and that part is a migration, app by app, which is the work nobody schedules.
Pull, don’t bake
The design I landed on is simple to say: images and manifests carry an identity, never a credential.
Each service runs under its own Kubernetes service account. At container start, the pod proves who it is to the vault, and the vault hands back only the paths that service’s role allows. There’s no secret in the image, no .env copied into the build, and nothing for a leaked image to give away.
Least privilege lives in the vault’s policy, not in the app’s good behaviour. One review comment from that work stuck with me: not loading a key is not access control. A service that simply doesn’t read a path can still be allowed to. The policy is what decides.
I rolled it out the boring way: development first, then staging until it ran with no secrets file at all, then production as a canary on one app. One app turned out to prove too little, so the canary widened to two before anything else moved. PHP, Go and Node services now all read from it the same way. When a pod in another repository needs one value, it gets a role for exactly that one path.
Why I built my own vault
The vault I deployed is uBixVault, which I built from scratch and released as open source. That’s a decision worth defending, because “I wrote my own secrets manager” is usually a warning sign.
My condition for using it at work was a written exit path. uBixVault speaks the standard HashiCorp-compatible API and path layout, so the applications talk to an interface, not to my code. If it ever needs replacing, that’s a configuration change, not a rewrite. The vault is a backend, never an interface.
Building it taught me where these systems actually break: unsealing after a restart, high availability under a node failure, who is allowed to grant access to whom. The latest release is about exactly that last one: handing out role administration without handing out root.
What AI changed
Here’s why this matters more now than it did two years ago.
Leaked keys get used in minutes, not months. Automated scanners have watched public repositories for credentials for years. What’s new is how cheap it has become to go further: take what was found, work out what it unlocks, and try it.
A leaked codebase used to be a haystack. Now it comes with a guide. Hand a model a stolen repository and ask where the payment path is, which key opens what, which session token can be forged. Reading a large unfamiliar codebase used to take an attacker days. It doesn’t anymore.
Your own AI tools read your repository too. Coding agents load whatever is in the working tree into their context. A .env in the repo can end up in an agent’s prompt, its logs and someone else’s logs. In my own merge gate, the AI triage bot that proposes fixes is never allowed to read a secret, and it says so when it couldn’t look. The simplest way to keep secrets away from every agent is to keep them out of the repository.
So the assumption has to change. “Nobody will find it” was never a good defence, and now it isn’t one at all. Assume your code will be read, by people and by models, and make it worthless to them.
The checklist
- Find every secret. Classify every key: secret, setting, or dead. Most of the file won’t be secret.
- Separate the tiers. Development credentials must never match production’s.
- Pull at start, never bake. Images carry an identity; the vault decides what it gets.
- Scan every merge. A secret scanner that blocks the merge request catches the next one before it lands.
- Rotate as you move. A secret that was ever in git is already leaked. Moving it isn’t enough.
- Write the exit first. Whatever vault you choose, the apps should talk to a standard interface.
- Migrate per service, canary first. The big-bang version fails on the one app nobody checked.
None of this is new advice. What’s new is how little time you have between a mistake and someone, or something, finding it.