Firewall rules concept in GCP - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with firewall rules in cloud networks, it's important to understand how the time to process these rules grows as you add more rules.
We want to know how the system handles checking many rules when a network packet arrives.
Analyze the time complexity of checking firewall rules for incoming packets.
// Example: Checking firewall rules for a packet
firewallRules = getFirewallRules() // list of rules
for (rule in firewallRules) {
if (packet matches rule) {
apply rule action
break
}
}
This sequence checks each firewall rule one by one until it finds a match for the incoming packet.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Checking each firewall rule against the packet.
- How many times: Once per rule until a match is found or all rules are checked.
As the number of firewall rules increases, the number of checks grows roughly in the same way.
| Input Size (n) | Approx. Rule Checks |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows directly with the number of rules.
Time Complexity: O(n)
This means the time to check rules grows linearly with the number of firewall rules.
[X] Wrong: "Adding more firewall rules won't affect packet processing time much."
[OK] Correct: Each new rule adds more checks, so processing time increases with more rules.
Understanding how firewall rules scale helps you design networks that stay fast and secure as they grow.
"What if firewall rules were organized in groups and checked in parallel? How would the time complexity change?"
Practice
Solution
Step 1: Understand what firewall rules do
Firewall rules are designed to control network traffic by specifying which traffic is allowed or denied.Step 2: Identify the correct function in GCP context
In GCP, firewall rules specifically allow or block traffic based on protocols, ports, and IP ranges.Final Answer:
To control network traffic by allowing or blocking it based on defined conditions -> Option AQuick Check:
Firewall rules control traffic = B [OK]
- Confusing firewall rules with data storage
- Thinking firewall rules monitor logs
- Assuming firewall rules handle backups
Solution
Step 1: Identify the protocol and port for HTTP traffic
HTTP uses TCP protocol on port 80.Step 2: Check the source IP range
'0.0.0.0/0' means any IP address, which matches the requirement.Final Answer:
protocol: 'tcp', ports: ['80'], sourceRanges: ['0.0.0.0/0'] -> Option AQuick Check:
TCP port 80 from any IP = A [OK]
- Using UDP instead of TCP for HTTP
- Specifying wrong port like 22
- Using ICMP protocol for port-based rules
{"direction": "INGRESS", "allowed": [{"IPProtocol": "tcp", "ports": ["22"]}], "sourceRanges": ["192.168.1.0/24"]}Which traffic will be allowed?
Solution
Step 1: Analyze the allowed protocol and port
The rule allows TCP protocol on port 22 only.Step 2: Check the source IP range
Only IPs in 192.168.1.0/24 are allowed, so 192.168.1.15 is included, but 10.0.0.5 is not.Final Answer:
TCP traffic on port 22 from IP 192.168.1.15 -> Option BQuick Check:
TCP port 22 from 192.168.1.x allowed = C [OK]
- Allowing wrong port like 80
- Allowing UDP instead of TCP
- Ignoring source IP range restrictions
Solution
Step 1: Understand traffic direction for incoming HTTPS
HTTPS traffic comes into the VM, so firewall rule must be INGRESS.Step 2: Check the rule direction
If the rule is EGRESS, it controls outgoing traffic, so incoming HTTPS is blocked.Final Answer:
The firewall rule direction is set to EGRESS instead of INGRESS -> Option DQuick Check:
Ingress needed for incoming traffic = D [OK]
- Confusing ingress and egress directions
- Changing port from 443 to 80 incorrectly
- Opening sourceRanges too wide unnecessarily
Solution
Step 1: Understand default firewall behavior
By default, GCP denies all traffic unless explicitly allowed.Step 2: Allow only SSH from office IP
Allowing TCP port 22 from 203.0.113.5 only permits SSH from that IP; no deny rule needed.Step 3: Avoid conflicting rules
Adding deny rules can cause conflicts; simplest is to allow only the trusted IP.Final Answer:
Allow TCP port 22 from 203.0.113.5 only, no other rules needed -> Option CQuick Check:
Allow trusted IP only; default deny others = A [OK]
- Adding unnecessary deny rules causing conflicts
- Allowing all IPs then trying to deny one
- Using wrong protocol or port
