Chapters

Firewall 1: VPC firewall rules

Chapter 1.2 showed that GCP's firewall lives in the data plane beside every VM. This chapter covers how its rules work: which traffic they judge, how they pick VMs, how priority settles conflicts, and the mistakes that make a rule look safe when it is not. Chapter 5.2 adds the policies that sit above these rules.

Controlling access · chapter 6 of 26 · 11 min read

A firewall that is everywhere

Builds on16. TCP connections31. Ports and sockets

A traditional firewall is a place: a box that packets pass through. GCP's firewall is distributed. The rules that protect a VM are enforced at that VM's own virtual network card, by the data plane on its host (chapter 1.2 section 09). So every VM-to-VM connection is checked twice:

kade-api-110.10.1.10egress checkkade-api-1's rulesingress checkkade-db's ruleskade-db10.10.2.5:5432TCP SYN :5432on host A, as it leaveson host B, as it arrivesBoth must allow, or the packet is dropped.Same subnet or same host makes no difference: both checks always happen.
  • Your VPC
  • Allowed traffic and replies
  • Firewall and access rules

Two separate checks against two separate rule sets. The common failure: ingress is set up carefully, egress is forgotten, and traffic breaks the day someone tightens egress.

Wide by design - scroll it sideways.

Nothing is trusted for being inside

Two VMs in the same subnet, even on the same host, are still firewalled against each other. Being "inside the VPC" gives no permission by itself. That is why rules like default-allow-internal and Kadé's kade-allow-internal exist: internal traffic is not trusted by default, and those rules choose to allow it. Delete them, and every VM-to-VM connection needs its own rule.

A few kinds of traffic ignore your rules entirely:

#TrafficWhat happensWhy
1To the metadata server 169.254.169.254 (DHCP, DNS, time, metadata)always allowedThe VM could not boot or resolve names without it (chapter 1.2)
2Outgoing TCP port 25 (SMTP) to the internetalways blockedGoogle blocks it to stop spam. Use a mail service on 587 or 465
3Health checks and Google Front Ends to backendsNeed a ruleThey arrive on special paths (chapter 4), but the firewall still judges them. This is why kade-allow-lb exists

Stateful: rules are about who starts the conversation

A rule decides who may start a connection, not which single packets pass. Once a connection is allowed to start, both directions flow, and the replies are not a separate decision. The data plane keeps a record of each allowed connection, the same idea as the NAT table in the fundamentals course.

Each tab follows one connection through the data plane. Ticks are checks that pass, by a rule or because of the connection table; red crosses drop the packet.

What if
Google Front End35.191.8.20data planekade-api-1's hostkade-api-110.10.1.10:80801. SYN to :8080✓INGRESS rule kade-lb-to-apiallow tcp:8080 from 35.191.0.0/16→ connection recorded2. SYN-ACK✓matches the connection record→ allowed, no rule checked3. ACK, request data✓same connection → allowedCONNECTION TABLE (on the host)35.191.8.20:51234⇄ 10.10.1.10:8080 · TCPcreated by message 1

Only message 1 is judged by a rule. It creates an entry in the connection table, and messages 2 and 3 pass because they belong to that connection. No rule is needed for the replies.

Show as config
Rule: allow INGRESS tcp:8080 from 35.191.0.0/16 to kade-api-1

Google Front End ──▶ SYN      to :8080    ← allowed by the ingress rule
kade-api-1       ──▶ SYN-ACK  back        ← allowed AUTOMATICALLY
Google Front End ──▶ ACK, request data    ← same connection, allowed

The fields of a rule

#FieldWhat it saysValues
1directionWhich way this rule judgesINGRESS or EGRESS
2priorityWho wins when rules overlap0 to 65535; lower number wins
3actionAllow or blockallow or deny
4targetWhich VMs the rule protectsall instances, network tags, or service accounts (section 05)
5source (ingress only)Who may reach the targetsIP ranges, network tags, service accounts
6destination (egress only)What the targets may reachIP ranges
7protocol and portsWhich traffictcp:443, udp:53, icmp, all
8disabledSwitch the rule off without deleting ittrue or false
9loggingRecord connections the rule matcheson or off (chapter 16)
The direction picks the field: ingress rules use source, egress rules use destination, never both.

Five rules, each drawn as a picture: where traffic comes from, the rule itself, and which of Kadé's four VMs it covers. The sentence under each one is the rule read out in plain words.

INGRESS · ALLOWpriority 1000TARGETservice account sa-kade-apiSOURCE35.191.0.0/16, 130.211.0.0/22PROTOCOLtcp:8080FROM (SOURCE)TO (TARGET VMs)IP RANGE35.191.0.0/16Google front ends + health checksIP RANGE130.211.0.0/22Google front ends + health checkskade-api-1SA sa-kade-api · tag apicoveredkade-api-2SA sa-kade-api · tag apicoveredkade-dbSA sa-kade-dbnot coveredkade-workerSA sa-kade-workernot covered

