Sub topic 15
Testing and auditing the trust boundary
A trust policy that looks correct can still allow more than you intended. This chapter covers three checks: inspect the claims a workflow actually sends, prove that unwanted callers are denied, and review the roles and logs you already have.
This chapter assumes you have read the earlier chapters. It uses their GitHub Actions and AWS IAM examples, including the sub, aud, and environment claims. You also need the AWS CLI, jq, and permission to run workflows in a test repository.
What you are verifying
A trust policy makes a claim about who can assume a role. Testing means trying to break that claim. Check each of these:
| Question | Test | Expected result |
|---|---|---|
| Does the intended workflow work? | Run it from `main` or the `production` environment | Role is assumed |
| Is another branch blocked? | Run the same job from a feature branch | `AccessDenied` |
| Is another repository blocked? | Use a second repository with the same role ARN | `AccessDenied` |
| Is the audience pinned? | Request a token with a different audience | Rejected |
| Are pull requests blocked? | Run a job triggered by `pull_request` | `AccessDenied` unless you allow it |
| Did the policy drift? | Scan every role that trusts the provider | No unexpected wildcards or missing conditions |
The allowed case proves the policy is not too strict. The denied cases prove it is not too loose. Teams often run only the first test.
Step 1: Read the real claims
Do not guess the sub value. Print it from a workflow. The workflow below requests a token and prints only selected claims.
name: inspect-oidc-claims
on: workflow_dispatch
permissions:
id-token: write
contents: read
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Print selected 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)
python3 - "$TOKEN" <<'PY'
import base64, json, sys
payload = sys.argv[1].split(".")[1]
payload += "=" * (-len(payload) % 4)
claims = json.loads(base64.urlsafe_b64decode(payload))
for key in ("aud", "sub", "ref", "repository", "environment",
"job_workflow_ref", "event_name"):
print(key, "=", claims.get(key))
PYRun it from different places and compare the output:
- From
main,subshould berepo:example-org/example-repo:ref:refs/heads/main. - From a job with
environment: production,subshould end inenvironment:production. Therefclaim still shows the branch. - From a pull request,
subshould berepo:example-org/example-repo:pull_request.
Compare each value with the StringEquals or StringLike pattern in your trust policy. If they differ by even one character, the role will never be assumed.
Step 2: Test that denial works
Write the negative test as a workflow that tries to assume the role and fails the run if it succeeds. Run it from a branch that is not allowed.
name: trust-negative-test
on: workflow_dispatch
permissions:
id-token: write
contents: read
jobs:
expect-denied:
runs-on: ubuntu-latest
env:
AWS_REGION: us-east-1
ROLE_ARN: arn:aws:iam::123456789012:role/example-deploy-role
steps:
- name: Try to assume the role
run: |
TOKEN=$(curl -sS \
-H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" \
| jq -r .value)
if aws sts assume-role-with-web-identity \
--role-arn "$ROLE_ARN" \
--role-session-name trust-test \
--web-identity-token "$TOKEN" \
--duration-seconds 900 > /dev/null 2> err.txt; then
echo "FAIL: role was assumed from $GITHUB_REF but should be denied"
exit 1
fi
echo "PASS: assumption was denied"
cat err.txtThe assume-role-with-web-identity call is not signed, so it needs no AWS credentials. Dispatch the workflow on a feature branch. A passing run prints an AccessDenied message such as "Not authorized to perform sts:AssumeRoleWithWebIdentity". If the job fails with "role was assumed", your trust policy is too broad.
Repeat the test for each boundary you rely on:
- Wrong branch. Dispatch from a feature branch against a role limited to
main. - Wrong environment. Run a job with no
environment:key against the production role. Thesubwill be a branch subject, so an environment-only policy must deny it. - Wrong repository. Copy the workflow into another repository you own and use the same
ROLE_ARN. - Wrong audience. Change
audience=sts.amazonaws.comto another value. AWS should reject the token before it evaluates your conditions. - Pull request. Trigger the workflow from a
pull_requestevent in the same repository.
A fork pull request usually cannot request an OIDC token at all, so it fails before it reaches AWS. That does not replace testing the same-repository pull request subject.
Step 3: Audit what AWS recorded
Every assumption attempt through the provider is logged by CloudTrail as an AssumeRoleWithWebIdentity event. Successful calls show the session. Denied calls carry an errorCode. Both are useful. The following command lists recent events with the caller, role, and outcome:
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=AssumeRoleWithWebIdentity \
--max-results 50 \
--output json \
| jq -r '.Events[].CloudTrailEvent | fromjson
| [.eventTime,
(.userIdentity.userName // "-"),
(.requestParameters.roleArn // "-"),
(.errorCode // "ok")]
| @tsv'For web identity callers, the userName field typically holds the sub claim. Look for these patterns:
- A successful event with a
subyou did not expect, such as another repository or a pull request subject. - Many denied events for one role. These can mean a misconfigured workflow or someone probing the role.
- Success from a role that should only be used on a schedule or during a release.
lookup-events covers management events from the past 90 days. For longer retention, send CloudTrail to an S3 bucket or a CloudTrail Lake event data store, then query it there.
Also watch for changes to the policy itself. Alert on UpdateAssumeRolePolicy events, because one edit can widen trust for every workflow.
Step 4: Scan roles for weak trust policies
Over time, roles pile up. The script below lists each role that trusts the GitHub provider and flags the properties that matter most.
aws iam list-roles --output json | jq -r '
.Roles[] | . as $r
| (.AssumeRolePolicyDocument.Statement
| if type == "array" then .[] else . end)
| select((.Principal.Federated? // "") | tostring
| contains("token.actions.githubusercontent.com"))
| (.Condition // {} | tostring) as $c
| [$r.RoleName,
(if ($c | contains(":sub")) then "has-sub" else "NO-SUB" end),
(if ($c | contains("*")) then "wildcard" else "exact" end),
(if ($c | contains(":aud")) then "has-aud" else "NO-AUD" end)]
| @tsv'Read the output this way:
NO-SUBis the most serious result. Any repository that can reach the provider might assume the role. Fix it first.NO-AUDmeans the audience is not pinned. Add theaudcondition.wildcardis not always wrong. A pattern likerepo:example-org/example-repo:ref:refs/heads/release/*can be intentional. Open each flagged role and confirm that the wildcard sits only where you expect it. A pattern such asrepo:example-org/*trusts every repository in the organization, including ones created later.
The scan is crude. It searches the condition text and does not evaluate it. Treat it as a way to find roles worth reading, not as proof that a role is safe.
Common gaps in testing
- Only the happy path was tested. The deploy works, so the policy is assumed correct. Add denial tests.
- Testing one branch name. If the policy uses a wildcard such as
release/*, test a name that should match and one that should not, such asreleases/xorfeature/release/x. - Forgetting that the environment key changes
sub. After you addenvironment: production, the subject no longer contains the branch. A policy that matchesref:refs/heads/mainstops working for that job. - Changing the custom
subtemplate without retesting. If you customize the template, every trust policy for that repository must be updated and tested again. - Not testing the audience. If
audis missing from the policy, a token issued for another service might be accepted. - Trusting the console summary. The IAM console shows the policy document, not what the provider sends. Always compare it with the printed claims.
Putting it together
For a new role, follow this order. Print the claims from the intended workflow. Write the narrowest trust policy that matches them. Run the positive test, then each negative test. Finally, add the CloudTrail query and the role scan to a routine review. If any step surprises you, return to the claims. That is where most mismatches start.
Exercise
Your trust policy for the production role requires sub to equal repo:example-org/example-repo:environment:production. A negative test run from a feature branch, with no environment: key in the job, is denied. A teammate says this proves the policy is safe. What else should you test, and why?
Show answer
A single denial only proves that one case is blocked. You should also test a job from the main branch without the environment key, since the subject for that job is a branch subject, and a job from a different repository that sets environment: production, since a matching environment name in another repository must not satisfy the repo-specific subject. Also confirm the audience check by requesting a token with a different aud. Each test covers a different part of the sub and aud conditions.
Exercise
The role scan prints deploy-role has-sub wildcard has-aud. What do you do next, and what would make the wildcard acceptable?
Show answer
Open the role's trust policy and read the sub condition. The wildcard is acceptable if it appears only in a narrow, intended place, such as repo:example-org/example-repo:ref:refs/heads/release/*. It is not acceptable if it covers the repository part, such as repo:example-org/*, or the whole subject, because that trusts other repositories. After reading it, fix the pattern if needed, then rerun the negative tests for branches and repositories that should be denied.