Sub topic 9
Trust relationships and conditions
This chapter assumes you know what a GitHub OIDC token contains (the iss, aud, and sub claims) and how a workflow requests one. It also assumes you have a general idea of IAM roles and policies. Here we cover the cloud side: the configuration that decides whether a valid token is allowed to do anything.
What a trust relationship is
A valid token only proves that GitHub issued it. It does not prove that your cloud account should accept it. The trust relationship is the cloud-side setup that closes this gap. It has two parts:
- Provider registration. You tell the cloud account that tokens from a specific issuer,
https://token.actions.githubusercontent.com, are a recognized source of identities. In AWS this is an IAM OIDC identity provider. - Role trust policy. You attach conditions to a role that say which tokens from that issuer may assume it.
Registration is a one-time, account-wide step. The trust policy is where most of your security decisions happen, and you write one per role.
aws iam create-open-id-connect-provider \
--url https://token.actions.githubusercontent.com \
--client-id-list sts.amazonaws.comThe --client-id-list value is the set of audiences AWS accepts for this provider. AWS validates GitHub's signing keys against its own library of trusted certificate authorities, so you don't need to supply a thumbprint for GitHub. Older CLI versions may still demand --thumbprint-list.
Two policies, two questions
Every role has two separate policies:
| Policy | Question it answers | Evaluated |
|---|---|---|
| Trust policy (assume-role policy) | Who may become this role? | When the workflow calls `AssumeRoleWithWebIdentity` |
| Permissions policy | What can the role do once assumed? | On every API call made with the role's credentials |
A common mistake is to spend all effort on the permissions policy and leave the trust policy loose. Even a narrowly permissioned role can be abused if any repository can assume it. Both policies must be tight.
Anatomy of a trust policy
Here is the policy from the chapter's running example:
{
"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"
}
}
}
]
}Each part has a job:
Principal.Federatednames the registered identity provider. Only tokens from that issuer are even considered.Actionissts:AssumeRoleWithWebIdentity, the call that exchanges a JWT for temporary credentials. This differs from the plainsts:AssumeRoleused with IAM principals.Conditioncompares token claims to the values you require. AWS exposes claims as condition keys named<provider-host>:<claim>, such astoken.actions.githubusercontent.com:sub.
The aud condition checks that the token was minted for AWS. The sub condition checks that it came from the workflow context you intend.
How AWS evaluates the conditions
The condition block follows normal IAM logic, and a few rules matter for OIDC:
- Different keys are combined with AND. In the example above, both
audandsubmust match. - Multiple values for one key are combined with OR. Supply an array, and any one match satisfies that key.
- A missing claim fails a positive match. If you test a claim the token doesn't carry,
StringEqualsandStringLikedo not match, so the request is denied. - The operator decides whether wildcards work.
StringEqualsis exact.StringLikesupports*and?.
| Operator | Matching | Typical use |
|---|---|---|
| `StringEquals` | Exact, case-sensitive | `aud`, and `sub` when you have one fixed value |
| `StringLike` | `*` and `?` wildcards | `sub` across several branches or tags |
| `ForAnyValue:StringLike` | Any listed pattern matches a multivalued key | Rarely needed for GitHub claims |
Here is a trust policy that accepts several subjects. The array gives OR semantics, and StringLike allows a wildcard for release tags:
{
"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"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": [
"repo:octo-org/octo-repo:ref:refs/heads/main",
"repo:octo-org/octo-repo:ref:refs/tags/v*"
]
}
}
}
]
}Keep the wildcard as narrow as you can. The pattern repo:octo-org/* would trust every repository in the organization, including ones created next year.
The exchange step by step
The diagram shows where the trust policy is consulted.
Signature, issuer, and expiry checks are generic to any OIDC token. The trust policy is the only step that is specific to your account, so it is the only step you control.
Subject formats and what they mean for conditions
The sub claim changes shape depending on what triggered the job. Your condition must match the shape that the workflow actually produces:
| Trigger context | Example `sub` |
|---|---|
| Push to a branch | `repo:octo-org/octo-repo:ref:refs/heads/main` |
| Push of a tag | `repo:octo-org/octo-repo:ref:refs/tags/v1.2.0` |
| Pull request | `repo:octo-org/octo-repo:pull_request` |
| Job using an environment | `repo:octo-org/octo-repo:environment:production` |
When a job declares an environment:, the subject uses the environment form, not the branch form. The branch is no longer in sub. This is why a policy written for ref:refs/heads/main stops matching once someone adds environment: production to the job.
Environments are often the better anchor for production roles. Protection rules on the environment, such as required reviewers or branch restrictions, then become part of the gate. A role trusted only for environment:production can be assumed only by jobs that passed those rules.
Pull requests deserve care. A pull_request subject does not include the branch name, and a wildcard such as repo:octo-org/octo-repo:* would match pull request jobs too. Whether a pull request workflow can obtain an OIDC token at all also depends on its origin (workflows from forks don't get write access to id-token). Don't rely on that as your only safeguard.
Why the `aud` condition is not enough
The audience says which service the token is meant for. It says nothing about who is asking. Only claims like sub, repository, or job_workflow_ref identify the caller, so at least one of them must appear in the conditions.
Testing a trust policy
You can check a policy without waiting for a workflow run:
- Open the failing or successful run and read the token claims (decode a token you requested, as shown in the earlier chapter, but never post a live token publicly).
- Compare each claim, character for character, with the value in your condition. Case and the
repo:prefix both matter. - After changing the policy, rerun the job. IAM changes usually propagate within seconds, but a rerun right after an edit can occasionally still fail.
If the call returns Not authorized to perform sts:AssumeRoleWithWebIdentity, a condition did not match, or the provider ARN in Principal doesn't match the registered provider. Troubleshooting is covered in more depth in a later chapter.
Design checklist
- Register the provider once per account, and create one role per purpose (deploy, read-only, release) rather than one shared role.
- Use
StringEqualsforaudand forsubwherever a fixed value works. - Anchor production roles to an environment, so its protection rules are enforced.
- Keep the permissions policy as narrow as the trust policy.
- Review wildcards in
subas you would a firewall rule. Each*widens who can assume the role.
Exercise
A role's trust policy has StringEquals on sub with the value repo:octo-org/octo-repo:ref:refs/heads/main. A developer adds environment: production to the deploy job, and the job now fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity". Why does it fail, and how would you fix the trust policy?
Show answer
When a job uses an environment, the sub claim changes to repo:octo-org/octo-repo:environment:production, so the old branch-based value no longer matches. Update the sub condition to the environment form (or list both values in an array if both kinds of job need access).
Exercise
Your trust policy has an aud condition set to sts.amazonaws.com and no sub condition. Who can assume the role, and why is that a problem?
Show answer
Any workflow in any GitHub repository can request a token with that audience, and it will pass signature, issuer, and audience checks. Without a sub (or similar) condition, nothing ties the token to your repository, so outsiders could assume the role.