Brains4Cloud
ESEN
Practice

Topic 3 of 3

D4 · Configuring access and security

Impersonation, short-lived credentials, and Workload Identity

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.

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.

  • Required permission: iam.serviceAccounts.getAccessToken, included in Service Account Token Creator (roles/iam.serviceAccountTokenCreator).
  • Uses: temporary elevated access without changing policies, testing whether a set of permissions is enough, developing locally apps that only work as a service account, and authenticating external apps.
  • In the console you always authenticate with your user credentials: you can't impersonate a service account there.
  • Using an attached account or a key isn't impersonation: only the account's identity is involved.
gcloud iam service-accounts add-iam-policy-binding \
  deployer@my-project.iam.gserviceaccount.com \
  --member=user:ana@example.com --role=roles/iam.serviceAccountTokenCreator

Impersonation with gcloud and ADC

# 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.

Short-lived credentials

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.

  • Often they are obtained automatically: with --impersonate-service-account, with an account attached to a VM (client libraries use ADC), or with Workload Identity Federation.
  • The Service Account Credentials API creates them manually: OAuth 2.0 access tokens, OpenID Connect ID tokens, self-signed JWTs, and self-signed blobs.
  • An access token's maximum lifetime can be extended to 12 hours by adding the account to the iam.allowServiceAccountCredentialLifetimeExtension constraint. ID tokens last 1 hour.
  • Self-impersonation isn't allowed: using an account's short-lived token to generate another token for the same account (except with self-signed JWTs), so a stolen token can't be refreshed indefinitely.
  • Credential Access Boundaries downscope a short-lived token's permissions, but only for Cloud Storage.

Impersonation and audit logs

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.

Application Default Credentials

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:

  1. The GOOGLE_APPLICATION_CREDENTIALS environment variable (a Workload or Workforce Identity Federation configuration file, or a key, which isn't recommended).
  2. The file created with gcloud auth application-default login.
  3. The attached service account, returned by the metadata server.

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.

ConceptWhat it is
Workload identity poolGroups external identities. Google recommends one pool per environment (development, staging, production)
ProviderThe trust relationship with the IdP (AWS, Entra ID, GitHub, Okta…)
Attribute mappingCEL expressions that map IdP claims to google.subject (required), google.groups, and up to 50 attribute.*
Attribute conditionA CEL expression that accepts or rejects credentials; limits which identities can authenticate
  • The IdP gives a credential to the Security Token Service, which verifies it and returns a federated token (OAuth 2.0 token exchange).
  • Google recommends direct access: grant roles to principal://… or principalSet://… on resources. The alternative is impersonating a service account, which needs roles/iam.workloadIdentityUser.
  • Granting access to every identity in the pool is risky: limit it with attributes and conditions.
  • The external app uses a credential configuration file, not a key.

Workload Identity Federation for GKE

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.

  • When you enable it, GKE creates the fixed pool PROJECT_ID.svc.id.goog, registers the cluster as a provider, and deploys the GKE metadata server on every node.
  • Enabling it grants no permissions: you must give roles to the Kubernetes ServiceAccount.
  • On Autopilot every node always uses it. On Standard you enable it on the cluster and on each node pool.
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

Workforce Identity Federation

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.

Exam scenarios

SituationAnswerWhy
Test whether a service account has enough permissions before deployingImpersonate it with --impersonate-service-accountNo keys; short-lived credentials.
Give a user temporary elevated access without changing resource policiesToken Creator on a service account with those permissionsImpersonation for temporary elevation.
Make gcloud always use a service account on your machinegcloud config set auth/impersonate_service_accountConfiguration property.
Develop locally an app that only works as a service accountgcloud auth application-default login --impersonate-service-accountClient libraries impersonate the account.
A GitHub Actions pipeline must deploy to Google CloudWorkload Identity Federation with a GitHub OIDC providerNo service account keys.
An app on AWS must read from BigQueryWorkload Identity Federation with an AWS providerToken exchange with the Security Token Service.
A GKE pod must write to a bucketWorkload Identity Federation for GKE and a role for the Kubernetes ServiceAccountAlways on in Autopilot.
Employees must sign in with Okta without syncing accounts to GoogleWorkforce Identity FederationSSO without GCDS.

Common exam traps

❓ 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?

Ready to test "Impersonation, short-lived credentials, and Workload Identity"?

Original questions, each with a full explanation.

Start a mock exam

Sign in to save your progress.

You finished the last topic of this deep dive.

Back to the index →
Previous: Service accounts