Grant least-privilege access with Google Cloud IAM
Write an IAM binding that gives one principal read access to one bucket and explain what it inherits from higher levels.
By Invicly Tech, , in Google Cloud Engineering and Services. About 10 minutes to read.
In short
Google Cloud IAM answers one question: who can do what on which resource. You never grant single permissions directly; you bind a principal to a role on a resource, and that binding also applies to everything below it in the hierarchy. Least privilege means the smallest predefined role, on the smallest resource, for the right principal.
Think of it like this: A binding is like a hotel key card: the card holder is the principal, the room types it opens are the role, and the floor it works on is the resource.
Explained simply
IAM stands for Identity and Access Management. It decides who can do what to which thing in Google Cloud. The who is called a principal: a person, a group, a service account used by code, or an identity from another system.
The what is a permission, such as storage.objects.get, which means read an object in Cloud Storage. Permissions are never handed out one at a time. They are packed into roles, such as Storage Object Viewer, and you grant the role.
The which thing is a resource. You attach the grant to a resource, such as one bucket, one project or a whole folder. A grant on a project covers every bucket inside it, and a grant on a folder covers every project inside it.
The safe rule is least privilege: give the smallest role, on the smallest resource, to the right principal. If Ana only needs to read receipts, give her Storage Object Viewer on the receipts bucket, not Editor on the project.
Press Play on a diagram to walk through it one step at a time.
- Binding 1
- role: roles/storage.objectViewer
- member: user:ana@example.com
- Binding 2
- role: roles/storage.objectAdmin
- member: serviceAccount:orders-api@...
- Metadata
- etag: protects against overwrites
- version: 3 if conditions are used
- 1Request
Ana reads an object
- 2Needed permission
storage.objects.get
- 3Collect policies
Object's bucket, project, folders, organisation
- 4Any matching grant?
A role holding the permission for Ana
- 5Allow or deny
Deny policies can still block it
- 1Start small
Predefined role on one resource
- 2Test the task
Note any permission denied
- 3Add only what failed
Smallest role with that permission
- 4Review
Recommender and audit logs show unused access
- Back to step 1
In depth
Principals and how they are written
In a policy, every principal has a prefix. user:ana@example.com is a Google account. group:orders-readers@example.com is a Google group, and the best way to grant access to people because membership changes without touching policies. serviceAccount:orders-api@orders-lab-4821.iam.gserviceaccount.com is a service account. domain:example.com means everyone in a Workspace or Cloud Identity domain. principal:// and principalSet:// identifiers name federated identities from outside Google.
Two special values need care. allUsers means anyone on the internet, signed in or not, and allAuthenticatedUsers means any signed-in Google account. Both make a resource effectively public. New organisations often block them through a domain restriction policy.
- user: one person
- group: many people, easiest to manage
- serviceAccount: code
- domain: a whole company domain
- allUsers: the public internet
Roles: basic, predefined and custom
Basic roles are Owner, Editor and Viewer. They date from before IAM and span almost every service, so Editor can change nearly anything in a project. Predefined roles are curated by Google per service and per job, such as roles/storage.objectViewer, roles/run.invoker or roles/cloudsql.client. Custom roles are your own permission lists, used when no predefined role fits; you must maintain them as services add permissions.
To pick a role, start from the action. Find the permission the action needs in the error message or the docs, then search the roles reference for the smallest predefined role holding it. The console's IAM recommender can later suggest smaller roles based on what a principal actually used.
- Owner, Editor, Viewer
- Thousands of permissions
- Quick but far too broad
- Avoid outside small labs
- Made by Google per service
- Named after a job
- Updated as services change
- Default choice
- Your own permission list
- Org or project level
- You maintain it
- Use when nothing fits
Reading an allow policy
An allow policy is a small document with a list of bindings, an etag and a version. Each binding has one role, a list of members and optionally a condition, such as access that expires at a date. The etag is a fingerprint: if two people edit at once, the second write fails instead of silently overwriting the first. Policies that use conditions must be version 3.
Prefer add-iam-policy-binding and remove-iam-policy-binding over get-iam-policy followed by set-iam-policy, because the targeted commands change one binding and handle the etag for you. To read a busy policy, flatten it so each row is one member and one role, then filter for the principal you care about.
- bindings: role + members (+ condition)
- etag: prevents lost updates
- version 3 needed for conditions
- --flatten=bindings --filter for readable output
Inheritance and deny policies
Effective access on a resource is the union of its own allow policy and every ancestor's: the bucket, the project, each folder and the organisation. Allow policies only add access, so a lower level cannot take away what a higher level granted. That is why access reviews must look upwards.
Deny policies are the exception. They are attached to an organisation, folder or project and list permissions that certain principals must never use, whatever their roles say. IAM checks deny policies first; if one matches, the request fails. Use them sparingly for hard rules, such as no one but a break-glass group may delete projects. When access is confusing, the Policy Troubleshooter in the console explains which binding allowed or denied a principal's permission on a resource.
- 1Organisation
e.g. a group grant
- 2Folder
e.g. non-production viewers
- 3Project
e.g. roles/storage.admin
- 4Bucket
e.g. roles/storage.objectViewer
- 5Effective
Union of all, minus any deny
Learn it in 5 short steps
About 30m each. Track them with a timer in your plan.
- 1Name principals, roles and permissions~30m
- 2Read an allow policy and its bindings~30m
- 3Follow inheritance down the hierarchy~30m
- 4Pick a predefined role over a basic role~30m
- 5Scope storage access to one bucket~30m
Key points
- A principal is who: a user, group, service account, domain or federated identity.
- A role is a named set of permissions; prefer predefined roles over the broad Owner, Editor and Viewer basic roles.
- Allow policies add up down the hierarchy; a grant on a project cannot be removed lower down, only by a deny policy.
- Grant on the narrowest resource that works, such as one bucket instead of the project.
- Grant roles to groups for people, so joiners and leavers change group membership, not policies.
Key terms
Tap a card to flip it. 0/6 known
Examples
gcloud storage buckets add-iam-policy-binding gs://orders-lab-4821-receipts \
--member=user:ana@example.com --role=roles/storage.objectViewer
gcloud storage buckets get-iam-policy gs://orders-lab-4821-receipts
# what Ana might also inherit from above
gcloud projects get-iam-policy orders-lab-4821 \
--flatten=bindings --filter="bindings.members:ana@example.com"Grant read on one bucket, then check what is inherited from the project.
Give a reviewer read access to receipts only
Prerequisites: a lab bucket and permission to change its policy. Step 1: grant Storage Object Viewer on the bucket to the reviewers group. Step 2: read the bucket policy and confirm the binding. Step 3: check the project policy for inherited storage roles held by the same group. Expected evidence: the bucket policy lists the group with objectViewer, and the project policy shows no storage role for it. Rollback: remove the binding with the matching remove command.
B=gs://orders-lab-4821-receipts
G=group:orders-reviewers@example.com
gcloud storage buckets add-iam-policy-binding $B \
--member=$G --role=roles/storage.objectViewer
gcloud storage buckets get-iam-policy $B --format=json
gcloud projects get-iam-policy orders-lab-4821 --flatten=bindings \
--filter="bindings.members:orders-reviewers@example.com" \
--format="table(bindings.role)"
# rollback
gcloud storage buckets remove-iam-policy-binding $B \
--member=$G --role=roles/storage.objectViewerDiagnosing a permission denied error
The orders API fails with 403 and a message that the caller does not have storage.objects.create on the bucket. Step 1: note the principal (the API's service account) and the permission. Step 2: find a predefined role containing storage.objects.create; Storage Object Creator fits if the API only writes. Step 3: grant it on that bucket, not the project. Step 4: retry and expect a 200. Step 5: confirm no broader role was added.
gcloud iam roles describe roles/storage.objectCreator \
--format="value(includedPermissions)"
gcloud storage buckets add-iam-policy-binding gs://orders-lab-4821-receipts \
--member=serviceAccount:orders-api@orders-lab-4821.iam.gserviceaccount.com \
--role=roles/storage.objectCreatorQuick reference
- Binding = role + principals (+ optional condition) on one resource
- Permission format: service.resource.verb, e.g. storage.objects.get
- Prefer predefined roles; avoid Owner, Editor and Viewer outside labs
- Allow policies add up down the hierarchy; only deny policies subtract
- Grant people access through groups, code access through service accounts
- Use add-iam-policy-binding, not set-iam-policy, for single changes
- allUsers = public internet; check for it in every review
Common mistakes
- Granting Editor on the project because a predefined role took longer to find.
- Removing a bucket binding and assuming access is gone while a project-level role still grants it.
- Using set-iam-policy with an old file and wiping bindings someone else added; prefer add-iam-policy-binding.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write the binding that lets the service account orders-api in project orders-lab-4821 read objects in gs://orders-lab-4821-receipts.
- Exercise 2medium
List every role a given user holds on a project, including ones granted through the project's ancestors.
- Exercise 3hard
A contractor needs read access to the receipts bucket for 30 days only. Design the binding and explain how it expires.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between a role and a permission?
- A permission is one action. A role is a named bundle of permissions, and roles are what you grant.
- How long does an IAM change take to apply?
- Usually seconds to a couple of minutes, but it can take longer to propagate, so retry before assuming the change failed.
- Can I see who used a permission?
- Yes. Cloud Audit Logs record admin actions by default, and data access logs can be turned on for reads.