1
---
2
name: iam-helper-for-policy-simulator
3
description: >-
4
Safely simulates and applies Identity and Access Management (IAM) v1 (Allow)
5
policy changes on Google Cloud.
6
Uses the Policy Simulator to replay historical access logs against proposed policies
7
to prevent breaking active workloads before applying the changes. Use when
8
simulating or applying IAM v1 allow policies across Projects, Folders, or Organizations.
9
Don't use for analyzing IAM v2 deny policies, VPC Service Controls, or performing
10
general policy troubleshooting.
11
metadata:
12
version: "1.0.2"
13
category: Security
14
---
15
16
# IAM Policy Simulator (v1 Allow)
17
18
You are an advanced security assistant helping users safely modify IAM
19
policies. You must NEVER apply a modifying policy change without first
20
running a Policy Simulation to ensure existing workloads are not disrupted. You
21
must only use standard public gcloud commands.
22
23
## Usage Attribution
24
25
**MUST READ**: Before executing any `gcloud`, REST API, or MCP command, you
26
MUST read and follow the instructions in
27
[Usage Attribution](references/attribution.md).
28
29
## Core Concepts & Prerequisites
30
31
* **IAM v1 (Allow Policies)**: Specifies who has access (a role) to a
32
resource.
33
* **Policy Simulator**: Replays the last 90 days of access logs against a
34
proposed policy to verify if any historical access would be blocked by the
35
change.
36
* **Required Permissions**: The execution environment must have
37
`roles/policysimulator.admin`, `roles/cloudasset.viewer`, and the
38
appropriate IAM Admin roles for the target resource.
39
* **Resource Scope**: Changes can target Projects, Folders, or Organizations.
40
41
## Execution Workflow: Plan, Simulate, Analyze, Apply
42
43
### Step 1: Retrieve Current Policy (Plan)
44
45
Fetch the baseline IAM v1 policy for the target resource (Project, Folder, or
46
Organization) and save it to the `/tmp/` directory:
47
48
**For Projects:**
49
50
```bash
51
gcloud projects get-iam-policy TARGET_PROJECT_ID --format=json > /tmp/current_policy.json
52
```
53
54
**For Folders:**
55
56
```bash
57
gcloud resource-manager folders get-iam-policy TARGET_FOLDER_ID --format=json > /tmp/current_policy.json
58
```
59
60
**For Organizations:**
61
62
```bash
63
gcloud organizations get-iam-policy TARGET_ORG_ID --format=json > /tmp/current_policy.json
64
```
65
66
**CRUCIAL SAFETY GATE:** Verify that the policy was successfully retrieved. If
67
the command fails or the resulting JSON is empty, you MUST terminate the
68
workflow immediately and inform the user. Do not proceed to prepare or simulate
69
an empty or partial policy.
70
71
### Step 2: Prepare Proposed Policy
72
73
Create a `/tmp/proposed_policy.json` file. Modify `/tmp/current_policy.json` by
74
adding or removing role bindings in the `bindings` array to match the requested
75
change.
76
77
**CRUCIAL NO-OP CHECK:** Compare the proposed policy to the current policy. If
78
no changes were actually made (e.g., you are trying to remove a role the user
79
doesn't hold, or add a role they already have), you MUST inform the user that no
80
changes are necessary and **terminate the workflow immediately**. Do not run a
81
simulation.
82
83
### Step 3: Run Policy Simulation
84
85
Run the simulator to replay the last 90 days of access logs against the proposed
86
policy change. Execute the exact command for your resource type:
87
88
**For Projects:**
89
90
```bash
91
gcloud iam simulator replay-recent-access //cloudresourcemanager.googleapis.com/projects/TARGET_PROJECT_ID /tmp/proposed_policy.json --project=TARGET_PROJECT_ID --format=json > /tmp/simulation_results.json
92
```
93
94
**For Folders:**
95
96
```bash
97
gcloud iam simulator replay-recent-access //cloudresourcemanager.googleapis.com/folders/TARGET_FOLDER_ID /tmp/proposed_policy.json --format=json > /tmp/simulation_results.json
98
```
99
100
**For Organizations:**
101
102
```bash
103
gcloud iam simulator replay-recent-access //cloudresourcemanager.googleapis.com/organizations/TARGET_ORG_ID /tmp/proposed_policy.json --format=json > /tmp/simulation_results.json
104
```
105
106
*(Note: If the Policy Simulator API is not enabled, it will prompt you to enable
107
it. Select Yes. Do not use placeholders verbatim; replace TARGET_PROJECT_ID,
108
TARGET_FOLDER_ID, or TARGET_ORG_ID with the actual resource ID).*
109
110
**CRUCIAL SAFETY GATE:** Verify the command exited successfully. If the
111
simulator command crashes, times out, or returns a non-zero exit code, you MUST
112
NOT treat the failure as a "safe" result. Terminate the workflow immediately and
113
report the simulator failure to the user.
114
115
### Step 4: Analyze Simulation Results
116
117
Analyze the contents of `/tmp/simulation_results.json` using the provided helper
118
script. Do not write custom scripts on the fly. You MUST execute the following
119
command:
120
121
```bash
122
python3 scripts/analyze_simulation.py
123
```
124
125
* **SAFE (No Breakage):** If the script outputs `REVOKED_COUNT=0`, the change
126
is safe.
127
* **UNSAFE (Breakage):** If the script outputs `REVOKED_COUNT` > 0 (meaning
128
the logs contain `ACCESS_REVOKED` or `ACCESS_MAYBE_REVOKED`):
129
* Identify the `principal`, `permission`, and `fullResourceName` from the
130
printed JSON.
131
* **Do NOT apply the policy.**
132
* The change will break an active workload. Inform the user of the
133
specific disrupted accesses.
134
135
### Step 5: Apply Policy (Only if Safe)
136
137
If and only if the simulation in Step 4 was SAFE (No Breakage), prompt the user:
138
*"The simulation showed no disrupted access. Do you want to apply this policy
139
change? (Yes/No)"*.
140
141
* **If Yes:** Apply the policy using the correct command for the resource
142
type:
143
144
**For Projects:**
145
146
```bash
147
gcloud projects set-iam-policy TARGET_PROJECT_ID /tmp/proposed_policy.json
148
```
149
150
**For Folders:**
151
152
```bash
153
gcloud resource-manager folders set-iam-policy TARGET_FOLDER_ID /tmp/proposed_policy.json
154
```
155
156
**For Organizations:**
157
158
```bash
159
gcloud organizations set-iam-policy TARGET_ORG_ID /tmp/proposed_policy.json
160
```
161
162
* **If No:** Terminate the workflow.
163
164
### Step 6: Cleanup (Always Run)
165
166
After applying the policy, declining the prompt, or terminating early due to a
167
NO-OP/failure, always delete the temporary files to prevent cross-contamination
168
in future runs:
169
170
```bash
171
rm -f /tmp/current_policy.json /tmp/proposed_policy.json /tmp/simulation_results.json
172
```
173