A service account key JSON stored as a GitHub secret is a long-lived Google Cloud credential that, by default, never expires, and any workflow step that can read it can use it from anywhere. Workload Identity Federation replaces that key: GitHub issues a short-lived OIDC token for each job, Google's Security Token Service checks it against a workload identity pool provider you control, and the job receives a short-lived Google credential. This guide walks through the Google Cloud side in the order you build it: the pool, the provider, attribute mapping, the attribute condition, the IAM binding (direct access or service account impersonation) and the workflow, then how to log, test and retire the old keys. It is for platform, DevOps and cloud security engineers who already know Google Cloud IAM.
Key takeaways
- The attribute condition is not optional. GitHub uses one issuer,
https://token.actions.githubusercontent.com, for every organisation, so a provider without a condition admits tokens from any repository on GitHub. - Use numeric IDs, not names. Condition on
repository_owner_idandrepository_id; names can be re-registered after an organisation or repository is deleted. - Map before you match. Google can only evaluate claims you have mapped to attributes, and mapping errors are the most common cause of failed exchanges.
- Prefer direct resource access. Google recommends granting roles straight to the federated principal; use service account impersonation only where an API needs it or you need OAuth access tokens or ID tokens.
- Scope the binding, not just the pool. Grant roles to a
principalSetfor one repository (or a single subject), never to the whole pool.
How the exchange works
Each job that declares id-token: write can ask GitHub for an OIDC token. The google-github-actions/auth action requests one with the audience set to the full provider resource name, sends it to the Security Token Service (STS) ExchangeToken method, and receives a federated access token. Google checks the token's issuer and signature, applies the provider's attribute mapping, evaluates the attribute condition, and only then issues credentials. With impersonation, a second call to the IAM Service Account Credentials API (GenerateAccessToken) swaps the federated token for a service account access token.
| Component | Where it lives | What you set | Security role |
|---|---|---|---|
| Workload identity pool | Google Cloud project (dedicated project recommended) | Pool ID, display name, location global | Container for external identities; one pool per identity provider |
| OIDC provider | Inside the pool | Issuer URI, attribute mapping, attribute condition, allowed audiences | Decides which GitHub tokens are admitted and how they are named |
| IAM binding | On the target resource or service account | principal:// or principalSet:// member and a role | Decides what an admitted identity can do |
| Workflow | GitHub repository | permissions: id-token: write, workload_identity_provider, optional service_account | Requests the token and exports Application Default Credentials |
| Audit logs | Projects holding pools and service accounts | Data Access logs for IAM and STS | Records every exchange and impersonation |
Before you start
- Project: Google's best practice is a single, dedicated project for workload identity pools and providers, separate from the projects holding the resources. This keeps the power to edit providers away from application teams.
- Role: the person creating the pool needs Workload Identity Pool Admin (
roles/iam.workloadIdentityPoolAdmin). Owner also works but should not be used in production. - Organisation policy: Google recommends setting
constraints/iam.workloadIdentityPoolProvidersto deny all at the organisation root and allowing only trusted issuers, such ashttps://token.actions.githubusercontent.com, in the federation project. This stops anyone creating a rogue provider elsewhere. See the Google Cloud Organization Policy Guide. - Identifiers: look up the numeric organisation and repository IDs from the GitHub REST API (the
idfield of the organisation and repository objects) and record them. You will also need the Google Cloud project number, not the project ID: federation resource names only accept the number. - Claim review: decide which GitHub claims express "this workflow may deploy". The
subclaim encodes the repository plus the branch, tag, environment orpull_requestcontext;ref,environment,workflow_ref,job_workflow_refandrunner_environment(github-hostedorself-hosted) are available separately.
Step-by-step setup
- Create the pool.
gcloud iam workload-identity-pools create "github" --project="${PROJECT_ID}" --location="global" --display-name="GitHub Actions Pool". Use one pool for GitHub only: Google advises one provider per pool and a separate pool per identity provider, to avoid subject collisions. - Create the OIDC provider with mapping and condition in one step. Use
gcloud iam workload-identity-pools providers create-oidcwith--issuer-uri="https://token.actions.githubusercontent.com", an--attribute-mappingand an--attribute-condition. Never create the provider first and add the condition later: in the gap, any GitHub repository can enter the pool. - Map the claims you will use. At minimum
google.subject=assertion.sub. Addattribute.repository_id=assertion.repository_id,attribute.repository_owner_id=assertion.repository_owner_idand any claim you plan to use in a condition or binding, such asattribute.ref=assertion.reforattribute.environment=assertion.environment. Mapped attribute values are limited to 127 bytes, which long repository or branch names can exceed. - Write the condition on immutable IDs. A minimal, robust condition is
assertion.repository_owner_id == '<ORG_ID>'. Tighten it where you can, for example adding&& assertion.repository_id == '<REPO_ID>'or&& assertion.ref == 'refs/heads/main'. Values are case-sensitive in Google Cloud even where GitHub is not, so copy them from a real token. - Keep the default audience. Leave allowed audiences at the default, which is the provider's full resource name. The auth action sends exactly that value. Google lists requiring the provider URL as audience as a protection against confused deputy attacks.
- Grant access. For direct access, bind a role on the resource to a
principalSetfor one repository, for exampleprincipalSet://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/github/attribute.repository_id/REPO_ID(this requires therepository_idmapping). For impersonation, grantroles/iam.workloadIdentityUseron one dedicated service account to that sameprincipalSet, and give the service account the resource roles. - Configure the workflow. In the job, set
permissions: contents: readandid-token: write, runactions/checkoutbefore the auth step, then callgoogle-github-actions/authwithworkload_identity_providerset toprojects/PROJECT_NUMBER/locations/global/workloadIdentityPools/github/providers/PROVIDER_ID. Addservice_accountonly for impersonation. Pin the action to a full commit SHA rather than a tag; see How to Pin GitHub Actions to Commit SHAs. - Test from a branch that should fail. Run once from an allowed branch or environment and once from one that is not allowed. The failed run proves the condition is doing work. Allow up to five minutes after any pool, provider or IAM change: these resources are eventually consistent.
- Retire the key. Once runs succeed, delete the service account key in Google Cloud (deleting only the GitHub secret leaves the key valid), then remove the
credentials_jsonsecret. See How to inventory Google Cloud service account keys and replace them with federation.
Rollback: if a deployment breaks, fix the mapping, condition or binding rather than restoring a key. Keep the old key disabled, not deleted, for a short agreed window only if a rollback path is genuinely required, and record the exception with a date.
Direct access or service account impersonation
| Question | Direct resource access | Impersonation through a service account |
|---|---|---|
| Google's position | Recommended default | Use where an API has limitations or a service account is required |
| What the job receives | Federated token from STS | Service account OAuth access token (or ID token) via GenerateAccessToken |
| Credential lifetime | Short; the auth action's documentation notes these tokens last minutes, not hours | access_token_lifetime default 3600s, maximum one hour unless constraints/iam.allowServiceAccountCredentialLifetimeExtension allows up to 12 hours |
| Where roles are granted | On each resource, to the principalSet | On the service account; the principalSet only needs roles/iam.workloadIdentityUser |
| Audit trail | Resource logs show the federated principal | Resource logs show the service account; the federated subject appears in the GenerateAccessToken log entry |
| Typical fit | Artifact Registry pushes, Secret Manager reads, Cloud Storage uploads where supported | Tools that need an access token or ID token, APIs that do not accept federated principals, Domain-Wide Delegation |
Decision guidance: start with direct access for each target resource. Fall back to impersonation per API when a call fails because the service does not accept federated principals, or when a downstream step needs token_format: access_token or id_token. In large estates, impersonation with one service account per application and environment is often easier to review because role grants stay in one place; small teams with a few resources usually find direct grants simpler. Do not grant roles/iam.serviceAccountTokenCreator to the federated identity unless Domain-Wide Delegation is truly needed.
Writing conditions and bindings that hold
Admission and authorisation are two layers. The attribute condition decides who may enter the pool at all; the IAM binding decides what each admitted identity may do. Use both.
| Goal | Condition (pool admission) | Binding (resource access) |
|---|---|---|
| Only your organisation | assertion.repository_owner_id == 'ORG_ID' | Per repository principalSet |
| One repository deploys to production | Organisation ID plus assertion.repository_id == 'REPO_ID' | attribute.repository_id/REPO_ID on production resources only |
| Production only from protected environment | Add assertion.environment == 'production' | Separate mapped attribute for environment, bound only on production |
| Only GitHub-hosted runners | Add assertion.runner_environment == 'github-hosted' | Unchanged |
| Only a central reusable workflow may deploy | Condition on assertion.job_workflow_ref matching the approved workflow path and ref | Bind production roles to that attribute value |
- Environments beat branches for production: GitHub environments can require reviewers and limit which branches deploy, so a condition on
environmentinherits those protections. Note that when a job references an environment, thesubclaim uses the environment form (repo:ORG/REPO:environment:NAME) rather than the branch form. - Immutable subject format: GitHub now documents an immutable
subformat (repo:OWNER@OWNER-ID/REPO@REPO-ID:...) for repositories created after 15 July 2026 or that opt in. If any condition orprincipal://member matches onsub, check which format your repositories emit before writing it. - Pull requests: do not bind deployment roles to the
pull_requestsubject. Code from a pull request should not obtain production credentials. See the GitHub Actions Script Injection Guide. - Restrict provider edits: anyone with
iam.googleapis.com/workloadIdentityPoolProviders.updatecan relax a condition. Limit Workload Identity Pool Admin to the platform team. See the How to Scope CI OIDC Trust to Branch and Environment guide for patterns that apply across CI systems.
Logging, detection and metrics
Token exchanges are only visible if you turn them on. Enable Data Access audit logs (Admin Read) for the IAM API and the Security Token Service API in the federation project and in every project holding service accounts used for impersonation.
- Exchange events:
google.identity.sts.v1.SecurityTokenService.ExchangeTokenentries in thedata_accesslog carry the GitHubsubinprotoPayload.authenticationInfo.principalSubject, the mappedprincipal://identifier inprotoPayload.metadata.mapped_principaland the provider inprotoPayload.resourceName. Client errors such as rejected credentials are not logged there, so a failing condition shows up in the workflow, not in this log. - Impersonation events:
GenerateAccessTokenentries name the target service account inresource.labels.email_idand the federated subject inprincipalSubject. - Alerts: provider create, update or delete outside change control; an exchange whose subject belongs to a repository or environment not in your approved list; a new service account key created for an identity that has moved to federation. See the GCP Audit Log Principal Fields guide for reading these entries.
Metrics
- Number of GitHub repositories still using
credentials_json(target zero). - Active user-managed keys on CI service accounts (target zero).
- Providers without an attribute condition, and providers outside the federation project (target zero).
- Bindings granted to the whole pool (
workloadIdentityPools/POOL_ID/*) (target zero). - Share of deployments using direct access rather than impersonation.
Common failure patterns
- No attribute condition: because GitHub's issuer is shared, any repository on GitHub can be admitted, leaving only the IAM binding between an attacker's workflow and your project.
- Name-based conditions: conditions on
repository_ownerorrepositorynames can be satisfied by someone who registers the same name after you rename or delete it. - Pool-wide grants: granting
roles/iam.workloadIdentityUserto.../workloadIdentityPools/POOL_ID/*lets every repository the condition admits impersonate the service account. - Keys left behind: teams add federation but keep the key JSON secret "just in case". Stored keys are what leak when a workflow is compromised: in the tj-actions/changed-files compromise, a poisoned action printed runner secrets, including cloud access keys, into workflow logs. Federated credentials can still be exposed in such an attack, but they expire within the hour.
- Credentials file in artefacts: the auth action writes a
gha-creds-*.jsonfile into the workspace. Add it to.gitignoreand.dockerignoreso it is never committed or baked into an image. - Wrong identifiers: using the pool name instead of the provider name, or the project ID instead of the project number, in
workload_identity_provider. Errors mentioning "Failed to generate Google Cloud federated token" point at admission (condition or mapping); "Failed to generate OAuth 2.0 Access Token" points at impersonation. See the GitHub Actions OIDC Errors Troubleshooting Guide.
Framework mapping
| Framework | Reference | How federation supports it |
|---|---|---|
| NIST SP 800-53 Rev 5 | IA-5 Authenticator Management | Removes long-lived keys in favour of short-lived, automatically issued credentials |
| NIST SP 800-53 Rev 5 | IA-9 Service Identification and Authentication | Each job authenticates as a named workload identity |
| NIST SP 800-53 Rev 5 | AC-6 Least Privilege | Per repository and environment bindings |
| NIST SP 800-53 Rev 5 | AU-2 Event Logging | STS and IAM Data Access logs |
| NIST CSF 2.0 | PR.AA-01, PR.AA-05 | Workload credentials managed and access scoped |
| NIST SP 800-204D | Software supply chain security in CI/CD pipelines | Short-lived pipeline credentials and protected build identities |
| MITRE ATT&CK | T1552.001 Credentials In Files, T1078.004 Cloud Accounts | Removes stored key files that attackers harvest and reuse |
Where this fits
This page is the Google Cloud build. For cross-cloud hardening rules for GitHub's OIDC tokens (claim choice, reusable workflows, review cadence) see the GitHub Actions OIDC Federation Hardening Guide, and for Google Cloud service account design see the Google Cloud IAM and Service Accounts Guide.
Practitioner checklist
- Create pools and providers only in a dedicated federation project.
- Set
constraints/iam.workloadIdentityPoolProvidersto deny by default and allow only GitHub's issuer where needed. - Create every provider with an attribute condition on
repository_owner_id, plus repository or environment where possible. - Map every claim used in a condition or binding; keep
google.subject=assertion.sub. - Grant roles to a per-repository
principalSet, never to the whole pool. - Prefer direct resource access; use one dedicated service account per application where impersonation is needed.
- Set
id-token: writeonly on jobs that need Google access and pin the auth action to a commit SHA. - Test a denied branch or environment before declaring the setup done.
- Delete the old service account keys in Google Cloud, then the GitHub secrets.
- Enable IAM and STS Data Access logs and alert on provider changes and new keys.
Standards and references
- Google Cloud: Configure Workload Identity Federation with deployment pipelines
- Google Cloud: Best practices for using Workload Identity Federation
- Google Cloud: Example logs for Workload Identity Federation
- GitHub Docs: OpenID Connect reference
- google-github-actions: auth action documentation
- google-github-actions: Security considerations
- NIST: SP 800-204D Strategies for the Integration of Software Supply Chain Security in DevSecOps CI/CD Pipelines
Related NHI Mgmt Group resources: GitHub Actions OIDC Federation Hardening Guide · How to inventory Google Cloud service account keys and replace them with federation · Google Cloud IAM and Service Accounts Guide · Cross-cloud workload identity federation