Sub topic 7
Moving secrets out of code and into the environment
This chapter assumes you know basic Git and the command line, what an API key is for, and how environment variables work. It also assumes you have used an AI coding assistant. The goal is to keep credentials out of source files. An assistant that reads your repository then has nothing to leak.
Why source code is the wrong place
An assistant reads the files in your project, and Git keeps every committed version forever. A key written into a source file is exposed in three places:
- The assistant's context, and so the provider's servers.
- Your Git history, even after you delete the line.
- Every clone, fork, and CI log that touches the repository.
Moving the value into the environment separates the code, which is safe to share, from the credential, which is not.
Read the value at runtime
Replace the literal with a lookup. Using os.environ[...] raises a KeyError when the variable is missing, so a misconfigured run fails loudly instead of sending an empty credential.
import os
api_key = os.environ["PAYMENTS_API_KEY"]Avoid os.environ.get("PAYMENTS_API_KEY", "sk-test-123"). A fallback default puts a real-looking secret back into the source.
Use a local .env file, and ignore it
For local development, many teams keep values in a .env file that a loader reads into the environment. This file holds real secrets, so it must never be committed. Commit a .env.example file with placeholder values instead, so teammates know which variables to set.
.env
.env.*
!.env.example
*.pem
*.key
terraform.tfstate
terraform.tfstate.*The !.env.example line is a negation. It re-includes the template after .env.* would otherwise exclude it. Many AI tools support a similar ignore file to skip the same paths, but the file name and syntax depend on the tool. Check your tool's documentation rather than assuming it reads .gitignore.
Common errors
| Mistake | Why it matters |
|---|---|
| Committing `.env` before adding it to `.gitignore` | Ignoring a file does not remove it from history once it is tracked. |
| Putting real values in `.env.example` | The template is committed, so the secrets are published. |
| Printing `os.environ` while debugging | The output goes into logs and into any prompt you paste it into. |
| Hardcoding a "temporary" key | Temporary keys stay in history permanently. |
Beyond a local file
On shared systems, inject secrets from a managed store at deploy time instead of copying a .env file around. Examples include your cloud provider's secrets service and your CI system's encrypted variables. The code stays the same, because it still reads os.environ. Only the source of the value changes.
If you find a secret already committed, moving it to the environment is not enough. The old value is still in history and must be treated as leaked. Revoke and rotate it, as described in the incident response chapter.
Exercise
A teammate removes a hardcoded API key from config.py, replaces it with os.environ["API_KEY"], and commits. Is the key now safe? Explain what else is needed.
Show answer
No. The old commit still contains the key in Git history, and any assistant, clone, or fork that saw it may have a copy. The key must be treated as leaked: revoke it, issue a new one, and load the new value only from the environment.
Exercise
Why does the .gitignore example include !.env.example after .env.*?
Show answer
The pattern .env.* also matches .env.example. The ! negation re-includes it, so the placeholder template can be committed while real .env files stay ignored.