All guides

Google Cloud service accounts without keys

Holding a service account key is similar to knowing a user's password, and by default the key has no expiry time. Google's guidance is to choose a more secure way to authenticate whenever possible.

A person holding a phone above a laptop keyboard

Why a key is a risk

A service account key is the private key of an RSA key pair. A user who can access a valid key can use it to authenticate as the service account. Google points out that service accounts are not subject to any additional sign-in verification, and that authenticating with a key requires no prior authentication.

Google's pages add three facts:

  • By default a key has no expiry time and stays valid until you delete it.
  • Bad actors look for keys in source code repositories, public Cloud Storage buckets, email inboxes, file shares and backup storage.
  • An audit log entry for an activity initiated by a service account contains the service account's email address. It does not say which user or application was using the service account.

Attach a service account on Google Cloud

Some Google Cloud resources accept an attached user-managed service account and then act under it as their identity. Applications on the resource that call Google Cloud APIs through a client library automatically use the attached service account.

Shell

gcloud compute instances create my-vm \
  [email protected] \
  --scopes=https://www.googleapis.com/auth/cloud-platform

To attach a service account to a resource, you need the Service Account User role (roles/iam.serviceAccountUser) on that service account.

Impersonation for people and local development

With impersonation, you start as an authenticated principal, such as your user account, and request short-lived credentials for a service account. You need the Service Account Token Creator role (roles/iam.serviceAccountTokenCreator) on that service account, and the Service Account Credentials API has to be enabled in the project.

Shell

gcloud storage buckets list \
  --impersonate-service-account=my-app@my-project.iam.gserviceaccount.com

gcloud auth application-default login \
  --impersonate-service-account [email protected]

The first command uses impersonation for one gcloud command. The second sets up a local Application Default Credentials file, which client libraries that support impersonation use automatically.

Workload Identity Federation for outside workloads

Workload Identity Federation gives on-premises and multicloud workloads access to Google Cloud resources with federated identities in place of service account keys. It covers workloads on AWS or Azure, pipelines such as GitHub and GitLab, and any identity provider that supports OpenID Connect or SAML 2.0.

You create a workload identity pool and a provider that describes the relationship with your identity provider. The workload gives a credential from that provider to Google's Security Token Service, which verifies it and returns a federated token. Access is then granted in one of two ways:

  • Direct access. Grant IAM roles to the federated principals on specific resources.
  • Service account impersonation. Grant access to a service account, which then accesses the resources.

Google recommends a new pool for each environment outside Google Cloud, such as development, staging or production.

Block new keys with an organization policy

Google's page on restricting service accounts lists two constraints that disable key creation: the managed constraint iam.managed.disableServiceAccountKeyCreation and the legacy managed constraint iam.disableServiceAccountKeyCreation. Each one stops new external service account keys and new Cloud Storage HMAC keys from being created.

For a managed constraint, create a policy file and set it. This file fills in Google's template; the organization ID is yours:

YAML

name: organizations/<organization-id>/policies/iam.managed.disableServiceAccountKeyCreation
spec:
  rules:
  - enforce: true

Shell

gcloud org-policies set-policy policy.yaml --update-mask=spec

Four facts before you apply it:

  • Google's pages differ on which name is enforced by default. The security baseline page says iam.managed.disableServiceAccountKeyCreation is enforced for organizations created on or after 3 May 2024. The page on restricting service accounts attaches the same date to the legacy name.
  • The constraint is not retroactive. It does not prevent the use of existing keys.
  • Google's migration guide warns that it can disrupt workloads that rotate their keys, and describes enforcing it first on one folder, such as the development folder.
  • A changed organization policy needs up to 15 minutes before it is fully enforced.

A second managed constraint, iam.managed.disableServiceAccountKeyUpload, disables the upload of external public keys to service accounts.

Find the keys you already have

List the keys of one service account:

Shell

gcloud iam service-accounts keys list \
  [email protected]

The output has a row for each key with its ID, creation time and expiry time.

For a whole organization, Google's migration guide points to Cloud Asset Inventory. Its reference architecture queries the inventory to identify the service account keys in the organization.

To check whether a key is still in use, Google's best practices page names two tools. Service account insights flag every service account with no use in the past 90 days. The Key Authentication Events metric shows when a key was last used.

Where to start

  1. Block new keys. Google's migration guide tags the projects and folders that are exempt, then enforces the constraint on all other resources.
  2. Remove unused keys. Google's advice is to disable a key as soon as it is not needed, then delete it when you are certain it is no longer needed.
  3. Move each workload. Attach a service account to code on Google Cloud, use impersonation for people, and use federation for workloads elsewhere.
  4. Exposed keys. The iam.serviceAccountKeyExposureResponse constraint sets what Google Cloud does with a key it detects as exposed. With no value set, it disables the key. Google does not guarantee that it will detect exposed keys.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides