TExcelTAD
Home › Secrets & Policy as Code: Vault, SOPS, OPA & Conftest › JWT/OIDC Auth for CI Pipelines

JWT/OIDC Auth for CI Pipelines

The classic way to give a pipeline access to Vault is a long-lived token stored as a CI secret. It never expires, it is usable from anywhere, and it ends up shared by every job in the repository. OIDC federation replaces it: GitHub Actions and GitLab CI can mint a short-lived, signed JWT for each job, describing exactly which repository, branch, environment and workflow is running. Vault's jwt auth method verifies the signature and the claims, and returns a Vault token for that job only.

GitHub Actions job ──(1) request ID token (aud=https://github.com/acme)──► GitHub OIDC provider
                   ◄─(2) signed JWT: repo, ref, environment, workflow ───┘
                   ──(3) auth/jwt/login role=linkly-deploy jwt=… ──► Vault
                                     verifies signature (JWKS) + bound claims
                   ◄─(4) Vault token, 10 min, policy linkly-deploy ─┘

The same pattern gives pipelines AWS, Azure and GCP credentials without stored keys (covered in Hardening Workflows).

The claims that matter

A GitHub Actions ID token for a deploy job looks like this (abridged):

json
{
  "iss": "https://token.actions.githubusercontent.com",
  "aud": "https://github.com/acme",
  "sub": "repo:acme/linkly:environment:production",
  "repository": "acme/linkly",
  "ref": "refs/heads/main",
  "environment": "production",
  "job_workflow_ref": "acme/linkly/.github/workflows/deploy.yml@refs/heads/main",
  "exp": 1793091600
}

The Vault role decides which of these must match. Binding only repository lets every branch and every pull-request workflow in that repository read production secrets. Bind the ref or, better, the environment, which GitHub protects with required reviewers and branch rules (Environments & Manual Approvals).

Trying it offline

The sandbox has no GitHub, so the session plays GitHub's part: it creates a signing key, configures Vault to trust its public key (jwt_validation_pubkeys, instead of oidc_discovery_url against the real issuer), and mints two tokens with openssl. Vault does exactly the checks it would do for a real job:

bash · try it yourself
vault server -dev -dev-root-token-id=root -dev-listen-address=127.0.0.1:8200 > vault.log 2>&1 &
for i in $(seq 50); do vault status > /dev/null 2>&1 && break; sleep 0.2; done
export VAULT_TOKEN=root
vault kv put -mount=secret linkly/db password='c0rrect-h0rse' > /dev/null
printf 'path "secret/data/linkly/*" {\n  capabilities = ["read"]\n}\n' | vault policy write linkly-deploy - > /dev/null

# play the CI provider: a signing key pair
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out issuer.key 2> /dev/null
openssl pkey -in issuer.key -pubout -out issuer.pub

vault auth enable jwt > /dev/null
vault write auth/jwt/config jwt_validation_pubkeys=@issuer.pub \
  bound_issuer=https://token.actions.githubusercontent.com > /dev/null
vault write auth/jwt/role/linkly-deploy - > /dev/null <<'JSON'
{
  "role_type": "jwt",
  "user_claim": "repository",
  "bound_audiences": ["https://github.com/acme"],
  "bound_claims": {"repository": "acme/linkly", "environment": "production"},
  "token_policies": ["linkly-deploy"],
  "token_ttl": "10m",
  "token_type": "batch"
}
JSON

b64url() { openssl base64 -A | tr '+/' '-_' | tr -d '='; }
mint() {   # $1 = environment claim; signs a GitHub-shaped ID token valid for 5 minutes
  local now=$(date +%s)
  local header=$(printf '{"alg":"RS256","typ":"JWT"}' | b64url)
  local claims=$(printf '{"iss":"https://token.actions.githubusercontent.com","aud":"https://github.com/acme","sub":"repo:acme/linkly:environment:%s","repository":"acme/linkly","ref":"refs/heads/main","environment":"%s","iat":%d,"exp":%d}' \
    "$1" "$1" $now $((now + 300)) | b64url)
  local sig=$(printf '%s.%s' "$header" "$claims" | openssl dgst -sha256 -sign issuer.key -binary | b64url)
  printf '%s.%s.%s' "$header" "$claims" "$sig"
}

echo "--- deploy job in the production environment"
TOKEN=$(vault write -field=token auth/jwt/login role=linkly-deploy jwt=$(mint production))
echo "token type: ${TOKEN:0:4}"
VAULT_TOKEN=$TOKEN vault kv get -mount=secret -field=password linkly/db; echo
echo "--- a job in the staging environment of the same repo"
vault write auth/jwt/login role=linkly-deploy jwt=$(mint staging) 2>&1 | grep -E '^\*'
--- deploy job in the production environment
token type: hvb.
c0rrect-h0rse
--- a job in the staging environment of the same repo
* error validating claims: claim "environment" does not match any associated bound claim values

The staging job carries a perfectly valid signature and is still refused, because the role binds environment=production. token_type: batch fits CI: thousands of jobs, each token used for a few minutes, nothing to store or revoke.

In a real setup, replace jwt_validation_pubkeys with discovery, so Vault fetches GitHub's rotating keys itself:

bash
vault write auth/jwt/config \
  oidc_discovery_url=https://token.actions.githubusercontent.com \
  bound_issuer=https://token.actions.githubusercontent.com

GitHub Actions

yaml
# .github/workflows/deploy.yml (non-run)
permissions:
  id-token: write          # allow the job to request an OIDC token
  contents: read
jobs:
  deploy:
    runs-on: ubuntu-24.04
    environment: production
    steps:
      - uses: actions/checkout@v7
      - name: Read linkly's DB password from Vault
        uses: hashicorp/vault-action@v4      # check the current major; pin to a full commit SHA
        with:
          url: https://vault.acme.internal:8200
          method: jwt
          role: linkly-deploy
          jwtGithubAudience: https://github.com/acme
          secrets: |
            secret/data/linkly/db password | DB_PASSWORD
      - run: ./deploy.sh      # DB_PASSWORD is in the environment and masked in logs

GitLab CI

GitLab issues ID tokens through id_tokens; the audience is whatever you choose:

yaml
# .gitlab-ci.yml (non-run)
deploy:
  environment: production
  id_tokens:
    VAULT_ID_TOKEN:
      aud: https://vault.acme.internal
  script:
    - export VAULT_TOKEN=$(vault write -field=token auth/jwt/login role=linkly-deploy jwt=$VAULT_ID_TOKEN)
    - export DB_PASSWORD=$(vault kv get -field=password -mount=secret linkly/db)
    - ./deploy.sh

GitLab's claims include project_path, ref, ref_protected and environment; bind ref_protected: "true" so unprotected branches can't log in. GitLab Premium also has a secrets: keyword that fetches Vault secrets without scripting.

Test yourself

Why is binding only the repository claim on a CI role risky?

  1. It is too slow
  2. Every branch, pull request and workflow in the repository could log in and read the role's secrets
  3. Vault rejects it
  4. Repository names change daily

What does a GitHub Actions workflow need to request an OIDC token?

  1. A PAT secret
  2. permissions: id-token: write
  3. A self-hosted runner
  4. secrets: inherit

Why are batch tokens a good fit for CI logins?

  1. They never expire
  2. They are cheap, not stored in Vault and expire on their own, which suits many short jobs
  3. They can create child tokens
  4. They have root access