Stop Using Service Account Keys: Workload Identity Federation on GCP
From the publisher’s feed, collected in the ZeroLabs newsroom.
Can you drive the car ?When working with public cloud providers, we often need to authenticate workloads to access cloud resources. For years, the most common approach was to export a JSON service account key and store it somewhere in our CI/CD platform, application, or infrastructure.If you have worked in a company with a security team, you have probably heard the same message repeatedly: “Do not use service account keys.”Security teams dislike service account keysBut why are these keys considered risky? And if we should not use them, what is the modern alternative?In this article, we will explore how Google Cloud Workload Identity Federation enables keyless authentication, how OAuth 2.0 token exchange works behind the scenes, and how to implement it end-to-end using Terraform and GitHub Actions.1. Why service account keys are problematicWhen you export a service account key in Google Cloud Platform, many issues appeared. Whoever has this key in his possession can use it. It is difficult to track usage and which person or workloads are leveraginga specific key. It becomes a long-lived credential and the rotation become particularly complex. It is very easy to store it in a file system or even commit in git by accident.In short static long-lived service account keys become a security risk.2. OAuth 2.0 and Token Exchange-the mental modelBefore looking at Workload Identity Federation, it is important to understand the building blocks behind modern cloud authentication.OAuth 2.0OAuth 2.0 is the industry-standard authorization framework used by most modern applications and cloud providers. Instead of sharing long-lived credentials such as passwords or service account keys, OAuth 2.0 relies on short-lived tokens that can be exchanged securely between trusted parties.The key idea is simple:Do not share credentials. Exchange temporary tokens based on a trusted identity.JSON Web Tokens (JWT)A JSON Web Token (JWT) is a signed token used to convey information about an identity.A JWT is composed of three parts:header.payload.signatureFor example:eyJhbGciOi....eyJzdWIiOi....KMUFsIDTn...JWTs are Base64URL-encoded and can be inspected using tools such as jwt.io.When decoded, a JWT typically contains:Header:{ "alg": "RS256", "typ": "JWT"}Payload:{ "iss": "https://token.actions.githubusercontent.com", "sub": "repo:my-org/my-repo:ref:refs/heads/main", "aud": "google-cloud", "iat": 1710000000, "exp": 1710003600}Common JWT claims include:iss (Issuer): who issued the tokensub (Subject): the identity represented by the tokenaud (Audience): who the token is intended foriat (Issued At): token creation timeexp (Expiration): token expiration timeThe JWT is digitally signed by the issuer. If an attacker modifies any part of the token, the signature validation will fail and the token will be rejected.The resource server verifies the signature using the issuer’s public key, ensuring the token has not been tampered with.OpenID Connect (OIDC)OpenID Connect (OIDC) is an identity layer built on top of OAuth 2.0.Its purpose is to allow a client to prove its identity.In our example, Github Actions acts as an Identity Provider (IdP) and can issue an OIDC token representing a specific workflow execution.Identity Provider (IdP)An Identity Provider is a trusted system that authenticates identities and issues signed tokens.Examples of IdP include:Github ActionsMicrosoft Entra IDOktaGoogle IdentityAuth0Resource ServerA Resource Server is the system protecting resources that require authorization.Examples include:Google Cloud APIsCloud StorageBigQueryCloud RunThe Resource Server validates the token before granting access.Workload Identity FederationWorkload Identity Federation allows Google Cloud to trust identities coming from external Identity Providers.Instead of storing a long-lived JSON service account key in Github Secrets, Github presents a short-lived OIDC token to Google Cloud.Google Cloud validates the token, verifies that the workflow matches the configured trust conditions, and issues temporary credentials that can be used to access cloud resources.This approach eliminates the need to manage service account keys while significantly reducing the impact of credential leakage.Let us assume we want to deploy a Cloud Run application from GitHub Actions. The workflow needs to authenticate against Google Cloud. Instead of exporting a JSON service account key and storing it in GitHub Secrets, we will leverage Workload Identity Federation and short-lived credentials.3. Workload Identity Federation ArchitectureWorkload Identity Federation in action in GCPGoogle Cloud Platform trusts GitHub identities under specific conditions🤔 Workload Identity Federation does not replace service accounts ! A service account is the technical identity that has permissions inside Google Cloud Platform. Workload Identity Federation only replaces the mechanism used to authenticate and obtain those permissions.4. Concrete Terraform ImplementationYou will need in this section to have:GCP Project IDGCP Project NumberName of the github repositoryIn the following example, we create through terraform:a workload identity poola workload identity provider (a pool can have multiple providers)attribute mappingthe issuer uri allows Google Cloud Platform to verify the issuer and the signatureresource "google_iam_workload_identity_pool" "github_wip" { project = var.project_id workload_identity_pool_id = "github-wip" display_name = "github-wip" description = "Workload Identity pool for github actions"}resource "google_iam_workload_identity_pool_provider" "github_provider" { project = var.project_id workload_identity_pool_id = google_iam_workload_identity_pool.github_wip.workload_identity_pool_id workload_identity_pool_provider_id = "github-provider" attribute_mapping = { "google.subject" = "assertion.sub" "attribute.actor" = "assertion.actor" "attribute.aud" = "assertion.aud" "attribute.repository" = "assertion.repository" } attribute_condition = "attribute.repository == assertion.repository" oidc { issuer_uri = "https://token.actions.githubusercontent.com" }}We also have to provision a service account with proper permissions to be used in the github actions:### Service Account for CI/CD through github actionsmodule "service_account_github_actions_cicd" { source = "terraform-google-modules/service-accounts/google" version = "~> 4.0" project_id = var.project_id names = ["${var.sa_gh_cicd}"] project_roles = [ "${var.project_id}=>roles/run.developer", ]}resource "google_service_account_iam_member" "sa_gh_ingest_wi_user" { service_account_id = module.service_account_github_actions_cicd.service_account.name role = "roles/iam.workloadIdentityUser" member = "principalSet://iam.googleapis.com/projects/${var.project_number}/locations/global/workloadIdentityPools/github-wip/attribute.repository/"}5. GitHub Actions ExampleYou will need the following information to use Workload Identity Federation from github actions:GCP project number you want to deploy componentsPool IDPool Providerservice account you want to impersonate deploy: environment: ${{ github.ref_name }} runs-on: ubuntu-latest permissions: contents: 'read' id-token: 'write' steps: - name: Checkout code repository uses: actions/checkout@v6 - name: Authentication to Google Cloud Platform uses: google-github-actions/auth@v3 with: token_format: access_token workload_identity_provider: 'projects/${{ env.GCP_PROJECT_NUMBER }}/locations/global/workloadIdentityPools/${{ env.WIF_POOL_ID }}/providers/${{ env.WIF_POOL_PROVIDER }}' service_account: ${{ env.SERVICE_ACCOUNT_GH }} access_token_lifetime: 300sYou first need to grant access for your repository to claim for a github token that will be needed in case of OIDC exchange. This step will store an access token for further steps. For instance, one may leverages gcloud cli to deploy cloud run service like:- name: Deploy Cloud Run Service (auth required and internal ingress) run: | IMAGE=${{ env.GCP_REGION }}-docker.pkg.dev/${{ env.GCP_PROJECT_ID }}/cloud-run/app:${{ github.sha }} gcloud run deploy ${{ env.PROJECT }} \ --image=$IMAGE \ --region=${{ env.GCP_REGION }} \ --platform=managed \ --memory=1Gi \ --no-allow-unauthenticated \ --ingress=internal-and-cloud-load-balancing \ --service-account=${{ env.SERVICE_ACCOUNT_RUNTIME }} \ --set-env-vars GCP_PROJECT_ID=${{ env.GCP_PROJECT_ID }} \ --set-env-vars INPUT_DATASET=${{ env.INPUT_DATASET }} \ --set-env-vars OUTPUT_DATASET=${{ env.OUTPUT_DATASET }}6. Security TakeawaysWorkload Identity Federation fundamentally changes how workloads authenticate to Google Cloud. Instead of distributing long-lived JSON service account keys, we establish a trust relationship between Google Cloud and an external Identity Provider such as GitHub Actions.This approach provides several security and operational benefits:No static credentials to store in CI/CD systems or secret managers.Short-lived access tokens that expire automatically.Reduced blast radius in the event of credential leakage.Auditable identities, allowing us to trace which repository, workflow, or branch performed an action.Simplified credential management, removing the burden of key rotation.Least-privilege access through Service Account impersonation and IAM.Ephemeral trust, where access is granted only for the duration of a specific workload execution.The future of cloud authentication is not better secret management. It is eliminating long-lived secrets altogether.Stop Using Service Account Keys: Workload Identity Federation on GCP was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.