Sub topic 5
Writing a Role Trust Policy
This chapter assumes you know what a JSON Web Token (JWT) is, can read GitHub Actions workflow YAML, and understand that an AWS IAM role has a permissions policy and a trust policy. Earlier chapters covered the token's aud and sub claims. Here you combine them into a trust policy that decides which workflows can assume a role.
Two policies on every role
An IAM role carries two separate policies:
- The permissions policy says what the role can do once assumed, such as
s3:PutObjecton one bucket. - The trust policy says who can assume the role at all.
The trust policy is the security boundary for OIDC federation. If it is too broad, any workflow it matches receives the role's permissions, and the permissions policy cannot prevent that. A careful permissions policy does not make up for a loose trust policy.
For OIDC, the trust policy is a resource-based policy attached to the role. It names the OIDC identity provider as the principal and uses conditions to test the claims in the token.
The parts of a trust policy
| Element | Value for GitHub Actions | Purpose |
|---|---|---|
| `Effect` | `Allow` | Grants the assumption when every condition is true. |
| `Principal` | `{"Federated": "<OIDC provider ARN>"}` | Says the caller is an identity from the registered provider. |
| `Action` | `sts:AssumeRoleWithWebIdentity` | The AWS Security Token Service (STS) call that exchanges a JWT for temporary credentials. |
| `Condition` | Tests on `aud` and `sub` | Narrows the provider's identities to the workflows you intend. |
The Principal is the weakest part on its own. It matches every token that GitHub signs for any repository in the world, so the conditions do all the real restricting.
Condition keys for an OIDC provider use the provider's URL without https://, followed by a colon and the claim name. For GitHub the keys are token.actions.githubusercontent.com:aud and token.actions.githubusercontent.com:sub. AWS reads these claims from the validated token. You don't need a condition to check the token's signature, because STS verifies that itself.
A first complete policy
This policy lets only workflows running on the main branch of example-org/example-repo assume the role, and it requires the audience to be sts.amazonaws.com.
{
"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"
}
}
}
]
}Read it from the top down:
Federatedpoints at the IAM OIDC provider resource in account123456789012. This resource must already exist. It is created once per account for GitHub's issuer URL,https://token.actions.githubusercontent.com.sts:AssumeRoleWithWebIdentityis the only action allowed. Do not usests:AssumeRolehere. That action is for IAM principals, not web identities.- The
audcondition makes sure the token was minted for AWS STS and not for some other service that also accepts GitHub tokens. - The
subcondition pins the repository and the branch. The stringrepo:example-org/example-repo:ref:refs/heads/mainis the exact value GitHub puts in thesubclaim for a run onmain.
Within a single StringEquals block, the keys are combined with logical AND. Every listed condition must hold. If you give one key a list of values, those values are combined with OR. That gives you a way to allow several exact subjects:
"token.actions.githubusercontent.com:sub": [
"repo:example-org/example-repo:ref:refs/heads/main",
"repo:example-org/example-repo:ref:refs/heads/hotfix"
]What happens at assumption time
The diagram shows where each check happens. The trust policy is evaluated at step 6, after STS has already verified the token.
Failures at step 4 and failures at step 6 both appear as an access error, but they have different causes. A signature or expiry failure points to a missing provider or a stale token. A condition failure means your claim values and your policy disagree.
Creating the role from the policy
Save the policy as trust-policy.json and create the role with it. The permissions come from a separate policy that you attach afterward.
aws iam create-role \
--role-name example-deploy-role \
--assume-role-policy-document file://trust-policy.json
aws iam attach-role-policy \
--role-name example-deploy-role \
--policy-arn arn:aws:iam::123456789012:policy/example-deploy-permissionsTo change who can assume the role later, use aws iam update-assume-role-policy with the same file format. This replaces the whole trust policy, so edit the full document and don't send a fragment.
Why both conditions are needed
The two conditions answer different questions:
audanswers: was this token meant for AWS?subanswers: which repository, and which branch, tag, pull request, or environment, produced it?
Removing either one leaves a gap. A missing sub is the more serious gap, because it opens the role to everyone.
Matching operators in the trust policy
The first policy uses StringEquals, which requires an exact, case-sensitive match. If you need a pattern, 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 accepts any branch under release/. The trailing * matches any characters, including more slashes, so a branch named release/anything/else also passes. Keep wildcards as far to the right and as narrow as you can, and never place one before the repository name. Later chapters cover the risks of wildcards in depth.
Use StringEquals for the audience in every policy. The audience has one correct value, so there is no reason to allow a pattern.
Pairing the policy with the workflow
The trust policy only has an effect if the workflow can obtain a token and present it. The workflow needs the id-token: write permission, and it names the role to assume:
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 does not grant any access to repository contents or to AWS. It only lets the job request a token from GitHub. The contents: read line is needed because declaring permissions removes all unlisted defaults, and actions/checkout must still read the repository. The final step prints the assumed role's ARN, which is a quick way to confirm the exchange worked.
Because this workflow runs on a push to main, the sub claim is repo:example-org/example-repo:ref:refs/heads/main. It matches the trust policy above. If you change the trigger to pull_request, the sub changes to repo:example-org/example-repo:pull_request, and the assumption fails.
Troubleshooting common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Not authorized to perform sts:AssumeRoleWithWebIdentity` | The `sub` in the token does not equal the policy value. | Print the actual `sub` and compare it character by character. |
| Same error after the workflow works on another branch | The trigger or branch differs from the one in the policy. | Match the trigger. A pull request or tag run has a different `sub`. |
| `No OpenID Connect provider found in your account` | The `Principal` ARN is wrong, or the provider was never created. | Create the provider for `https://token.actions.githubusercontent.com` and copy its ARN exactly. |
| `Credentials could not be loaded` from the action | The job lacks `id-token: write`. | Add the permission at workflow or job level. |
| Policy rejected when saved | A malformed condition key, or a missing `Version`. | Use the `token.actions.githubusercontent.com:` prefix and `"Version": "2012-10-17"`. |
The most frequent mistake is a one-character difference in the repository name, a branch name, or the ref:refs/heads/ prefix. Copy the real value from a failing run instead of typing it from memory.
Exercise
A role's trust policy has StringEquals on token.actions.githubusercontent.com:aud set to sts.amazonaws.com, and no condition on sub. Who can assume the role, and which single condition would you add so that only the release branch of example-org/example-repo can? Write the condition key and value.
Show answer
Any workflow on GitHub that can request a token with the audience sts.amazonaws.com can assume the role, including workflows in repositories owned by other organizations. Add a sub condition under StringEquals: key token.actions.githubusercontent.com:sub, value repo:example-org/example-repo:ref:refs/heads/release. The aud and sub conditions in the same block are combined with AND, so both must match.
Exercise
A workflow that triggers on pull_request fails with Not authorized to perform sts:AssumeRoleWithWebIdentity, while the same role works for pushes to main. The trust policy matches repo:example-org/example-repo:ref:refs/heads/main. Why does it fail?
Show answer
A pull request run produces a different subject claim, repo:example-org/example-repo:pull_request, which does not equal the branch subject in the policy. The policy is working as written. If pull requests should assume a role, create a separate role with a narrow permissions policy and a trust policy that matches the pull request subject, and do not widen the production role.