Google Cloud Tutorial · Networking · Lesson 16 of 27

Firewall Rules and Policies

How Google Cloud firewall rules work: priorities, targets by tag or service account, implied rules and hierarchical policies.

Members can save this page, take notes and track progress. See membership · Sign in

VPC firewall rules

Firewall rules belong to a VPC network and are stateful (replies are allowed automatically). Each rule has a direction (ingress or egress), an action (allow or deny), a priority from 0 (highest) to 65535, sources or destinations, protocols and ports, and targets: all instances, instances with a network tag, or instances running as a service account (harder to spoof than tags).

Every network has two implied rules: deny all ingress and allow all egress, both at the lowest priority.

gcloud compute firewall-rules create allow-lb-health-checks --network=acme-vpc \
  --direction=INGRESS --action=ALLOW --rules=tcp:8080 \
  --source-ranges=35.191.0.0/16,130.211.0.0/22 --target-service-accounts=[email protected]

Evaluation

The matching rule with the lowest priority number wins; at equal priority, deny beats allow.

Evaluating firewall rules by priorityTry it yourself
rules = [  # (priority, action, source, port, target_tag)
    (1000, "allow", "0.0.0.0/0", 443, "web"),
    (900, "deny", "198.51.100.0/24", 443, "web"),
    (1000, "allow", "10.10.0.0/20", 5432, "db"),
    (65535, "deny", "0.0.0.0/0", None, None),       # implied deny all ingress
]

def evaluate(src_net, port, tag):
    import ipaddress
    for prio, action, source, rport, rtag in sorted(rules, key=lambda r: (r[0], r[1] != "deny")):
        if ipaddress.ip_address(src_net) in ipaddress.ip_network(source) and rport in (port, None) \
                and rtag in (tag, None):
            return f"{action} (priority {prio})"

for src, port, tag in [("203.0.113.9", 443, "web"), ("198.51.100.7", 443, "web"),
                       ("10.10.3.4", 5432, "db"), ("203.0.113.9", 5432, "db")]:
    print(f"{src:14} -> {tag}:{port:<5} {evaluate(src, port, tag)}")

Output

203.0.113.9    -> web:443   allow (priority 1000)
198.51.100.7   -> web:443   deny (priority 900)
10.10.3.4      -> db:5432  allow (priority 1000)
203.0.113.9    -> db:5432  deny (priority 65535)

Firewall policies

Hierarchical firewall policies at the organisation or folder level apply before VPC rules and can delegate decisions to the network (goto_next), so a security team can enforce company-wide rules (block risky ports everywhere) while app teams manage their own. Turn on firewall rules logging when troubleshooting.

Test yourself

Two rules match a packet at priority 1000: one allows, one denies. What happens?

  1. It's allowed: allow rules win
  2. It's denied: at equal priority, deny wins
  3. The newer rule wins
  4. Both are ignored
Show answer

Answer: It's denied: at equal priority, deny wins

Lowest priority number wins; ties go to deny.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake