Sub topic 12
Replacing Stored Secrets with Short-Lived Credentials
This chapter assumes you can read a GitHub Actions workflow, know what a GITHUB_TOKEN is, and have used an IAM role or an equivalent cloud identity. It uses AWS in the examples. Google Cloud, Azure, and HashiCorp Vault support the same pattern with different setup steps.
Why stored credentials are a problem on runners
A stored secret is a value you create once and keep, such as an AWS access key in a repository secret. It stays valid until someone rotates it, and rotation is usually forgotten. Anyone who copies it can use it from anywhere until then.
Earlier chapters showed how persistent runners leak state between jobs. Long-lived keys make that worse in three ways:
- A job that writes the key to disk, to a log, or to a build cache leaves a usable credential behind.
- A compromised job can send the key to an outside server. The attacker keeps it long after the runner is gone.
- A key stored in a secret is available to every workflow that can read that secret. It is not tied to one branch, environment, or workflow file.
Ephemeral runners limit how long the machine lives. They do nothing about a credential that outlives the machine. The fix is to stop storing cloud keys. The job instead proves who it is at run time and receives credentials that expire within minutes or hours.
Three kinds of credentials on a runner
Short-lived credentials already exist in GitHub Actions. It helps to separate the three kinds a job can hold.
| Credential | Created by | Lifetime | Scope |
|---|---|---|---|
| Runner registration token or JIT config | Your runner provisioner | Registration token: about one hour. JIT config: used once. | Lets one machine join GitHub as a runner |
| `GITHUB_TOKEN` | GitHub, per job | Expires when the job ends, at most 24 hours | The repository, limited by the `permissions` block |
| OIDC ID token | GitHub's OIDC provider, on request | Minutes | A signed identity statement for one job run |
| Cloud session credentials | Your cloud provider, in exchange for the ID token | Typically one hour for AWS by default | Whatever the assumed role allows |
Only the last two are new here. The first two follow the same rule: nothing is issued before it is needed, and nothing outlives its purpose.
How OIDC works for a job
OIDC stands for OpenID Connect. GitHub acts as an identity provider. A job asks GitHub for a signed JSON Web Token (JWT) that describes the job. The cloud provider has been told to trust tokens signed by GitHub. It checks the signature and the claims, then returns temporary credentials.
No secret is stored at any step. The only thing the job holds is its own run-time identity.
What the token says
The JWT is issued by https://token.actions.githubusercontent.com. These claims matter most for trust decisions:
aud: the audience, who the token is meant for. For AWS this issts.amazonaws.com.sub: the subject, which identifies the job's context. With default settings it takes forms likerepo:OWNER/REPO:ref:refs/heads/main,repo:OWNER/REPO:pull_request, orrepo:OWNER/REPO:environment:production.repository,ref,workflow,job_workflow_ref, andenvironment: further detail about where the job came from.
Your cloud trust policy is where you turn these claims into access rules.
Setting it up on AWS
There are two pieces on the AWS side. First, create an IAM OIDC identity provider for https://token.actions.githubusercontent.com, with sts.amazonaws.com as the audience. Second, create a role whose trust policy accepts tokens only from the contexts you intend.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:OWNER/REPO:ref:refs/heads/main"
}
}
}
]
}The sub condition is the important part. Only jobs running on the main branch of OWNER/REPO can assume this role. A pull request job has a different sub, so it is refused.
Then the workflow requests the token and uses it. This is the deploy example from the plan:
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: [self-hosted, linux, x64, ephemeral]
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/example-deploy-role
aws-region: us-east-1
- run: aws sts get-caller-identityid-token: write lets the job request an ID token. It does not grant write access to anything in the repository. The action requests the token, calls STS, and exports the temporary credentials as environment variables for later steps. The last step prints the assumed role, which confirms the exchange worked.
Writing trust policies that hold up
A trust policy that checks only aud accepts tokens from any repository on GitHub. This is the most common mistake. Anyone can create a workflow that requests a token with that audience. Always pin sub, or pin claims like repository and job_workflow_ref.
Choose the narrowest pattern that fits the use:
| Goal | `sub` condition |
|---|---|
| Deploy only from main | `repo:OWNER/REPO:ref:refs/heads/main` |
| Deploy only through a protected environment | `repo:OWNER/REPO:environment:production` |
| Read-only access for tests | A separate role with read-only permissions and its own `sub` |
| Any branch of one repository | `StringLike` with `repo:OWNER/REPO:*`. Use this with care, since pull request jobs match too. |
Environments add a gate. If the deploy job targets an environment with required reviewers, the job does not start until a person approves. The sub then ends in environment:production, and the role refuses anything else. Branch rules and review rules end up controlling the cloud access.
Separate roles for separate jobs keep each role small. A test job should not hold the same role as a deploy job. If a test job on a pull request is compromised, it can reach only what the test role allows.
How this fits with ephemeral runners
OIDC and ephemeral runners cover different gaps, so use both.
- Ephemeral runners remove leftover files, caches, and processes after one job.
- OIDC credentials expire on their own. Even if a job exfiltrates them, they stop working soon after.
- Trust policies limit what any single job can reach, no matter which runner it ran on.
Check two other places where credentials hide on a self-hosted runner:
- The host's own cloud identity. If the runner runs on an EC2 instance with an instance profile, every job on that machine can reach the instance metadata service and use that role. Keep the instance role minimal, or require IMDSv2 with a hop limit of 1 so containers on the host cannot reach it. Where possible, give the host no meaningful permissions and let jobs assume their own roles through OIDC.
- Files written by earlier steps. Credentials exported by
configure-aws-credentialsare written to the job's environment, not to a long-lived file. Avoid tools that cache them in~/.aws. On an ephemeral runner that cache is discarded, but on a persistent runner it is not.
Common errors
- "Not authorized to perform sts:AssumeRoleWithWebIdentity". The token's
suborauddoes not match the trust policy. Print the claims during setup (decode the JWT in a throwaway job) and compare them with the policy text. Check branch names, sincemainandmasterproduce different subjects. - "Credentials could not be loaded" or a missing token error. The job lacks
id-token: write, or the workflow runs from a fork where that permission is withheld. - A role that works in one repo but not in a reusable workflow. The
subreflects the caller by default. If you want to trust a specific shared workflow, match on thejob_workflow_refclaim. - A trust policy that is too broad.
repo:OWNER/*lets every repository in the organization assume the role. Test by running the job from a different repository and confirming it is denied. - Session expiry in long jobs. Temporary credentials last for the role's session duration. A job that runs longer must refresh them, for example by running the credentials action again.
Summary of the pattern
- Remove cloud access keys from repository and organization secrets.
- Create the OIDC provider and one role per purpose in your cloud account.
- Pin
audandsubin each trust policy. - Add
id-token: writeonly to the jobs that need it, withcontents: readand nothing more. - Run those jobs on ephemeral runners from restricted runner groups.
- Keep the runner host's own cloud identity minimal.
Exercise
A trust policy for a deploy role checks only that token.actions.githubusercontent.com:aud equals sts.amazonaws.com. Why is this unsafe, and what change fixes it? Name one claim you would add and the sub value for deploys from the main branch of OWNER/REPO.
Show answer
The audience is the same for every GitHub Actions token, so any workflow in any repository on GitHub can obtain a token with that aud and assume the role. Add a sub condition so the role accepts only your repository and context. For deploys from main the value is repo:OWNER/REPO:ref:refs/heads/main. Alternatively or additionally, pin the repository or job_workflow_ref claim.
Exercise
Your deploy workflow sets permissions: id-token: write and the checkout step now fails on a private repository. What is the cause and the fix?
Show answer
Declaring any permission in the block sets all unlisted permissions to none, so the job lost contents: read, which checkout needs. Add contents: read to the permissions block next to id-token: write.
Exercise
Your runners are EC2 instances with an instance profile that allows writing to a production S3 bucket. Jobs also use OIDC roles. Why does the instance profile undermine the OIDC setup, and what two steps reduce the risk?
Show answer
Any job on the host can read the instance metadata service and use the instance role, bypassing the per-job trust policies. Reduce the instance profile to the minimum the runner software needs, and require IMDSv2 with a hop limit of 1 so containerized jobs cannot reach it. Jobs then obtain their cloud access only through OIDC roles.