New: Access Diff now reviews Terraform pull requests on GitHub

Technical guides / Google Cloud /

Service account impersonation and inheritance in Google Cloud IAM

A service account can reach more than its own roles suggest, because other principals may be allowed to create tokens for it, and because bindings flow down from folders and projects. How to read both.

In Google Cloud, a role binding on a project does not tell you everything about what a service account can reach. Two mechanisms add to it. IAM policies inherit down the resource hierarchy, and a principal that is allowed to impersonate a service account can act with everything that account can do.

Both are intended features. Both also make an access review harder, because the grant that matters may be in a different project, or at a different level of the hierarchy, than the resource it exposes. This guide explains how each one works and how to check for it by hand.

Impersonation in one paragraph

A service account is both an identity and a resource. As an identity it holds roles on other resources, such as a bucket. As a resource it has its own IAM policy that says who may use it. Impersonation is one principal using that second policy to act as the service account.

Two roles are the common ways to grant it:

  • roles/iam.serviceAccountTokenCreator lets the principal create credentials for the service account: access tokens, ID tokens, and signed blobs and JWTs. With a token, the caller can call Google Cloud APIs as the service account.
  • roles/iam.serviceAccountUser lets the principal attach the service account to a resource, such as a virtual machine or a Cloud Run service, so the workload runs as it.

Neither role gives the caller anything on the service account’s own resources. They give the caller the service account itself, and with it everything it holds. That is why a binding on the service account is worth the same scrutiny as a binding on the data.

Inheritance in one paragraph

IAM allow policies are inherited down the resource hierarchy: organization, folders, projects, then resources inside a project. A role granted at one level applies to every resource below it. A service account is a resource inside a project, so a Token Creator binding on a project applies to every service account in that project, and one on a folder applies to every service account in every project under it. The effective policy of a resource is the union of its own policy and the policies of its ancestors. Deny policies and organization policy constraints can narrow the result, but a plain allow binding above is not visible when you look only at the resource.

Example: one binding, a new path

In the sample environment, the deploy.yml workflow in platform-prod authenticates as the ci-deploy service account through Workload Identity Federation. In customer-prod, the records-exporter service account has read access to the gs://customer-records-prod bucket (storage.objects.get, through roles/storage.objectViewer). Today, nothing lets ci-deploy act as records-exporter.

Pull request #207, “Update service account access”, changes infra/gcp/identity.tf:

resource "google_service_account_iam_member" "ci_deploy_token" {
  service_account_id = google_service_account.records_exporter.name
  role               = "roles/iam.serviceAccountTokenCreator"
  member             = "serviceAccount:[email protected]"
}

The diff adds one resource and no new permission on the bucket. The result is a new path across two projects:

deploy.yml                          (GitHub Actions, in platform-prod)
  -> ci-deploy@platform-prod        (Workload Identity Federation)
  -> records-exporter@customer-prod (Token Creator, service account impersonation)
  -> gs://customer-records-prod     (storage.objects.get)

A reviewer of identity.tf sees an IAM member added to a service account. The question that decides whether it is acceptable is whether a deployment workload should be able to read customer exports, and that question is only visible when you follow the chain to the bucket.

The same grant, made wider

Where the grant is made matters as much as who receives it. Compare:

# On one service account (PR #207): one target.
resource "google_service_account_iam_member" "ci_deploy_token" {
  service_account_id = google_service_account.records_exporter.name
  role               = "roles/iam.serviceAccountTokenCreator"
  member             = "serviceAccount:[email protected]"
}

# On a folder: every service account in every project under it.
resource "google_folder_iam_member" "ci_deploy_token_all" {
  folder = "folders/123456789012"
  role   = "roles/iam.serviceAccountTokenCreator"
  member = "serviceAccount:[email protected]"
}

The second block looks similar and is far broader. If both projects sit under that folder, ci-deploy can now create tokens for records-exporter and for any service account created in those projects later. Nothing in the second block mentions records-exporter.

Terraform’s resource types also differ in how much they claim:

Resource type What it manages
google_service_account_iam_member One member for one role, leaving other members alone
google_service_account_iam_binding All members of one role, removing any that are not listed
google_service_account_iam_policy The whole policy, replacing everything else

The binding and policy types can remove access that was added outside the code. That is sometimes what you want, and always worth noticing in a plan.

Checking by hand

Start from the service account you are worried about, and from the identity you want to trace.

  1. Who can impersonate this service account? Read its own policy.

    gcloud iam service-accounts get-iam-policy \
      [email protected]

    Look for roles/iam.serviceAccountTokenCreator, roles/iam.serviceAccountUser, and any role that includes the same permissions.

  2. What is inherited? Read the policies of the project, its folders and the organization, and check for the same roles granted at those levels.

    gcloud projects get-iam-policy customer-prod
    gcloud resource-manager folders get-iam-policy 123456789012
    gcloud organizations get-iam-policy 987654321098
  3. What does each identity hold? For every principal you found, list its own bindings and repeat the first step for it. A service account that can impersonate another account that can impersonate a third is a chain.

  4. What can the end of the chain reach? Read the bindings on the resources it holds roles on, such as buckets and datasets. Cross-project resources have their own policies, so look in each project involved.

  5. Check the narrowing controls. Look for IAM deny policies and organization policy constraints that apply along the way.

You can test a suspected path directly with a token request. gcloud auth print-access-token --impersonate-service-account=<email> succeeds only if the caller can impersonate the target. Do this from a non-production principal, and only where your policy allows it, because a successful call is a real use of the access.

In a pull request, ask three questions of every binding on a service account:

  • Does the new member need to act as this account, or only to use its resources?
  • What can the target do, in every project it has roles in?
  • Is the grant made on the account itself, or at a level above that reaches others?

Fixing it

For #207, if the deployment workload does not need to impersonate records-exporter, remove the Token Creator binding and leave the service account’s own access untouched. If something does need the data, give that workload its own service account with a role on the bucket, rather than a path through an account built for a different job. If the grant is needed, keep it on the single service account, not on a project or folder.

How AccessRoot surfaces it

AccessRoot reads service accounts, IAM bindings with inheritance, impersonation relationships and workload identity, so it can follow a path from a workflow to a bucket instead of stopping at the first binding. For #207, Access Diff reports the new path to sensitive production customer exports on the pull request, as a proposed path and not observed use. The Google Cloud connection lists what is read, and Access Graph shows impersonation paths that cross projects.