Sub topic 6
Writing the role trust policy
This chapter assumes you know basic IAM (roles, policies, principals), can read JSON, and have already registered token.actions.githubusercontent.com as an IAM OIDC identity provider. The goal is a role that only your workflow can assume.
What a trust policy does
Every IAM role has two kinds of policy. The permissions policy says what the role can do. The trust policy, also called the assume-role policy document, says who can become the role. When a workflow calls AWS STS (the Security Token Service) with its GitHub token, STS evaluates the trust policy. If the token fails any check, no credentials are issued.
For GitHub, the trust policy has three parts:
- Principal: the OIDC provider you registered. It accepts tokens from GitHub and nobody else.
- Action:
sts:AssumeRoleWithWebIdentity, the STS call that exchanges a web identity token for temporary credentials. - Condition: checks on claims inside the token. These checks decide which repository and branch are allowed.
The policy
{
"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:my-org/my-repo:ref:refs/heads/main"
}
}
}
]
}Replace 123456789012 with your account ID and my-org/my-repo with your repository.
How the conditions are evaluated
Condition keys are the provider's URL (without https://), a colon, and the claim name. All conditions must pass.
audmust equalsts.amazonaws.com. This is the audience, the party the token was issued for. It matches the client ID you gave the provider, and theconfigure-aws-credentialsaction requests this value by default.submust match the pattern. This is the subject, which names the repository and the ref or environment that started the job.
Common mistakes
- Using
StringEqualswith wildcards.StringEqualstreats*as a literal character. UseStringLikewhen the value contains a wildcard. - Leaving the provider prefix wrong. The condition key must be
token.actions.githubusercontent.com:sub, with nohttps://. - Wildcarding too much. A value like
repo:my-org/*lets every repository in the organization assume the role. Prefer one repository and one branch or environment per role. - Mismatched case or ref format. The
subvalue is case-sensitive. A branch isref:refs/heads/main, a tag isref:refs/tags/v1, and a pull request job ispull_request.
Exercise
A trust policy has the aud condition but its sub condition is "StringEquals": {"token.actions.githubusercontent.com:sub": "repo:my-org/*"}. Workflows in my-org/my-repo cannot assume the role. Why, and how do you fix it?
Show answer
StringEquals compares the text literally, so * is not a wildcard and no real subject matches. Move the sub condition under StringLike. Then narrow it, for example to repo:my-org/my-repo:ref:refs/heads/main, so that other repositories in the organization cannot assume the role.