New: Access Diff now reviews Terraform pull requests on GitHub

Technical guides / CI/CD /

Least privilege for CI/CD identities that federate with OIDC

A pipeline that signs in to AWS through OIDC has no stored key, but it still has an identity with permissions. How to scope who can assume the role, split deploy roles from data roles, and review what a workflow can reach.

OpenID Connect (OIDC) federation removes long-lived cloud keys from CI/CD systems. A workflow asks the CI provider for a short-lived signed token, and the cloud provider exchanges it for temporary credentials. That is a real improvement over a stored access key. It does not decide what the pipeline can do, though. The role it lands in, and the trust policy that lets it in, still need the same care as any other privileged identity.

This guide uses GitHub Actions and AWS. The same ideas apply to other CI systems and other clouds, with different claim names.

What the trust policy checks

When a workflow runs with the id-token: write permission, GitHub issues a token whose claims describe the run. Two matter for access:

  • aud, the audience the token was requested for. For AWS this is normally sts.amazonaws.com.
  • sub, the subject, which says where the run came from.

For a GitHub Actions run, the sub value has a predictable shape:

Run context Example sub
Push or manual run on a branch repo:halvorne/platform-infra:ref:refs/heads/main
Job that uses an environment repo:halvorne/platform-infra:environment:production
Pull request workflow repo:halvorne/platform-infra:pull_request

The role’s trust policy decides which of those tokens are accepted. The target role uses sts:AssumeRoleWithWebIdentity, with GitHub’s OIDC provider as the federated principal:

{
  "Effect": "Allow",
  "Action": "sts:AssumeRoleWithWebIdentity",
  "Principal": {
    "Federated": "arn:aws:iam::111122223333:oidc-provider/token.actions.githubusercontent.com"
  },
  "Condition": {
    "StringEquals": {
      "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
      "token.actions.githubusercontent.com:sub": "repo:halvorne/platform-infra:ref:refs/heads/main"
    }
  }
}

The sub condition is the part that matters. Without it, the trust policy accepts any token GitHub issues for that audience, from any repository. The signature check only proves that GitHub signed the token. It says nothing about whose workflow it was.

Scope the subject as narrowly as the job allows

Compare three versions of the same condition.

repo:halvorne/*                                     every repository in the organization
repo:halvorne/platform-infra:*                      every branch, tag, environment and pull request in one repo
repo:halvorne/platform-infra:ref:refs/heads/main    one branch in one repo

Patterns with a wildcard need the StringLike operator rather than StringEquals. Each step down makes the set of workflows that can assume the role smaller:

  • The organization-wide pattern covers repositories that do not exist yet. Anyone who can create a repository, or push a workflow into one, can reach the role.
  • The single-repository pattern also matches pull_request and every feature branch. A person who can open a pull request that edits a workflow can then run code as the role, unless something else stops them.
  • The branch pattern limits the role to runs on main, which should be protected: required review, no direct pushes, and limits on who can change the workflows.

For deploys, an environment subject is often the better anchor. If the job declares environment: production, the sub carries the environment name, and GitHub’s environment rules (required reviewers, allowed branches) decide when a job can run in it. The trust policy then follows rules that people already manage in one place. An environment subject no longer includes the branch, so set the environment’s own branch restrictions.

Keep deploy roles and data roles apart

A deployment pipeline needs to publish artifacts and update infrastructure. It rarely needs to read or change production records. Splitting these into separate roles keeps a compromise of one workflow from becoming access to the other.

In the sample account, ci-deploy-role publishes to the deploy-artifacts bucket (s3:PutObject). Separately, data-maintenance-role can write to the CustomerRecords table. The deploy role’s permissions should read like its job:

# Deploy role: build and release only.
resource "aws_iam_role_policy" "ci_deploy_artifacts" {
  name = "publish-artifacts"
  role = aws_iam_role.ci_deploy.id
  policy = jsonencode({
    Statement = [{
      Effect   = "Allow"
      Action   = ["s3:PutObject"]
      Resource = "${aws_s3_bucket.deploy_artifacts.arn}/*"
    }]
  })
}

Three habits keep the roles apart:

  • No assumption between them. Do not give the deploy role sts:AssumeRole on the data role, and do not add it to the data role’s trust policy. A pipeline role that can reach a data role is a data role with extra steps. The trust policy guide shows how that path forms.
  • A separate trust path for rare jobs. If a scheduled or manual workflow really must touch data, give it its own role with its own sub condition, such as a dedicated environment, and review that role on its own terms.
  • Permissions named by action and resource. s3:PutObject on one bucket is a statement a reviewer can reason about. s3:* on * is not.

The workflow side

The cloud role is half of the control. The workflow file is the other half, because whoever can edit it can choose which role to request. A minimal deploy workflow, deploy.yml, looks like this:

name: deploy
on:
  push:
    branches: [main]

permissions:
  id-token: write   # needed to request the OIDC token
  contents: read

jobs:
  publish:
    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::111122223333:role/ci-deploy-role
          aws-region: eu-west-1
      - run: ./scripts/publish.sh

Where you can, grant id-token: write at the job level, so that only jobs that need cloud access can request a token. Protect the workflows directory with code owners, and treat a change under .github/workflows as an access change.

Reviewing what a pipeline can reach

For each workflow that uses OIDC, work through the same short list.

  1. Which role does it request? Find role-to-assume in the workflow.

  2. Who else can request that role? Read the trust policy’s sub condition and compare it with the repositories and branches you expect.

    aws iam get-role --role-name ci-deploy-role \
      --query 'Role.AssumeRolePolicyDocument'
  3. What does the role allow directly? List inline and attached policies.

    aws iam list-role-policies --role-name ci-deploy-role
    aws iam list-attached-role-policies --role-name ci-deploy-role
  4. What can it become? Look for sts:AssumeRole in those policies, then read the trust policy of each target and repeat. The paths that matter are usually the second and third hop.

  5. Is anything outside code? A policy added by hand in the console does not appear in the Terraform. Compare the role’s policies in AWS with the policies in your repository.

Write the answer as a chain: deploy.yml, then ci-deploy-role, then whatever it can write to. If a chain reaches production data from a pipeline that only deploys artifacts, that is the finding.

How AccessRoot surfaces it

AccessRoot reads the federation relationship, the role’s policies and the trust relationships behind them, and draws the chain from the workflow outward. In the sample workspace, Access Graph shows deploy.yml federating into ci-deploy-role, with the source file for each hop, and Access Diff reports on a pull request when a change gives a pipeline a new path. The GitHub page covers how the check appears next to your own CI checks.