Sub topic 10
Adding approvals and branch rules to environments
This chapter assumes you can read a GitHub Actions workflow and an AWS IAM trust policy. It also assumes you know that the sub claim in the OIDC token identifies the job that requested access. The previous chapter showed how to write a trust policy that matches environment:production. That policy has a gap, and this chapter closes it.
Why the trust policy alone is not enough
A trust policy that matches repo:example-org/example-repo:environment:production checks only the text of the sub claim. It does not check who wrote the workflow or which branch it ran from. Any workflow in the repository can declare environment: production. A job on a feature branch that does so receives a token with the matching subject, and AWS issues credentials.
Environment protection rules close this gap. GitHub enforces them before it issues the token. If the rules block the job, the job never runs, so no token is minted and AWS is never asked.
Protection rules to configure
Open the repository, then go to Settings, then Environments, then select production. The rules that matter for OIDC trust are these.
| Rule | Effect | Why it helps |
|---|---|---|
| Required reviewers | A listed person or team must approve each deployment. | A human sees the run before credentials exist. |
| Deployment branches and tags | Only matching branches or tags can deploy to the environment. | A feature branch cannot obtain the production subject. |
| Prevent self-review | The person who triggered the run cannot approve it. | One account cannot push and approve alone. |
| Wait timer | The job waits a set number of minutes before it starts. | It gives time to cancel a bad run. |
Set the deployment branch rule to main, or to a protected branch pattern. Add at least one required reviewer.
How the pieces fit together
The workflow below uses the environment from the earlier chapter. Nothing in the YAML changes. The protection rules live in repository settings, not in the file.
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/example-deploy-role
aws-region: us-east-1
- run: aws sts get-caller-identityWhen this job starts, GitHub pauses it and waits for a reviewer. The configure-aws-credentials step requests the token only after approval. The trust policy still requires sub to equal repo:example-org/example-repo:environment:production, so the two layers check different things. GitHub decides whether the job may use the environment. AWS decides whether the token's subject may assume the role.
Common mistakes
- Leaving the deployment branch rule at "All branches". This is the default. The environment then restricts nothing about the branch.
- Adding reviewers but no branch rule. A reviewer might approve a run from an unexpected branch without noticing.
- Letting repository admins bypass the rules. Admins can bypass protection rules unless you turn off that option. Check the setting if your threat model includes admin accounts.
- Forgetting that the environment subject hides the branch. The subject
environment:productiondoes not include the branch name. The branch rule in GitHub is the only place that limit exists, unless you also check another claim such asjob_workflow_ref(covered in a later chapter).
Exercise
A trust policy matches only repo:example-org/example-repo:environment:production. The production environment has required reviewers but its deployment branch rule is set to "All branches". What can an engineer do that you did not intend, and which setting fixes it?
Show answer
An engineer can push a feature branch whose workflow declares environment: production. After a reviewer approves the run, the token's subject matches the trust policy, so the branch's code can assume the role. Set the environment's deployment branch rule to main (or a protected branch pattern). Then GitHub blocks the job before it requests a token.