The Least-Privilege CI Identity Trap: Why Repository-Level GitHub OIDC Bindings Are Not Enough
Enterprise Security · 11 min
Replacing static cloud keys with GitHub OIDC Workload Identity Federation is only step one. If your IAM binding checks only assertion.repository, any pull request or non-main workflow can impersonate your production deployer.
Replacing long-lived JSON service-account keys in GitHub Secrets with OpenID Connect (OIDC) Workload Identity Federation is table stakes for modern cloud security (ADR-008). Yet most reference architectures stop at a single repository-level condition:
# DANGEROUS Anti-Pattern: Do NOT ship this to production
attribute_condition = "assertion.repository == 'org/repo'"
If your cloud IAM binding grants roles/iam.workloadIdentityUser on your production deployer service account to principalSet://.../attribute.repository/org/repo, every workflow trigger in that repository—including pull_request runs from unreviewed branches—can mint a token for the production deployer service account.
When I audited and hardened the CI/CD identity perimeter for this site (ADR-020 and ADR-043), I broke that single over-privileged identity into two distinct service accounts and locked the OIDC provider down to immutable numeric IDs and trigger-scoped claims.
1. Split `tf-planner` (PRs) from `deployer` (Push to `main`)
A pull request should be able to run terraform plan to verify infrastructure diffs, but it must never be able to mutate state or deploy containers. A push to main, conversely, needs to push a verified image digest and roll out the revision.
I provision two separate pipeline identities in terraform/bootstrap/ (workload_identity.tf, deployer.tf, planner.tf) alongside the ai-evals identity in terraform/environments/dev/ai_evals.tf:
tf-planner: Can read Terraform state and describe GCP resources (roles/viewer+ state bucket object reader), bound exclusively to read-only triggers (pull_request, weeklydrift.ymlschedule, andrelease.ymlplan preview).deployer-pipeline: Can push to Artifact Registry and update Cloud Run services, bound exclusively topushandworkflow_dispatchevents onrefs/heads/main,v*release tag pushes, and theproductionenvironment.
2. Pin Numeric `repository_id` and `repository_owner_id` Against Repo-Rename Hijacking
GitHub repository names and organization handles can be renamed or transferred. If an account is renamed and someone registers the old namespace, a name-only OIDC condition can be satisfied by a different repository. Pinning both the human-readable name and GitHub's immutable numeric repository_id and repository_owner_id closes that gap permanently.
Toggle the selector above to compare how I enforce trigger-scoped, least-privilege OIDC conditions across Google Cloud, AWS, and Azure:
[cloud:gcp]
### Google Cloud (Workload Identity Federation — ADR-043)
In GCP, I map a composite custom attribute attribute.event_ref (<event_name>:<ref>, with refs/tags/v* collapsed to release-tag) inside the Workload Identity Pool Provider in terraform/bootstrap/workload_identity.tf:
resource "google_iam_workload_identity_pool_provider" "github" {
workload_identity_pool_id = google_iam_workload_identity_pool.github.workload_identity_pool_id
workload_identity_pool_provider_id = "github-provider"
Provider-level gate: pins immutable numeric repository and owner IDs attribute_condition = join(" && ", [ "assertion.repository == 'cmrcode/charlieroffe-website'", "assertion.repository_id == '1247159083'", "assertion.repository_owner_id == '122574'", ])
attribute_mapping = {
"google.subject" = "assertion.sub"
"attribute.repository" = "assertion.repository"
"attribute.ref" = "assertion.ref"
"attribute.environment" = "assertion.environment"
"attribute.event_name" = "assertion.event_name"
"attribute.event_ref" = "assertion.event_name + ':' + (assertion.ref.startsWith('refs/tags/v') ? 'release-tag' : assertion.ref)"
"attribute.repository_id" = "assertion.repository_id"
"attribute.repository_owner_id" = "assertion.repository_owner_id"
}
oidc {
issuer_uri = "https://token.actions.githubusercontent.com"
}
}
Deployer SA: only assumable on main push/dispatch, v* release tags, or production environment resource "google_service_account_iam_binding" "deployer_workload_identity" { service_account_id = google_service_account.deployer.name role = "roles/iam.workloadIdentityUser" members = [ "${local.pool_principal}/attribute.event_ref/push:refs/heads/main", "${local.pool_principal}/attribute.event_ref/workflow_dispatch:refs/heads/main", "${local.pool_principal}/attribute.event_ref/push:release-tag", "${local.pool_principal}/attribute.environment/production", ] }
Planner SA: only assumable on pull_request, drift schedule, or main dispatch resource "google_service_account_iam_binding" "planner_workload_identity" { service_account_id = google_service_account.planner.name role = "roles/iam.workloadIdentityUser" members = [ "${local.pool_principal}/attribute.event_name/pull_request", "${local.pool_principal}/attribute.event_ref/schedule:refs/heads/main", "${local.pool_principal}/attribute.event_ref/workflow_dispatch:refs/heads/main", ] } ``` [/cloud]
[cloud:aws]
### Amazon Web Services (IAM OIDC Trust Policies)
In AWS IAM, GitHub's sub claim defaults to repo:ORG/REPO:ref:refs/heads/main on push and repo:ORG/REPO:pull_request on PRs. Never use a wildcard repo:ORG/REPO:* on a deployment role; split the planner and deployer IAM roles and lock sub and job_workflow_ref:
# Production Deployer Role: only assumable on push to refs/heads/main
data "aws_iam_policy_document" "deployer_trust" {
statement {
effect = "Allow"
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [aws_iam_openid_connect_provider.github.arn]
}
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:aud"
values = ["sts.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:cmrcode/charlieroffe-website:ref:refs/heads/main"]
}
}
}
PR Planner Role: read-only, scoped strictly to pull_request subject claim data "aws_iam_policy_document" "planner_trust" { statement { effect = "Allow" actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [aws_iam_openid_connect_provider.github.arn]
}
condition {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:sub"
values = ["repo:cmrcode/charlieroffe-website:pull_request"]
}
}
}
```
[/cloud]
[cloud:azure]
### Microsoft Azure (Entra ID Federated Identity Credentials)
Azure Entra ID requires exact literal matches on subject in azurerm_federated_identity_credential (it does not support wildcards). Take advantage of this by creating two separate Managed Identities—one for PR plans and one for main deployments:
resource "azurerm_user_assigned_identity" "deployer" {
name = "id-github-deployer-prod"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
}
resource "azurerm_federated_identity_credential" "deployer_main" {
name = "fic-github-main-push"
resource_group_name = azurerm_resource_group.rg.name
parent_id = azurerm_user_assigned_identity.deployer.id
audience = ["api://AzureADTokenExchange"]
issuer = "https://token.actions.githubusercontent.com"
subject = "repo:cmrcode/charlieroffe-website:ref:refs/heads/main"
}
resource "azurerm_user_assigned_identity" "tf_planner" {
name = "id-github-planner-pr"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
}
resource "azurerm_federated_identity_credential" "planner_pr" {
name = "fic-github-pull-request"
resource_group_name = azurerm_resource_group.rg.name
parent_id = azurerm_user_assigned_identity.tf_planner.id
audience = ["api://AzureADTokenExchange"]
issuer = "https://token.actions.githubusercontent.com"
subject = "repo:cmrcode/charlieroffe-website:pull_request"
}
```
[/cloud]
3. Testing Your CI Identity Perimeter in Vitest
Security controls that only live in HCL can silently regress during refactors. I enforce these invariants in tests/backend/ciIdentity.test.ts, which parses my terraform/bootstrap/*.tf stack and GitHub Actions YAML files on every PR to verify:
- Every workflow job that authenticates to GCP maps to a Workload Identity binding that accepts its exact event, ref, and environment claims.
pull_requestand non-mainbranch runs can never obtain credentials fordeployer-pipelineorai-evals.tf-plannerjobs run strictly read-only with-lock=falseand never executeterraform apply.