Topic 3 of 3
Section 4.2 ends with the secure ways to authenticate as a service account: impersonating it, using short-lived credentials, and federating external identities without keys.
Contents
An authenticated principal (a user or another service account) gets a token for the service account and uses it to act as that account, with all its permissions. There are always two identities: the impersonator and the impersonated account. It's similar to AWS AssumeRole.
iam.serviceAccounts.getAccessToken, included in Service Account Token Creator (roles/iam.serviceAccountTokenCreator).gcloud iam service-accounts add-iam-policy-binding \ deployer@my-project.iam.gserviceaccount.com \ --member=user:ana@example.com --role=roles/iam.serviceAccountTokenCreator
# A single command gcloud storage buckets list \ --impersonate-service-account=deployer@my-project.iam.gserviceaccount.com # Every gcloud command gcloud config set auth/impersonate_service_account \ deployer@my-project.iam.gserviceaccount.com # Client libraries on your machine (ADC file) gcloud auth application-default login \ --impersonate-service-account deployer@my-project.iam.gserviceaccount.com
In all three cases gcloud requests short-lived credentials for the service account and makes the calls with them.
According to Google, they are the most secure way to authenticate as a service account: an OAuth 2.0 access token that expires after 1 hour by default and isn't refreshed automatically. They carry less risk than keys.
--impersonate-service-account, with an account attached to a VM (client libraries use ADC), or with Workload Identity Federation.iam.allowServiceAccountCredentialLifetimeExtension constraint. ID tokens last 1 hour.With impersonation
Most audit logs record both identities: the impersonated account and whoever created the credentials. Exceptions: App Engine and GKE don't log who created the credentials.
With keys or attached accounts
Only the service account appears, not the person who used the key or ran code on the VM. That creates privilege-escalation and non-repudiation risks.
ADC is the strategy the libraries use to find credentials automatically based on the environment, so the same code works in development and production. Search order:
GOOGLE_APPLICATION_CREDENTIALS environment variable (a Workload or Workforce Identity Federation configuration file, or a key, which isn't recommended).gcloud auth application-default login.The order doesn't say which is better; only where it looks first.
Gives on-premises or multicloud workloads access to Google Cloud with federated identities, without service account keys. It works with AWS, Azure, on-premises Active Directory, GitHub, GitLab, X.509 certificates, and any IdP that supports OIDC or SAML 2.0.
| Concept | What it is |
|---|---|
| Workload identity pool | Groups external identities. Google recommends one pool per environment (development, staging, production) |
| Provider | The trust relationship with the IdP (AWS, Entra ID, GitHub, Okta…) |
| Attribute mapping | CEL expressions that map IdP claims to google.subject (required), google.groups, and up to 50 attribute.* |
| Attribute condition | A CEL expression that accepts or rejects credentials; limits which identities can authenticate |
principal://… or principalSet://… on resources. The alternative is impersonating a service account, which needs roles/iam.workloadIdentityUser.It's the recommended way for pods to access Google Cloud APIs: each app has its own identity and gets roles through IAM, with no key files. Google manages the pool and provider.
PROJECT_ID.svc.id.goog, registers the cluster as a provider, and deploys the GKE metadata server on every node.gcloud container clusters update my-cluster --location=europe-west1 \ --workload-pool=my-project.svc.id.goog gcloud container node-pools update default-pool --cluster=my-cluster \ --location=europe-west1 --workload-metadata=GKE_METADATA # Give access to the Kubernetes ServiceAccount "reader" in namespace "app" gcloud storage buckets add-iam-policy-binding gs://app-data --role=roles/storage.objectViewer \ --member=principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/my-project.svc.id.goog/subject/ns/app/sa/reader
Federates users (employees, partners, contractors) from an external OIDC or SAML 2.0 IdP (Entra ID, AD FS, Okta…) so they sign in to Google Cloud with SSO. It doesn't store accounts in Google Cloud, so no sync is needed with Google Cloud Directory Sync. Workforce identity pools are configured at the organization level and access is granted with allow policies.
Workload Identity Federation
Federates workloads: apps, pipelines, pods.
Workforce Identity Federation
Federates people with SSO from their IdP.
| Situation | Answer | Why |
|---|---|---|
| Test whether a service account has enough permissions before deploying | Impersonate it with --impersonate-service-account | No keys; short-lived credentials. |
| Give a user temporary elevated access without changing resource policies | Token Creator on a service account with those permissions | Impersonation for temporary elevation. |
| Make gcloud always use a service account on your machine | gcloud config set auth/impersonate_service_account | Configuration property. |
| Develop locally an app that only works as a service account | gcloud auth application-default login --impersonate-service-account | Client libraries impersonate the account. |
| A GitHub Actions pipeline must deploy to Google Cloud | Workload Identity Federation with a GitHub OIDC provider | No service account keys. |
| An app on AWS must read from BigQuery | Workload Identity Federation with an AWS provider | Token exchange with the Security Token Service. |
| A GKE pod must write to a bucket | Workload Identity Federation for GKE and a role for the Kubernetes ServiceAccount | Always on in Autopilot. |
| Employees must sign in with Okta without syncing accounts to Google | Workforce Identity Federation | SSO without GCDS. |
❓ Which role do you need to impersonate a service account?
Service Account Token Creator (iam.serviceAccounts.getAccessToken). Service Account User is for attaching it to resources.
❓ Can you impersonate a service account from the console?
No. The console always uses your user credentials; impersonation is done with gcloud, ADC, or the API.
❓ How long does a short-lived access token last by default?
1 hour. It can be extended to 12 hours with the iam.allowServiceAccountCredentialLifetimeExtension constraint.
❓ An app on Azure needs access to Google Cloud. Do you create a key?
No. Workload Identity Federation avoids keys; it's the recommended option.
❓ Does enabling Workload Identity Federation for GKE give pods permissions?
No. You must grant roles to the Kubernetes ServiceAccount principal.
❓ Workforce or Workload Identity Federation for employees signing in with their IdP?
Workforce: it federates people. Workload federates applications.
Did you get this topic?
Test what you just learned
A GitHub Actions pipeline deploys to Cloud Run using a service account JSON key stored as a repository secret. The security team wants to get rid of that key. What do you do?
Original questions, each with a full explanation.
Start a mock examSign in to save your progress.
You finished the last topic of this deep dive.
Back to the index →