Sub topic 15
Customizing the subject claim
This chapter assumes you know the default sub formats from the subject claim chapter and can write an AWS trust policy with StringEquals conditions. You also need admin rights on the repository or organization you want to change.
Why change the default
The default sub claim identifies the repository plus one context: a branch, a tag, an environment, or a pull request. That is enough for many setups. It falls short when you want the trust policy to check something the default value leaves out. Two common cases:
- You want a role to be assumable only from a specific reusable workflow, so you need
job_workflow_refin the subject. - You want the subject to survive a repository or organization rename, so you need immutable IDs instead of names.
A policy that matches on sub is easier to read and audit than one that combines sub with several other claim conditions. Cloud providers differ in which claims they let you test, and sub is the one they all support.
How the template works
GitHub builds a custom sub from a list of claim keys that you choose. Each key is a claim name from the token, such as repo, context, job_workflow_ref, repository_owner_id, or repository_id. The order of the list sets the order of the parts in the subject.
You can set the template at two levels:
| Level | Effect |
|---|---|
| Organization | Sets the default template for repositories in the organization. |
| Repository | Overrides the organization setting for that repository, or follows it with `use_default: true`. |
Both levels use the REST API. You set use_default to false and give a list in include_claim_keys.
Setting a template for one repository
This command uses the GitHub CLI and needs a token with admin access to the repository.
gh api --method PUT \
repos/octo-org/octo-repo/actions/oidc/customization/sub \
--input - <<'EOF'
{
"use_default": false,
"include_claim_keys": ["repo", "context", "job_workflow_ref"]
}
EOFTo read the current setting, send a GET to the same path. To go back to the default format, send {"use_default": true}.
For an organization, use orgs/octo-org/actions/oidc/customization/sub as the path. Repositories that have not set their own template then pick up the organization's template.
Update the trust policy first
Changing the template changes the sub value in every token the repository issues from then on. A trust policy that still expects repo:octo-org/octo-repo:ref:refs/heads/main will reject the new tokens. The workflow then fails with an assume-role error until the policy is updated.
A safe order of work:
- Apply the new template in a test repository, or in a branch-only workflow.
- Request a token and decode it. Read the exact
substring instead of guessing it from the key list. - Add the new value to the trust policy as an extra allowed value, using a list under
StringEquals. - Roll the template out, check that deployments work, then remove the old value from the policy.
Gotchas
- Template changes apply to new tokens only. A token already issued keeps its old
subuntil it expires. - A repository-level setting hides later organization changes. If a repository seems to ignore the organization template, check whether it has its own override.
- Some key combinations make the subject longer. Cloud providers can limit the length of a condition value, so check the limit for your provider.
- Names can be reused. If an organization deletes a repository and someone creates a new one with the same name, a name-based
submatches the new repository. Addingrepository_idorrepository_owner_idto the template closes that gap.
Exercise
Your trust policy allows only repo:octo-org/octo-repo:environment:prod. You change the repository template to ["repo", "context", "job_workflow_ref"], and the next deploy fails at the assume-role step. What caused it, and what do you do?
Show answer
The new template changed the sub value, so it no longer equals the string in the trust policy, and AWS rejects the token. Decode a token from the updated workflow to read the exact new sub, add that value to the policy's StringEquals list, confirm the deploy works, then remove the old value.