Prepare and test your skills

Prepare and test your skills

Secure workflows for requesting and using impersonated credentials describe how an authenticated principal obtains and uses a short-lived credential to act as a service account. Service account impersonation (opens in a new tab) always involves two identities: the authenticated principal who starts the request and the target service account that is impersonated. The secure pattern is consistent: the principal first gets a token for the service account, then uses that token to authenticate as the service account to access what the service account can access.
Principals request impersonated credentials through three supported paths that all produce a short-lived credential for the target service account. Each path creates the credential first, then uses that credential to authenticate as the service account:
--impersonate-service-account flag or the impersonate-service-account property, then runs that command with those credentials.Application Default Credentials (ADC) can also carry impersonation for local development when the ADC file is set up with the --impersonate-service-account flag. In that setup, client libraries that support impersonation automatically impersonate the service account without the developer handling keys. When a principal accesses resources while impersonating, most audit logs record both the principal's identity and the impersonated service account's identity.
Short-lived impersonated credentials have a limited lifetime of a few hours or shorter and are not automatically refreshed. The principal must request a new token after the credential expires, which limits how long a stolen token remains useful. Short-lived credentials therefore create less risk than long-lived credentials such as service account keys, which remain valid until they are revoked.
Secure configuration for impersonation means granting impersonation ability only to authenticated principals that need it, because only principals with the appropriate permissions can impersonate a service account. Impersonation is useful when temporary elevated access is needed without changing Identity and Access Management (IAM) policies:
To reduce privilege escalation, prefer short-lived impersonated credentials over downloadable service account keys and choose a more secure alternative to keys whenever possible.
Authentication without impersonation involves only one authenticated identity, not two. A workload that uses an attached service account authenticates to Google APIs with that service account as its own identity, so no second identity is involved. A principal that uses a service account key also authenticates directly as the service account, so key-based authentication is not impersonation even though it grants the service account's permissions.
Exam tip: The Google Cloud console never uses impersonation; console access always uses user credentials, while impersonation requires gcloud CLI, the API, or Workload Identity Federation to obtain a token for the service account.
Required IAM roles and permission structures for impersonation control which identities can act as which service accounts. Service account impersonation lets one identity act as another service account to perform work with that account's permissions. Secure impersonation depends on granting the right impersonation role on the right resource and keeping those grants as narrow as possible.
Identity and Access Management (IAM) (opens in a new tab) provides two central roles that enable impersonation in different ways. The Service Account User role (roles/iam.serviceAccountUser) contains the iam.serviceAccounts.actAs permission and lets a principal attach a service account to a resource, such as a Compute Engine instance, a Cloud Run worker pool, or a Cloud Build deployment. The Service Account Token Creator role (roles/iam.serviceAccountTokenCreator) lets a principal create short-lived access tokens to authenticate directly as the service account without attaching it to a resource.
The two roles are often used together when automation needs both kinds of access. For example, the default Dataform service agent needs Service Account Token Creator on a custom service account for all repository operations, plus Service Account User when workflow configurations run scheduled tasks as that custom account. A deployer account that creates or updates a Cloud Run worker pool needs Service Account User on the service identity, while the service identity itself needs separate roles to access Google Cloud APIs from inside the container.
Exam tip: Choose Service Account User when the task attaches a service account to a resource, and choose Service Account Token Creator when the task creates short-lived tokens to act as the account.
Cloud Build (opens in a new tab) must impersonate a managed service account when it deploys an image, because the deployed image runs using the identity of that managed service. The Cloud Build service account therefore needs two kinds of permission: a resource-level admin or developer role to manage the target service, and Service Account User on the specific service account to impersonate. Deployment actions flow from the Cloud Build service account to the managed service account, and the deployed resource then acts with the managed service account's permissions.
| Managed service | Service account that runs the workload | Required deployment role |
|---|---|---|
| App Engine | App Engine service account | App Engine Admin (roles/appengine.appAdmin) |
| Cloud Run functions | Runtime service account | Cloud Functions Developer (roles/cloudfunctions.developer) |
| Cloud Run | Service identity | Cloud Run Admin (roles/run.admin) |
| Compute Engine | Compute Engine service account | Compute Instance Admin v1 (roles/compute.instanceAdmin.v1) |
After the deployment role is enabled, Cloud Build grants Service Account User (roles/iam.serviceAccountUser) on the chosen service account to impersonate. That binding connects the source identity to the destination identity, so the deployer can act only as that one managed account.
Least-privilege impersonation means granting impersonation rights on a single service account rather than on a whole project or folder. A service account is both a principal that can access resources and a resource that has its own allow policy, so granting Service Account User or Service Account Token Creator on the individual service account limits the blast radius. Granting the same role at the project level would let the holder impersonate every service account in that project, including highly privileged default accounts.
Secure patterns keep the impersonator and the target closely matched in privilege and make the check enforceable:
roles/editor), use dedicated service accounts per workload, and enforce permission checks with organization policy constraints such as constraints/appengine.enforceServiceAccountActAsCheck and constraints/composer.enforceServiceAccountActAsCheck.These controls also support auditing because each narrow grant names both sides of the relationship: who can act and which account they can act as. Teams can review IAM allow policies to see which deployer or automation account holds iam.serviceAccounts.actAs on which target, check Cloud Logging for missing act-as permissions, and use role recommendations to replace broad roles with smaller ones.
The purpose and function of service account impersonation is to let an authorized identity act as a service account to access resources. The service account becomes the principal that makes the request, while the original user or external workload only borrows that identity through Identity and Access Management (IAM). This creates controlled, auditable access without sharing passwords or downloading key files.
Impersonation works because a service account is both an identity and a resource that accepts IAM policies. Permissions flow in two layers: the service account is granted roles on target resources such as buckets or projects, and then selected users or federated identities are granted permission to use that service account. The request flows from the original identity to the service account, and then from the service account to the target resource.
A left-to-right process flow showing an authenticated principal getting a short-lived token for a service account and then using it as that account to access a target resource, with two setup layers.
Setting up impersonation for an external workload follows an ordered workflow:
roles/iam.workloadIdentityUser) on the service account, or grant a Google user the Service Account User role (roles/iam.serviceAccountUser) on the service account.The setup depends on required APIs and policies. The project needs the IAM, Security Token Service, and Service Account Credentials APIs enabled, and cross-project use requires the organization policy iam.disableCrossProjectServiceAccountUsage to be unenforced or set to Off. The service account does not need to be in the same project as the Workload Identity Pool (opens in a new tab).
Service account impersonation is used when many different callers need the same controlled permissions without receiving those permissions permanently. The service account holds the needed roles once, and authorized identities temporarily act through it:
roles/workflows.invoker) to trigger a workflow execution.Direct resource access, service account impersonation, and downloaded keys differ in who acts as the principal and where permissions are granted. With direct access, the federated identity is the principal on the resource. With impersonation, the service account is the principal on the resource and the federated identity only has permission to use the service account.
| Access method | Who is the principal on the resource | Where access is granted |
|---|---|---|
| Direct resource access | Federated user from the workload identity pool | Federated principal (principal://... or principalSet://...) receives roles on the bucket or other resource |
| Service account impersonation | Service account acting as the identity | Service account receives roles on the resource, and the federated identity receives roles/iam.workloadIdentityUser on the service account |
| Downloaded service account key | Service account using a key file | Service account receives roles on the resource, and a key file such as key.json is created and set as GOOGLE_APPLICATION_CREDENTIALS |
The preferred method for workloads outside Google Cloud is Workload Identity Federation with direct access or impersonation, rather than creating and storing a key file. Direct access is recommended first, and impersonation is chosen when API limitations prevent direct federated access.
Exam tip: Grant direct resource access to the federated principal by default, and use service account impersonation when the API endpoint does not support federated identities.
The Service Account User role lets a principal attach a service account to a resource such as a Compute Engine instance, a Cloud Run worker pool, or a Cloud Build deployment, while the Service Account Token Creator role lets a principal create short-lived access tokens to authenticate directly as the service account without attaching it to a resource. Choose Service Account User when the task attaches a service account to a resource and choose Service Account Token Creator when the task creates short-lived tokens to act as the account.
Direct resource access is preferred for workloads outside Google Cloud, with impersonation used when the API endpoint does not support federated identities directly. With direct access the federated principal receives roles on the resource, while with impersonation the service account receives roles on the resource and the federated identity receives permission to use the service account.
A principal first creates short-lived credentials for the target service account and then uses those credentials to authenticate as the service account. The Google Cloud CLI (gcloud CLI) creates them when a command runs with the impersonate-service-account flag or property, the Service Account Credentials API creates them directly for an API request, and Workload Identity Federation uses a credential configuration file to let an external application use its environment-specific credentials to get them.
Last updated