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:
- 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:
| # | Traffic | What happens | Why |
|---|---|---|---|
| 1 | To the metadata server 169.254.169.254 (DHCP, DNS, time, metadata) | always allowed | The VM could not boot or resolve names without it (chapter 1.2) |
| 2 | Outgoing TCP port 25 (SMTP) to the internet | always blocked | Google blocks it to stop spam. Use a mail service on 587 or 465 |
| 3 | Health checks and Google Front Ends to backends | Need a rule | They 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.
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
| # | Field | What it says | Values |
|---|---|---|---|
| 1 | direction | Which way this rule judges | INGRESS or EGRESS |
| 2 | priority | Who wins when rules overlap | 0 to 65535; lower number wins |
| 3 | action | Allow or block | allow or deny |
| 4 | target | Which VMs the rule protects | all instances, network tags, or service accounts (section 05) |
| 5 | source (ingress only) | Who may reach the targets | IP ranges, network tags, service accounts |
| 6 | destination (egress only) | What the targets may reach | IP ranges |
| 7 | protocol and ports | Which traffic | tcp:443, udp:53, icmp, all |
| 8 | disabled | Switch the rule off without deleting it | true or false |
| 9 | logging | Record connections the rule matches | on or off (chapter 16) |
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.
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 type | Means | Strength |
|---|---|---|---|
| 1 | All instances (no target) | Every VM in the VPC, now and every VM created later | Widest; what you get when you forget to narrow |
| 2 | Network tags, e.g. api | VMs that carry the tag | weak |
| 3 | Service accounts, e.g. sa-kade-api | VMs running as that service account | strong |
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:
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 rule | Direction | Action | Priority |
|---|---|---|---|---|
| 1 | Implied allow egress | EGRESS | allow to 0.0.0.0/0 | 65535 |
| 2 | Implied deny ingress | INGRESS | deny from 0.0.0.0/0 | 65535 |
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.
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.
Denied by the implied deny ingress rule at 65535. Nothing above matched.
| Rule | From | Prio | Target | Result |
|---|---|---|---|---|
| kade-allow-icmp | 203.0.113.45/32, 198.51.100.20/32 · icmp | 1000 | all | does not cover tcp:5432 |
| kade-allow-internal | 10.10.0.0/16 · all | 1000 | all | source 203.0.113.99 is not in 10.10.0.0/16 |
| kade-allow-lb | 35.191.0.0/16, 130.211.0.0/22 · tcp:8080 | 1000 | tag api | does not cover tcp:5432 |
| kade-allow-ssh | 0.0.0.0/0 · tcp:22 | 1000 | all | does not cover tcp:5432 |
| kade-allow-web | 0.0.0.0/0 · tcp:80,tcp:443,tcp:8080 | 1000 | all | does not cover tcp:5432 |
| implied deny | 0.0.0.0/0 · all | 65535 | all | reached: DENY |
Changing rules without breaking things
| # | Habit | Why |
|---|---|---|
| 1 | Add and verify before you remove | Create the narrow replacement first, confirm traffic still works, then take the wide rule away |
| 2 | Disable before you delete | A disabled rule is off but still there. If something breaks, enable it again in seconds instead of rebuilding it from memory |
| 3 | Turn on logging for the rule you plan to remove | A few days of logs show who still uses it (chapter 16) |
| 4 | Allow a few seconds after each change | Rules spread to every host's data plane; testing too fast gives false results |
| 5 | Remember existing connections | Removing 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/0on 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-bSee 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.commeans 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-loggingDisable a rule, and enable it again (changes the project)
- What it shows: Switches a rule off without losing it.
- What to look for:
DISABLED Truein 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-disabledChange 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-aDelete 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