Brains4Cloud
ESEN
Practice

Topic 1 of 3

D4 · Configuring access and security

IAM: policies, inheritance, and roles

Exam section 4.1 covers managing IAM: viewing and changing policies, understanding inheritance in the resource hierarchy, choosing between basic, predefined, and custom roles, and applying least privilege.

IAM controls who can do what on which resource. To give access, you grant a role to a principal on a resource. Permissions are never granted directly: they always come inside roles.

Principal

People: Google Accounts, Google groups, and federated identities in workforce pools. Workloads: service accounts and identities in workload identity pools. Previously called "members".

Role

A set of permissions in the form service.resource.verb, for example resourcemanager.projects.list.

Resource

A service resource (VM, bucket, topic) or a container: project, folder, or organization.

Allow policies

An allow policy (formerly "IAM policy") is a JSON or YAML object attached to a resource with a list of role bindings: each one ties a role to the principals who hold it.

{
  "bindings": [
    { "role": "roles/storage.objectViewer",
      "members": ["group:analysts@example.com"] },
    { "role": "roles/compute.instanceAdmin.v1",
      "members": ["user:ana@example.com"],
      "condition": { "title": "until-dec",
        "expression": "request.time < timestamp('2026-12-31T00:00:00Z')" } }
  ],
  "etag": "BwUjMhCsNvY=",
  "version": 3
}
  • Up to 1,500 principals per policy.
  • The etag provides concurrency control: it stops two simultaneous changes from overwriting each other.
  • Version: 1 doesn't support conditions; 3 adds the condition field (2 is internal).
  • Changes usually take effect within about 2 minutes, but sometimes take 7 minutes or more.

Inheritance and the effective policy

An allow policy on a container applies to that container and everything below it: organization → folders → projects → resources. A resource's effective policy is the union of its own policy and those of all its ancestors: a binding at any level is enough to grant access.

  • A permission granted higher up can't be removed with an allow policy lower down. To restrict it you need a deny policy.
  • Resources that don't accept their own allow policy (for example, log buckets) inherit from their ancestors: grant the role on the project.
  • Google best practices: grant roles to groups instead of individual users; few teams with roles on the organization (security, network); folders that mirror the company structure.

TypeDetail
BasicHighly permissive, with thousands of permissions across all services. Google says not to use them in production unless there's no alternative
PredefinedCreated and maintained by Google for specific tasks in each service; updated automatically when features are added. E.g. roles/pubsub.publisher
CustomYou create them with the exact permissions. They help with least privilege, but maintenance is on you

Basic roles (preview)

  • roles/reader: read-only actions.
  • roles/writer: Reader plus actions that modify state.
  • roles/admin: Writer plus sensitive tasks, such as managing roles and permissions or setting up billing.
  • Not granted from the console: use the API or gcloud.

Legacy basic roles

  • roles/viewer, roles/editor, and roles/owner, which predate IAM (formerly "primitive").
  • They also give special service permissions, such as Cloud Storage convenience values.
  • They don't support conditions.
  • Owner doesn't include every permission: for example, it can't change payment information or create deny policies.

Creating and managing custom roles

  • Created in an organization or project, never a folder (to use one in a folder, create it in the organization). They can only be granted within that organization or project.
  • Limit: 300 per organization and 300 per project.
  • A project role can't include permissions that only work at the folder or organization level.
  • Not every permission is supported: each has a SUPPORTED, TESTING, or NOT_SUPPORTED level.
  • Some permissions only work together: updating a policy needs getIamPolicy as well as setIamPolicy. Starting from a predefined role helps.
  • Informational launch stage: ALPHA, BETA, GA, DEPRECATED. With DISABLED the role stays in policies but has no effect.
  • You can't change the role ID. A deleted role can be undeleted for 7 days, and its ID can't be reused until 44 days have passed.
  • Anyone who can edit custom roles can add any permission to them: limit that power to very few people.
gcloud iam roles create vmOperator --project=my-project \
  --title="VM Operator" --description="Start and stop VMs" \
  --permissions="compute.instances.get,compute.instances.list,compute.instances.start,compute.instances.stop" \
  --stage=GA

gcloud iam roles create orgAuditor --organization=123456789012 --file=role.yaml
gcloud iam roles update vmOperator --project=my-project --stage=DISABLED
gcloud iam roles delete vmOperator --project=my-project
gcloud iam roles undelete vmOperator --project=my-project

Viewing, granting, and revoking access

# Grant or remove a role (one-off change)
gcloud projects add-iam-policy-binding my-project \
  --member=group:devs@example.com --role=roles/compute.viewer
