Prepare and test your skills
Prepare and test your skills
Authentication security for service accounts means replacing long-lived user-managed keys with short-lived, keyless credentials. Identity and Access Management (IAM) (opens in a new tab) distinguishes Google-managed short-lived credentials, which are created and rotated automatically, from user-managed keys, which the customer must store, rotate, and protect. Workload Identity Federation (opens in a new tab) is the preferred replacement for hybrid and multi-cloud workloads because no long-lived secret crosses the trust boundary.
User-managed service account keys create persistent risk because each key stays valid until it is deleted or rotated. An attacker who steals a leaked key keeps access to Google Cloud resources for as long as the key lives, which can be forever without rotation. Default service accounts add to this risk when Google Cloud automatically grants them the Editor role (roles/editor) on the project, so one compromised workload can affect the whole project.
Organization Policy Service (opens in a new tab) enforces least privilege at the organization level. The constraints/iam.automaticIamGrantsForDefaultServiceAccounts boolean constraint set to False stops automatic high-privilege grants to default service accounts. The constraints/iam.disableServiceAccountKeyCreation constraint set to True blocks creation of user-managed keys, while the separate constraints/iam.disableServiceAccountKeyUpload constraint blocks upload of externally generated public keys.
Blocking key creation with organization policy stops new unmanaged credentials from entering affected projects. Resource Manager tags (opens in a new tab) allow narrow exceptions for workloads that cannot yet migrate. The organization inherits a default enforcement value, and exempt folders or projects override that value with their own tag.
To block key creation in all non-exempt projects, complete the steps in order:
roles/resourcemanager.tagAdmin) and Organization Policy Administrator (roles/orgpolicy.policyAdmin) to create tags and enforce policy.disableServiceAccountKeyCreation with values enforced and not_enforced.enforced, so all child folders and projects inherit enforcement.not_enforced to each exempt project or folder, which overrides the inherited value for that subtree.iam.disableServiceAccountKeyCreation that does not enforce where resource.matchTag('ORGANIZATION_ID/disableServiceAccountKeyCreation', 'not_enforced') and enforces on all other resources.The policy flows from the organization down through folders to projects, so a key-creation request to an exempt project succeeds while the same request to a non-exempt project is denied. Tags must be attached before enforcement to avoid breaking legitimate workloads.
Exam tip: Key creation and key upload are blocked by two separate constraints — iam.disableServiceAccountKeyCreation stops new user-managed keys, while iam.disableServiceAccountKeyUpload stops upload of external public keys.
Key rotation limits how long a stolen credential stays useful when keys cannot be removed. Identity and Access Management (IAM) requires rotation at least every 90 days for any remaining user-managed keys. A fixed lifetime bounds attacker access that would otherwise last indefinitely.
Enforcement uses the constraints/iam.serviceAccountKeyExpiryHours constraint with operator <= and value 2160, where 2160 hours equals 90 days. The constraint flows from the organization policy to projects in scope, and any key that would live longer violates the control. Rotation is containment only, not a substitute for keyless authentication.
Workload Identity Federation lets workloads outside Google Cloud authenticate without a service account key. A workload identity pool, identified by iam.googleapis.com/WorkloadIdentityPool, trusts an external identity provider such as another cloud, an on-premises system, or a CI/CD platform. The external workload exchanges its native identity for short-lived Google credentials, so no long-lived secret flows from Google Cloud to the external environment.
When a client uses client libraries or the REST API, credentials flow from the external provider to Google Cloud in a fixed order:
Least privilege depends on both sides of impersonation. The impersonated service account should live in the same project as the resources it accesses, and each application should use a dedicated service account. The roles/iam.workloadIdentityUser role on the service account must be granted only to specific external identities or attribute conditions, never to all pool members.
Remediation of existing workloads means working with owners to pick a keyless method and then delete keys without breaking production. Application Default Credentials (ADC) (opens in a new tab) makes migration portable because client libraries check known locations in the runtime for credentials. The same code can use user credentials during local development and short-lived service account credentials in production. ADC cannot authenticate the gcloud CLI itself and cannot be used with API keys, which work only with specific APIs.
| Authentication option | Workload location | How access is obtained |
|---|---|---|
| Attached service account | Google Cloud resources outside Kubernetes | The service account attaches directly to the resource, such as a Compute Engine virtual machine, and the workload uses its short-lived credentials |
| Workload Identity Federation for GKE | Containers in Google Kubernetes Engine (GKE) | The Kubernetes pod maps to a Google service account, so pods authenticate without keys |
| Workload Identity Federation | Other clouds, on-premises, or CI/CD systems | The external identity federates through a workload identity pool and exchanges for short-lived Google credentials |
Choose an attached service account when the workload already runs inside Google Cloud and can inherit identity from its host. Choose Workload Identity Federation for GKE when containers need pod-level isolation. Choose Workload Identity Federation when the trust boundary crosses clouds or organizations, because the external provider remains the source of identity while Google Cloud enforces authorization.
Provisioning and lifecycle management of service accounts is the practice of deciding where accounts live, controlling how they are created, and securing how they authenticate. Identity and Access Management (IAM) (opens in a new tab) provides service accounts as workload identities, while the Organization Policy Service (opens in a new tab) enforces guardrails at the organization, folder, and project levels. Policies set at a parent node are inherited by all child folders and projects unless a child has its own overriding policy.
Service account placement determines which project owns the service account for its entire lifetime. Each service account lives in one project and cannot be moved after creation, so the first project choice defines ownership. The number of accounts per project is limited by quota, which can be viewed in the Google Cloud console by searching for Service Account Count.
| Placement pattern | Management model | Tradeoff |
|---|---|---|
| Same-project accounts | Service accounts and the resources that use them live in the same project | Easier to start, but accounts spread across projects are harder to track |
| Centralized accounts | Service accounts live in a small number of dedicated projects | Easier to manage, but requires setup to attach accounts to resources in other projects |
When a centralized account in one project accesses a resource in another project, the API for that resource must usually be enabled in both projects. For example, if the service account lives in my-service-accounts and the Cloud SQL instance lives in my-application, the Cloud SQL API must be enabled in both projects. Cross-project use depends on attaching the service account to the resource in the other project, which lets workloads on that resource authenticate as the attached account.
Organization policy controls where service accounts can be created and what permissions they receive by default. The constraints/iam.disableServiceAccountCreation constraint blocks service account creation when enforced on an organization, folder, or project, which supports centralization and hierarchy boundaries. Before broad enforcement, account for two limits: some Google Cloud services cannot create default service accounts in restricted projects, and features such as Workload Identity Federation (WIF) (opens in a new tab) require service account creation.
If default accounts are unavailable, enable service account impersonation across projects so workloads can use centrally created accounts. The constraints/iam.automaticIamGrantsForDefaultServiceAccounts boolean constraint set to False disables automatic high-privilege grants when services create default accounts. Without it, a default account can receive the Editor role (roles/editor) on its project, so one workload compromise could lead to control of the whole project.
Exam tip: A service account cannot be moved to a different project after creation, and an unrestricted default service account receives roles/editor automatically — enforce constraints/iam.automaticIamGrantsForDefaultServiceAccounts as False to require deliberate, least-privilege grants.
The secure key lifecycle replaces long-lived user-managed credentials with short-lived authentication. The iam.disableServiceAccountKeyCreation boolean constraint set to True blocks creation of user-managed keys, and the iam.disableServiceAccountKeyUpload boolean constraint blocks upload of external public keys. When keys must remain, the constraints/iam.serviceAccountKeyExpiryHours constraint enforces rotation at least every 90 days, which is 2160 hours or less. Rotation limits how long a leaked key stays usable, while without rotation an attacker keeps access indefinitely.
Workload Identity Federation lets CI/CD systems and workloads outside Google Cloud authenticate without keys, which reduces leakage risk. Enforcement is represented by requiring iam.googleapis.com/WorkloadIdentityPool to be set. To stop new key creation while keeping narrow exceptions, apply the key-creation constraint with tags:
roles/resourcemanager.tagAdmin) and Organization Policy Administrator (roles/orgpolicy.policyAdmin) at the organization level.disableServiceAccountKeyCreation with values enforced and not_enforced.enforced, so children inherit enforcement.not_enforced to each exempt project or folder, which overrides the inherited value.iam.disableServiceAccountKeyCreation is not enforced when resource.matchTag('ORGANIZATION_ID/disableServiceAccountKeyCreation', 'not_enforced') and is enforced on all other resources.For existing workloads, choose the replacement by environment. First determine whether code runs in single-user development such as a workstation or Cloud Shell versus production, then determine whether it runs inside or outside Google Cloud. Code in containers on Google Kubernetes Engine (GKE) uses Workload Identity Federation for GKE to attach accounts to pods, while other code in Google Cloud attaches a service account directly to the resource.
Ongoing management keeps the service account inventory understandable and limits who can act as each account. The display name of a service account captures purpose, owner, or contact information at creation time, which prevents confusion as account numbers grow. Consistent display names and exemption tag naming support governance across folders and projects.
Access to use a service account is granted by binding a role to a principal on the service account or on a parent resource. For example, granting Service Account Token Creator (roles/iam.serviceAccountTokenCreator) on the service account lets the principal impersonate that account. Impersonation transfers all of the service account's resource access to the impersonating user, so use caution with highly privileged Compute Engine and App Engine default service accounts.
Authorization, impersonation, and least-privilege enforcement follow from the dual nature of a service account (opens in a new tab). A service account is both a principal that can receive Identity and Access Management (IAM) roles on resources such as Cloud Storage buckets, and a resource that other principals such as users or groups can be given permission to use. Least privilege must apply on both sides: limit what the service account can access and limit who can use the service account.
Authorization for service accounts as principals controls what Google Cloud resources the service account itself can reach. A service account gains access when bound to a role on a resource, for example Storage Object Viewer (roles/storage.objectViewer) on a bucket or Compute Admin (roles/compute.admin) on a project, and it can also be limited by a deny policy such as denying resourcemanager.projects.setIamPolicy. Traffic flows from the workload authenticated as the service account to the target API, and IAM checks both allow and deny policies before permitting the call.
Least-privilege authorization requires precise, narrow grants:
iam.automaticIamGrantsForDefaultServiceAccounts organization policy constraint, enforced by default for organizations created after May 3, 2024.Single-purpose accounts preserve attribution, because Cloud Audit Logs record the service account email that acted but do not record which application behind a shared account acted.
Control of service accounts as resources determines which principals can attach, administer, or impersonate a specific account. A service account belongs to a project in the resource hierarchy, so access can be granted on the individual account, the enclosing project, a parent folder, or the organization. The policy that protects the service account applies to whoever tries to use it, while the roles granted to the service account control what that user can then reach.
Manage access on each individual service account rather than at the project or folder level. If a user receives the Service Account Token Creator role on a project, that user can impersonate every service account in the project and can potentially reach all resources those accounts can access, including resources outside that project. Individual bindings keep the blast radius to one workload identity and avoid over-granting through hierarchy inheritance.
Privilege-escalation boundaries around attached accounts enforce the same principle. Any code on a Compute Engine virtual machine (VM) instance with an attached service account can by default reach the metadata server to request tokens for that account, so untrusted startup scripts, guest policies, CI/CD code, or user-submitted code must be as well protected as the account itself. Shell access must be equally restricted, with OS Login on Linux requiring both OS Login permission and the iam.serviceAccounts.actAs permission on the attached account, and with host firewalls or Identity-Aware Proxy TCP forwarding limiting who can reach the VM and metadata server.
Service account impersonation occurs when an already authenticated principal authenticates as a service account to gain its permissions. The impersonating principal's requests flow to Google Cloud APIs under the service account identity, so the principal can access whatever the service account can access without changing resource IAM policies. Impersonation is used for temporary elevated access, permission testing, local development as a service account, and authenticating workloads outside Google Cloud.
| Role | Core permission | What it allows |
|---|---|---|
Service Account User (roles/iam.serviceAccountUser) | iam.serviceAccounts.actAs | Attach the service account to a resource, such as a Compute Engine instance, so code on that resource runs as the service account |
Service Account Token Creator (opens in a new tab) (roles/iam.serviceAccountTokenCreator) | iam.serviceAccounts.getAccessToken | Request short-lived access tokens for the service account directly, including with gcloud config set auth/impersonate_service_account |
Choose Service Account User when the principal needs to deploy a workload that runs as the service account. Choose Service Account Token Creator when the principal needs to act as the account on demand from the Google Cloud CLI, client libraries, or the Service Account Credentials API. A local Application Default Credentials (ADC) file created with gcloud auth application-default login --impersonate-service-account is supported only for the Go, Java, Node.js, and Python client libraries.
Exam tip: Granting Service Account Token Creator at the project level lets the holder impersonate any service account in that project; grant Token Creator and Service Account User on the individual service account to enforce least privilege.
Temporary elevation with the Service Account Credentials API lets an application use high privilege only when needed instead of running continuously with broad access. A supervisor service account holds Service Account Token Creator on narrowly scoped workload accounts, and a dedicated token-broker component uses the supervisor to mint short-lived tokens for each job. Long-lived keys are avoided because anyone holding the private key can authenticate as the service account from anywhere.
Credentials move from the broker to each workload part in order:
Credential Access Boundaries further downscope those short-lived tokens to a subset of permissions, and domain-wide delegation is avoided because it would let a service account impersonate any user in a Cloud Identity or Google Workspace account, including super-admins. When delegation or external access cannot use Workload Identity Federation, prefer impersonation with user credentials over creating a service account key.
Monitoring with Cloud Audit Logs provides the record that links service account activity to potential abuse, including privilege escalation, spoofing, and non-repudiation threats. When logs show that a service account changed a resource or accessed data, that entry alone may not identify which human or workload impersonated the account, so correlation depends on IAM data-access logs and application context. Enable data access logs for IAM APIs so token creation, getIamPolicy, and setIamPolicy calls on service accounts are visible alongside the resource changes they enabled.
Effective detection depends on least-privilege design. Shared accounts, project-level Token Creator grants, and generic default accounts make it impossible to trace an event to one application, while single-purpose accounts, per-account bindings, CI/CD history correlated with audit timestamps, and custom application log entries for individual end users restore the chain from impersonating principal to service account to target resource. Unused service accounts are disabled before they are deleted so lateral movement paths shrink without immediately breaking unknown dependencies, and role recommendations and policy audits keep remaining grants aligned to actual use.
iam.serviceAccounts.actAs) allows attaching an account to a resource, while Service Account Token Creator (iam.serviceAccounts.getAccessToken) allows requesting short-lived tokens directly, and both must be granted on the individual service account for least privilege.The Service Account User role with permission iam.serviceAccounts.actAs allows attaching a service account to a resource such as a Compute Engine instance so code on that resource runs as the account, while the Service Account Token Creator role with permission iam.serviceAccounts.getAccessToken allows requesting short-lived access tokens for the account directly. Both roles must be granted on the individual service account for least privilege because a project-level Token Creator grant lets the holder impersonate every service account in the project.
Use Workload Identity Federation (WIF) for workloads in other clouds, on-premises systems, or CI/CD platforms instead of creating user-managed keys because no long-lived secret crosses the trust boundary. The workload obtains a credential from its trusted identity provider, exchanges it with the Security Token Service for a federated token, and impersonates a service account to obtain a short-lived Google access token.
Organization Policy Service blocks new keys with the iam.disableServiceAccountKeyCreation constraint while Resource Manager tags provide narrow exemptions, which is separate from the iam.disableServiceAccountKeyUpload constraint that blocks upload of external public keys. To allow exemptions, create tag key disableServiceAccountKeyCreation with values enforced and not_enforced at the organization level, attach enforced to the organization so children inherit it, attach not_enforced to each exempt project or folder to override inheritance, and create a policy that does not enforce where the resource matches not_enforced and enforces on all other resources.
User-managed service account keys stay valid until deleted or rotated, so any remaining keys must be rotated at least every 90 days to bound how long a stolen credential stays useful. Enforcement uses the constraints/iam.serviceAccountKeyExpiryHours constraint with operator <= and value 2160, where 2160 hours equals 90 days, and rotation is containment only and not a substitute for keyless authentication.
Your enterprise organization is establishing a secure operational framework for service account usage across its development teams. You must configure IAM permissions to support two distinct operational requirements while strictly enforcing the principle of least privilege:
devops-leads@example.com) must be able to impersonate a production deployment service account (prod-deployer@project-id.iam.gserviceaccount.com) from the Google Cloud CLI to obtain short-lived OAuth 2.0 access tokens for emergency rollouts without managing static keys.app-devs@example.com) must be able to provision Compute Engine virtual machines that run workloads under a dedicated workload service account (app-runner@project-id.iam.gserviceaccount.com), but they must not be permitted to directly generate credentials or mint tokens for this workload service account from their local workstations.Which IAM role bindings should you configure?
Last updated