Sub topic 14
Trusting reusable workflows and custom claims
This chapter assumes you have built the trust policy and deployment workflow from the earlier chapters. You should know what the aud and sub claims are, how sts:AssumeRoleWithWebIdentity works, and how a workflow requests a token with id-token: write. Here you will tie role access to the shared workflow code that runs, not only to the repository that called it.
Why the default subject is not enough
The default sub claim describes the repository and the trigger context. For a push to main it looks like repo:my-org/my-repo:ref:refs/heads/main. It says nothing about which workflow file ran.
That causes two problems.
- Any workflow in the repository can match. A trust policy that allows
repo:my-org/my-repo:ref:refs/heads/mainaccepts every workflow on that branch. A contributor with write access can add a new workflow file that requests a token and calls AWS directly. Your reviewed deployment workflow is not the only code that can use the role. - Reusable workflows are invisible in
sub. When a job calls a reusable workflow, the token'ssubstill names the caller repository. The shared workflow in a central repository leaves no trace in it.
GitHub puts the workflow information in other claims. You can match those claims, or you can change what sub contains.
The claims that identify workflow code
| Claim | What it holds | Example |
|---|---|---|
| `workflow_ref` | The top-level workflow that was triggered, including its ref | `my-org/my-repo/.github/workflows/release.yml@refs/heads/main` |
| `job_workflow_ref` | The workflow file that contains the running job. For a job in a reusable workflow, this is the reusable workflow's path and ref. | `my-org/shared-workflows/.github/workflows/deploy-aws.yml@refs/tags/v1` |
| `job_workflow_sha` | The commit SHA of that workflow file | a 40-character SHA |
| `repository` | The repository that triggered the run | `my-org/my-repo` |
job_workflow_ref is the claim that matters here. In a job inside a reusable workflow it names the called workflow. In a job defined directly in the caller, it names the caller's own file.
Looking at the real token first
Before you write conditions, print the claims your workflow actually receives. This step decodes the token's payload and shows only the claims you need.
- name: Show selected OIDC claims
run: |
curl -sS \
-H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" \
| jq -r '.value' \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson
| {sub, repository, workflow_ref, job_workflow_ref}'The job needs id-token: write for this to work. Print only selected claims, and remove the step once you have what you need.
A reusable workflow that assumes the role
The shared workflow lives in a central repository. It declares workflow_call and does the AWS work.
name: deploy-aws
on:
workflow_call:
inputs:
role-to-assume:
required: true
type: string
bucket:
required: true
type: string
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ inputs.role-to-assume }}
aws-region: us-east-1
- run: aws s3 sync ./site s3://${{ inputs.bucket }} --deleteThe caller grants the permission and passes the inputs.
name: release
on:
push:
branches: [main]
jobs:
deploy:
permissions:
id-token: write
contents: read
uses: my-org/shared-workflows/.github/workflows/deploy-aws.yml@v1
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
bucket: my-example-bucketThe caller's permissions are a ceiling. A called workflow can lower its permissions but cannot raise them above what the caller grants. If the caller omits id-token: write, token requests in the reusable workflow fail.
Pinning trust to the shared workflow
There are two ways to make the role accept only tokens from the shared workflow. Both rely on the claims in the table above.
Option 1: match `job_workflow_ref` directly
Some GitHub claims can be used as IAM condition keys. A condition on job_workflow_ref would look like this.
{
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:job_workflow_ref": "my-org/shared-workflows/.github/workflows/deploy-aws.yml@refs/tags/v1"
}
}IAM only evaluates claims it exposes as condition keys, and that list has changed over time. Check the current AWS documentation for the GitHub provider before relying on a key. An unsupported key in a trust policy is a common cause of a role that nobody can assume.
Option 2: customize the `sub` claim
GitHub lets a repository or organization choose which claims make up sub. Because sub is always available to IAM, this works without any extra condition keys. This call changes it for one repository.
gh api --method PUT \
repos/my-org/my-repo/actions/oidc/customization/sub \
--field use_default=false \
--field 'include_claim_keys[]=repo' \
--field 'include_claim_keys[]=context' \
--field 'include_claim_keys[]=job_workflow_ref'The new sub joins the chosen keys and their values with colons. Print a token and read the exact string before you write the trust policy. Then match it.
{
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:*job_workflow_ref:my-org/shared-workflows/.github/workflows/deploy-aws.yml@refs/tags/v1"
}
}Gotchas
- A customized
subbreaks existing trust policies. Every role that matched the default format stops working for that repository until you update it. Change the claim and the policy together, and test on a non-production role first. - Pin the reusable workflow ref.
@refs/heads/maininjob_workflow_refmeans anyone who can push to the shared repository's main branch can change what the role trusts. A tag is better, and a full commit SHA is the most strict. If you pin to a tag, protect that tag in the shared repository so it cannot be moved. - Trust the repository that holds the shared workflow. Restricting
job_workflow_refonly helps if the shared repository has branch protection, review requirements, and limited write access. - Wildcards widen trust.
StringLikewith*is convenient for matching a ref, butmy-org/shared-workflows/*trusts every workflow in that repository. Make the pattern as narrow as you can. - Combine the checks. Keep the
audcondition. Pair workflow identity with an environment or branch restriction when the role can change production.
Fixing failures
- Request rejected after you add a claim condition. Print the token and compare the claim string to the policy character by character. Check the ref form (
refs/tags/v1, notv1). - Condition key not recognized or never matches. The key may not be supported for this provider. Switch to the customized
subapproach. - Token request fails inside the reusable workflow. The caller job is missing
id-token: write. - Works from the caller repository, fails from another. The
repopart of a customizedsubnames the caller. Add each caller repository, or use a pattern for the organization with care.
Exercise
Your trust policy allows repo:my-org/my-repo:ref:refs/heads/main. A contributor adds a new workflow file on main that runs aws s3 rm with the role. Why does the policy not stop this, and what two changes would make the role accept only your shared deployment workflow?
Show answer
The default sub identifies only the repository and branch, so every workflow on main matches, including the new one. First, make the identity of the workflow part of the match: either condition on job_workflow_ref if IAM supports that key, or customize sub to include job_workflow_ref and match the resulting string. Second, pin the workflow reference to a protected tag or commit SHA, and protect the shared repository so its contents cannot be changed without review.