Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 45 ยท Identity and Security

Least Privilege and Organization Policies

Apply least privilege on Google Cloud: custom roles, predefined vs basic roles, organization policy constraints, IAM Recommender and audit logs.

The principle of least privilege says every identity should hold only the permissions it needs for its job, and no more. It sounds obvious, yet most real projects drift the other way: someone gets Editor "just to unblock things", the access is never removed, and a year later a leaked laptop or a bug in one app can touch everything.

Google Cloud gives you three tools for staying on the right side of this: well-chosen roles, organization policies that set guardrails nobody can step over, and analysis and audit features that show where access is too wide. This lesson builds on IAM basics and service accounts.

Choosing the right role

Three sizes of role

Basic Owner, Editor, Viewer. Thousands of permissions each.
Predefined Made by Google for one product and one job.
Custom You choose the exact permissions.

Start with predefined; go custom only when none fits.

1 / 2

Basic roles (roles/owner, roles/editor, roles/viewer) span almost every service. Editor can change nearly anything, and Owner can also manage IAM and billing links. They are convenient in a throwaway sandbox and wrong for production workloads.

Predefined roles are the daily choice. A pipeline that only writes objects needs roles/storage.objectCreator, not Editor. A person who only inspects VMs needs a Compute viewer role. Read the role's permission list in the console before granting it, because names can be misleading.

Custom roles exist for the gap between the two. You list permissions yourself, at the project or organization level:

Terminal
gcloud iam roles create orderReporter \
  --project=my-project-id \
  --title="Order Reporter" \
  --permissions=bigquery.jobs.create,bigquery.tables.getData,bigquery.tables.get \
  --stage=GA

Custom roles are yours to maintain: when a product adds a permission you need, nothing updates the role for you. Each one also adds something to review, so prefer a predefined role whenever one fits.

Organization policies: guardrails

IAM answers "who may do this?". An organization policy answers "may anyone do this here?". It applies a constraint to an organization, a folder or a project, and it holds even for a project Owner. Some common constraints (check the current list in the documentation, since new ones appear regularly):

Constraint Effect
constraints/iam.disableServiceAccountKeyCreation Nobody can create service account key files.
constraints/storage.publicAccessPrevention Buckets cannot be made public.
constraints/gcp.resourceLocations Resources may be created only in listed locations (data residency).
constraints/compute.vmExternalIpAccess Limits which VMs may have public IP addresses.
constraints/iam.allowedPolicyMemberDomains Only identities from approved domains can receive roles.

Organization policies inherit down the hierarchy like IAM does, but a child can be allowed to override or merge, depending on how the parent set it. A typical setup turns key creation and public access off at the top, and allows a specific sandbox folder to be looser.

Terminal
gcloud org-policies set-policy policy.yaml --organization=123456789012

The advantage over a written rule is that a guardrail is enforced when someone tries the forbidden action, rather than found in an audit months later. You need organization-level access to set these, so a personal account without an organization usually cannot use them; the concept still matters when you work for a company.

Finding excess access

You rarely know in advance exactly what an identity needs. Two features help you trim afterwards:

  • IAM Recommender looks at which permissions a principal actually used over a window (roughly the past 90 days) and suggests a smaller role, or the removal of an unused binding. You review the suggestion; it never applies changes by itself.
  • Policy Analyzer answers questions such as "who can read this bucket?" by expanding groups and inheritance.

And you watch what happens through Cloud Audit Logs. Admin Activity logs, which record changes to configuration and IAM, are written for most services by default. Data Access logs, which record reads of your data, are off by default for most services because of volume, so enable them selectively for sensitive resources. We use logs in Cloud Logging, Monitoring and Alerting.

Terminal
gcloud recommender recommendations list \
  --project=my-project-id \
  --location=global \
  --recommender=google.iam.policy.Recommender

A stand-in that finds unused permissions

The model below imitates what the Recommender does at its core: compare what a role grants against what an identity used in a log, then propose a smaller custom role. The numbers and permission names are invented, and the real service uses far more signals, but the arithmetic is the same.

JS Stand-in: shrink a role to what was used

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real recommendations come with caveats: a permission used only once a year (for example during a disaster recovery drill) will not appear in a 90-day window, so review suggestions before applying them.

A guardrail checker

Organization policies block an action at the moment of the request. This stand-in imitates that check: the policy is a list of rules, each request is tested, and even an "owner" is refused.

JS Stand-in: an organization policy blocks the request

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Identity roles and organization policies work together: a request must be permitted by IAM and not forbidden by an organization policy.

A practical routine

  1. Start new workloads with a dedicated service account and the narrowest predefined role you can find.
  2. Put guardrails at the organization or folder level: no key files, no public buckets, approved locations.
  3. After a few weeks of real use, review Recommender suggestions and trim.
  4. Turn on audit logging for sensitive data, and set alerts on IAM changes.
  5. Review who holds Owner at least every quarter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which kind of role is the best default for a production workload?

    Predefined roles are maintained by Google and scoped to one product and task. Basic roles cover far more than most workloads need.
  2. 2. What makes an organization policy different from an IAM binding?

    IAM grants permissions to principals. An organization policy restricts what can be done in a part of the hierarchy, regardless of who asks.
  3. 3. What does IAM Recommender base its suggestions on?

    It compares granted permissions with observed usage and proposes smaller roles. You review and apply changes yourself.
  4. 4. Why should you be careful with custom roles?

    Google updates predefined roles when products add permissions, but a custom role stays as you wrote it, so it needs maintenance.

Summary

  • Least privilege means each identity holds only what its job needs.
  • Prefer predefined roles; use custom roles for gaps and avoid basic roles in production.
  • Organization policies are guardrails (no keys, no public buckets, allowed locations) that hold even for Owners.
  • IAM Recommender, Policy Analyzer and audit logs show where access is too wide.
  • A request needs both an IAM grant and a clean pass through organization policies.

Next: Secret Manager and Cloud KMS