Sub topic 7
Restricting access by repository, branch, and environment
This chapter assumes you have registered the GitHub OIDC provider in IAM and written a basic trust policy. You should also know how the aud and sub claims appear in the token.
Why the sub condition matters
The IAM OIDC provider trusts every token that GitHub signs, including tokens from repositories you don't own. Without a sub condition, any GitHub workflow could assume your role if it knew the role ARN. The sub condition is what ties the role to your code.
How GitHub builds the sub claim
GitHub builds sub from the workflow's context. The format depends on how the job runs:
| Job context | `sub` value |
|---|---|
| Push to a branch | `repo:my-org/my-repo:ref:refs/heads/main` |
| Push of a tag | `repo:my-org/my-repo:ref:refs/tags/v1.0.0` |
| Pull request | `repo:my-org/my-repo:pull_request` |
| Job that uses an environment | `repo:my-org/my-repo:environment:production` |
When a job declares an environment:, GitHub puts the environment in sub and leaves out the branch. A policy that matches only ref:refs/heads/main will therefore reject that job.
Matching exactly or with wildcards
Use StringEquals for an exact match. Use StringLike when you need * or ?. This policy lets any branch under release/ assume the role:
{
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:ref:refs/heads/release/*"
}
}To allow several exact subjects, give the condition key a list. IAM treats the values as OR:
{
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": [
"repo:my-org/my-repo:ref:refs/heads/main",
"repo:my-org/my-repo:environment:production"
]
}
}Using environments as a gate
A branch condition only checks which branch started the job. An environment condition adds GitHub's protection rules, such as required reviewers and wait timers, because the job can't start until they pass. Pin production roles to the environment:
{
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:environment:production"
}
}The workflow must then name the environment:
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write
contents: readCommon mistakes
- Branch pattern with an environment job. The
subcontains the environment, so the branch pattern never matches. - Pull request jobs. The
subisrepo:org/repo:pull_requestand doesn't include the branch name. Give pull request jobs a separate role with read-only permissions. - Case differences. Repository names in
subkeep their original capitalization, and the comparison is case-sensitive.
Exercise
A deploy job sets environment: production and runs on a push to main. The role's trust policy allows only repo:my-org/my-repo:ref:refs/heads/main. Will the role assumption succeed? If not, what is the smallest fix?
Show answer
It will fail. When a job uses an environment, sub is repo:my-org/my-repo:environment:production, which doesn't match the branch pattern. Add that environment subject to the trust policy, either as a second value in a list or by replacing the branch pattern.