gcloud projects remove-iam-policy-binding my-project \
  --member=group:devs@example.com --role=roles/compute.viewer

# Bulk changes: read, modify, write
gcloud projects get-iam-policy my-project --format=json > policy.json
gcloud projects set-iam-policy my-project policy.json
  • Principal formats: user:, group:, serviceAccount:, domain:.
  • set-iam-policy overwrites the whole policy. Always follow the read-modify-write pattern so you don't drop bindings by accident; the etag rejects the write if someone else changed it in between.
  • The same commands exist for folders (gcloud resource-manager folders), organizations, and individual resources.

IAM Conditions

Attribute-based access control: the binding only grants the role if the expression, written in a subset of CEL (Common Expression Language), is true. Useful, for example, for temporary access or access limited to resources with a certain name. They're also used in deny rules and Principal Access Boundary.

request.time < timestamp('2026-12-31T00:00:00Z')
resource.name.startsWith('projects/_/buckets/exampleco-site-assets/')
  • Not allowed with legacy basic roles (Owner, Editor, Viewer) or with allUsers or allAuthenticatedUsers.
  • A conditional binding doesn't override an unconditional one: if the principal already has the role without a condition, they always have it.
  • On a Cloud Storage bucket they require uniform bucket-level access.
  • Google recommends no more than 100 conditional bindings per policy.

Deny policies

  • Stop certain principals from using certain permissions even if they hold a role that includes them: IAM evaluates deny policies before allow policies.
  • Attached to a project, folder, or organization and inherited downward.
  • Each rule: denied principals, denied permissions, an optional condition, and exceptions (exceptionPrincipals, exceptionPermissions).
  • Up to 500 deny policies per resource.

Principal Access Boundary (PAB)

  • Attached to principal sets, not resources, and define which resources principals are eligible to access.
  • If a resource is outside the boundary, there's no access even if an allow policy grants it.
  • Useful to block access to resources in other organizations (phishing, exfiltration) or keep service accounts to certain projects.
  • They only block supported permissions.

Privileged Access Manager (PAM) lets principals request temporary, auditable access, with justification or approval, instead of holding a role permanently.

Policy Analyzer, Troubleshooter, and recommendations

ToolAnswers…
Policy Analyzer"Who has access to this service account?", "Which datasets can this user read?" (part of Cloud Asset Inventory)
Policy Troubleshooter"Why does this user have, or not have, this permission?"
Role recommendationsRemove or replace roles with excess permissions
Activity AnalyzerWhen service accounts and their keys were last used
Service account insightsService accounts unused in the past 90 days
Lateral movement insightsRoles that let a service account impersonate one in another project

Exam scenarios

SituationAnswerWhy
Give the data team read access to every project in a folderGrant the predefined role to the group on the folderIt's inherited; groups simplify management.
No predefined role fits and the closest one grants too muchCustom role at the project or organization levelLeast privilege; folder-level custom roles don't exist.
Give a contractor access only until the end of the monthRole binding with a request.time conditionNeeds policy version 3.
Stop everyone except one group from deleting projects across the organizationDeny policy with exceptionPrincipalsEvaluated before allow policies.
Find out why a user can't read a bucketPolicy TroubleshooterExplains which policy grants or denies.
Find out who has access to a BigQuery datasetPolicy AnalyzerAnalyzes allow policies.
Remove permissions a user hasn't used in monthsRole recommendations (IAM recommender)Suggests removing or replacing roles.
Prevent service accounts from accessing resources in other organizationsPrincipal Access Boundary policyDefines which resources principals can access.

Common exam traps

❓ Can you remove, on a project, a role that was granted on the folder?

Not with an allow policy: the effective policy is the union of every level. To block it, use a deny policy.

❓ Can you create a custom role at the folder level?

No. Only at the organization or project level; for a folder, create it in the organization.

❓ Can you add a condition to an Editor role binding?

No. Legacy basic roles (Owner, Editor, Viewer) don't support conditions.

❓ What happens if you run set-iam-policy with a file that only has the new binding?

It overwrites the whole policy and the other bindings are lost. Read, modify, then write.

❓ Does Owner have every permission?

No. For example, it can't change Cloud Billing payment information or create deny policies.

❓ Which is evaluated first, a deny or an allow policy?

The deny policy. If it denies the permission, the principal's roles don't matter.

Did you get this topic?

Test what you just learned

A support team should only be able to start and stop VMs in the prod project. No predefined role is limited to that. Following best practices, what do you do?

Ready to test "IAM: policies, inheritance, and roles"?

Original questions, each with a full explanation.

Start a mock exam