Chapters

Firewall 2: firewall policies

Chapter 5.1 fixed Kadé's rules. This chapter is about keeping them fixed, sharing them across networks, and writing rules in terms of names and groups instead of raw IP lists. That is what firewall policies add on top of VPC firewall rules.

Controlling access · chapter 7 of 26 · 8 min read

Why policies exist

VPC firewall rules have two built-in weaknesses:

#Weakness of VPC rulesWhat it meansWhat policies add
1They live inside one projectAnyone with firewall permission there can create, change or delete any rule. There is no way to say "this rule applies company-wide and no project owner may remove it"A hierarchical policy on the organization or a folder applies to every project below it, and project owners cannot remove or override it
2They belong to one VPC and cannot be sharedSeveral VPCs that need the same baseline get copies, kept in sync by handA policy is written once and attached to many VPCs

The three kinds

#KindAttached toApplies toWho should own it
1HierarchicalThe organization or a folderEvery VPC in every project below itA central security or platform team
2Global networkOne or more VPCsThose VPCs, in all regionsThe project's network admin
3Regional networkOne or more VPCs, in one regionThose VPCs, in that region onlyThe project's network admin

For the rest of the course, Kadé has an organization kade.lk with a folder production. kade-prod sits in it now, and kade-staging joins in chapter 14:

organization kade.lkhierarchical policy (org)folder productionhierarchical policy (folder)project kade-prod · kade-vpcVPC firewall ruleschapter 5.1global network policykade-vpc-policy · when neededproject kade-staging (ch 14)gets both hierarchicalpolicies automatically

Hierarchical policies flow down to every project in their scope. Network policies and VPC rules stay inside the VPCs they are attached to.

Wide by design - scroll it sideways.

How policy rules differ from VPC rules

#VPC firewall rulesFirewall policy rules
1TargetsAll instances, network tags, service accountsAll instances, service accounts, secure tags. no network tags
2Actionsallow, denyallow, deny, goto_next (pass the decision to the next layer), and inspection by Cloud NGFW Enterprise
3Sources and destinationsIP ranges (plus tags and service accounts as sources)IP ranges, secure tags, and matching objects: address groups, domain names, countries, threat lists (section 05)
4PriorityMany rules may share a numberEach rule in a policy has its own unique number
5Shared byOne VPCMany VPCs, or a whole folder or organization

The order of the layers

When a packet arrives, GCP walks the layers from the top. The first layer that makes a firm decision stops the walk. The first tab shows the order; the others follow one packet each.

1 · Hierarchical policyorganization kade.lkalways first2 · Hierarchical policyfolder productionalways first3 · VPC firewall ruleskade-vpc, project ownerswappable4 · Global network policyattached to kade-vpcswappable5 · Regional network policykade-vpc, one regionswappable6 · Implied rulesalways present, @65535always lastlayers 3 to 5swappable pair(enforcement order)

A packet walks down the layers. allow or deny stops the walk; goto_next or "no rule matches" falls through to the next layer. Hierarchical policies are always first. Layers 3 to 5 keep this order unless the VPC's enforcement order is changed (Example F).

Show as config
  1. Hierarchical policy - organization
  2. Hierarchical policy - folders, outer to inner
  3. VPC firewall rules                    ┐ this pair can be swapped
  4. Global network policy                 │ by a VPC setting
  5. Regional network policy               ┘
  6. Implied rules (allow egress / deny ingress at 65535)

  At each layer:  allow           → stop, packet allowed
                  deny            → stop, packet dropped
                  goto_next or
                  no rule matched → fall through to the next layer

Try it: walk the layers

  1. 1.Hierarchical policy · organization
  2. 2.Hierarchical policy · folder
  3. 3.VPC firewall rules
  4. 4.Global network policy
  5. 5.Regional network policy

Incoming packet is allowed by the vpc firewall rules.

  • 1. Hierarchical policy · organization: no rule matches, falls through
  • 2. Hierarchical policy · folder: no rule matches, falls through
  • 3. VPC firewall rules: ALLOW, walk stops
  • 4. Global network policy: not reached
  • 5. Regional network policy: not reached

Matching objects: names instead of IP lists

Policy rules can match on things a VPC rule cannot express. They let a rule say what it means ("our trusted IPs", "PayGate's API") instead of holding a raw list of IPs that goes stale.

#ObjectMatches onMainly forCloud NGFW tier
1Address groupA named, reusable list of IP rangesShared allow-lists (office IPs, partners)Essentials (free)
2FQDN objectA domain name, resolved to IPs by GCPEgress allow-listsStandard (paid)
3Geolocation objectThe country an IP is registered toRough ingress filteringStandard (paid)
4Threat Intelligence listGoogle-maintained lists of known-bad IPsCutting background noiseStandard (paid)
Cloud NGFW Essentials (free)WITHOUT A GROUPWITH A GROUPkade-allow-icmp198.51.100.20/32 · office203.0.113.45/32 · homecopycopykade-monitoring198.51.100.20/32 · office203.0.113.45/32 · homecopycopyNew office → edit every rule that holds the list, by handaddress groupkade-trusted-ips198.51.100.20/32 · office203.0.113.45/32 · homekade-allow-icmpsrc: kade-trusted-ipskade-monitoringsrc: kade-trusted-ipsNew office → update the group once; both rules follow

