Why fixes drift back, and the two answers
A fix made by hand stays fixed only as long as nobody with access changes it. There are two kinds of answer, and a good setup uses both:
| # | Prevent | Detect | |
|---|---|---|---|
| 1 | Means | The risky change is refused when someone tries it | The change happens, and you find out quickly |
| 2 | Tools | Organization policies, hierarchical firewall policies, IAM roles, VPC Service Controls | Audit log alerts, Security Command Center, Cloud Asset Inventory, Firewall Insights |
| 3 | Strength | Nothing to clean up afterwards | Catches what prevention cannot express, and mistakes in the prevention itself |
| 4 | Risk | Too strict and it blocks real work; people look for ways around it | Too noisy and alerts get ignored (chapter 16.2) |
The resource hierarchy: where rules are set
Every GCP resource sits in a project, projects sit in folders, folders in the organization. IAM permissions and policies set high up are inherited by everything below. That is what makes governance possible: set a rule once on the folder, and every current and future project in it follows (chapter 5.2 used this for hierarchical firewall policies).
Set a rule as high as it is true for everything below it. Stricter rules for live systems go on the production folder; experiments get their own folder.
Organization policies: how they work
An organization policy sets a constraint on a node of the hierarchy: "VMs in here may not have external IPs", "Cloud SQL in here may not have a public IP". It does not grant or remove anyone's access. It limits what anyone can create or configure, including Owners.
| # | Kind of constraint | Settings | Example |
|---|---|---|---|
| 1 | Boolean | Enforced or not | compute.skipDefaultNetworkCreation: new projects get no default network |
| 2 | List | Allowed values, denied values, or deny all | run.allowedIngress: only internal and internal-and-cloud-load-balancing |
| 3 | Custom | Your own condition on a resource's fields, for supported resource types | For rules no built-in constraint expresses |
Four behaviours decide whether a policy does what you expect. Each tab shows one:
Set once on the folder; every project, now and later, follows
The policy is set once, on the production folder. kade-prod and kade-staging have nothing set themselves, so they inherit it. A project created in the folder next year is covered the moment it exists.
Show as config
folders/production: compute.vmExternalIpAccess → denyAll kade-prod: (not set) → inherits denyAll kade-staging: (not set) → inherits denyAll
The constraints that protect a network
These are the constraints that lock in this course's fixes. Pick an area; each card says what it blocks, which chapter's fix it protects, and Kadé's setting:
| Constraint | Type | Blocks | Chapter | Kadé |
|---|---|---|---|---|
| compute.vmExternalIpAccess | list | VMs with external IPs, except those listed | ch 3, 6.2 | deny all on production |
| compute.vmCanIpForward | list | VMs that can forward packets (routers, NAT VMs) | ch 3, 4 | deny all |
| compute.requireOsLogin | boolean | VMs without OS Login | ch 6.2 | enforced on the org |
| compute.requireShieldedVm | boolean | VMs without Shielded VM features | ch 12.2 | enforced on production |
| compute.disableSerialPortAccess | boolean | Serial console access. Note: this also removes the break-glass path from chapter 6.1 | ch 6.1 | not set: Kadé keeps break-glass |
Try it: which guardrail stops this?
Pick a risky change someone might make. The tool shows each layer it meets, in order, and which one refuses it, or which one at least notices:
- 1 · Org policySTOPS ITcompute.vmExternalIpAccess refuses it
- 2 · Hierarchical firewallnot reached
- 3 · IAM rolenot reached
- 4 · Detect (alert / SCC)not reached
Refused before it happened.
IAM for the network: who can change what
Organization policies limit what can exist. IAM decides who can change the things that are allowed. The basic roles are the problem:
| # | Role | Can change the network? | Use |
|---|---|---|---|
| 1 | roles/owner | everything, including IAM itself | One or two break-glass accounts, not daily work |
| 2 | roles/editor | almost everything: firewall rules, routes, IPs | Avoid. It is the usual way "a developer opened port 22" happens |
| 3 | roles/compute.networkAdmin | Networks, subnets, routes, load balancers, NAT, VPN. Not firewall rules or certificates | Whoever builds the network |
| 4 | roles/compute.securityAdmin | Firewall rules, firewall policies, SSL certificates and policies | Whoever owns security. Kept separate on purpose |
| 5 | roles/compute.networkViewer | Read only | Developers who need to see, not change |
| 6 | roles/compute.networkUser | Use a (shared) subnet when creating resources | Service projects in a Shared VPC (chapter 14) |
| 7 | roles/dns.admin, roles/compute.loadBalancerAdmin | Only their own area | Narrow jobs |
| # | Habit | Why |
|---|---|---|
| 1 | Grant to groups, not people | kade-platform@kade.lk holds the roles; people join and leave the group. One place to see and remove access |
| 2 | Grant high, narrowly | Network roles on the folder cover staging too; developer roles on single projects |
| 3 | Time-limit special access | An IAM condition with an expiry date for a contractor or an incident (chapter 6.1 did this for IAP) |
| 4 | Remove what is unused | The IAM Recommender lists permissions not used in 90 days |
| 5 | No service account keys | A leaked key file works from anywhere. Workload identity and attached service accounts need no keys; the constraint iam.disableServiceAccountKeyCreation enforces it |
VPC Service Controls: a perimeter around Google APIs
Firewall rules protect traffic to your VMs. They cannot protect Google APIs such as Cloud Storage or BigQuery: those are reached over Google's public endpoints, and IAM alone decides who can call them. If credentials leak, the attacker can download Kadé's data from anywhere. VPC Service Controls draws a perimeter around projects, so their protected APIs only answer requests from inside the perimeter, and data can only be copied to projects inside it.
Allowed: both sides inside, IAM decides as usual
kade-worker, inside kade-prod, reads a product photo from Cloud Storage. Both the caller and the bucket are inside the perimeter, so the request is checked by IAM as usual and allowed.
Show as config
perimeter kade-prod-perimeter: projects kade-prod restricted services: storage.googleapis.com, sqladmin.googleapis.com, bigquery.googleapis.com
| # | Piece | Job |
|---|---|---|
| 1 | Service perimeter | A set of projects and the APIs protected for them |
| 2 | Access level | A rule for letting in requests from outside: the office IP range, or company-managed devices |
| 3 | Ingress and egress rules | Narrow exceptions: "this outside service account may read this bucket" |
| 4 | restricted.googleapis.com | The private API addresses that only serve perimeter-supported APIs (chapter 9) |
| 5 | Dry-run perimeter | Logs what would be blocked without blocking it. Always run it for a few weeks first |
Detect: finding what changed, and what is already wrong
| # | Tool | Answers | Cost |
|---|---|---|---|
| 1 | Audit logs + log-based alert | "Someone just changed a firewall rule, route or external IP." Admin Activity logs are always on (chapter 16.1); an alert on them tells you within minutes | Free logs; a small metric |
| 2 | Security Command Center | "What is risky right now?" Findings such as an open firewall, an SSH port open to the internet, a public IP on a VM, a public Cloud SQL instance, flow logs disabled | Standard tier included; Premium adds more detectors |
| 3 | Cloud Asset Inventory | "Show me every external IP, every firewall rule from 0.0.0.0/0, every Owner, across the organization." One search instead of project-by-project clicking | Included |
| 4 | Firewall Insights, Network Analyzer | Unused and overly broad rules; misconfigurations (chapter 16.2) | Included |
| 5 | IAM Recommender, Policy Analyzer | "Who can change firewall rules in kade-prod, and through which role?" | Included |
The network written down: infrastructure as code
The strongest answer to "what should the network look like?" is a file. With Terraform (or a similar tool), the network from this course is described in code, kept in Git, reviewed before it changes, and applied by a CI job. A console change then shows up as drift: the next plan says "the real firewall differs from the code".
| # | Rule | Why |
|---|---|---|
| 1 | Only the CI service account holds network write roles in kade-prod | People change the network by changing code, which gets reviewed |
| 2 | Humans keep Network Viewer, plus a break-glass group for emergencies | Incidents still need hands; every use of break-glass is visible in the audit log |
| 3 | A scheduled plan (daily) reports drift | Catches changes made outside the pipeline |
| 4 | Start by importing what exists | Rewriting a working network from scratch is a risk of its own; import, then improve |
Rollout plan
In order of protection gained per hour spent, for a platform starting with Owners everywhere and no policies:
1.Day 1
about an hour
- Remove the freelancer and unused Owners
- Two break-glass Owners; everyone else to narrow roles via a group
- Alert on firewall, route and address changes
2.Week 1
a few hours
- Find violations with Asset Inventory and fix them
- Org: skip default network, require OS Login
- Folder: no external IPs, no public Cloud SQL, Cloud Run ingress
3.Month 1
spread out
- Security Command Center findings reviewed weekly
- No service account keys
- Dry-run VPC Service Controls perimeter
4.Later
when stable
- Import the network into Terraform; CI as the only writer
- Enforce the VPC-SC perimeter after a clean dry run
- Custom constraints for gaps
Back to Kadé
Commands in this chapter
Commands 1 to 5 are read-only. Commands 6 to 8 change the organization; run them with the organization or folder ID, after dry run.
What policy actually applies to a project?
- What it shows: The effective policy for one constraint on kade-prod, after inheritance and merging.
- What to look for:
enforce: trueor the allowed and denied values you expect. If it is empty, nothing above sets it.
gcloud org-policies describe compute.vmExternalIpAccess --project=kade-prod --effectiveList the policies set on the folder
- What it shows: Every constraint with a policy set directly on the production folder.
- What to look for: The constraints from section 11's table.
gcloud org-policies list --folder=FOLDER_IDFind every external IP in the organization
- What it shows: Reserved and in-use external addresses across all projects.
- What to look for: Anything besides the load balancer and NAT IPs. Fix these before enforcing
vmExternalIpAccess.
gcloud asset search-all-resources --scope=organizations/ORG_ID \ --asset-types=compute.googleapis.com/Address --query="addressType:EXTERNAL" \ --format="table(displayName,project,location,additionalAttributes.address)"Who holds Owner or Editor?
- What it shows: Every IAM binding granting the basic roles, anywhere in the organization.
- What to look for: Individual users and former contractors. The goal is two break-glass Owners and no Editors.
gcloud asset search-all-iam-policies --scope=organizations/ORG_ID \ --query="policy:(roles/owner OR roles/editor)" --format="table(resource,policy.bindings.role,policy.bindings.members)"Who can change firewall rules in kade-prod?
- What it shows: Every identity that holds the permission to update firewall rules, and through which role and level.
- What to look for: Only the platform group and the CI service account.
gcloud asset analyze-iam-policy --organization=ORG_ID \ --full-resource-name=//cloudresourcemanager.googleapis.com/projects/kade-prod \ --permissions=compute.firewalls.updateBlock external IPs on VMs in the folder, dry run first (changes the organization)
- What it shows: A policy file with a dry-run spec only: violations are logged, nothing is blocked. Move the rules to
specto enforce. - What to look for: Dry-run violations in the audit logs for a week; zero means it is safe to enforce. Dry run is not available for every constraint; where it is not, preview with the Policy Simulator in the console, and fix what command 3 found first.
cat > no-external-ip.yaml <<'EOF'name: folders/FOLDER_ID/policies/compute.vmExternalIpAccessdryRunSpec: rules: - denyAll: trueEOFgcloud org-policies set-policy no-external-ip.yamlRequire OS Login and skip default networks, organization-wide (changes the organization)
- What it shows: Two boolean constraints enforced on the organization.
- What to look for: Command 1 on any project shows
enforce: true. A new test project has no default network.
gcloud org-policies set-policy require-oslogin.yaml # constraints/compute.requireOsLogin, enforce: truegcloud org-policies set-policy skip-default-network.yaml # constraints/compute.skipDefaultNetworkCreation, enforce: trueAlert on network changes (changes the project)
- What it shows: A log-based metric counting changes to firewall rules, routes, addresses and security policies, for an alert to watch (chapter 16.2).
- What to look for: The counter rising when you change a test rule; the alert's notification arriving.
gcloud logging metrics create kade_network_changes \ --description="Changes to firewall rules, routes, addresses, security policies" \ --log-filter='logName:"cloudaudit.googleapis.com%2Factivity" AND (protoPayload.methodName:("compute.firewalls." OR "compute.routes." OR "compute.addresses." OR "compute.securityPolicies."))'