📋 In This Post
Lab Overview
The problem. Week 7 needed an identity that could read a
Cloud Asset Inventory feed. No read-only role can: cloudasset.viewer
lacks cloudasset.feeds.get, and the only role that has it —
cloudasset.owner — also grants create, update and delete. The
read-only half of this lab's CI pipeline was handed the ability to delete the
thing it was only supposed to read.
Objective. Replace that over-grant with a custom role carrying exactly the two permissions a plan needs, and add a deny policy so the destructive permission cannot be granted back by accident.
Expected outcome. The plan identity can read a feed and cannot delete one, demonstrated with Policy Troubleshooter v3 — and an explanation of why v1 would have told us the opposite.
| Difficulty | Advanced — deny policies use a different principal syntax from everything else in IAM |
|---|---|
| Deployment | Terraform via HCP Terraform remote execution, Workload Identity Federation |
| Reproduce it | Lab guide — v2 principal identifiers, v3 queries, cleanup |
| Cost | $0 — custom roles, deny policies and Policy Troubleshooter are all free |
Cloud Services and Concepts
| Service | Purpose here | What it is |
|---|---|---|
| IAM custom roles | Grants exactly the permissions a predefined role over-grants | Carry exactly the two permissions a Terraform plan needs to read an asset feed, where no predefined role fits. |
| IAM deny policies | Refuses a permission no matter who granted it | Refuse a permission outright, regardless of what roles anyone holds. Evaluated before allow policies. |
| Policy Troubleshooter v3 | Explains why a given principal was allowed or refused | Answers what a principal can actually do, taking deny policies into account. v1 does not. |
| Cloud Asset Inventory | Finds the grants the deny policy has to cover | The resource being protected — the feed Week 7 created. |
The concept this lab turns on: deny beats allow. An IAM allow policy answers "has anyone granted this?" A deny policy answers "has anyone forbidden it?" — and the deny is evaluated first. That makes it the only mechanism that holds against a future over-grant, including one made by an Organization Administrator. It is also why the exception list matters more than the rule: a deny with no way out gets deleted the first time somebody legitimately needs the denied action.
Architecture and Workflow
The flow
- A custom role,
tfPlanAssetReader, is created with exactly two permissions:cloudasset.feeds.getandcloudasset.feeds.list. - The plan identity is bound to it, and Week 7's
roles/cloudasset.ownerbinding is removed. - A deny policy refuses
cloudasset.googleapis.com/feeds.deletetoprincipalSet://goog/public:all— everyone. - Two principals are exempted: the apply identity, which legitimately manages the feed, and one named human as break-glass.
- Any delete request is checked against the deny first; only the exempted two reach the allow policy at all.
- Policy Troubleshooter v3 is asked what each principal can do. v1 is asked the same question, as a demonstration of why it must not be used.
Two decisions worth naming
A custom role, not a narrower predefined one. There isn't one. This is the documented case for a custom role: no predefined role fits, and the reason is recorded alongside it.
The deny has exactly two exceptions, and one is a human. A deny with no exception is a guardrail people learn to switch off. One named exception is safer than a policy that gets deleted under pressure.
Prerequisites
- An existing over-grant to take back. Week 7 supplies one; otherwise create an equivalent, or the lab has nothing to narrow.
roles/iam.organizationRoleAdminto create a custom role, androles/iam.denyAdminto write a deny policy, both at the organization.roles/iam.denyReviewerfor the operator, to read deny policies back.- A quota project with
policytroubleshooter.googleapis.comenabled. - Terraform 1.9+ with
hashicorp/google~> 6.0.
Exact permissions, variables and the v2 principal syntax are in the lab guide.
Implementation
Three resources. Commands and expected outputs are in the lab guide; what follows is why each exists.
Step 1 — A custom role carrying only what a plan reads
Why: cloudasset.viewer cannot read a feed and
cloudasset.owner can delete one. Nothing in between exists, which
is the documented case for a custom role.
Step 2 — Remove the over-grant
Why: the custom role is pointless while the broader binding remains. This is the actual remediation; the rest is prevention.
Step 3 — Deny the destructive permission outright
Why: removing a role stops today's over-grant. A deny policy stops the next one, because deny is evaluated before allow and holds even against Organization Administrator.
denied_principals = ["principalSet://goog/public:all"]
exception_principals = [
"principal://iam.googleapis.com/projects/-/serviceAccounts/${var.apply_service_account}",
"principal://goog/subject/${var.break_glass_principal}",
]
denied_permissions = ["cloudasset.googleapis.com/feeds.delete"]
Those principal:// strings are IAM v2
identifiers and are not the member strings used everywhere else in Terraform.
Using the allow-policy spelling fails with "invalid principal … as part of
the exempted principals list".
Validation and Evidence
| Test | Expected | Actual | Status |
|---|---|---|---|
| Infrastructure deployed | 3 resources | 3 added, 0 changed, 0 destroyed | Pass |
| Custom role carries only two permissions | feeds.get, feeds.list | exactly two, nothing else | Pass |
| Week 7's over-grant removed | no cloudasset.owner on the plan identity | removed | Pass |
| Deny policy exempts the right two | apply identity + break-glass human | both present | Pass |
| Plan identity may not delete a feed | CANNOT_ACCESS (v3) | CANNOT_ACCESS | Pass |
| Plan identity may still read one | CAN_ACCESS | CAN_ACCESS | Pass |
| Apply identity may still manage one | CAN_ACCESS | CAN_ACCESS | Pass |
| A real delete attempt is refused in practice | audit log shows a denial | not tested — validation is an evaluation, not an observed denial | Unverified |
Deployment is not the control working. Row one says Terraform applied. Rows five to seven are the control: the same question asked of three principals, with three different answers. The last row is honest about what was not done — no delete was actually attempted and no audit entry captured, so the evidence is an evaluation rather than an observed refusal.
Security and Cost
Security decisions
| Control | Decision and trade-off |
|---|---|
| Least privilege | A custom role with two permissions replaces a role with create, update and delete. The reason a custom role was necessary is recorded with it. |
| Defence in depth | Removing the role fixes today. The deny policy holds against the next over-grant, including one made by an Organization Administrator. |
| Break-glass | One named human is exempted. Deliberate, and a single point of failure — a group would be better. |
| Instrumentation | Policy Troubleshooter v3 only. v1 cannot see deny policies and reports GRANTED while one is blocking. |
| Accepted gap | One permission on one resource type. This demonstrates the mechanism rather than covering the estate. |
For production, the deny would cover the destructive verbs across the services that matter, break-glass would be a group with membership alerting, and the evidence would include an audit log entry from a real refused attempt rather than an evaluation.
Cost
$0. Custom roles, deny policies and Policy Troubleshooter are all free, and nothing in this lab runs.
Troubleshooting and Lessons
The instrument could not see the control
Problem: with the deny policy active and the permission
plainly blocked, Policy Troubleshooter reported GRANTED.
Root cause: gcloud policy-troubleshoot iam
calls v1, which evaluates allow policies only. It cannot see a
deny policy at all.
Resolution: call the v3 endpoint directly. Measured
2026-10-05: v1 said GRANTED, v3 said CANNOT_ACCESS,
same tuple, same moment.
Lesson: an instrument that cannot observe the control you are testing will confirm whatever you hoped. A validator built on v1 would have passed while the guardrail did nothing.
Deny policies speak a different principal language
Problem: the apply failed with "invalid principal … as part of the exempted principals list".
Root cause: deny policies use IAM v2 principal identifiers
— principal://iam.googleapis.com/projects/-/serviceAccounts/…
for a service account, principal://goog/subject/… for a human
— not the serviceAccount: and user: member
strings used everywhere else in Terraform.
Resolution: use the v2 forms. Only "everyone" takes the
principalSet:// shape.
Lesson: the error names the field but not the syntax, and the surrounding Terraform uses the other spelling consistently. Expect the two IAM generations to disagree on identifiers.
Reading a deny policy back is harder than writing one
Problem: gcloud iam policies describe returned
nothing for an organization attachment point, and the list endpoint returned
names without rules.
Resolution: read it over IAM v2 REST. Only the GET shows the exceptions.
Lesson: if the exceptions are the part that matters, use the call that returns them. A validator that lists policy names proves only that a policy exists.
Cleanup and Key Takeaways
Cleanup
terraform destroy removes all three resources. Verify against the
cloud: the deny policy should 404 from the v2 GET, and the custom role should
report as deleted.
Destroying re-opens Week 7's gap. The plan identity loses the
narrow custom role, and restoring the old binding gives back
cloudasset.owner. Decide deliberately which state you want rather
than destroying by habit. Commands are in the lab guide.
Ongoing charges: none.
Key takeaways
- An instrument that cannot see the control will confirm whatever you hoped. Policy Troubleshooter v1 reports GRANTED while a deny policy is actively blocking. Use v3.
- Deny policies use IAM v2 principal identifiers, which differ from the member strings used everywhere else. The error message names the field, not the syntax.
- A deny with no exception gets deleted, not used. The exception list is the part that determines whether the guardrail survives an incident.
- "No predefined role fits" is the documented case for a custom role — and the reason belongs next to the role, because the next person will wonder why it exists.
References
- Lab guide — reproduction steps, v2 syntax, known limitations
- Deny policies overview
- Principal identifiers
- Policy Troubleshooter
- Source repository
Comments