Opens tcp:8080 · from 2 ranges · applies to 2 of 4 VMs · checked at priority 1000

"Google's load balancer may reach VMs running as sa-kade-api on port 8080." Two sources, two of four VMs, one port.

Show as config
direction: INGRESS   priority: 1000   action: allow
target:    service account sa-kade-api
source:    35.191.0.0/16, 130.211.0.0/22
protocol:  tcp:8080

Target and source: two separate questions

Every rule answers two questions that have nothing to do with each other:

  • Target: which VMs does this rule protect?
  • Source (ingress) or destination (egress): who may reach them, or what may they reach?

Three ways to choose targets

#Target typeMeansStrength
1All instances (no target)Every VM in the VPC, now and every VM created laterWidest; what you get when you forget to narrow
2Network tags, e.g. apiVMs that carry the tagweak
3Service accounts, e.g. sa-kade-apiVMs running as that service accountstrong
One rule uses tags or service accounts for its targets, not both. Firewall policies add a fourth, stronger option, secure tags (chapter 5.2).

Why tags are weak

A tag is just a string on the VM. Anyone allowed to edit the VM can add api to it and instantly pull in every rule that targets api, with no firewall permission at all. Nothing checks that a tagged VM should have the tag.

Why service accounts are stronger

The service account is the VM's identity. Attaching one needs the iam.serviceAccountUser role on it, and it can only be changed while the VM is stopped. The trade-off: it is more rigid, and it ties firewall design to IAM design. That rigidity is useful at production boundaries.

Sources

An ingress rule can name sources by IP range, network tag or service account. Two details catch people out:

  • If a rule lists both ranges and tags (or service accounts), a packet matches if it matches either.
  • Source tags and source service accounts only match traffic from VMs inside the same VPC, sent to internal IPs. A packet that arrives through an external IP never matches them, even from your own VM (chapter 3 section 06).

The scoping trap

Narrowing the target does nothing to the source. A rule has two widths that have nothing to do with each other: how many VMs it covers (the target), and who may reach them (the source). A rule can be tightly targeted and wide open at the same time. The four combinations:

source narrowsource wide
target narrowA · scopedthe trap
target wideB · survivableC · dangerous
INGRESS · ALLOWpriority 1000TARGETnetwork tag apiSOURCE0.0.0.0/0PROTOCOLtcp:9100FROM (SOURCE)TO (TARGET VMs)IP RANGE0.0.0.0/0the whole internetkade-api-1SA sa-kade-api · tag apicoveredkade-api-2SA sa-kade-api · tag apicoveredkade-dbSA sa-kade-dbnot coveredkade-workerSA sa-kade-workernot covered

Opens tcp:9100 · from 1 range · applies to 2 of 4 VMs · checked at priority 1000

A metrics port opened "for monitoring". The tag makes it look scoped, but it only decides which VMs have the door. The source decides who may walk through it, and here that is the whole internet. Compare kade-allow-lb: the same tag as its target, but its source is only Google's load balancer ranges.

Show as config
kade-allow-metrics:
  targetTags:   [api]          ← TARGET scoped: only VMs tagged api
  sourceRanges: 0.0.0.0/0      ← SOURCE wide open: the whole internet
  allow:        tcp:9100

Reads as: "the whole internet may reach port 9100 on any VM tagged api."

Implied rules and priority

Every VPC has two rules that are always there and always checked last:

#Implied ruleDirectionActionPriority
1Implied allow egressEGRESSallow to 0.0.0.0/065535
2Implied deny ingressINGRESSdeny from 0.0.0.0/065535
65535 is the highest number, so they are the weakest rules: the backstop when nothing else matches. They do not show in firewall-rules list.
  • All egress is allowed by default. A new VM reaches the internet (if it has a path) with no egress rule. Egress hardening is opt-in: you add deny rules on top.
  • All ingress is denied by default. A new VM is unreachable until an allow rule opens a specific hole.

How priority works

A packet is judged by walking the rules from the lowest number to the highest, and the first match decides. Two rules: lower number wins, and at the same priority, deny beats allow. The second one lets you add a deny next to an allow without renumbering anything. The implied rule sits at the bottom and is reached only if nothing above it matched.

PACKET · INGRESSRULES, LOWEST NUMBER FIRSTRESULTtcp:9000from 203.0.113.99a scannerto kade-api-1900ALLOW tcp:22from 198.51.100.20/32kade-office-sshno matchwrong port + address1000ALLOW tcp:8080from 35.191.0.0/16, 130.211.0.0/22kade-lb-to-apino matchwrong port + address65535DENY allimplied deny ingress · always last✕DROPPEDnothing above matched

Dropped by the implied deny ingress at 65535. None of the rules above it matched.

No rule mentions port 9000, so the walk reaches the bottom and the implied deny ingress at 65535 drops the packet. That is why a new VM is unreachable until an allow opens a hole.

Show as config
Inbound tcp:9000 to kade-api-1. No rule mentions 9000.
→ walk finishes → implied deny ingress @65535 → DROPPED

