Sub topic 9
Trust relationships and conditions
This chapter assumes you know what a GitHub OIDC token is and which claims it carries, including iss, aud, and sub. It also assumes you have a general idea of IAM roles and policies. Here you will see how a cloud provider decides whether to accept a token.
What a trust relationship is
A trust relationship is a rule on the cloud side that says "tokens from this identity provider may act as this role, under these conditions." GitHub does not grant access. It only signs a statement about the workflow. The cloud account decides what that statement is worth.
In AWS, the rule is the role's trust policy. It has three parts:
- Principal: the identity provider you trust, here GitHub's OIDC provider.
- Action:
sts:AssumeRoleWithWebIdentity, the call that exchanges a token for temporary credentials. - Condition: tests on the token's claims that must all pass.
A trust policy, line by line
{
"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:octo-org/octo-repo:ref:refs/heads/main"
}
}
}
]
}The Principal names the OIDC provider registered in your AWS account. The token's issuer must match that provider, and its signature must verify.
The aud condition checks that the token was minted for AWS. Without it, a token that GitHub issued for another service could be replayed here.
The sub condition checks who is asking. This value is repo:octo-org/octo-repo:ref:refs/heads/main, so only runs on the main branch of that repository can assume the role.
AWS joins the conditions with AND. Every one must pass.
How the decision is made
Matching subjects: StringEquals and StringLike
StringEquals requires an exact match. Use StringLike when you need a wildcard, for example to allow any branch:
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:octo-org/octo-repo:*"
}
}The pattern repo:octo-org/octo-repo:* matches pushes, pull requests, tags, and environments in that one repository. Anyone who can open a pull request or push a branch there can then assume the role. Prefer the narrowest value that still works.
Common mistakes
| Mistake | Effect |
|---|---|
| Condition on `aud` only | Any repository on GitHub can assume the role |
| Wildcard such as `repo:octo-org/*` | Every repository in the organization gets access |
| Subject written for a branch, but the job uses an environment | The `sub` value differs, so the request is denied |
| Provider ARN in `Principal` does not match the account's provider | Assumption fails before conditions are checked |
The third row matters most in practice. When a job declares environment: production, the subject becomes repo:octo-org/octo-repo:environment:production, not a branch reference. Write the condition to match the subject your job actually produces.
Exercise
A trust policy has only a StringEquals condition on the aud claim set to sts.amazonaws.com. Why is this unsafe, and which condition would you add?
Show answer
Every GitHub workflow can request a token with that audience, so any repository could assume the role. Add a condition on the sub claim, for example StringEquals on repo:octo-org/octo-repo:ref:refs/heads/main, so only that repository and branch qualify.