Sub topic 13
Customizing the Subject Claim
This chapter assumes you can read a GitHub Actions workflow, write a basic AWS IAM trust policy, and know that an OIDC token is a signed JWT with claims. You should also know the default sub formats, such as repo:example-org/example-repo:ref:refs/heads/main and repo:example-org/example-repo:environment:production.
Why change the default
By default, GitHub builds sub from the repository and either the branch, tag, pull request, or environment. That works for most teams. It falls short when you want to trust something the default doesn't encode, or when you manage many repositories and want one predictable format.
Customization changes which claims GitHub joins together to form sub. The trust policy still compares a single string, so the policy and the template must change together.
Setting a template
You set a template per repository, or per organization, through the GitHub API. This call sets a repository-level template that contains only the repo and context claims:
gh api -X PUT repos/example-org/example-repo/actions/oidc/customization/sub \
-F use_default=false \
-f 'include_claim_keys[]=repo' \
-f 'include_claim_keys[]=context'With this template, the sub value becomes repo:example-org/example-repo:context:production for a job in the production environment. The context claim holds the environment name, branch ref, tag, or pull request reference, depending on what triggered the job. The branch-specific part of the default format is gone.
A matching trust condition looks like this:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:context:production"
}
}
}Any existing trust policy that expects the old format stops matching once the template changes. The workflow then fails with an Not authorized to perform sts:AssumeRoleWithWebIdentity error.
Using other claims instead
A custom sub is not always the best answer. The token also carries claims such as job_workflow_ref, which names the reusable workflow file that is running. AWS lets you match some of these claims in a trust policy if they are exposed as condition keys, and GitHub's sub template can include them. Putting them in sub is the portable approach, because every cloud provider supports checking sub.
For example, including job_workflow_ref lets you say "only this shared deploy workflow may assume the role", no matter which repository calls it.
Rules for safe customization
- Keep
repoin the template. Without it, asubvalue can look identical across repositories, and a trust policy cannot tell them apart. - Keep the policy on exact matching with
StringEqualswhere you can. - Change the template and the trust policy in the same rollout, and test in a non-production repository first.
- Remember that a repository template can be overridden by organization settings. Check which one is in effect.
Checking the result
Print the claims in a throwaway job, or review a failed assumption in AWS CloudTrail, to see the exact sub string. Then write the condition against that string.
Exercise
You set a template with only repo and context. A job on the production environment of example-org/example-repo requests a token. What is the sub value, and what happens to a trust policy that still expects repo:example-org/example-repo:environment:production?
Show answer
The sub is repo:example-org/example-repo:context:production. The old policy no longer matches, so the role assumption is denied until you update the condition to the new format.