Sub topic 9
External secret managers and fetching secrets at run time
This chapter assumes you can read a GitHub Actions workflow, know what an environment variable is, and have used a platform secret store such as repository secrets. It covers when to move secrets out of the CI platform and how a job fetches them at run time.
Why use an external secret manager
A platform-native store is simple, but it has limits. Secrets are copied into each repository or organization. Rotating one means editing it in every place it lives. Audit detail is thin, and the secret is only available to that one CI platform.
An external secret manager is a service built to hold secrets, such as AWS Secrets Manager, HashiCorp Vault, Google Secret Manager, or Azure Key Vault. It keeps one authoritative copy and checks every read against an access policy. It also logs each read and can rotate values on a schedule.
| Concern | Platform-native store | External secret manager |
|---|---|---|
| Setup effort | Very low | Higher |
| Single source of truth | No, copied per repository | Yes |
| Access policy detail | Repository or environment level | Per secret, per identity |
| Read-level audit log | Limited | Detailed |
| Automatic rotation | No | Often built in |
For a small team with a few secrets, the platform store is often enough. Move to a manager when many pipelines share secrets or when you need per-secret audit trails.
How fetching works
The job does not hold the secret before it starts. Instead, it proves who it is to the manager, asks for one secret, and uses it. The step that does the proving is the weak point. If you store a manager token as a platform secret, you have only moved the problem. The next chapter shows how to remove that stored credential with OIDC. Here, the examples assume the job already has a way to authenticate.
A worked example
This step fetches a database password from AWS Secrets Manager and passes it to a command. It assumes an earlier step has already configured AWS credentials.
steps:
- name: Run migration with a fetched password
run: |
DB_PASSWORD=$(aws secretsmanager get-secret-value \
--secret-id prod/app/db-password \
--query SecretString \
--output text)
echo "::add-mask::$DB_PASSWORD"
DB_PASSWORD="$DB_PASSWORD" ./run-migration.shThree details matter here:
- The
add-maskcommand is needed because GitHub only masks values it knows came from its own secret store. A value fetched at run time is unknown to it until you register it. - The value lives in a shell variable for this one step. It is not written to
$GITHUB_ENV, so later steps cannot read it. - The command line has no secret in it. Arguments are visible in process listings, so the secret is passed as an environment variable instead.
Common errors
- Fetching everything. Pulling a whole bundle of secrets into every job defeats the point. Fetch only what the job uses, and give the job's identity access to only those secrets.
- Forgetting to mask. A fetched value printed by a debug flag appears in plain text.
- Using a broad policy. A policy like
secretsmanager:*on all resources lets a compromised job read every secret. AllowGetSecretValueon named secret ARNs. - Caching the value. Writing a fetched secret to a file in the workspace can leak it through artifacts or caches. If a tool needs a file, write it to a temporary path and delete it in the same step.
- Long-lived manager credentials. A static token for the manager is still a secret that can leak. Prefer short-lived identity.
Choosing between options
Use the platform store when secrets are few, tied to one repository, and rarely rotated. Use an external manager when several systems need the same secret, when you must prove who read what, or when rotation should happen without editing pipelines. Fetching adds a network call and a new dependency, so a manager outage can block deployments. Plan for that by keeping the manager highly available and by failing the job clearly when the fetch fails.
Exercise
A job runs aws secretsmanager get-secret-value and stores the result in a variable, then a later step prints a debug dump that includes it. Name two changes that reduce the chance the value leaks in the logs.
Show answer
First, register the value with echo "::add-mask::$VALUE" immediately after fetching it, so the runner hides exact matches in logs. Second, keep the value inside a single step instead of exporting it to $GITHUB_ENV, and remove the debug dump, so later steps never receive it. Fetching only in the step that needs it is the stronger fix, because masking is not a security boundary.
Exercise
Your team stores a Vault token as a repository secret so that jobs can fetch other secrets from Vault. What problem remains, and what is the usual fix?
Show answer
The Vault token is itself a long-lived secret stored in the CI platform, so a leak of that token exposes everything it can read. The usual fix is to have the job authenticate with a short-lived identity issued through OIDC federation instead of a stored token.