Sub topic 15
Designing workflows that keep secrets out of code
This chapter assumes you can commit, push and branch in Git, and that you have already seen how secret scanning alerts and push protection work. Detection and remediation are the safety net. This chapter covers how to build workflows so credentials rarely reach a commit.
Why prevention comes first
Every leaked secret costs work: revoke it, rotate it, check the provider's logs, and close the alert. Push protection blocks many leaks. It can't block patterns it doesn't recognize, and a user can bypass it. A good workflow reduces how often a developer holds a raw credential at all.
Keep the value out of the source
The code should name a secret and never contain its value. The application reads the value from its environment when it starts.
import os
api_key = os.environ["PAYMENTS_API_KEY"]os.environ[...] raises a KeyError if the variable is missing. That is the behavior you want, because the program fails at startup instead of running with an empty credential.
Where the value comes from depends on where the code runs:
| Where the code runs | Where the secret lives |
|---|---|
| Local development | A local `.env` file that is git-ignored, or a developer-specific credential |
| GitHub Actions | Repository, environment or organization secrets, read as `${{ secrets.NAME }}` |
| Production | A secrets manager or the platform's secret store, injected at deploy time |
Commit a .env.example file that lists variable names with fake values. New team members then know what to set, and the real file stays out of the repository.
# .gitignore
.env
*.pemAdd the ignore rules before anyone creates the file. Once a file is tracked, .gitignore no longer applies to it.
Prefer short-lived credentials
A long-lived key that is leaked stays dangerous until someone revokes it. Short-lived credentials limit the damage. For example, GitHub Actions can use OpenID Connect (OIDC) to get a temporary token from a cloud provider. No stored key is needed. Where you can't use OIDC, give each key the narrowest scope it needs and set an expiry.
Layer the controls
Each layer catches what the one before it missed. The local check is optional, and it runs on the developer's machine, so it can be skipped. Push protection runs on the server, so it applies to everyone who pushes.
Make the safe path the easy path
- Document one standard way to load secrets, and put it in the project template.
- Give developers a shared sandbox credential for local work, so nobody pastes a production key into a test.
- Use placeholders such as
YOUR_API_KEY_HEREin documentation instead of real-looking values. - Add custom patterns for internal token formats, so your own credentials get the same blocking as provider tokens.
- Exclude test fixtures and docs that contain dummy values, so alerts stay meaningful. Narrow exclusions are better than broad ones.
Review what bypasses tell you
Alerts marked as bypassed through push protection show where the workflow is failing. If one team bypasses often, ask why. Common causes are a missing secret store, a test that needs a credential, or a pattern that raises false positives. Fix the cause, because repeated bypasses train people to ignore the block.
Exercise
A developer's tests need an API key. They keep the key in config.py and rely on push protection to catch it. Name two changes that follow the practices in this chapter.
Show answer
Read the key from an environment variable (for example os.environ["PAYMENTS_API_KEY"]) and store the real value in a git-ignored .env file locally and in a secret store such as GitHub Actions secrets in CI. Also commit a .env.example with a fake value, and consider a low-privilege sandbox key for tests. Push protection stays as a backstop, not the main control.