Sub topic 15
Testing and auditing the trust boundary
This chapter assumes you have written the trust policies from the earlier chapters. It also assumes you can read GitHub Actions YAML and run the AWS CLI. A trust policy that works is not necessarily a trust policy that is safe. You need to prove that the intended workflows get in and that everything else stays out.
Test both directions
Most people only test the positive case: the workflow on main assumes the role. That test passes even if the policy trusts every repository in the world. Add negative tests, which are runs that should fail.
| Scenario | Expected result |
|---|---|
| Push to `main` in `example-org/example-repo` | Role assumed |
| Run from a feature branch (branch-restricted role) | `Not authorized to perform sts:AssumeRoleWithWebIdentity` |
| Job without `environment: production` (environment-restricted role) | Same error |
| Same workflow in a different repository | Same error |
| Token requested with a different audience | Same error |
Keep one throwaway branch and one throwaway repository for these checks. Run them again after every trust policy change.
Inspect the token your job receives
When a run fails unexpectedly, compare the token's sub with the value in your policy. The step below requests a token and prints only selected claims.
- name: Show token claims
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"
echo "$TOKEN" | jq -R 'split(".")[1]
| gsub("-";"+") | gsub("_";"/")
| @base64d | fromjson
| {sub, aud, ref, environment, job_workflow_ref}'This step needs permissions: id-token: write on the job. The add-mask line keeps the raw token out of the logs. Never print the full token, because anyone who reads it can replay it until it expires. Remove the step once you have found the problem.
Audit what is deployed
Read the policy that is live in AWS, not the copy in your repository. The two can drift.
aws iam get-role \
--role-name example-deploy-role \
--query Role.AssumeRolePolicyDocumentReview each role against this checklist:
- The
Principalis the GitHub OIDC provider ARN and nothing broader. - An
audcondition equalssts.amazonaws.com. - A
subcondition exists. A policy with only anaudcheck trusts every repository on GitHub. - Each
subvalue starts withrepo:example-org/example-repo. AStringLikepattern such asrepo:example-org/*trusts the whole organization. - Wildcards appear only at the end of a path segment you chose on purpose, such as
release/*.
Review who actually assumed the role
CloudTrail records each AssumeRoleWithWebIdentity call. Look at recent calls and check that the subjects match what you expect.
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=AssumeRoleWithWebIdentity \
--max-results 20In each event, check the identity provider and the subject value. An unexpected repository, a pull_request subject, or a branch you did not approve means the policy is wider than intended. Tighten the policy, then repeat the negative tests.
Exercise
A role's trust policy checks aud equals sts.amazonaws.com and sub matches repo:example-org/*:ref:refs/heads/main with StringLike. A positive test on example-org/example-repo passes. What negative test exposes the problem, and how do you fix it?
Show answer
Run the workflow from main in a different repository in the same organization, for example example-org/other-repo. It will assume the role, because the * matches any repository name. Replace the pattern with the exact subject repo:example-org/example-repo:ref:refs/heads/main and use StringEquals. Then repeat the other-repository test and confirm it now fails.