Sub topic 12
Using other claims such as job_workflow_ref
This chapter assumes you can read a GitHub Actions workflow, know what a JWT is, and have written an AWS IAM trust policy with sub and aud conditions. The sub claim tells you which repository and ref or environment asked for access. It does not tell you which workflow file ran. Other claims in the token can fill that gap.
Why sub alone is sometimes not enough
Anyone who can push a workflow file to an allowed branch can request a token that has the same sub. If a repository has many workflows, a lint workflow and a deploy workflow produce the same subject on main. Both can assume the role.
Extra claims let you pin the role to one specific workflow file. This is useful when the deploy logic lives in a shared, reviewed workflow.
Useful claims
| Claim | What it holds | Typical use |
|---|---|---|
| `repository` | `owner/repo` | Pin the repository without parsing `sub` |
| `ref` | Full ref, such as `refs/heads/main` | Pin a branch or tag |
| `environment` | Deployment environment name | Pin an environment |
| `job_workflow_ref` | Path of the workflow file that runs the job, with its ref | Pin a specific workflow file |
| `workflow_ref` | Path of the top-level workflow file, with its ref | Pin the calling workflow |
| `runner_environment` | `github-hosted` or `self-hosted` | Reject self-hosted runners |
For a reusable workflow, job_workflow_ref points to the called workflow. workflow_ref points to the workflow that called it. This difference matters, as the next section shows.
Matching job_workflow_ref in AWS
AWS IAM exposes only some claims as condition keys for the GitHub provider: aud, sub, and amr. The job_workflow_ref claim is not available as a direct condition key. AWS does let you condition on it if you include it in the sub claim by customizing the template, which the previous chapter covered.
The usual way to get this effect is to add job_workflow_ref to the sub template. Set the template at the repository level:
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[]=job_workflow_ref'The sub value is now repo:example-org/example-repo:job_workflow_ref:example-org/shared-workflows/.github/workflows/deploy.yml@refs/heads/main. The trust policy can then match it exactly:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:job_workflow_ref:example-org/shared-workflows/.github/workflows/deploy.yml@refs/heads/main"
}
}
}Only a job that runs deploy.yml from the main branch of shared-workflows can assume this role. A different workflow in the same repository gets a different sub and is refused.
Gotchas
- A custom template changes the
subvalue for every workflow in that repository. Update all trust policies that match it before you change the template, or those workflows will fail. - Include
repoin the template. Without it, the subject no longer names the repository, and a role could trust workflows from other repositories. - Pin the ref at the end of
job_workflow_ref. A pattern likedeploy.yml@*trusts any branch of the workflow file, including unreviewed ones. - Matching a reusable workflow in another repository trusts whoever can change that repository. Limit write access there.
Exercise
A repository has lint.yml and deploy.yml, and both run on main. With the default sub template, both produce the same subject. What change lets the role trust only deploy.yml, and what must you do first so existing workflows keep working?
Show answer
Customize the sub template to include repo and job_workflow_ref, then change the trust policy to match the new sub value for deploy.yml at its pinned ref. Before changing the template, update every existing trust policy in that repository that matches the old sub format, because the template change alters the subject for all its workflows.