Sub topic 14
Trusting reusable workflows and custom claims
This chapter assumes you can write a role trust policy and use the sub condition to limit a role to a repository, branch, or environment. It also assumes you have used workflow_call in GitHub Actions.
The problem with the default subject
The default sub claim names the repository and the branch or environment. It does not say which workflow file is running. Any workflow in my-org/my-repo on main produces the same sub. A pull request that adds a new workflow file to that branch gets the same access as your reviewed deploy workflow.
Reusable workflows let you fix this. A central repository holds one reviewed deploy workflow, and other repositories call it. You then want AWS to trust only that workflow.
The job_workflow_ref claim
Every GitHub OIDC token carries a job_workflow_ref claim. In a reusable workflow, it points to the called workflow, not the caller. For example:
my-org/shared-workflows/.github/workflows/deploy.yml@refs/heads/mainThe sub claim still describes the caller. So the token tells AWS both who called the workflow and which workflow code ran.
AWS can only check claims it exposes as condition keys. The reliable way to get this claim into a condition is to customize the sub claim so it includes it.
Customizing the subject claim
GitHub lets you set a template for sub on a repository. The following command makes sub contain the repository, the context (branch or environment), and the workflow reference:
gh api --method PUT \
repos/my-org/my-repo/actions/oidc/customization/sub \
--input - <<'EOF'
{
"use_default": false,
"include_claim_keys": ["repo", "context", "job_workflow_ref"]
}
EOFThe new sub has this shape:
repo:my-org/my-repo:environment:production:job_workflow_ref:my-org/shared-workflows/.github/workflows/deploy.yml@refs/heads/mainThe trust policy condition
Match the whole customized value in the role's trust policy:
{
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:environment:production:job_workflow_ref:my-org/shared-workflows/.github/workflows/deploy.yml@refs/heads/main"
}
}Pin the reference to a protected branch or a tag. A wildcard such as @* would let anyone who can push a branch to the shared repository obtain the role.
The calling side
The caller must grant the token permission, because a reusable workflow cannot raise permissions above what the caller allows:
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
uses: my-org/shared-workflows/.github/workflows/deploy.yml@main
with:
role-arn: arn:aws:iam::123456789012:role/github-deployCommon mistakes
- Customizing
suband forgetting to update the trust policy. The result isNot authorized to perform sts:AssumeRoleWithWebIdentity. - Matching a branch name in
job_workflow_refwhile the caller uses a tag. The reference must match exactly. - Omitting
id-token: writein the caller. The reusable workflow cannot request a token without it. - Guessing the
substring. Print the claims in a test run, or check the CloudTrail event, to see the real value.
Exercise
A reusable workflow in my-org/shared-workflows is called from my-org/my-repo. Why does the default sub claim not let you restrict the role to that shared workflow, and what change fixes it?
Show answer
The default sub describes the calling repository and its branch or environment, not the workflow file that ran. Customize the repository's sub template to include jobworkflowref, then match the new sub value, pinned to a protected ref, in the role's trust policy.