Prepare and test your skills
Prepare and test your skills
Resource-Level IAM Scoping and Policy Restriction is the practice of attaching an IAM allow policy to the lowest-level resource that needs it, such as a single Cloud Storage bucket or Pub/Sub topic. A policy attached at a higher level reaches everything below it, while a policy attached at the resource level reaches only that resource. This tight scoping applies least privilege to cloud-native applications and prevents lateral movement if one identity is compromised.
The Google Cloud resource hierarchy defines where an Identity and Access Management (IAM) (opens in a new tab) allow policy can attach and how far its access reaches. The Organization resource sits at the top and provides central visibility and control over every resource in the organization. An organization contains folders, folders group related projects, and projects contain service resources such as Compute Engine virtual machines (VMs), Pub/Sub topics, Cloud Storage buckets, and Cloud VPN endpoints.
An IAM allow policy applies to the resource where the policy is attached. A policy attached to a project grants access across all service resources inside that project, while a policy attached to a specific bucket or topic grants access only to that bucket or topic. Choosing the resource-level attachment keeps the trust boundary tight, so a compromised service account that can read one bucket cannot move sideways to read other buckets, topics, or VMs in the same project.
The IAM access model decides whether a request from a principal to a resource is allowed. The IAM service binds identities to permissions at a chosen resource, and the request flows from the principal to the resource where the attached policy is checked. A principal cannot perform any action that its bound roles do not permit on that resource.
Using job-function roles keeps each binding limited to what the job needs. Creating custom roles from predefined roles lets teams remove unneeded permissions without starting from broad access.
Granular controls in Google Cloud restrict what a scoped policy can reach even after access is granted. Tags in Resource Manager add annotations to resources so policies can conditionally allow or deny access based on whether a resource has a specific tag. Tag-based conditions provide fine-grained control across the hierarchy without creating a separate project for every isolation need.
Two organization-wide guardrails reinforce resource-level scoping. The constraints/iam.automaticIamGrantsForDefaultServiceAccounts constraint disables automatic grants of the Editor role (roles/editor) to default service accounts, because that project-wide role would let an attacker who compromises one component control the entire project. The constraints/iam.allowedPolicyMemberDomains constraint for domain restricted sharing limits which customer IDs can be added to IAM policies, so only trusted, managed corporate identities receive access. Principal access boundary (PAB) policies add another limit on the principal side. A PAB policy attached through iam.googleapis.com/PrincipalAccessBoundaryPolicy caps the maximum access a principal can use, which reduces the extent of an attack with a compromised identity and helps block phishing and data exfiltration.
Exam tip: Attach the IAM allow policy to the specific bucket or topic the workload needs, not to the containing project, because project-level access allows lateral movement to every other resource in that project.
Service Account Management and Custom Role Implementation is the practice of replacing broad default identities with dedicated, minimally privileged identities for cloud-native applications. A workload authenticates as a service account (opens in a new tab), and IAM controls what that account can do through roles. Least privilege means each application gets only the permissions it needs and nothing more.
A service account is a non-human identity for workloads, such as a custom application that accesses resources without end-user involvement. Service accounts have no password and cannot be used for browser sign-in, and each service account belongs to a Google Cloud project and is identified by a unique email address. A service account is both a principal and a resource: as a principal it can be granted access to resources like a Cloud Storage bucket, and as a resource it can be accessed and impersonated by other principals like a user or group.
Because of this dual nature, a compromised service account creates two kinds of harm: harm done by the account and harm done to the account. Common abuse patterns include:
Limiting privileges reduces harm when a service account acts as a principal, while protecting impersonation, keys, and access reduces harm when it acts as a resource.
Risks of default service accounts and broad access come from automatic identities combined with primitive roles. Default service accounts are created automatically when certain Google Cloud APIs are first enabled, and depending on organization policy they may automatically receive the Editor role (roles/editor) on the project. That role allows reading and modifying all resources in that project, and the grant exists for convenience and is not essential for services to work.
If one part of the system is compromised, an Editor-granted default account lets an attacker move across the entire project. Access scopes on Compute Engine VMs do not fix this, because scopes are coarse-grained. For example, a storage read-only scope can limit calls to read-only operations but cannot limit access to specific buckets, so scopes cannot replace fine-grained allow policies. To prevent automatic over-granting, enforce the iam.automaticIamGrantsForDefaultServiceAccounts organization policy constraint on the organization or folder.
Exam tip: Enforce constraints/iam.automaticIamGrantsForDefaultServiceAccounts to block automatic Editor grants to default service accounts; for organizations created after May 3, 2024, this constraint is enforced by default.
Single-purpose user-managed service accounts are dedicated identities that belong to one task, application, or resource, such as one VM instance or one integration. Creating a separate service account for each task makes access easier to manage and makes audit logs easier to trace to a specific workload. Grant each account only the permissions needed for its intended task, and attach the account directly to the compute resource so code obtains short-lived tokens from the metadata server instead of using long-lived keys.
Manage service accounts as resources tied to the lifecycle of their workload, not as standalone users. Follow a clear naming and documentation convention, and choose generic names like deployer instead of revealing software like jenkins, because allow policies are widely readable and email addresses can disclose project IDs and tooling. When an account is no longer needed, first move it to disabled and delete it only after a waiting period; disabling preserves IAM bindings so the account can be re-enabled safely, while deleting removes the identity. Deleted default service accounts cannot be recreated, so leave an unused default account disabled if future use is possible.
IAM roles for service accounts divide into broad defaults and tailored least-privilege options. Predefined roles bundle permissions by job function, while custom roles define fine-grained permissions tailored to one application requirement. For example, a custom role can allow a service account to read and write data in a Cloud Storage bucket but not delete it.
| Role type | Scope of permissions | Choose when |
|---|---|---|
Primitive role such as Editor (roles/editor) | Broad read and modify across all project resources | Almost never for applications, because compromise affects the whole project |
| Predefined role | Job-function bundle, such as Workflows Invoker or Cloud Tasks Enqueuer | The workload matches a standard Google Cloud job function |
| Custom role | Only the specific permissions the application needs | Predefined roles grant more than required and a tighter set is needed |
Replace an existing Editor grant by first using Policy Simulator to see the impact of the change, then granting the needed predefined or custom roles and revoking Editor. Avoid managing impersonation rights at the project or folder level, because granting Service Account Token Creator at the project level lets a user impersonate every service account in that project and inherit access to resources outside it.
Right-sizing is the ongoing process of removing unused permissions and narrowing short-term access so service accounts stay at least privilege as applications change. The IAM recommender, part of Policy Intelligence, uses machine learning to recommend removal of unused roles and to identify abandoned service accounts. Activity Analyzer shows recent authentication activity to confirm whether a shared account is still used, and the Service Account Credentials API lets a supervisor service account with the Service Account Token Creator role issue short-lived access tokens for dedicated workload accounts.
Additional controls further reduce the blast radius around privileged accounts:
Workload Identity and Ephemeral Credentialing is the practice of giving programmatic workloads a Google Cloud identity that uses short-lived tokens instead of long-lived keys. A workload is programmatic access by an application, while workforce is access by a human user. Workload Identity Federation (opens in a new tab) exchanges an external or Kubernetes identity for short-lived Google credentials, so each service gets only the permissions it needs and the attack surface stays small.
A token is a digital object that proves a caller exchanged proper credentials for temporary access. The token carries the identity of the principal making the request and the type of access that principal is authorized for, like a hotel key that opens a guest room and elevator but not other rooms or service areas. With the exception of API keys, Google APIs do not accept credentials directly, so an application must acquire or generate a token and present that token to the API.
Service account impersonation and federation both follow this pattern: the workload presents its own identity to an authority, receives a short-lived token, and traffic flows from the workload to the Google Cloud API with that token. Because the token expires quickly, a leaked token is useful to an attacker for only a short time.
Service account authentication choices are the methods an application can use to act as a service account, ordered from most secure to least secure. Production code running on Google Cloud should use the most secure option that fits.
Giving Google Kubernetes Engine (GKE) Pods access to Google Cloud resources means connecting a Kubernetes identity to a Google Cloud identity under least privilege. Three options differ in how the Pod obtains its identity and how broadly that identity is shared.
| Option | How access works | Security and isolation |
|---|---|---|
| Workload Identity Federation for GKE | A Kubernetes service account runs as an IAM service account, and Pods that run as that Kubernetes service account receive the permissions of the IAM service account | Most secure because each workload maps to its own IAM service account with minimum roles |
| Node service account | Pods authenticate with the credentials of the service account attached to the node's Compute Engine VM through the VM metadata server | Weak isolation because any Pod in the cluster can use the node's credentials |
| Service account JSON key | A JSON key for an IAM service account is mounted into the Pod through a Kubernetes Secret | Strongly discouraged because keys are difficult to manage, rotate, and revoke safely |
Because the node service account is shared by every Pod on the node, a compromise of one Pod can expose the credentials used by all Pods on that node. Federation preserves least privilege because the permission boundary follows the Kubernetes service account to a single IAM service account, and revoking that mapping affects only the paired application.
Exam tip: Choose Workload Identity Federation for GKE for Pod access to Google Cloud; use a service account JSON key only as a last resort because key management violates least-privilege operations.
Authenticating a workload on Google Cloud depends on the service account attached to the compute resource where the code runs, such as a Compute Engine VM instance. This attached-identity approach is the preferred method for code running on a Google Cloud compute resource, and Application Default Credentials (ADC) lets client libraries obtain tokens from that attached identity automatically. For most services the service account must be attached when the resource is created and cannot be added or replaced later, while Compute Engine allows a service account to be attached to a VM instance at any time.
Authenticating a workload from on-premises or another cloud provider depends on Workload Identity Federation, where the workload exchanges its external token through the Secure Token Service (STS) for short-lived Google credentials. The workload depends on STS to validate the external token and return Google credentials, so no long-lived Google key ever leaves Google Cloud.
Workload Identity Federation reduces the number of stored secrets by reusing tokens an application already has. An application that integrates with an OpenID or SAML identity provider often stores a client secret to authenticate to that provider, and giving that application Google Cloud access leaves two choices: create a service account key and store it alongside the client secret, or exchange tokens from the existing provider for Google credentials through federation. The first path requires managing and rotating two secrets, while the second path requires only one, which simplifies rotation and improves security.
When token exchange must stay in a specific region for data residency, the workload depends on a regional STS endpoint such as https://sts.REGION.rep.googleapis.com instead of the global https://sts.googleapis.com endpoint. The regional endpoint keeps the exchange inside that region while still returning short-lived credentials for Google Cloud APIs.
A workload identity pool is a view that surfaces identities from external identity providers so they can be used as IAM principals, not a directory that contains user accounts like Cloud Identity. Because federation uses claims-based identities, two tokens from the same issuer that map to the same google.subject value are treated as the same user. Ambiguous mappings can let several external identities map to one principal or one identity map to many principals, which enables spoofing, especially with multi-tenant providers like GitHub that share one issuer URL across all tenants.
Least-privileged services depend on three linked controls: resource-level IAM policies limit where access applies, single-purpose service accounts with tailored roles limit what access exists, and Workload Identity Federation with short-lived tokens limits how long access lasts. A tight resource attachment contains a compromise, a dedicated identity keeps that containment tied to one workload, and ephemeral credentialing removes the long-lived keys that would let an attacker reuse stolen access.
constraints/iam.automaticIamGrantsForDefaultServiceAccounts organization policy constraint blocks automatic grants of the Editor role to default service accounts.An Identity and Access Management allow policy attached to a project grants access across all service resources inside that project, while a policy attached to a specific bucket or topic grants access only to that bucket or topic. Choosing the resource-level attachment keeps the trust boundary tight so a compromised service account that can read one bucket cannot move sideways to read other buckets, topics, or virtual machines in the same project.
Workload Identity Federation for Google Kubernetes Engine should be used because a Kubernetes service account runs as a mapped Identity and Access Management service account so pods receive only the permissions of that account. The node service account provides weak isolation because any pod on the node can use the credentials of the service account attached to the node Compute Engine virtual machine, and a service account JSON key is strongly discouraged because keys are difficult to manage, rotate, and revoke safely.
The constraint blocks automatic grants of the Editor role to default service accounts that are created automatically when certain Google Cloud application programming interfaces are first enabled. Blocking that project-wide grant is important because the Editor role allows reading and modifying all resources in the project, so a compromised default account could move across the entire project.
Last updated