Bird
Raised Fist0
GCPcloud~10 mins

IAM conditions for fine-grained control in GCP - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - IAM conditions for fine-grained control
User Request
IAM Policy Check
Evaluate Conditions
Allow Access
Service Response
When a user makes a request, GCP checks the IAM policy and evaluates any conditions. If conditions are met, access is allowed; otherwise, it is denied.
Execution Sample
GCP
bindings:
- role: roles/storage.objectViewer
  members:
  - user:alice@example.com
  condition:
    title: "Time-based access"
    expression: "request.time < timestamp('2024-12-31T23:59:59Z')"
This IAM policy grants Alice read access to storage objects only before the end of 2024.
Process Table
StepRequest TimeCondition ExpressionCondition ResultAccess Decision
12024-06-01T12:00:00Zrequest.time < timestamp('2024-12-31T23:59:59Z')TrueAllow
22025-01-01T00:00:00Zrequest.time < timestamp('2024-12-31T23:59:59Z')FalseDeny
💡 Access decision made based on condition evaluation result.
Status Tracker
VariableStartAfter Step 1After Step 2
request.timeN/A2024-06-01T12:00:00Z2025-01-01T00:00:00Z
condition resultN/ATrueFalse
access decisionN/AAllowDeny
Key Moments - 2 Insights
Why does access get denied even though the user has the role?
Because the IAM condition evaluates to False (see execution_table step 2), so the policy does not grant access.
What happens if the condition is missing?
Without a condition, the role applies unconditionally, so access would be allowed regardless of request time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the access decision at step 1?
AAllow
BDeny
CConditional
DUnknown
💡 Hint
Check the 'Access Decision' column in execution_table row for step 1.
At which step does the condition evaluate to False?
ANever
BStep 1
CStep 2
DBoth steps
💡 Hint
Look at the 'Condition Result' column in execution_table.
If the request time was 2024-11-30T10:00:00Z, what would the access decision be?
ADepends on role
BAllow
CDeny
DDepends on user
💡 Hint
Compare with execution_table step 1 where condition was True and access was allowed.
Concept Snapshot
IAM conditions add rules to roles.
They check request details like time or IP.
If condition is True, access is granted.
If False, access is denied even if role matches.
Use conditions for fine-grained control.
Full Transcript
When a user requests access, Google Cloud checks the IAM policy. If the policy has conditions, it evaluates them using request data like time. If the condition is true, access is allowed. If false, access is denied even if the user has the role. For example, a policy can allow access only before a certain date. This helps control access precisely.

Practice

(1/5)
1. What is the main purpose of using IAM conditions in Google Cloud?
easy
A. To add extra rules that control access more precisely
B. To create new user accounts automatically
C. To increase the storage capacity of a project
D. To monitor network traffic in real-time

Solution

  1. Step 1: Understand IAM conditions

    IAM conditions allow adding rules that specify when and how permissions apply.
  2. Step 2: Identify the purpose

    The purpose is to control access more precisely by adding conditions like time or IP restrictions.
  3. Final Answer:

    To add extra rules that control access more precisely -> Option A
  4. Quick Check:

    IAM conditions = precise access control [OK]
Hint: IAM conditions add rules to limit access precisely [OK]
Common Mistakes:
  • Confusing IAM conditions with user creation
  • Thinking IAM conditions increase storage
  • Mixing IAM conditions with network monitoring
2. Which of the following is the correct syntax to add a condition in an IAM policy binding in JSON?
easy
A. "condition": "request.time < timestamp('2024-12-31T23:59:59Z')"
B. "condition": {"title": "exp", "expression": "request.time < timestamp('2024-12-31T23:59:59Z')", "description": "Expire end of 2024"}
C. "condition": {"title": "exp", "expr": "request.time < timestamp('2024-12-31T23:59:59Z')"}
D. "condition": {"title": "exp", "expression": "request.time > timestamp('2024-12-31T23:59:59Z')"}

Solution

  1. Step 1: Check the required fields for IAM condition

    The condition must have title, expression, and description fields in JSON.
  2. Step 2: Verify the expression syntax

    "condition": {"title": "exp", "expression": "request.time < timestamp('2024-12-31T23:59:59Z')", "description": "Expire end of 2024"} correctly uses "expression" with a valid timestamp comparison and includes title and description.
  3. Final Answer:

    "condition": {"title": "exp", "expression": "request.time < timestamp('2024-12-31T23:59:59Z')", "description": "Expire end of 2024"} -> Option B
  4. Quick Check:

    Correct JSON fields and expression = "condition": {"title": "exp", "expression": "request.time < timestamp('2024-12-31T23:59:59Z')", "description": "Expire end of 2024"} [OK]
