Sub topic 1
Restricting OIDC Trust with Subject Claims, Branches, and Environments
Prerequisites
Assume you know basic CI/CD terms, GitHub Actions workflow YAML, IAM roles, and JSON Web Tokens (JWTs). A JWT is a signed set of claims about the workflow requesting access.
Why CI Workflows Use OIDC Instead of Stored Cloud Keys
OpenID Connect (OIDC) lets a workflow exchange its identity for short-lived cloud credentials. The workflow does not store a permanent access key in a repository secret.
With stored keys, a leaked secret may remain useful until someone revokes it. OIDC credentials expire quickly. The cloud provider also checks claims in the workflow's token before issuing them.
The trust boundary therefore moves from “who knows this secret?” to “which workflow is this?”
What Is Inside the OIDC Token
A GitHub Actions OIDC token is a JWT with claims such as:
iss: the token issuer, such as GitHub's OIDC service.aud: the intended audience.sub: the subject, identifying the repository and workflow context.repository: the repository name.job_workflow_ref: the workflow file and Git reference.
A trust policy should match claims that identify the intended workload. The sub claim is especially useful because its value changes for branches, tags, pull requests, and environments.
Pinning the Audience
The aud claim says which service should accept the token. For AWS, require sts.amazonaws.com. Without this check, a token intended for another audience could satisfy an overly broad policy.
The sub value below permits only the main branch of one repository:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main"
}
}
}
]
}How the Subject Claim Encodes Repository, Branch, and Environment
For a branch, the default subject format is:
repo:ORGANIZATION/REPOSITORY:ref:refs/heads/BRANCH
For a deployment environment, it is:
repo:ORGANIZATION/REPOSITORY:environment:ENVIRONMENT
Thus, a policy can trust main or production without trusting every branch in the repository.
Writing a Role Trust Policy
A trust policy names the OIDC provider, permits sts:AssumeRoleWithWebIdentity, and matches both aud and sub. Start with StringEquals. Use StringLike only when a narrow wildcard is necessary, because wildcards widen the set of workflows that can enter the role.
Exercise
Which two claims should an AWS trust policy require for a GitHub Actions OIDC role?
Show answer
It should require aud equal to sts.amazonaws.com and sub equal to the intended repository and branch or environment.