Sub topic 11
Replacing long-lived credentials with OIDC
This chapter assumes you can read a GitHub Actions workflow, know what a kubeconfig secret is (covered earlier), and have a basic idea of AWS IAM roles. The example uses Amazon EKS. Other clouds follow the same pattern with different actions.
The problem with stored credentials
A kubeconfig secret or a cloud access key is long-lived. It works until someone rotates it. If it leaks through a log, a fork, or a compromised action, an attacker can use it from anywhere.
OpenID Connect (OIDC) federation removes the stored secret. Each job asks GitHub for a short-lived signed token. The cloud provider checks the token and returns temporary credentials. Nothing reusable sits in your repository settings.
How the exchange works
The token is a JSON Web Token (JWT). It contains claims such as the repository, the branch, and the environment name. AWS compares those claims with the trust policy on the IAM role. If they match, it issues credentials that expire, by default after one hour.
The workflow
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.AWS_ROLE_ARN }}
aws-region: us-east-1
- name: Configure kubectl for EKS
run: aws eks update-kubeconfig --name my-cluster --region us-east-1
- run: kubectl get deployments -n shopKey points:
id-token: writelets the job request an OIDC token. Without it, the job fails when it tries to assume the role.- The role ARN is not secret, so it lives in a repository or environment variable (
vars), not insecrets. aws eks update-kubeconfigwrites a kubeconfig that calls AWS for a fresh token on eachkubectlcommand. No credential is written to disk.
One-time setup
- In AWS IAM, add GitHub as an OIDC identity provider with the URL
https://token.actions.githubusercontent.comand the audiencests.amazonaws.com. - Create an IAM role with a trust policy that allows
AssumeRoleWithWebIdentityfrom that provider. - Restrict the trust policy with a
subcondition, for examplerepo:my-org/my-repo:environment:production. - Map the role to a Kubernetes identity, using an EKS access entry or the
aws-authConfigMap. Bind that identity to the namespaced Role from the RBAC chapter.
Common errors
- Not authorized to perform sts:AssumeRoleWithWebIdentity. The
subclaim does not match the trust policy. When a job usesenvironment: production, the claim becomesrepo:ORG/REPO:environment:production, not a branch reference. - Credentials could not be loaded. The job is missing
id-token: write, or the workflow runs from a fork, which does not receive tokens. kubectlreturns Unauthorized. The IAM role works but is not mapped to a cluster identity.
Exercise
A deploy job fails with "Not authorized to perform sts:AssumeRoleWithWebIdentity". The trust policy requires sub to equal repo:my-org/shop:ref:refs/heads/main, and the job declares environment: production. What is the likely cause and fix?
Show answer
Jobs that declare an environment send a sub claim of the form repo:my-org/shop:environment:production, so the condition never matches. Change the trust policy condition to that value, or remove the environment from the job if you want branch-based matching. The environment form is preferable because it ties the role to the protected environment.
Exercise
Why is the role ARN stored in vars instead of secrets?
Show answer
An ARN identifies the role but grants nothing by itself. Only a valid OIDC token that satisfies the trust policy can assume it, so the ARN does not need secret handling.