Sub topic 8
Allowing only the main branch
This chapter shows how to write a role trust policy that only workflow runs on main can satisfy. It assumes you know how a GitHub Actions workflow is structured, what an IAM role and trust policy are, and that a JWT carries claims. Earlier chapters covered the token's claims and the aud check. Here you combine them into a branch restriction and learn where it can fail.
What the branch restriction does
When a job requests an OIDC token, GitHub fills in the sub (subject) claim from the event that started the run. For a push to a branch, the subject has this shape:
repo:<owner>/<repo>:ref:refs/heads/<branch>For a run on main in example-org/example-repo, the subject is exactly:
repo:example-org/example-repo:ref:refs/heads/mainAWS evaluates the trust policy against the token's claims. If you require that exact string, a token minted on any other branch, in any other repository, or for any other event is refused. The branch restriction is therefore a string comparison on one claim, plus the audience check.
The trust policy
This policy trusts the GitHub OIDC provider registered in the account. It allows only sts:AssumeRoleWithWebIdentity, and it requires two conditions.
{
"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"
}
}
}
]
}Each part has a job:
Principal.Federatednames the OIDC provider. Without a matchingsubcondition, any repository on GitHub could present a token to this provider and satisfy it.Actionissts:AssumeRoleWithWebIdentity, the call used when you exchange a web identity token for credentials.- The
audcondition pins the audience tosts.amazonaws.com, so a token issued for a different relying party is rejected. - The
subcondition pins the repository and branch.
The condition keys are the provider hostname, a colon, then the claim name. Both conditions sit under StringEquals, and all conditions in a block must pass.
The workflow side
The trust policy only decides who may assume the role. The workflow must ask for a token and then perform the assumption.
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-identityid-token: write lets the job request an OIDC token. Without it, the credentials action fails before it reaches AWS. Setting permissions at the top level also resets every unlisted permission to none, which is why contents: read is listed for the checkout step.
This job has no environment: key. That detail matters, as the next section explains.
What the subject looks like for each event
The sub value depends on how the run was triggered, not on which file contains the workflow. This table shows the common cases for the default subject format.
| Trigger | Subject |
|---|---|
| Push to `main` | `repo:example-org/example-repo:ref:refs/heads/main` |
| Manual run (`workflow_dispatch`) selected on `main` | `repo:example-org/example-repo:ref:refs/heads/main` |
| Push to branch `feature/x` | `repo:example-org/example-repo:ref:refs/heads/feature/x` |
| Pull request | `repo:example-org/example-repo:pull_request` |
| Push of tag `v1.0.0` | `repo:example-org/example-repo:ref:refs/tags/v1.0.0` |
| Job that declares `environment: production` | `repo:example-org/example-repo:environment:production` |
Two rows deserve attention. A manual run started from main gets the same subject as a push, so the policy cannot tell them apart. And a job that declares an environment gets an environment subject even when the run is on main. The branch is no longer part of the subject, so the policy above will not match.
Exact match versus a wildcard
StringEquals compares the whole string and is case-sensitive. That makes it the safest choice for a single branch.
Sometimes you need several branches, for example every branch under release/. Use StringLike, which supports * and ?:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/release/*"
}
}
}This matches release/1.0 and release/hotfix/urgent, because * also matches slashes. It does not match release itself or main. Anyone who can push a branch named release/anything can now assume the role, so the wildcard's reach is the set of people who can create such branches. If you use a pattern like this, protect it with a branch ruleset that limits who can create release/* branches.
Keep the wildcard away from the repository part. A pattern like repo:example-org/*:ref:refs/heads/main trusts the main branch of every repository in the organization, including ones created later by people who should not have deploy access.
Making "main" really mean main
The trust policy proves a run happened on a branch named main. It does not prove the code on that branch was reviewed. Anyone who can push to main can change the workflow and deploy with the role. Pair the policy with repository settings:
- Protect
mainso changes need a pull request and at least one approval. - Block force pushes and branch deletion on
main. - Restrict who can bypass those rules.
- Use CODEOWNERS for
.github/workflows/so workflow changes need review by the right people.
Without these, the strict sub condition guards the door while the lock on the branch stays open.
Common mistakes
- Adding
environment:to the job and keeping a branch-only policy. The subject changes to the environment form, and the assumption fails with anAccessDeniederror. Either change the policy or change the workflow. Chapter "Allowing only a deployment environment" covers the other form. - Leaving out the
audcondition. The role then accepts tokens minted for other audiences. - Mixing up the claim name. The condition key is
token.actions.githubusercontent.com:sub, not...:subject. A condition on a key that never appears in the token never matches, so the role denies everyone. That is safe but confusing. - Wrong case or typo in the repository name.
StringEqualsis case-sensitive, and a renamed repository produces a new subject. - Using
StringLikeout of habit. If you mean one branch, useStringEquals. - Forgetting that other triggers share the branch. A push, a manual run, and a scheduled run on
mainall produce the same subject. If only pushes should deploy, enforce that in the workflow'son:block, since the trust policy cannot distinguish them.
Checking that it works
Run the workflow on main and read the output of aws sts get-caller-identity. It should show an assumed-role ARN that ends in a session name, not an IAM user. Then confirm the negative cases. Start the same workflow from a feature branch, using workflow_dispatch, and check that the credentials step fails with an access denied error. A policy you have only seen succeed has not been tested.
Exercise
A job in example-org/example-repo runs on a push to main, but it declares environment: production. The role's trust policy requires sub to equal repo:example-org/example-repo:ref:refs/heads/main. Does the role assumption succeed? Explain, and give one fix.
Show answer
It fails. A job that declares an environment gets the subject repo:example-org/example-repo:environment:production, not the branch form, so the StringEquals condition does not match. Fixes: change the trust policy's sub to the environment subject, or remove the environment: key from the job if you want the branch-based restriction.
Exercise
You change the trust policy's sub condition to use StringLike with repo:example-org/example-repo:ref:refs/heads/*. Name two kinds of runs that can now assume the role that could not before.
Show answer
Any branch in the repository can now assume the role, for example a push to feature/x or a manual run from any branch. Because * also matches slashes, nested branch names such as release/hotfix/urgent are included. To go back to one branch, use StringEquals with refs/heads/main.