A coding agent reads the working directory to understand a project, and a .env file sitting there is simply part of the project. The .gitignore entry protecting it only tells Git what to leave out of version control. An agent opening files never consults it.
Most teams already accept that .env is a liability. What they lack is an exit plan. That plan is below: a seven-step runbook to migrate .env secrets to workload identity, including the parallel-run and verification steps that make cutover safe, and a list of what workload identity cannot replace. The case against .env itself is covered in why .env files fail.
Why Did .env Stop Being Good Enough?
Four reasons, briefly:
- Agents treat the file as context. Knostic found Claude Code loading .env files unprompted, which surfaced when a researcher traced an unexplained proxy error back to one.
- The value sits in plaintext on disk for as long as the file exists, and in process memory for as long as the process runs.
- Copies multiply. Laptops, CI settings, built images, and backups each keep one, and none of them expire.
- Leaks already happen. A branch pushed during a sandboxed test session at Knostic carried the CEO’s Gemini API key, which Claude Code had written into a test file. That is how secrets in git begin.
On env file security, the last point settles the argument. No malice was involved; the agent simply had access nobody meant to give it.
Where Does Your Team Sit on the Four Levels?
| Level | What it looks like | What the agent can see | What remains exposed |
|---|---|---|---|
| 0 | A plain .env file any process on the host can open | Every secret, in plaintext | Everything |
| 1 | Secrets fetched from a manager at startup | Every value, held in process memory | Values live in the process |
| 2 | A broker or proxy adds credentials to each outbound call | Nothing reusable | Static secrets still exist upstream |
| 3 | Workload identity: the platform attests to the workload | Only short-lived tokens, no stored secret | Targets that cannot federate |
Most teams that believe they are at Level 1 are still at Level 0 somewhere, such as a developer laptop or an old CI job, and teams that migrate .env secrets to workload identity usually find those corners first.
How to Migrate .env Secrets to Workload Identity: A Seven-Step Runbook
- Inventory every .env file and its equivalents: repositories and their history, CI variables, container images, and MCP server config files. Anything holding a credential is in scope, including hardcoded API keys in source files.
- Classify each secret by what it unlocks: a cloud provider API, a service inside your cluster, a database, or a third-party product. The target decides whether workload identity is even available.
- Map each secret to the workload that uses it, then give that workload an identity, such as an IAM role, a Kubernetes service account, or a SPIFFE ID, trusted through workload identity federation wherever the target accepts it.
- Run both paths in parallel. Switch on the new identity while the old secret stays valid, and log which path each request takes, so cutover breaks nothing and you can see when the old path goes quiet.
- Cut over one workload at a time, highest-risk target first: production databases and payment systems before internal tools.
- Retire the old secret. Revoke it at the source; where a provider cannot revoke a key outright, fall back to rotating the retired credential so the old value stops working.
- Verify. Scan repository history, images, and running environments for the retired value, and confirm the step 4 logs show no traffic on the old path. The same sweep usually surfaces hardcoded API keys nobody had listed, so budget time for a second pass.
| THE STEP TEAMS SKIP Step 6. A migration that adds workload identity but leaves the old key valid has created a second way in, and the first one is still open. |
What Still Needs a Vault?
Workload identity covers the cloud and cluster layer well. Three kinds of secret sit outside it:
- Keys issued by third-party APIs. Many payment, messaging, and model providers still authenticate with keys they issue themselves, so those keys stay in a managed store and get injected per request instead of shipping with the agent.
- Legacy systems that expect a username and password inside a connection string.
- Shared secrets such as webhook signing keys, which both parties must hold by design.
For that remainder, the goal is short-lived issuance rather than elimination: credentials that expire on their own where a target can issue them, and brokered injection where it does not. Most estates finish the migration with all three patterns running, which is why Akeyless covers federation, dynamic secrets, and brokering under one secrets management policy.
The Local Development Problem
Most advice to delete .env skips the laptop, which is why the file survives. A developer still needs to run the agent against something real.
The replacement is a short-lived developer credential scoped to a non-production target and fetched only when needed. A coding agent reading the directory then finds nothing, or a credential that opens a sandbox for a few hours. Akeyless covers this with secrets in the IDE for Cursor and VS Code: developers pull dynamic secrets on request, and each expires on the TTL an administrator sets. That approach also settles the secret zero question on the laptop, which a .env file answers by writing the secret down.
FAQs About Moving AI Agents off .env Files
Can AI Coding Agents Read My .env File?
Yes. Coding agents open project files to do their work, and a .env file is readable like any other, which is the core env file security problem. Path controls only partly help: Claude Code’s permission rules can deny reads of .env but do not stop scripts the agent runs, and Cursor’s ignore file does not govern terminal commands or MCP tools.
Does .gitignore Protect Secrets from an AI Agent?
No. Git’s documentation says the file “specifies intentionally untracked files to ignore.” It controls what gets committed, which helps against secrets in git, but an agent reading files from disk never looks at it.
What Replaces .env Files in Production?
Teams that migrate .env secrets to workload identity usually land on three patterns: workload identity for cloud and cluster targets, short-lived dynamic credentials for databases, and brokered injection for third-party keys that cannot federate, with one secrets platform holding policy and audit for all of them.
Does Workload Identity Remove Every Secret?
No. It eliminates stored secrets for targets that trust your platform’s identity, typically cloud providers and in-cluster services, though workloads still hold short-lived tokens. Third-party API keys, legacy connection strings, and signing keys remain, and those need short-lived issuance or brokering instead of a file.
What Do Developers Use Locally After Removing .env?
A short-lived credential pointed at a development or staging target and expiring within hours. Pulled on demand through a CLI or IDE plugin, it gives developers what .env did without leaving a durable secret for a coding agent to find.