Sub topic 15
Customizing the subject claim
This chapter assumes you have read the earlier chapters on the token claims, the sub claim, and cloud trust policies. You should be able to read an AWS trust policy with aud and sub conditions and use the gh CLI or make a REST call with a token.
Why change the default subject
The default sub value is built from the repository and one piece of context, as the earlier chapter on the subject claim showed:
| Run context | Default `sub` |
|---|---|
| Job uses an environment | `repo:octo-org/octo-repo:environment:production` |
| Pull request event, no environment | `repo:octo-org/octo-repo:pull_request` |
| Branch push, no environment | `repo:octo-org/octo-repo:ref:refs/heads/main` |
| Tag push, no environment | `repo:octo-org/octo-repo:ref:refs/tags/v1.2.0` |
The default works for one repository with one deployment path. It starts to hurt in three situations:
- Many repositories, one role. An organization wants a shared role for every repository in it. The default
subhas the repository name inside it, so the policy needs a wildcard such asrepo:octo-org/*:*. A wildcard that wide trusts every current and future repository, including ones that should never deploy. - Renames and transfers. The default
subcontains the repository name. If a repository is renamed, transferred, or deleted and its name is reused, a trust policy that matches on the name may match a different repository. - Workflow identity. The default
subsays nothing about which workflow file is running. Any workflow onmaincan assume the role, including one added in a pull request that was merged without much scrutiny.
Customizing sub lets you move other claims into the one string your trust policy can match on. This matters because some providers can only match on aud and sub. Check your provider's documentation for the claims it can use in conditions.
How the customized subject is built
You choose a list of claim keys, called include_claim_keys. GitHub builds the sub value by joining the values of those keys, in the order you give, with colons. For most keys the key name is written before the value. Two keys are special:
repogives theowner/nameof the repository.contextgives the same environment, pull request, or ref information as the default format. It renders asenvironment:production,pull_request, orref:refs/heads/main.
So ["repo", "context"] reproduces the default format, and ["repo", "job_workflow_ref"] produces a subject like this:
repo:octo-org/octo-repo:job_workflow_ref:octo-org/octo-repo/.github/workflows/deploy.yml@refs/heads/mainOther keys you can include are claims such as repository_owner_id, repository_id, workflow_ref, event_name, environment, ref, and actor. The REST API reference lists the full set of accepted keys.
Customizing sub does not remove anything from the token. The claims such as repository and job_workflow_ref are still present as separate claims. Only the string in sub changes.
Where the setting lives
A setting can exist at two levels, and the repository level wins when it is set.
Setting use_default to true on a repository tells it to ignore the organization setting and use the default format. Setting it to false together with a list of keys makes the repository use its own list.
You configure this through the REST API. You need admin access to the repository, or to the organization for organization-level settings.
Reading the current setting
gh api /repos/octo-org/octo-repo/actions/oidc/customization/subA repository on the default format returns:
{
"use_default": true
}Setting an organization-wide format
The following call makes every repository in octo-org that has not overridden the setting use the subject repo:<owner>/<name>:context..., plus the identity of the workflow file.
gh api \
--method PUT \
/orgs/octo-org/actions/oidc/customization/sub \
--input - <<'EOF'
{
"include_claim_keys": ["repo", "context", "job_workflow_ref"]
}
EOFTo override a single repository with immutable IDs instead of names:
gh api \
--method PUT \
/repos/octo-org/octo-repo/actions/oidc/customization/sub \
--input - <<'EOF'
{
"use_default": false,
"include_claim_keys": ["repository_owner_id", "repository_id", "context"]
}
EOFTo go back to the default for that repository:
gh api \
--method PUT \
/repos/octo-org/octo-repo/actions/oidc/customization/sub \
--input - <<'EOF'
{
"use_default": true
}
EOFSending the body as JSON with --input keeps use_default a real boolean. The shortcut -f use_default=false would send the string "false".
Checking what the token now contains
After changing the setting, confirm the result before you edit a trust policy. This step decodes the payload of the token for the current job and prints only the claims you care about. The payload is not secret, but the full token is a credential, so the script never prints it.
name: show-sub
on: workflow_dispatch
permissions:
id-token: write
contents: read
jobs:
show:
runs-on: ubuntu-latest
steps:
- name: Print sub and aud
run: |
token=$(curl -sS \
-H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" \
| jq -r '.value')
echo "::add-mask::$token"
payload=$(echo "$token" | cut -d. -f2 | tr '_-' '/+')
case $(( ${#payload} % 4 )) in
2) payload="$payload==" ;;
3) payload="$payload=" ;;
esac
echo "$payload" | base64 -d | jq '{iss, aud, sub, job_workflow_ref}'Run the workflow manually and read the sub value in the log. Copy it exactly into your trust policy condition.
Updating the trust policy to match
The trust policy must change in step with the setting. If the organization uses ["repo", "context", "job_workflow_ref"], a sub condition for the deploy workflow on main in an environment looks like this:
{
"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",
"token.actions.githubusercontent.com:sub": "repo:octo-org/octo-repo:environment:production:job_workflow_ref:octo-org/octo-repo/.github/workflows/deploy.yml@refs/heads/main"
}
}
}
]
}Use StringEquals when you can name the exact value. Use StringLike only when a wildcard is needed, and keep the wildcard as far to the right as possible.
Gotchas and trade-offs
- Changing the format breaks existing roles. A role whose policy expects the old
subrejects new tokens at once. Plan the change: update the setting and the policy together, or add the newsubvalue as a second allowed value in the policy first, then change the setting, then remove the old value. - Organization settings affect many repositories. One API call can break the deploys of every repository that inherits the setting. Test on a single repository with a repository-level override first.
- Key order matters.
["repo", "context"]and["context", "repo"]give different strings. Treat the order as part of the contract with your trust policies. - Names versus IDs. Names are readable. IDs such as
repository_iddo not change when a repository is renamed. IDs are harder to review in a policy, so add a comment or tag in your infrastructure code that records which repository each ID belongs to. - Long values. A
submade from several long claims can reach limits on policy size or condition length in some providers. Keep the list short. - Not every provider reads every claim. If your provider can match only
subandaud, the customizedsubis the only way to restrict by workflow or by immutable repository ID.
Exercise
An organization sets include_claim_keys to ["repo", "job_workflow_ref"]. A repository inside it sets use_default to true. What format does a job in that repository get, and why?
Show answer
It gets the default format, such as repo:octo-org/octo-repo:ref:refs/heads/main. The repository-level setting wins over the organization setting, and use_default: true means "ignore the organization list and use the default format".
Exercise
You change a repository's sub from the default to ["repository_owner_id", "repository_id", "context"], and the next deploy fails with a "not authorized to perform sts:AssumeRoleWithWebIdentity" error. What is the likely cause, and how do you find the correct value for the policy?
Show answer
The role's trust policy still expects the old sub string, so the condition no longer matches. Run a workflow with id-token: write that decodes the token payload and prints sub, then copy that exact value into the policy's sub condition. Print only the decoded payload and never the whole token.