Home› Blog› Week 8 — Taking Back a Permission I Had to Give Aw…
GCP Weekly Lab GCP Terraform

IAM deny policies and custom roles

Take back a permission a previous week had to over-grant, and prove the guardrail works with an instrument that can actually see it.

Verified against current vendor documentation on 9 October 2026. Pricing, limits and API behaviour were checked against the official docs on that date. Cloud services change fast — if you are reading this much later, treat the specifics as a starting point and re-check the linked sources.
GCP Platform Engineering Lab · Week 8 of 58

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.

DifficultyAdvanced — deny policies use a different principal syntax from everything else in IAM
DeploymentTerraform via HCP Terraform remote execution, Workload Identity Federation
Reproduce itLab guide — v2 principal identifiers, v3 queries, cleanup
Cost$0 — custom roles, deny policies and Policy Troubleshooter are all free

Cloud Services and Concepts

ServicePurpose hereWhat it is
IAM custom rolesGrants exactly the permissions a predefined role over-grantsCarry exactly the two permissions a Terraform plan needs to read an asset feed, where no predefined role fits.
IAM deny policiesRefuses a permission no matter who granted itRefuse a permission outright, regardless of what roles anyone holds. Evaluated before allow policies.
Policy Troubleshooter v3Explains why a given principal was allowed or refusedAnswers what a principal can actually do, taking deny policies into account. v1 does not.
Cloud Asset InventoryFinds the grants the deny policy has to coverThe 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

A request evaluated first against an IAM deny policy and only then against the allow policy, with Policy Troubleshooter explaining which rule decided the outcome.
Deny is evaluated before allow, so the order in this picture is the entire control.

The flow

  1. A custom role, tfPlanAssetReader, is created with exactly two permissions: cloudasset.feeds.get and cloudasset.feeds.list.
  2. The plan identity is bound to it, and Week 7's roles/cloudasset.owner binding is removed.
  3. A deny policy refuses cloudasset.googleapis.com/feeds.delete to principalSet://goog/public:all — everyone.
  4. Two principals are exempted: the apply identity, which legitimately manages the feed, and one named human as break-glass.
  5. Any delete request is checked against the deny first; only the exempted two reach the allow policy at all.
  6. 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.organizationRoleAdmin to create a custom role, and roles/iam.denyAdmin to write a deny policy, both at the organization.
  • roles/iam.denyReviewer for the operator, to read deny policies back.
  • A quota project with policytroubleshooter.googleapis.com enabled.
  • 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.

Repository structure: LAB.md, terraform, scripts and docs, each with a one-line responsibility.
How the week is organised. Full source on GitHub.

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.

Google Cloud console showing the tfPlanAssetReader custom role with two included permissions.
Validates Step 1: two permissions, both read-only. The validator additionally fails if a third ever appears — least privilege that drifts is no longer least privilege.

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".

Google Cloud console showing the deny policy with its denied permission and exempted principals.
Validates Step 3: the denied permission and both exemptions. The exception list is the part worth checking — a deny with no way out does not survive contact with an incident.

Validation and Evidence

TestExpectedActualStatus
Infrastructure deployed3 resources3 added, 0 changed, 0 destroyedPass
Custom role carries only two permissionsfeeds.get, feeds.listexactly two, nothing elsePass
Week 7's over-grant removedno cloudasset.owner on the plan identityremovedPass
Deny policy exempts the right twoapply identity + break-glass humanboth presentPass
Plan identity may not delete a feedCANNOT_ACCESS (v3)CANNOT_ACCESSPass
Plan identity may still read oneCAN_ACCESSCAN_ACCESSPass
Apply identity may still manage oneCAN_ACCESSCAN_ACCESSPass
A real delete attempt is refused in practiceaudit log shows a denialnot tested — validation is an evaluation, not an observed denialUnverified

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.

HCP Terraform run showing Applied with three resources.
Validates deployment only: three resources applied.

Security and Cost

Security decisions

ControlDecision and trade-off
Least privilegeA 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 depthRemoving the role fixes today. The deny policy holds against the next over-grant, including one made by an Organization Administrator.
Break-glassOne named human is exempted. Deliberate, and a single point of failure — a group would be better.
InstrumentationPolicy Troubleshooter v3 only. v1 cannot see deny policies and reports GRANTED while one is blocking.
Accepted gapOne 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

  1. 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.
  2. 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.
  3. A deny with no exception gets deleted, not used. The exception list is the part that determines whether the guardrail survives an incident.
  4. "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

Comments

How was your experience?
Your feedback helps improve this site.
PoorExcellent