Sub topic 7
Exact matching, wildcards, and why the choice matters
This chapter assumes you can read a JSON IAM trust policy, know that a GitHub Actions OIDC token carries a sub claim, and understand how a role is assumed with sts:AssumeRoleWithWebIdentity. Here you will learn how the condition operator you pick decides how much trust the role grants.
Two operators, two levels of trust
AWS evaluates the Condition block of a trust policy against the claims in the token. For the sub claim, two operators matter most:
| Operator | Matching rule | Typical use |
|---|---|---|
| `StringEquals` | The claim must equal the value exactly. | One repository and one branch, or one environment. |
| `StringLike` | The value may contain `*` (any run of characters) and `?` (one character). | A family of branches or tags. |
With StringEquals, a * in the value is an ordinary character. It does not act as a wildcard. People often forget this and are surprised when a pattern that looks permissive matches nothing.
An exact match
This condition accepts one subject only. Any other repository, branch, or environment is rejected.
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main"
}
}
}The failure mode is easy to see. If someone renames the branch to trunk, the deployment stops working. That failure is visible and safe, and you fix it by editing the policy.
A wildcard match
Now suppose releases are cut from branches such as release/1.0 and release/2.0. An exact match would need a policy edit for every new branch. A wildcard avoids that:
{
"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 refs/heads/release/1.0, refs/heads/release/hotfix, and any other branch whose name starts with release/. The role now trusts every branch someone can create under that prefix. If any contributor can push branches to the repository, any contributor can obtain the role's permissions. The wildcard trades convenience for a wider trust boundary.
Choosing between them
Use these questions to decide:
- Can you list every subject that should have access? If yes, use
StringEqualsand list them. - Who can create the things the wildcard would match? If any contributor can create a matching branch or tag, the wildcard is open to them.
- What can the role do? A role that only reads a test bucket tolerates a wider pattern than one that deploys to production.
- Is the wildcard at the end of a segment? Put it as far right as possible, and never in the repository part of the subject.
Wildcards inside a segment
A wildcard can match more than you intend. Compare these values:
repo:example-org/example-repo:ref:refs/heads/release/*matches only branches underrelease/.repo:example-org/example-repo:*matches branches, tags, pull requests, and environments.repo:example-org/example-repo*also matches a repository namedexample-repo-forkorexample-repo-sandboxin the same organization.
The last pattern is a common slip. Without the colon after the repository name, the wildcard continues into the repository name itself. Always keep a literal : after the repository.
Exercise
A trust policy uses StringLike with the sub value repo:example-org/example-repo*. Name one repository that this pattern would unintentionally trust, and write the corrected value.
Show answer
It would also trust example-org/example-repo-sandbox (or any repository whose name starts with example-repo). The corrected value keeps a literal colon after the repository name, for example repo:example-org/example-repo:ref:refs/heads/main with StringEquals, or repo:example-org/example-repo:* with StringLike if every subject in that one repository is acceptable.