Hint: Condition needs title, expression, and description keys [OK]
Common Mistakes:
  • Using string instead of object for condition
  • Missing description or title fields
  • Using wrong key name like 'expr' instead of 'expression'
3. Given this IAM condition expression:
request.time > timestamp('2024-01-01T00:00:00Z') && request.time < timestamp('2024-12-31T23:59:59Z')
What will happen if a user tries to access a resource on 2023-12-31?
medium
A. Access will be denied
B. Access will be allowed
C. Access will be allowed only if user is admin
D. Access will be allowed but logged as warning

Solution

  1. Step 1: Understand the time condition

    The condition allows access only if request time is after 2024-01-01 and before 2024-12-31.
  2. Step 2: Check the access date

    On 2023-12-31, the request time is before the allowed start date, so condition fails.
  3. Final Answer:

    Access will be denied -> Option A
  4. Quick Check:

    Request time outside range = deny access [OK]
Hint: Access allowed only within specified time range [OK]
Common Mistakes:
  • Assuming access allowed before start date
  • Confusing AND (&&) with OR (||) in condition
  • Thinking admin role bypasses condition
4. You wrote this IAM condition:
"condition": {"title": "IP Restriction", "expression": "request.ip == '192.168.1.1'"}
But it does not work as expected. What is the likely problem?
medium
A. IAM conditions do not support IP address restrictions
B. The title field is missing
C. The expression should use 'request.ip in ['192.168.1.1']' for exact match
D. The expression uses '==' instead of 'in' for IP matching

Solution

  1. Step 1: Check expression operator for IP

    IAM conditions require 'in' operator to match IPs, not '==' which is invalid for strings.
  2. Step 2: Confirm title presence and IP support

    Title is present and IP restrictions are supported, so problem is operator usage.
  3. Final Answer:

    The expression uses '==' instead of 'in' for IP matching -> Option D
  4. Quick Check:

    Use 'in' operator for IP matching [OK]
Hint: Use 'in' operator for IP address matching in conditions [OK]
Common Mistakes:
  • Using '==' instead of 'in' for IP
  • Removing title field
  • Believing IP restrictions are unsupported
5. You want to grant a user access to a Cloud Storage bucket only if the request comes from a specific label on the resource and during business hours (9 AM to 5 PM UTC). Which IAM condition expression correctly combines these requirements?
hard
A. "resource.labels.env == 'prod' && request.time >= timestamp('2024-01-01T09:00:00Z') && request.time <= timestamp('2024-01-01T17:00:00Z')"
B. "resource.labels.env == 'prod' || (request.time >= timestamp('09:00:00Z') && request.time <= timestamp('17:00:00Z'))"
C. "resource.labels.env == 'prod' && request.time >= timestamp('1970-01-01T09:00:00Z') && request.time <= timestamp('1970-01-01T17:00:00Z')"
D. "resource.labels.env == 'prod' && request.time >= timestamp('1970-01-01T09:00:00Z') && request.time <= timestamp('1970-01-01T17:00:00Z') && request.time.date() == request.time.date()"

Solution

  1. Step 1: Understand label and time conditions

    Label check uses resource.labels.env == 'prod'. Time must be between 9 AM and 5 PM UTC daily.
  2. Step 2: Check timestamp usage for daily time

    Since IAM conditions lack direct time-of-day functions, use timestamps with a fixed date (like 1970-01-01) to represent daily hours.
  3. Step 3: Evaluate options

    "resource.labels.env == 'prod' && request.time >= timestamp('1970-01-01T09:00:00Z') && request.time <= timestamp('1970-01-01T17:00:00Z')" correctly uses fixed date timestamps for time range and combines with label check using AND (&&).
  4. Final Answer:

    "resource.labels.env == 'prod' && request.time >= timestamp('1970-01-01T09:00:00Z') && request.time <= timestamp('1970-01-01T17:00:00Z')" -> Option C
  5. Quick Check:

    Label AND daily time range with fixed date timestamps = "resource.labels.env == 'prod' && request.time >= timestamp('1970-01-01T09:00:00Z') && request.time <= timestamp('1970-01-01T17:00:00Z')" [OK]
Hint: Use fixed date timestamps to represent daily time ranges [OK]
Common Mistakes:
  • Using OR instead of AND to combine conditions
  • Using actual dates instead of fixed date for daily time
  • Adding unnecessary redundant conditions