Sub topic 1
Why CI workflows use OIDC instead of stored cloud keys
Prerequisites
This chapter assumes you already know the following:
- Basic CI/CD concepts: workflows, jobs, runners, and branches.
- How a GitHub Actions workflow file is laid out in YAML.
- What an IAM role and a policy are in a cloud such as AWS.
- That a JSON Web Token (JWT) is a signed, base64-encoded set of claims.
The worked example uses GitHub Actions and AWS IAM. The same ideas apply to other CI systems and other clouds. Later chapters write the trust policies in detail. This chapter explains why the mechanism exists and how the pieces fit together.
The problem with stored cloud keys
A traditional pipeline deploys to the cloud with an access key pair. You create an IAM user, generate a key ID and secret, and paste both into the CI system's secret store. The workflow reads them as environment variables.
This works, but it has costs:
- The keys are long-lived. They stay valid until someone rotates or deletes them. Many teams never do.
- They can leak. A key can end up in a log, a build artifact, a forked workflow, or a compromised third-party action. After a leak, the attacker can use it from anywhere.
- They carry no context. The cloud sees only "this key was used". It cannot tell whether the call came from the main branch, a pull request, or a developer's laptop.
- They create rotation work. Every repository that holds a copy must be updated when the key changes.
- They are shared. One key often serves many repositories, so you cannot tell which one made a given call.
Keys are a secret that you must protect forever. OIDC replaces them with a proof that is created per run and expires in minutes.
How OIDC federation works
OpenID Connect (OIDC) is an identity protocol built on OAuth 2.0. In this setup, GitHub acts as an identity provider. It signs a JWT for each job that asks for one. The cloud acts as the relying party. It decides whether to trust that token.
Federation means the cloud trusts GitHub's identity system instead of holding a secret for each workflow. You set the trust up once:
- In AWS, create an OIDC identity provider for
token.actions.githubusercontent.com. This tells IAM which issuer to accept. - Create an IAM role with a trust policy. The policy states which tokens may assume the role.
- Attach permissions to the role for what the workflow needs to do.
At run time, the exchange looks like this:
Nothing secret is stored in the repository settings. The only values in the workflow are the role ARN and the region, and neither is a secret.
What makes the credentials safer
Three properties carry the security benefit.
- Short lifetime. The token is valid for a few minutes. The credentials STS returns last for a limited session, one hour by default for this call. A stolen credential expires soon.
- Per-run issuance. A token exists only because a specific job ran in a specific repository. It cannot be created outside a workflow run.
- Claims describe the caller. The token states which repository, branch, environment, and workflow produced it. The trust policy can require exact values, so the cloud makes a decision based on context.
The third property is the focus of this tutorial. Removing stored keys is only the first step. If the trust policy is loose, any workflow that can get a token may assume your role. OIDC gives you the tools for precise trust, but you have to write the conditions.
What the trust policy actually checks
When a job calls sts:AssumeRoleWithWebIdentity, STS performs these checks:
- The token's
iss(issuer) matches a registered OIDC provider. - The signature verifies against the keys the issuer publishes.
- The token has not expired.
- The role's trust policy allows the principal and the action.
- Every
Conditionin the policy matches the token's claims.
Steps 1 to 3 prove that GitHub issued the token. They say nothing about whether your repository issued it. GitHub signs tokens for every repository on the platform, including repositories you do not own. Steps 4 and 5 are the part you control, and they are what keeps strangers out.
Here is a trust policy that skips the claim checks:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity"
}
]
}This policy trusts the provider and nothing else. Any workflow in any GitHub repository can present a valid token and assume the role. It is the most common first mistake, and it is a serious one. A role that is "keyless" but open to everyone is worse than a guarded key.
The conditions fix this. This chapter previews the pair you will use in every policy:
token.actions.githubusercontent.com:audpins the audience, meaning who the token is meant for.token.actions.githubusercontent.com:subpins the subject, meaning which repository, branch, or environment is asking.
A first working example
The policy below accepts only jobs that run on the main branch of one repository, with the expected audience. You will study each part in later chapters.
{
"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-repo:ref:refs/heads/main"
}
}
}
]
}The workflow side needs two things: permission to request a token, and a step that exchanges it.
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
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 id-token: write permission lets the job request an OIDC token. Without it, the request fails, even if the trust policy is correct. The configure-aws-credentials action requests the token, calls STS, and exports the temporary credentials to later steps. The last step prints the assumed role, which confirms the exchange worked.
Trade-offs and limits
OIDC is not free of cost.
- More setup. You need an identity provider, a role, and a policy per trust boundary. A single IAM user is quicker to create.
- Errors are hard to read. A failed condition gives a generic "not authorized to perform sts:AssumeRoleWithWebIdentity" message. You must compare the token's real claims with your policy yourself.
- You depend on the issuer. If GitHub's OIDC endpoint is unavailable, jobs cannot get credentials.
- Trust is only as tight as the conditions. OIDC does not enforce least privilege on its own. A permissive policy leaves the role open to more callers than a key would.
- Not every target supports it. Some third-party services only accept static API keys. For those, you still store a secret.
For cloud deployments on AWS, Azure, or Google Cloud, these costs are small compared with the risk of long-lived keys.
Where this tutorial goes next
The rest of the tutorial builds from this base. You will read the claims inside the token, pin the audience, and learn how the sub claim encodes the repository, branch, environment, pull request, or tag. Then you will write policies that allow only one branch or one environment, compare exact and wildcard matching, add environment protection rules, and customize the sub template. Finally, you will learn how to test and audit the result.
Exercise
A team replaced its stored AWS access keys with OIDC. Their role's trust policy names the GitHub OIDC provider as the principal and allows sts:AssumeRoleWithWebIdentity, but it has no Condition block. Explain why this is unsafe, and name the two condition keys that should be added first.
Show answer
STS only verifies that GitHub signed the token. GitHub signs tokens for every repository on the platform, so a workflow in any repository, including one owned by an attacker, can present a valid token and assume the role. Conditions restrict this to the intended caller. Add token.actions.githubusercontent.com:aud set to sts.amazonaws.com to pin the audience, and token.actions.githubusercontent.com:sub to pin the repository and branch or environment.
Exercise
A deploy job fails with "Credentials could not be loaded" before reaching AWS, and the trust policy matches the token's claims exactly. The workflow has permissions: contents: read only. What is missing and why?
Show answer
The job needs id-token: write under permissions. Without it, the job cannot request an OIDC token from GitHub, so the credentials step has nothing to send to STS. The trust policy is never evaluated.