Without a group, the same list lives inside every rule that needs it, and each copy must be edited by hand. With a group, rules point at one named list. Change the list once and every rule follows. Groups can be defined at the organization level and shared by many projects.

Show as config
Without:  office and home IPs pasted into kade-allow-icmp
          the same IPs pasted into a monitoring rule
          a second office opens → edit both, by hand

With:     address group "kade-trusted-ips" = [198.51.100.20/32, 203.0.113.45/32]
          rule A → srcAddressGroups: [kade-trusted-ips]
          rule B → srcAddressGroups: [kade-trusted-ips]
          a second office opens → update the group once

Cloud NGFW tiers

Google groups these firewall features into three tiers under the name Cloud Next Generation Firewall (Cloud NGFW):

#TierAddsCost
1EssentialsVPC rules, hierarchical and network policies, secure tags, address groupsNo charge
2StandardFQDN objects, geolocation objects, Threat Intelligence listsCharged for traffic processed by rules that use them
3EnterpriseIntrusion detection and prevention, TLS inspection, URL filteringCharged for inspection endpoints and inspected traffic

Seeing what really applies

With several layers, working out by hand which rules apply to a VM means collecting every policy and rule, sorting them by layer and priority, and simulating the walk. Do not do that. GCP computes it:

#ToolAnswersUse when
1get-effective-firewalls on a VM"What can reach this VM, and what can it reach?"The usual question
2get-effective-firewalls on a network"What applies to this VPC, including from above?"Checking for policies from outside the project
3Firewall Insights"What is wrong with how my rules are written?"Audits and cleanup

Firewall Insights is different in kind. Instead of telling you what applies, it gives Google's analysis of your rules, based on the rules' logs:

  • Shadowed rules: never reached, because a higher-priority rule always matches first.
  • Unused rules: no hits for a long time; candidates for removal.
  • Overly broad rules: allowing more than the traffic actually uses.

It is most useful once rules have had logging on for a while (chapter 16).

Policy or VPC rule?

#NeedUse
1A rule that must apply to many projects, or that a project owner must not be able to removeHierarchical policy on the organization or a folder
2One baseline for every region of a VPC, or shared by several VPCs, as one objectGlobal network policy
3Address groups, FQDN, geolocation, threat lists, or secure tagsA policy; VPC rules cannot express them
4A small, single-VPC project with a handful of rulesVPC rules are fine. Google offers a tool to migrate them into a network policy later

Back to Kadé

Commands in this chapter

All read-only. Commands 3 and 5 need organization-level view permission.

See every rule that applies to one VM

  • What it shows: The resolved, ordered rules for kade-db across all layers.
  • What to look for: Where each rule comes from. An unexpected hierarchical policy rule explains "my VPC rule does nothing".
gcloud compute instances network-interfaces get-effective-firewalls kade-db \  --network-interface=nic0 --zone=asia-southeast1-b

See everything that applies to a network

  • What it shows: VPC rules plus every policy attached to kade-vpc or above it.
  • What to look for: Policies from outside the project.
gcloud compute networks get-effective-firewalls kade-vpc

List hierarchical policies on a folder

  • What it shows: The hierarchical policies attached to the production folder.
  • What to look for: Which policy is associated, and its rules. Replace the ID with your folder's.
gcloud compute firewall-policies list --folder=123456789012

Check the enforcement order of a VPC

  • What it shows: Whether network policies are checked after VPC rules (the default) or before.
  • What to look for: AFTER_CLASSIC_FIREWALL or empty means the default order.
gcloud compute networks describe kade-vpc \  --format="value(networkFirewallPolicyEnforcementOrder)"

List global network policies

  • What it shows: Network firewall policies in the project and which VPCs they are attached to.
  • What to look for: Empty for Kadé until chapter 13.
gcloud compute network-firewall-policies list --global

Ask Firewall Insights for problems

  • What it shows: Google's findings on the rules: shadowed, unused, overly broad.
  • What to look for: Any rule it flags. Needs the project named explicitly, and works best after logging has been on for a while.
gcloud recommender insights list \  --insight-type=google.compute.firewall.Insight \  --project=kade-prod --location=global

Spotted a mistake, or something unclear? Suggest an edit on GitHub

See it broken

Harbour is a platform built wrong on purpose. These defects are this chapter's ideas, left undone: what each one breaks, how you would find it, and the change that closes it.

  1. D8
  2. D9
    Egress is completely unrestricted

    highcloses in phase 5, Egress and load balancer

    Goes with Matching objects: names instead of IP lists

  3. D10
    No policy above the project

    mediumcloses in phase 6, Govern and tighten

    Goes with The order of the layers and The three kinds