Sub topic 10
Authenticating to the cloud with OIDC
This chapter assumes you know how secrets, GITHUB_TOKEN permissions, and environments work from earlier chapters. It uses AWS in its examples, but other clouds follow the same pattern.
Why long-lived keys are a problem
A common way to deploy is to create an IAM user, generate an access key, and paste it into a repository secret. That key does not expire. If it leaks through a log, a fork, or a compromised action, an attacker can use it from anywhere until you notice and rotate it.
OpenID Connect (OIDC) removes the stored key. Instead, each job asks GitHub for a short-lived signed token. The cloud provider checks the token and hands back temporary credentials that expire, usually within an hour.
How the exchange works
GitHub acts as an identity provider. The token it issues is a JSON Web Token (JWT), which carries claims about the run: the repository, the branch, the environment, and the workflow. Your cloud account is configured to trust GitHub as an identity provider and to accept only tokens whose claims match a rule you write.
Setting up the cloud side
In AWS you do two things once per account:
- Add GitHub as an OIDC identity provider, with the URL
https://token.actions.githubusercontent.comand the audiencests.amazonaws.com. - Create an IAM role whose trust policy allows
sts:AssumeRoleWithWebIdentityonly for tokens from your repository.
The trust policy condition is the important part. This one limits the role to jobs that run in the production environment of one repository:
{
"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:example-org/example-app:environment:production"
}
}
}
]
}The sub (subject) claim identifies who is asking. When a job uses an environment, the subject has the form repo:OWNER/REPO:environment:NAME. Without an environment it names a branch, such as repo:OWNER/REPO:ref:refs/heads/main.
Setting up the workflow side
The job needs the id-token: write permission so it can request the token. Setting permissions on a job replaces the defaults, so also list contents: read if the job needs to check out code.
deploy:
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
aws-region: us-east-1
- run: aws sts get-caller-identityThe action requests the token, calls AWS, and exports temporary credentials as environment variables for the later steps. The role ARN is not a secret, so you can store it in a variable or write it in the file.
Common errors
- "Credentials could not be loaded" or a missing token error. The job lacks
id-token: write. - "Not authorized to perform sts:AssumeRoleWithWebIdentity". The
subclaim does not match the trust policy. A job that declaresenvironment: productionsends an environment subject, not a branch subject, so a policy written forref:refs/heads/mainwill reject it. - Overly broad trust. A condition like
repo:example-org/*lets every repository in the organization assume the role. Match one repository and, for production, one environment.
Exercise
A deploy job fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity". The job declares environment: production, and the role's trust policy requires the subject repo:example-org/example-app:ref:refs/heads/main. What is wrong, and how do you fix it?
Show answer
Jobs that declare an environment send a subject of the form repo:example-org/example-app:environment:production, so the branch-based condition never matches. Change the trust policy's sub condition to the environment subject (or use a StringLike condition that lists both subjects if you need both).