4: Configuring access and security

    Viewing and creating IAM policies

    An IAM policy in Google Cloud is a JSON document attached to one resource node in the hierarchy.

    Each policy is a list of role bindings, where every binding pairs a role with a list of principals and, optionally, with a condition expression that filters when the binding applies.

    The effective set of permissions a principal holds against any resource is the union of every binding inherited from the Organization down to the leaf, intersected with any IAM Deny policies that match the same principal.

    Policies are versioned through an etag field that supports optimistic concurrency; if two operators read the same policy and one writes it before the other, the stale writer receives FAILED_PRECONDITION and must re-read the policy and reapply the change.

    How it works

    The problem this model solves is administrative: Google Cloud has tens of thousands of granular permissions across hundreds of services, and a flat permission list per principal would be unmanageable.

    By attaching bindings to resource nodes and inheriting them downward, an operator can grant roles/storage.objectViewer once at the Folder for "Data Platform" and have every Project, every bucket, and every object inside that Folder honor the grant.

    The trade-off is that inherited policies are easy to overlook: a principal who looks underpowered at the Project level may be over-privileged because of a binding three levels up.

    The Policy Troubleshooter and the IAM Recommender exist to surface this.

    A policy is read with gcloud projects get-iam-policy (or gcloud organizations get-iam-policy, gcloud resource-manager folders get-iam-policy, and the resource-specific variants like gcloud storage buckets get-iam-policy).

    Two write paths exist:

    • set-iam-policy, which expects a full JSON or YAML policy file and overwrites the entire policy in one round trip.
    • add-iam-policy-binding / remove-iam-policy-binding, which mutate a single binding through a read-modify-write loop that gcloud performs server-side.

    The single-binding helpers are safer for day-to-day grants because they reduce the chance of clobbering a binding added by another operator between reads.

    Key points

    Policy version 3 is the format that supports IAM Conditions. A binding gains a condition block with a CEL expression that scopes the grant by request attribute, resource path, or time window.

    The most common ACE patterns are:

    • time-bounded break-glass access (request.time < timestamp("2030-01-01T00:00:00Z"))
    • tag-bound access (resource.matchTag("env", "prod"))
    • resource-name prefixes (resource.name.startsWith("projects/_/buckets/data-prod-"))

    When you set a conditional binding, the policy is auto-promoted to version 3, and any subsequent read must pass --policy-version=3 or gcloud will return the binding without its condition and warn about a truncated view.

    IAM Deny policies are a separate policy resource attached to Organization, Folder, or Project nodes. Where a grant binding answers "may this principal call this method?", a deny rule answers "this principal must NEVER call this method, regardless of any grant."

    Deny policies are evaluated before grants, so a deny for iam.serviceAccountKeys.create at the Organization beats a roles/iam.serviceAccountKeyAdmin grant at a Project. Denies are how ACE-level operators reliably block residual permissions on Owner principals without removing the role.

    Reading a policy

    gcloud projects get-iam-policy data-prod-3142 \
     --format=json > policy.json
    
    gcloud storage buckets get-iam-policy gs://landing-bronze \
     --format=json
    

    The output prints bindings, etag, and version. The etag is base64-encoded and must be passed back unchanged on set-iam-policy.

    Writing a single binding

    gcloud projects add-iam-policy-binding data-prod-3142 \
     --member="user:lina@example.com" \
     --role="roles/bigquery.dataViewer"
    
    gcloud projects remove-iam-policy-binding data-prod-3142 \
     --member="serviceAccount:loader@data-prod-3142.iam.gserviceaccount.com" \
     --role="roles/storage.objectAdmin"
    

    The --member prefix is the type discriminator. Allowed prefixes are user:, group:, serviceAccount:, domain:, principal: (for Workforce Identity Federation), principalSet: (for Workload Identity Federation pools), allUsers, and allAuthenticatedUsers. The last two are unauthenticated and authenticated-but-untrusted public buckets respectively and are the most consequential typo in Google Cloud.

    Replacing a full policy

    gcloud projects set-iam-policy data-prod-3142 policy.json
    

    The whole policy file is sent in one call. If another operator added a binding between your get and set, the etag check fails and the call is rejected.

    Setting a conditional binding

    gcloud projects add-iam-policy-binding data-prod-3142 \
     --member="user:on-call@example.com" \
     --role="roles/compute.instanceAdmin.v1" \
     --condition='expression=request.time < timestamp("2030-01-01T00:00:00Z"),title=breakglass_window,description=expires 2030'
    

    The CEL expression evaluates per request. The title is required and is the display name in the Console. A binding without a title is rejected.

    Policy hierarchy resolution

    A read on Object1 evaluates bindings on the object, on the bucket, on the Project, on the Folder, and on the Organization, and unions every role granted to the calling principal. Any Deny policy attached to any of those nodes is checked first and short-circuits the allow if a matching deny rule exists.

    💡 Exam Trap: allUsers makes a resource accessible from the public internet without any credentials. allAuthenticatedUsers includes anyone with a Google account, including consumer accounts on gmail.com. Neither belongs in a production policy unless the resource is intentionally public, such as a static website asset.

    💡 Exam Trap: set-iam-policy overwrites the policy in one call. If you read the policy through the Console, edit it locally for an hour, and then re-apply it, you will roll back any binding added by another operator in the interim. The etag check protects against this only if the original etag is preserved in the file.

    📊 Limit: Google Cloud caps a single IAM policy at 1,500 principals (members across all bindings) on a Project resource and 250 conditional bindings; an Organization policy supports 20,000 principals. Hitting the principal cap on a Project signals that grants should be made to groups rather than to individual users.

    ⚠️ Anti-Pattern: Editing IAM policies by hand and uploading the JSON through set-iam-policy for routine grants. Use add-iam-policy-binding and remove-iam-policy-binding for single changes, reserve set-iam-policy for bulk reconciliations driven by Terraform or a similar codified source of truth.

    🎯 Scenario: A data engineer has been granted roles/bigquery.dataViewer on the data-prod-3142 Project, but a query against the warehouse.customers table returns Access Denied: BigQuery BigQuery: Permission bigquery.tables.getData denied. The Policy Troubleshooter shows the grant is inherited from the Folder, but a Deny policy at the Organization denies bigquery.tables.getData on the principal's group for any resource whose data_class tag equals pii. The fix is to remove the engineer from the denied group, not to add another grant.

    Decision Anchor: Choose add-iam-policy-binding when the change is a single grant or revocation that an operator will type. Choose set-iam-policy when reconciliation runs from a code-managed source of truth and the etag is part of the reconciliation loop.


    Continue with the interactive course

    Track your progress, jump into practice questions and use the Shark AI tutor inside the GCP - Associate Cloud Engineer learning hub.