Identity & AI agent security

Least Privilege for CI/CD Identities: GitHub Actions OIDC

Replace long-lived cloud keys in CI with GitHub Actions OIDC, then scope the trust policy, the role and GITHUB_TOKEN so a pipeline can do only its own job.

TrustIvan ·

A CI/CD pipeline is a non-human identity with unusual reach: it can build, sign and deploy to production, and it runs code from every branch that anyone with write access can push. Least privilege for CI/CD identities starts with removing the long-lived cloud keys stored as repository secrets and replacing them with GitHub Actions OIDC federation. That alone is not least privilege. The trust policy, the role's permissions and the workflow's own token each need scoping too, and a loose trust condition can leave you worse off than a well-guarded key.

Why are stored cloud keys in CI a problem?

A cloud access key saved as a repository secret has three properties that make it a poor CI credential. It does not expire on its own, so a copy taken once keeps working. It is available to any workflow that references it, which on many repositories means any branch a contributor pushes. And it is the same credential for every run, so the cloud's audit log cannot tell a release from an attacker's test.

The usual path to abuse is not a cloud breach. It is a workflow change: a step added on a branch that prints the secret or sends it somewhere, or a compromised third-party action that does the same. GitHub masks secrets in logs, but masking does not stop a step from sending the value elsewhere.

How does GitHub Actions OIDC work?

When a job has permission to request it, GitHub's OIDC provider issues the job a short-lived signed JSON Web Token. The token's claims describe the run:

ClaimExample
isshttps://token.actions.githubusercontent.com
audsts.amazonaws.com (set by the cloud action)
subrepo:example/payments-api:environment:production
repository, repository_ownerexample/payments-api, example
ref, environmentrefs/heads/main, production
job_workflow_refthe workflow file and ref that ran the job

The cloud provider verifies the signature, checks the claims against a trust policy you wrote, and returns credentials that expire, an hour by default on AWS. Nothing long-lived is stored in GitHub, so there is nothing to copy.

Everything now depends on the trust policy. It is the access control list for your cloud account, and its subject is "which runs of which workflows".

How do you write a least-privilege AWS trust policy?

Create an IAM OIDC identity provider for token.actions.githubusercontent.com with the audience sts.amazonaws.com, then a role whose trust policy names the repository and, for anything that deploys, the environment:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::111122223333: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/payments-api:environment:production"
        }
      }
    }
  ]
}

Three rules keep this tight.

Always condition on sub, with StringEquals where you can. A trust policy that checks only aud lets every GitHub repository in the world assume the role, because every repository's tokens carry the same audience. StringLike with repo:example/* lets every repository in your organization assume it, including one created tomorrow by any member.

Prefer environments to branches for deploy roles. A sub of ref:refs/heads/main trusts any workflow file on main. A sub of environment:production trusts only jobs that declare that environment, and the environment can carry protection rules: required reviewers, and a restriction on which branches or tags may deploy to it. GitHub recommends adding those rules whenever an environment appears in an OIDC policy.

Check the subject format your repository uses. GitHub's documentation notes that for repositories created after 15 July 2026, or that opt in to immutable subject claims, the sub includes the owner and repository IDs, as in repo:example@123456/payments-api@456789:environment:production. Renaming or transferring a repository then no longer changes who matches. A policy written for the old format will simply fail to match, which fails closed, but check before you wonder why a deploy stopped.

What should the role be allowed to do?

Only what the job does. A deploy job that pushes an image and updates one service does not need AdministratorAccess, and a plan job on a pull request needs no write access at all. Split roles by purpose rather than by repository:

  • a read-only role for pull requests and terraform plan, trusted on the repository's pull request subject (repo:example/payments-api:pull_request);
  • a deploy role for releases, trusted only on the protected environment, allowed to write only the resources that service owns.

Never give the pull-request role anything you would not give a stranger. Workflows triggered from forks do not receive OIDC tokens or secrets on the pull_request event, but a workflow on pull_request_target, or one that checks out and runs a fork's code after authenticating, hands the stranger your credentials anyway.

Keep sessions short with role-duration-seconds if your jobs finish quickly, and use role-session-name to put the run ID into CloudTrail so a session can be traced back to the run that made it.

What does the workflow need?

