Skip to content

LearnCloud

GCP service account impersonation review

Brief

In Google Cloud a principal that can mint tokens for a service account, or attach it to a workload, holds that account's roles as well as its own, so the real permissions of a group are the sum along every impersonation chain it can start. The review lists who holds those rights and on which accounts, follows the chains with Policy Analyzer, and dates every user-managed key and default account that widens them.

Track
Cloud
Stage
Escalation
Reviewed
On this page
  1. Check first
  2. GCP
  3. The defender sees
  4. Done when
  5. Not covered
  6. Tools
  7. Related research
  8. Where this page fits
  9. Reviewed

All commands

Situation
A read-only review identity at the organization with Cloud Asset Viewer, Security Reviewer and Organization Viewer. Every command here is a read: we never call generateAccessToken, never use --impersonate-service-account, never create or download a key, and we review only the organization the scope letter names.
Goal
An impersonation map: every grant of token creation or act-as with its holder and target, each chain from a low role to a high one, every user-managed key with its age, and every default service account that holds Editor.
Outputs
Representative examples on placeholder hosts, trimmed to the lines that matter. Not captured from a public fixture yet.

Check first

  • The organization id and the projects that hold production data and deployment pipelines.
  • Which groups the client considers low privilege, such as developers and analysts, written down before we read.
  • Whether the iam.managed.disableServiceAccountKeyCreation and iam.automaticIamGrantsForDefaultServiceAccounts constraints are enforced, from the organization policy review.
  • Whether Data Access audit logs are on for IAM, since token minting is logged there.

Steps

GCP

Who can mint tokens for, or act as, which service accounts?

gcloud asset search-all-iam-policies --scope=organizations/123456789012 \
  --query='policy.role.permissions:iam.serviceAccounts.getAccessToken' \
  --flatten='policy.bindings[].members' --format='csv[no-heading](resource,policy.bindings.role,policy.bindings.members)'

Expected output

//cloudresourcemanager.googleapis.com/projects/build-prod,roles/iam.serviceAccountTokenCreator,group:ci-maintainers@example.com   # <- every service account in build-prod
//iam.googleapis.com/projects/data-prod/serviceAccounts/etl@data-prod.iam.gserviceaccount.com,roles/iam.serviceAccountTokenCreator,serviceAccount:scheduler@ops-tools.iam.gserviceaccount.com

We run the same search for iam.serviceAccounts.actAs, which also returns Editor and Service Account User. A binding on a project or folder reaches every service account below it; a binding on one service account reaches only that one. Searching by permission rather than role catches custom roles that carry it.

Negative result. Token creation and act-as bound only on individual service accounts, to the workloads and pipelines that use them.

Fix. Remove project and folder bindings of Token Creator and Service Account User, and grant them on the service account resource instead.

Which chains lead from a low role to a high one?

gcloud asset analyze-iam-policy --organization=123456789012 \
  --identity='group:data-analysts@example.com' --permissions='resourcemanager.projects.setIamPolicy' \
  --analyze-service-account-impersonation

Expected output

analysisResults:
- attachedResourceFullName: //cloudresourcemanager.googleapis.com/projects/data-prod
  iamBinding:
    members:
    - serviceAccount:etl@data-prod.iam.gserviceaccount.com   # <- the analysts reach owner through the ETL account
    role: roles/owner

Policy Analyzer answers "can this group do this" across direct grants, group membership and impersonation, and names the service account in the middle. Google warns the impersonation option is expensive, so we ask it targeted questions: each low group against setIamPolicy on production projects and the organization. We then confirm each hop against the first step's list and record the chain; we do not walk it.

Negative result. No low group reaches setIamPolicy or an owner role through any service account.

Fix. Break the chain at its weakest hop, usually a pipeline or ETL account with owner, by giving that account the narrow roles its job uses.

Which user-managed keys exist, and how old are they?

for sa in $(gcloud iam service-accounts list --project data-prod --format='value(email)'); do
  gcloud iam service-accounts keys list --iam-account "$sa" --managed-by=user \
    --format='csv[no-heading](name.basename(),validAfterTime)' | sed "s|^|$sa,|"
done

Expected output

etl@data-prod.iam.gserviceaccount.com,7c3f0a91e2b44d5f8a6c19e0d2b7f34a1c9e8d05,2023-02-14T09:12:44Z   # <- a key over three and a half years old

The key id is a name, not the key, and listing never returns private key material. A user-managed key is a credential that works from anywhere until someone deletes it, so its age is how long it has had to leak. Policy Analyzer's activity query, gcloud policy-intelligence query-activity --activity-type=serviceAccountKeyLastAuthentication, shows whether it is still used.

Negative result. No user-managed keys, or keys a few days old with a rotation owner.

Fix. Enforce iam.managed.disableServiceAccountKeyCreation, move pipelines to workload identity federation, and delete keys unused for 90 days.

Which default service accounts still hold Editor?

gcloud asset search-all-iam-policies --scope=organizations/123456789012 --query='policy:roles/editor memberTypes:serviceAccount' \
  --flatten='policy.bindings[].members' --format='csv[no-heading](resource,policy.bindings.members)' \
  | grep -E 'developer\.gserviceaccount\.com|appspot\.gserviceaccount\.com'

Expected output

//cloudresourcemanager.googleapis.com/projects/data-prod,serviceAccount:210987654321-compute@developer.gserviceaccount.com   # <- every VM on the default account is a project editor
//cloudresourcemanager.googleapis.com/projects/web-prod,serviceAccount:web-prod@appspot.gserviceaccount.com

Default accounts got Editor automatically in older organizations, and Editor includes actAs, so anyone who can create an instance in that project can run code as Editor. The project first hour shows which instances run as them.

Negative result. No default account holds Editor anywhere in the organization.

Fix. Remove Editor from default accounts, give each workload its own service account, and enforce iam.automaticIamGrantsForDefaultServiceAccounts.

The defender sees

The searches and analyses appear in Admin Activity logs as Cloud Asset API calls under our review identity. Real impersonation shows as GenerateAccessToken in the IAM Credentials Data Access log, with the caller and target in the authentication info, but only where those logs are on. Key creation is an Admin Activity event, google.iam.admin.v1.CreateServiceAccountKey. If token minting is not logged, the chains above could be walked without a trace, and that belongs in the report beside them.

Done when

Every binding carrying getAccessToken or actAs is listed by holder and target, each chain to an owner or organization role is traced, user-managed keys are dated, and default compute and App Engine accounts are checked for Editor in every project.

Not covered

Project-level recon (see the GCP project first hour), organization policy constraints (see the organization policy review), and workload identity federation pools; the outputs shown are representative examples on a placeholder organization, not captured runs.