Try it: is this packet allowed in?

Pick a rule set, a target VM, where the packet comes from and which port. The tool walks the ingress rules in priority order, as the data plane does. It assumes the packet can physically reach the VM; for internet sources that means the VM would need an external IP.

Target VM
Packet from
Traffic

Denied by the implied deny ingress rule at 65535. Nothing above matched.

RuleFromPrioTargetResult
kade-allow-icmp203.0.113.45/32, 198.51.100.20/32 · icmp1000alldoes not cover tcp:5432
kade-allow-internal10.10.0.0/16 · all1000allsource 203.0.113.99 is not in 10.10.0.0/16
kade-allow-lb35.191.0.0/16, 130.211.0.0/22 · tcp:80801000tag apidoes not cover tcp:5432
kade-allow-ssh0.0.0.0/0 · tcp:221000alldoes not cover tcp:5432
kade-allow-web0.0.0.0/0 · tcp:80,tcp:443,tcp:80801000alldoes not cover tcp:5432
implied deny0.0.0.0/0 · all65535allreached: DENY

Changing rules without breaking things

#HabitWhy
1Add and verify before you removeCreate the narrow replacement first, confirm traffic still works, then take the wide rule away
2Disable before you deleteA disabled rule is off but still there. If something breaks, enable it again in seconds instead of rebuilding it from memory
3Turn on logging for the rule you plan to removeA few days of logs show who still uses it (chapter 16)
4Allow a few seconds after each changeRules spread to every host's data plane; testing too fast gives false results
5Remember existing connectionsRemoving an allow rule does not always cut connections that are already open. Test with a new connection

Back to Kadé

Commands in this chapter

Commands 1 to 3 are read-only. Commands 4 to 7 change the project.

List rules with the columns that matter

  • What it shows: Every rule with direction, priority, sources, ports and targets, including service account targets.
  • What to look for: 0.0.0.0/0 on anything but the load balancer path, and rules with no target (every VM).
gcloud compute firewall-rules list --filter="network~kade-vpc" \  --format="table(name,direction,priority,sourceRanges.list(),sourceServiceAccounts.list(),    allowed[].map().firewall_rule().list(),targetTags.list(),targetServiceAccounts.list(),disabled)"

See every rule that applies to one VM

  • What it shows: The rules GCP actually applies to kade-db, already resolved and in order. Chapter 5.2 explains the extra layers it can include.
  • What to look for: Any allow that should not be there. If your reading of the rules disagrees with this output, the output is right.
gcloud compute instances network-interfaces get-effective-firewalls kade-db \  --network-interface=nic0 --zone=asia-southeast1-b

See which service account and tags a VM has

  • What it shows: The identity and tags that firewall targets match against.
  • What to look for: ...-compute@developer.gserviceaccount.com means the default service account (finding 9).
gcloud compute instances describe kade-db --zone=asia-southeast1-b \  --format="value(serviceAccounts[0].email,tags.items)"

Create a rule between two identities (changes the project)

  • What it shows: Rule E: only VMs running as sa-kade-api reach VMs running as sa-kade-db on 5432, with logging on.
  • What to look for: The rule in command 1, and in command 2's output for kade-db.
gcloud compute firewall-rules create kade-api-to-db \  --network=kade-vpc --direction=INGRESS --priority=1000 --action=ALLOW \  --rules=tcp:5432 \  --source-service-accounts=sa-kade-api@kade-prod.iam.gserviceaccount.com \  --target-service-accounts=sa-kade-db@kade-prod.iam.gserviceaccount.com \  --enable-logging

Disable a rule, and enable it again (changes the project)

  • What it shows: Switches a rule off without losing it.
  • What to look for: DISABLED True in command 1. If anything breaks, run the second line.
gcloud compute firewall-rules update kade-allow-internal --disabledgcloud compute firewall-rules update kade-allow-internal --no-disabled

Change a VM's service account (changes the project)

  • What it shows: Moves kade-api-1 to its own identity. The VM must be stopped.
  • What to look for: Command 3 shows the new email. Do one API VM at a time so the other keeps serving.
gcloud compute instances stop kade-api-1 --zone=asia-southeast1-agcloud compute instances set-service-account kade-api-1 --zone=asia-southeast1-a \  --service-account=sa-kade-api@kade-prod.iam.gserviceaccount.com \  --scopes=cloud-platformgcloud compute instances start kade-api-1 --zone=asia-southeast1-a

Delete a rule (changes the project)

  • What it shows: Removes a rule for good, after it has been disabled and watched.
  • What to look for: Gone from command 1. There is no undo; keep the command 1 output from before as a record.
gcloud compute firewall-rules delete kade-allow-web kade-allow-internal kade-allow-lb

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. D9
    Egress is completely unrestricted

    highcloses in phase 5, Egress and load balancer

    Goes with Implied rules and priority

  2. D1
    One allow rule opens everything

    criticalcloses in phase 6, Govern and tighten

    Goes with The scoping trap and Implied rules and priority