Request the token in the job that needs it, and nothing else:

name: deploy
on:
  push:
    branches: [main]
 
permissions: {}            # nothing by default, for every job
 
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    permissions:
      id-token: write      # lets this job request an OIDC token
      contents: read       # lets actions/checkout read the repository
    steps:
      - uses: actions/checkout@v7
      - uses: aws-actions/configure-aws-credentials@v6.3.0
        with:
          role-to-assume: arn:aws:iam::111122223333:role/payments-api-deploy
          aws-region: eu-west-1
          role-session-name: payments-api-${{ github.run_id }}
          role-duration-seconds: 900
      - run: ./deploy.sh

id-token: write does not grant write access to anything. It only allows the job to request a token. The other half of this file is the empty top-level permissions block, which removes the default GITHUB_TOKEN scopes from every job that does not ask for them. Set the organization's default workflow permissions to read-only as well, so a new workflow without a permissions block starts with less.

Pin third-party actions to a full commit SHA rather than a tag. A tag can be moved to different code; a SHA cannot. The tags above are for readability.

How does the same idea work on Google Cloud and Azure?

The mechanism is the same: a trust configuration that accepts GitHub's tokens, and a condition on their claims.

On Google Cloud, Workload Identity Federation maps claims to attributes and filters with a CEL condition. Map the claims you need, then restrict:

gcloud iam workload-identity-pools providers create-oidc "payments-api" \
  --project="${PROJECT_ID}" --location="global" \
  --workload-identity-pool="github" \
  --issuer-uri="https://token.actions.githubusercontent.com" \
  --attribute-mapping="google.subject=assertion.sub,attribute.repository=assertion.repository,attribute.ref=assertion.ref" \
  --attribute-condition="assertion.repository == 'example/payments-api' && assertion.ref == 'refs/heads/main'"

Then grant roles to principalSet://.../attribute.repository/example/payments-api rather than to the whole pool, and use google-github-actions/auth@v3 in the workflow. The action's own documentation is explicit that you must map a claim to an attribute before you can condition on it.

On Azure, a federated identity credential on an app registration or a managed identity names an exact subject, such as repo:example/payments-api:environment:production, which gives you StringEquals behavior by default.

How do you find the CI identities you still have to fix?

Moving to OIDC leaves the old credentials behind unless someone deletes them. Look for three things.

Repository and organization secrets that hold cloud keys. gh secret list per repository, and the organization's secrets page, will show names like AWS_SECRET_ACCESS_KEY that should no longer exist.

IAM users with access keys and no console login. These are almost always machine identities, and many belong to CI:

aws iam list-access-keys --user-name ci-deployer
aws iam get-access-key-last-used --access-key-id AKIAEXAMPLEKEYID

A key last used months ago is a candidate for deletion. A key still in use after the migration means a workflow is still on the old path.

Trust policies that check aud without sub, or use wildcards. Review every role whose trust names the GitHub OIDC provider. IAM Access Analyzer's policy validation can help, but read the conditions yourself.

How does TrustIvan fit into this?

Two parts of TrustIvan touch CI identities, and both have limits worth knowing.

The TrustIvan GitHub Action that gates a build authenticates to TrustIvan with an API key, not with OIDC federation. The key can be scoped to exactly scans:create, scans:read and verdicts:request, it always has an expiry (90 days unless you set one), and that scope set leaves out findings:suppress, while no API key can hold policies:manage at all, so a stolen pipeline key cannot quiet the gate it feeds. Pass it through env rather than with, as the pipeline tutorial shows, and rotate it on a schedule.

NHI Security's AWS connector inventories IAM roles, machine users and their access keys with last-use dates, and draws an edge from each role's trust policy to who can assume it. That answers "which long-lived CI keys are left after the migration". It does not answer whether an OIDC trust is scoped correctly: the connector does not evaluate policy conditions, and federated trust is not collected, so a sub wildcard will not appear as a finding. The NHI limitations page lists the rest, and no connector has yet been run against a live AWS account.

For the identities themselves, read what non-human identity security is and the over-privileged NHI use case. For the static secrets OIDC is replacing, see finding and rotating secrets leaked into git history.

Primary sources and further reading

The standards and primary documentation on this subject, for checking a claim at its source: