Sub topic 10
Replacing stored cloud keys with OIDC and short-lived credentials
This chapter assumes you can read a GitHub Actions workflow, know what an IAM role is, and have used environment variables in a shell. It uses GitHub Actions and AWS as the example, but other CI systems and clouds follow the same model.
Why stored cloud keys are a problem
A long-lived access key saved as a repository secret works until it leaks. Once copied, it stays valid until someone notices and revokes it. It also works from anywhere, not only from your pipeline.
OpenID Connect (OIDC) federation removes the stored key. The CI platform issues a short-lived signed token for each job. The cloud provider checks that token and hands back temporary credentials. Nothing long-lived sits in your secret store.
How the exchange works
The token is a JSON Web Token (JWT), a signed set of claims. Two claims matter most:
audis the audience, the service the token is meant for. For AWS it issts.amazonaws.com.subis the subject. It identifies the repository and the context, such as a branch or environment.
AWS trusts the token because GitHub signed it and AWS has registered GitHub as an identity provider. The temporary credentials expire, by default after one hour.
The workflow side
The job must be allowed to request a token. That is the id-token: write permission.
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/deploy-role
aws-region: us-east-1
- run: aws sts get-caller-identitySetting permissions at the workflow level drops every other permission, so contents: read is listed explicitly. The configure-aws-credentials action requests the token, calls AWS, and exports temporary credentials to later steps. The last step prints which role the job is running as. That is a quick check that the exchange worked.
The cloud side: the trust policy
The role's trust policy decides who may assume it. This is where you limit the damage if a token is misused.
{
"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:my-org/my-repo:environment:production"
}
}
}
]
}The sub condition allows only workflows from my-org/my-repo that run in the production environment. Pair this with environment protection rules, such as required reviewers, so that reaching that environment needs approval.
Common errors
- Missing
id-token: write. The job fails with an error saying the OIDC token request variables are not set. Add the permission to the workflow or job. - Trust policy too broad. A condition like
repo:my-org/*lets every repository in the organization assume the role. Match the exact repository and, where possible, the environment or branch. - Missing
audcondition. Without it, a token meant for another service might be accepted. - Using
StringLikewith a loose wildcard. UseStringEqualsunless you need a pattern, and keep patterns narrow. - Subject mismatch. The
subformat changes with context. A job with anenvironment:key producesenvironment:production, notref:refs/heads/main. If assumption fails, compare the policy with the actual claim. - Over-permissioned role. OIDC proves who is asking. It does not limit what the role can do. Attach a least-privilege permissions policy.
What you gain
| Property | Stored access key | OIDC federation |
|---|---|---|
| Lifetime | Until revoked | Minutes to hours |
| Stored in CI | Yes | No |
| Tied to a repo or environment | No | Yes, through `sub` |
| Rotation needed | Yes | No |
Exercise
A workflow fails at the AWS credentials step with an error that the role cannot be assumed. The job uses environment: staging, and the trust policy has the sub value repo:my-org/my-repo:environment:production. What is wrong, and how do you fix it without widening access more than needed?
Show answer
The token's sub claim is repo:my-org/my-repo:environment:staging, which does not match the policy. Either create a separate staging role with a trust policy for environment:staging, or add that exact value to the condition as a list. Do not replace it with a wildcard such as repo:my-org/*.
Exercise
Why does adding permissions: id-token: write without contents: read break actions/checkout on a private repository?
Show answer
Setting permissions explicitly gives every unlisted permission the value none. Without contents: read, the checkout step cannot read the repository, so list both.