Sub topic 12
Replacing Stored Secrets with Short-Lived Credentials
This chapter assumes you can read a workflow file, know what an ephemeral runner is, and have a basic idea of IAM roles in a cloud provider. Earlier chapters showed that a persistent runner can leak state between jobs. Long-lived cloud keys make that leak worse, because a stolen key keeps working long after the job ends.
Why stored keys are a problem
A typical setup stores an access key and secret key as repository secrets. Every job that can read those secrets can use them, and so can anyone who gets code running in such a job. The keys stay valid until someone rotates them. Rotation is manual, so it is often skipped.
On a self-hosted runner the risk grows. A job can write the key to disk, to a cached file, or to a shell history. If the runner is reused, the next job can read it. Ephemeral runners remove the reuse path, but the stored key still exists in GitHub and still works from anywhere.
Short-lived credentials fix this by changing what you hold. Instead of a permanent key, the job receives a token that expires in minutes to hours and is limited to one role.
How OIDC works in a job
OIDC (OpenID Connect) lets GitHub act as an identity provider. A job asks GitHub for a signed token that says who it is: which repository, branch, and workflow. The cloud provider checks the signature and the claims in the token. If they match a trust rule you wrote, the provider returns temporary credentials.
No cloud key is stored anywhere. The only thing the runner holds is a credential that expires on its own.
The workflow
The job must be allowed to request the token. That is the id-token: write permission. Keep contents: read so checkout still works.
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-identityThe configure-aws-credentials action fetches the token, calls AWS, and exports the temporary credentials as environment variables for later steps. The last step prints the assumed identity, which confirms the exchange worked.
The trust policy is the real control
The workflow only names a role. AWS decides whether to allow it based on the role's trust policy. The key field is the sub claim, which encodes the repository and ref.
- Pin
subto a specific repository and branch, such asrepo:OWNER/REPO:ref:refs/heads/main. - Check the
audclaim equalssts.amazonaws.com. - Avoid wildcards such as
repo:OWNER/*. They let any repository in the organization assume the role.
A pull request from a fork gets a different sub value and, by default, no id-token access. A narrow trust policy keeps it that way.
Common errors
| Symptom | Likely cause |
|---|---|
| "Credentials could not be loaded" or the token request fails | The job lacks `permissions: id-token: write` |
| `Not authorized to perform sts:AssumeRoleWithWebIdentity` | The trust policy `sub` or `aud` does not match the workflow's repository and ref |
| Works on `main`, fails on a pull request | The `sub` claim differs for pull requests, so a branch-pinned policy rejects it |
| Checkout fails after adding permissions | Setting `permissions` removes defaults, so `contents: read` must be listed |
Combining with ephemeral runners
OIDC and ephemeral runners cover different gaps. The ephemeral runner ensures no job inherits files from another. OIDC ensures there is no permanent key to inherit or steal. Together, a compromised job can only use credentials that expire soon, for one role, from one run.
Give each role the least access its job needs. A test job should not assume the deploy role.
Exercise
A deploy job fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity". The workflow has id-token: write and the role ARN is correct. Name the first thing you check and why.
Show answer
Check the role's trust policy, specifically the sub and aud conditions. The token request succeeded, so the permission is fine. AWS rejected the token because its claims, such as the repository and branch in sub, did not match the policy. Compare the policy with the actual repository and ref that triggered the run.