Sub topic 6
Writing the role trust policy
A role trust policy decides who may assume an IAM role. For a GitHub Actions deployment, it is the control that keeps other repositories, other branches, and other people from getting your AWS access. This chapter assumes you have already registered the GitHub OIDC provider in IAM and that you know what the iss, aud, and sub claims in a GitHub token mean.
Trust policy versus permissions policy
An IAM role has two kinds of policy, and they answer different questions.
| Policy | Question it answers | Where it lives |
|---|---|---|
| Trust policy (assume role policy document) | Who may become this role? | On the role itself, exactly one |
| Permissions policy | What can the role do once assumed? | Attached to the role, any number |
The trust policy is a resource-based policy. Its Principal element is required, because the policy is about who is allowed in. Permissions policies come later in this tutorial. Here we only decide who can get a session.
The shape of the policy
A trust policy for GitHub has one statement with four parts: an effect, a principal, an action, and conditions.
{
"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"
}
}
}
]
}Each part does a specific job.
- Effect is
Allow. Trust policies normally only grant. ADenyis possible but rarely needed. - Principal uses the
Federatedkey and names the ARN of the OIDC provider you created. This says "tokens vouched for by this provider may be considered." It does not yet say which tokens. - Action is
sts:AssumeRoleWithWebIdentity. This is the STS call that exchanges an OIDC token for credentials. If you writests:AssumeRolehere, the call fails, because that action is for IAM principals and not for web identity tokens. - Condition is where the real restriction happens.
The provider ARN must match the provider in your account exactly. Replace 123456789012 with your own account ID.
Why conditions are not optional
The provider is shared infrastructure. GitHub signs tokens for every repository on the platform with the same issuer, https://token.actions.githubusercontent.com. If your trust policy has the correct principal and no conditions, a workflow in any GitHub repository, including one owned by a stranger, can present a valid token and assume your role.
Because of this, treat the aud and sub conditions as required. A trust policy for GitHub without a sub condition is a vulnerability, not a shortcut.
The condition keys
Condition keys for a web identity provider are the provider's URL, without https://, followed by a colon and the claim name.
token.actions.githubusercontent.com:audis the audience claim. Theconfigure-aws-credentialsaction requestssts.amazonaws.comby default, so this is the value to require.token.actions.githubusercontent.com:subis the subject claim, which identifies the repository and the context of the run.
Condition operators
The operator you choose changes how the value is compared.
| Operator | Behavior | Typical use |
|---|---|---|
| `StringEquals` | Exact, case-sensitive match. No wildcards. | `aud`, or an exact `sub` |
| `StringLike` | Case-sensitive match. `*` and `?` are wildcards. | `sub` with wildcards |
Use StringEquals whenever you do not need wildcards. It removes the risk of a wildcard matching more than you intended. The example above uses StringLike for sub only so that you can later widen it. With no wildcard in the value, it behaves like an exact match.
Subject claim formats
The sub value depends on what triggered the run. These are the common forms.
| Context | `sub` value |
|---|---|
| Push or manual run on a branch | `repo:my-org/my-repo:ref:refs/heads/main` |
| Job that uses an environment | `repo:my-org/my-repo:environment:production` |
| Pull request event | `repo:my-org/my-repo:pull_request` |
| Run on a tag | `repo:my-org/my-repo:ref:refs/tags/v1.0.0` |
Two details matter here. When a job references an environment, the sub uses the environment form and no longer contains the branch. A policy written for ref:refs/heads/main will reject that job. Also, pull request runs have a sub that does not name the branch, so a policy that only allows main rejects them. That is usually what you want, since pull requests can come from untrusted changes.
Widening and narrowing the match
You can allow several branches or all branches of a repository with wildcards.
{
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:*"
}
}This allows every run in my-org/my-repo, including pull requests. It is acceptable for a read-only role and risky for a deployment role.
To allow a specific list, give StringLike an array. The condition passes if any value matches.
{
"StringLike": {
"token.actions.githubusercontent.com:sub": [
"repo:my-org/my-repo:ref:refs/heads/main",
"repo:my-org/my-repo:ref:refs/heads/release/*"
]
}
}Be careful with a wildcard in the organization or repository part. A value like repo:my-org/* trusts every repository in the organization, including ones created later by people who should not deploy.
Creating the role
Save the policy to trust-policy.json, then create the role with the AWS CLI.
aws iam create-role \
--role-name github-deploy \
--assume-role-policy-document file://trust-policy.jsonTo change the policy on an existing role, use update-assume-role-policy.
aws iam update-assume-role-policy \
--role-name github-deploy \
--policy-document file://trust-policy.jsonThis replaces the whole trust policy. It does not merge with the old one.
How the check runs
When the workflow calls STS, AWS evaluates the trust policy against the token in a fixed order.
A failure at any step produces the same generic message, Not authorized to perform sts:AssumeRoleWithWebIdentity. You must work out which step failed by comparing the token's claims with your policy.
Common mistakes
- Wrong action. Using
sts:AssumeRoleinstead ofsts:AssumeRoleWithWebIdentity. - Mismatched provider ARN. A typo in the account ID or URL, or a provider that exists in a different account.
- Missing
audcondition. The role still works, but it accepts tokens meant for other audiences. - Branch-only
subwith environments. The job usesenvironment: production, so thesubhas changed shape. - Case differences.
StringEqualsandStringLikeare case-sensitive. A repository renamed fromMy-Repotomy-repoin your policy will not match. - Wildcard too wide.
repo:my-org/*orrepo:*grants far more than intended.
Exercise
A workflow job declares environment: production and runs on a push to main. The role's trust policy requires sub to equal repo:my-org/my-repo:ref:refs/heads/main, and the call to AssumeRoleWithWebIdentity is denied. Why, and what is the fix?
Show answer
When a job uses an environment, the sub claim becomes repo:my-org/my-repo:environment:production and no longer includes the branch ref. The condition therefore never matches. Change the sub condition to repo:my-org/my-repo:environment:production, or add both values as a list if some jobs use the environment and others do not.
Exercise
Why is a trust policy with the correct Principal and Action but no sub condition unsafe?
Show answer
The GitHub OIDC provider issues tokens for every repository on GitHub. Without a sub condition, any repository's workflow can present a valid token and assume the role.