📋 In This Post
Lab Overview
The problem. A guardrail that refuses something on its first day breaks whatever was already doing that thing, and you find out from an incident rather than a report. The other failure is quieter: adding a rule the platform already enforces, and believing you improved something.
Objective. Read what this organization already enforces, then add three managed constraints and one custom constraint in dry run, so violations are logged before anything is denied.
Expected outcome. Four policies reporting rather than refusing, one deliberate folder-level exception, and a measured figure for how long enforcement actually takes to take effect.
| Difficulty | Intermediate — the policies are simple; the API's concurrency and lag behaviour are not |
|---|---|
| Deployment | Terraform via HCP Terraform — the first fully remote week, no hand-run apply |
| Reproduce it | Lab guide — the baseline read, the 409, the lag measurement |
| Cost | $0 — organization policy is free |
Cloud Services and Concepts
| Service | Purpose here | What it is |
|---|---|---|
| Organization Policy Service | Applies the four guardrails this week sets | Applies restrictions to a resource and everything beneath it. Evaluated when a resource is created or changed, so it refuses rather than reports after the fact. |
| Managed constraints | Three of the four rules, shipped by Google | Rules Google ships, named *.managed.*. Some are enforced by a Google default you never set. |
| Custom constraints | The fourth rule, which Google does not ship | A CEL expression over a resource's own fields, for rules Google does not ship. |
| Dry-run policies | Lets every rule be observed before it denies anything | Evaluate and log, without denying. The difference between a guardrail and an outage on day one. |
| Cloud Audit Logs | Where the dry-run violations are read from | Where dry-run violations land — in the project log, not the organization's. |
Two concepts that decide how this week goes. First,
"set" and "enforced" are different: a managed constraint can be fully
in force because of a Google default, while appearing in no list of policies you
configured. Only describe --effective tells the truth, and reading
it first is the difference between adding a guardrail and re-adding one.
Second, dry run is not a test mode you skip. A constraint in dry run records what it would have refused. That log is the evidence you need before promoting it — and without a matching resource in the organization, it produces nothing at all, which looks exactly like a constraint that works.
Architecture and Workflow
The flow
- Read what is already enforced with
describe --effective, before designing anything. - Write three managed Compute constraints and one custom bucket-label constraint, all in dry run.
- Chain them with
depends_on, because the API serializes policy changes per parent. - Add one deliberate exception at
workloads/dev, disabling the serial-port constraint for that folder only. - Create a violating resource on purpose and confirm it is logged rather than refused.
- Leave them in dry run until there is evidence to promote on — which, for the Compute constraints, meant waiting for Week 04.
Two decisions worth naming
Dry run, not enforce. Three of these constraints govern Compute Engine, and at this point the organization contained no Compute resources at all. A constraint with nothing to evaluate produces no violations, which is indistinguishable from a constraint that is working — so promoting on "no violations" would have proven nothing.
One exception, named and scoped. workloads/dev
overrides the serial-port constraint. An exception that exists is honest; an
exception nobody can find is how a guardrail quietly stops applying.
Prerequisites
- An organization with a folder hierarchy — Week 1 supplies it, and the exception needs somewhere to live.
roles/orgpolicy.policyAdminat the organization on the apply identity.roles/logging.viewerat the organization for whoever reads dry-run violations — granted out of band, because the pipeline identity cannot be impersonated.orgpolicyandloggingAPIs enabled on the quota project. A clean plan is not evidence the apply will work — writing policy needs the API, reading it does not, and a plan only reads.- Terraform 1.9+ with
hashicorp/google~> 6.0.
The baseline-reading commands and the lag measurement are in the lab guide.
Implementation
Eight resources, all free. Commands are in the lab guide.
Step 1 — Read what the platform already decided
Why: because list shows what is set, not
what is enforced. This organization already had seven enforced
constraints from Google's security baseline, all carrying the same
updateTime from the moment it was created.
Checkpoint: list reported 12 policies; the
console reported 26 active. One constraint was added to this lab as a new
guardrail and turned out to be enforced already — removing our policy left it
enforced, which proves the default is independent of anything set here.
gcloud org-policies list, which is why the console and the CLI disagreed by fourteen policies.Step 2 — Write the constraints, chained
Why chained: the Organization Policy API serializes changes
per parent, and Terraform's default parallelism of 10 produces HTTP 409
CONCURRENT_POLICY_CHANGES partially and non-deterministically. The
CLI fix is -parallelism=1; HCP refuses it, so ordering has to be an
edge in the dependency graph.
Step 3 — Prove it reports rather than refuses
Action: create an unlabelled bucket on purpose, as an identity that holds permission to create it.
Expected: the request succeeds and a violation is logged.
Validation and Evidence
| Test | Expected | Actual | Status |
|---|---|---|---|
| Infrastructure deployed | 8 resources | 8 added, on the fourth attempt | Pass |
| What was already enforced | read before designing | 7 constraints from Google's baseline | Finding |
| This week's four, and each state | all dry run | all dry run | Pass |
| The custom constraint's definition | CEL expression present | present, CREATE + UPDATE on buckets | Pass |
The deliberate exception at workloads/dev | enforce = FALSE | present and scoped to one folder | Pass |
| A violating request is logged, not refused | bucket created, violation logged | created; OrgPolicyDryRunAuditMetadata in the project log | Pass |
| Enforcement lag, both directions | measured, not assumed | 75–100 s to enforce, 77–108 s to un-enforce, 3 trials | Pass |
| The three Compute constraints deny anything | a real denial | not tested this week — no Compute resource existed to evaluate; promoted in Week 04 | Unverified |
Row two changed the week. Reading --effective
first revealed seven constraints already enforced by Google's baseline — one of
which this lab had been about to add as an improvement.
Row seven is a real measurement and it started as a bad one. The lag was first written up from a single success followed by a single failure, with the same confidence as the reproducible findings around it. One success and one failure cannot distinguish a propagation delay from a transient error. Repeating it three times produced a range — and turned up the reverse lag, which is the operationally dangerous direction, by accident.
Row eight is honest about the limit: three of the four constraints govern Compute Engine, and nothing in the organization used Compute. They denied nothing because there was nothing to deny.
Security and Cost
Security decisions
| Control | Decision and trade-off |
|---|---|
| Dry run first | Report before refusing. A guardrail enforced on day one breaks whatever was already doing the thing, and you learn from an incident. |
| Read the baseline first | Seven constraints were already enforced. Adding a rule that already exists is not a security improvement, it is a false sense of one. |
| Scoped exception | One folder, one constraint, visible in the configuration. An exception nobody can find is how a guardrail silently stops applying. |
| Out-of-band grant | roles/logging.viewer at the organization was granted by hand so violations could be read — read-only, reversible, and recorded with a removal command. |
| Accepted gap | The Compute constraints could not be tested here. "Deployed" and "demonstrated" are different, and this week only achieved the first. |
A monitoring trap worth repeating: the dry-run audit record
carries status.code 7 POLICY_VIOLATED on a request that
succeeded. Anything alerting on status.code != 0
will report an outage that is not happening.
Cost
$0. Organization policy is free, and this week creates no billable resource at all.
Troubleshooting and Lessons
A clean plan, then `SERVICE_DISABLED`
Problem: terraform plan reported no problems and
the apply failed immediately.
Root cause: writing org policy needs
orgpolicy.googleapis.com on the quota project. Reading it does not,
and a plan only reads.
Lesson: a clean plan is not evidence an apply will work. This is the general form, and it recurs: the plan exercises read paths only.
HTTP 409 `CONCURRENT_POLICY_CHANGES`
Problem: policies applied partially and non-deterministically.
Root cause: the API serializes changes per parent; Terraform runs ten in parallel by default.
Resolution: -parallelism=1 fixes it on the CLI,
and HCP Terraform refuses the flag — "Custom parallelism
values are currently not supported." On remote execution the ordering must
be an edge in the dependency graph, so the resources are chained with
depends_on.
Lesson: applies to every later week that writes several policies to one parent.
A script that degraded a control and left it that way
Problem: the first version of the lag-measurement script crashed mid-cycle, having already flipped a constraint back to dry run, and left the organization in a state the published post contradicted — live, until someone noticed.
Root cause of the crash: trivial. subprocess
cannot launch a bare gcloud on Windows, because it is a
.CMD shim and CreateProcess does not apply
PATHEXT. Resolve with shutil.which.
The damage was the failure path, not the crash.
Lesson: a script that degrades a control must restore it in a
finally block, never at the end of the happy path.
Cleanup and Key Takeaways
Cleanup
terraform destroy removes all eight resources. Then, out of band,
remove the operator's organization-level roles/logging.viewer if it
is no longer needed — scripts/cleanup.sh carries the command,
because a grant made by hand will not be removed by Terraform.
Consider keeping it. These constraints are the baseline every later week assumes, and the three Compute ones were promoted to enforce in Week 04. Commands are in the lab guide.
Ongoing charges: none.
Key takeaways
gcloud org-policies listshows what is set, not what is enforced. Always read--effectivebefore concluding a constraint is unset — seven were already in force here.- One success followed by one failure is not a measurement. The enforcement lag was first written up from n=1; repeating it three times produced a range and revealed the reverse lag.
- A clean plan is not evidence an apply will work, because a plan exercises only read paths.
- Dry-run violations land in the PROJECT audit log, and the
record reports
POLICY_VIOLATEDon a request that succeeded — which will trip any alert keyed on a non-zero status code. - A script that degrades a control must restore it in
finally. The crash is never the damage; the failure path is.
References
- Lab guide — baseline read, the 409, lag measurement, known limitations
- Organization policy constraints
- Managed constraints
- Dry-run policies
- Source repository
Comments