Skip to content

InsightsSecurity

Replacing long-lived CI credentials with OIDC federation

How to let pipelines deploy to AWS, Azure and Google Cloud with short-lived tokens instead of stored keys, and how to scope the trust so only the right job can use it.

Published October 20, 20263 min read

Most pipelines still deploy with a cloud access key stored as a CI secret. The key was created once, has broad permissions because narrowing them was never anyone's job, and has not been rotated since the engineer who created it left. Anyone who can run a job, print an environment variable or read a careless log line can take it home.

Every major CI system can now prove its identity to a cloud provider directly. The pipeline asks for a short-lived token, the cloud checks who is asking, and no long-lived secret exists anywhere.

How the exchange works

job startssigned OIDC tokencloud checks claimstemporary credentials
fig. 1 — no secret is stored; each job proves who it is and gets credentials for minutes
  1. When a job starts, the CI platform issues it a signed JSON Web Token. Its claims say which repository, branch or environment the job belongs to.
  2. The job presents that token to the cloud provider.
  3. The provider verifies the signature against the CI platform's published keys, checks the claims against a trust policy you wrote, and returns credentials that expire, by default, within the hour.

The token is useless outside that job and expires quickly. The trust policy decides exactly which jobs may use which role.

GitHub Actions and AWS

First, register GitHub as an identity provider in the AWS account: token.actions.githubusercontent.com, with the audience sts.amazonaws.com. Then create a role whose trust policy names the repository and environment allowed to assume it:

payments-deploy trust policy
{
  "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:platform/payments:environment:production"
      }
    }
  }]
}

The workflow needs permission to request the token. After that, the official action does the exchange:

.github/workflows/deploy.yml
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/payments-deploy
          aws-region: eu-west-1
      - run: aws sts get-caller-identity

The subject claim is the whole security model

The sub claim says what the job is, and the trust policy is only as strict as the condition on it. GitHub produces different subjects depending on how the job runs:

Job runsSubject
In a deployment environmentrepo:platform/payments:environment:production
On a branch, without an environmentrepo:platform/payments:ref:refs/heads/main
For a pull requestrepo:platform/payments:pull_request

There are three rules worth enforcing:

  • Never trust pull_request for a role that can change anything. Pull request jobs run code that has not been reviewed yet.
  • Avoid wildcards such as repo:platform/*. They let every repository in the organization assume the role, including the next one somebody creates.
  • Prefer environments over branches for production. GitHub environments can require reviewers and restrict which branches may deploy, and those rules are enforced before the token is issued.

Azure, Google Cloud and GitLab

The same pattern exists everywhere, under different names:

  • Azure: workload identity federation. Add a federated credential to an app registration or a user-assigned managed identity, with the same issuer and subject.
  • Google Cloud: Workload Identity Federation. Create a pool and a provider for the CI issuer, then use an attribute condition to restrict which repositories may impersonate a service account.
  • GitLab CI: jobs request tokens with the id_tokens keyword. Trust policies match on claims such as project_path and ref.

Rolling it out

Moving an existing estate takes days, not months:

  1. Inventory every stored cloud credential in CI settings. Record which pipelines use each one.
  2. Create one role per repository and environment, with only the permissions that pipeline uses. Cloud access logs show what the old key actually did.
  3. Switch pipelines one at a time. Keep the old key until the new path has deployed successfully.
  4. Delete the keys, not just the secrets that held them.
  5. Alert on new access keys being created for CI users, so the old pattern does not creep back.

The result is a deployment path with no stored secret to leak, and a trust policy you can read to see exactly which jobs can touch production.