What if a tiny mistake in your network rules could open the door to hackers or block your own team?
Why Firewall rule components (target, source, protocol) in GCP? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a small office network and you want to control who can access your computers and what kind of communication is allowed.
You try to write down on paper which devices can talk to which others and what types of messages are allowed.
As the network grows, this list becomes huge and confusing.
Manually tracking who can connect to what and how is slow and easy to mess up.
One mistake can leave your network open to unwanted visitors or block important communication.
It's like trying to guard a building with a paper list instead of a proper security system.
Firewall rules let you clearly define who (source) can talk to whom (target) and by which method (protocol).
This makes managing network access simple, organized, and automated.
You can quickly update rules and be confident your network stays safe and works well.
Allow device A to talk to device B on port 80 Allow device C to talk to device D on port 22
source: device A, target: device B, protocol: TCP port 80 source: device C, target: device D, protocol: TCP port 22
It enables secure and precise control over network traffic, protecting resources while allowing needed communication.
A company uses firewall rules to let employees access the internet but blocks access to sensitive servers from outside the office.
Firewall rules organize network access by source, target, and protocol.
Manual tracking is error-prone and hard to maintain.
Using firewall rules improves security and simplifies management.
Practice
source component specify in a GCP firewall rule?Solution
Step 1: Understand the role of source in firewall rules
The source defines where the incoming traffic comes from, such as specific IP addresses or ranges.Step 2: Differentiate source from target and protocol
The target specifies which machines are affected, and protocol defines the communication type, so source is about origin.Final Answer:
The IP addresses or ranges where traffic originates -> Option CQuick Check:
Source = traffic origin [OK]
- Confusing source with target machines
- Mixing source with protocol type
- Thinking source is about rule priority
Solution
Step 1: Review GCP firewall rule syntax for protocol
Protocols are specified as strings, so they must be enclosed in quotes like "tcp" or "udp".Step 2: Identify correct syntax among options
"tcp" uses quotes correctly. tcp lacks quotes, protocol: tcp and "protocol:tcp" include extra text or wrong format.Final Answer:
"tcp" -> Option AQuick Check:
Protocol strings need quotes [OK]
- Omitting quotes around protocol
- Adding extra text inside protocol string
- Using incorrect syntax like key:value inside quotes
{"sourceRanges": ["192.168.1.0/24"], "targetTags": ["web-server"], "allowed": [{"IPProtocol": "tcp", "ports": ["80"]}]}Which machines will this rule apply to?
Solution
Step 1: Identify the target component in the rule
The rule uses "targetTags": ["web-server"], meaning it applies only to machines tagged "web-server".Step 2: Understand sourceRanges and allowed fields
SourceRanges limits traffic origin; allowed defines protocol and ports. TargetTags define which machines are affected.Final Answer:
Machines tagged with "web-server" -> Option AQuick Check:
TargetTags = affected machines [OK]
- Confusing sourceRanges with target machines
- Thinking sourceRanges limits target machines
- Assuming all machines are affected
{"sourceRanges": ["10.0.0.0/16"], "targetTags": ["db-server"], "allowed": [{"IPProtocol": tcp, "ports": ["5432"]}]}Why does this rule fail to deploy?
Solution
Step 1: Check the protocol field syntax
The protocol name "tcp" must be a string enclosed in quotes. Here, tcp is unquoted, causing syntax error.Step 2: Verify other fields
sourceRanges format is correct, targetTags accept tags, ports can be strings representing port numbers.Final Answer:
Missing quotes around protocol name "tcp" -> Option DQuick Check:
Protocol names need quotes [OK]
- Leaving protocol unquoted
- Confusing tags with IP addresses
- Using numeric ports without quotes (allowed but inconsistent)
Solution
Step 1: Match sourceRanges to the required IP range
The correct sourceRanges ["203.0.113.0/24"] matches the requirement, eliminating configurations using ["0.0.0.0/0"].Step 2: Check targetTags and allowed protocol/ports
{"sourceRanges": ["203.0.113.0/24"], "targetTags": ["frontend"], "allowed": [{"IPProtocol": "tcp", "ports": ["80"]}]} targets "frontend" and allows TCP on port "80" as strings, which is correct. {"sourceRanges": ["203.0.113.0/24"], "targetTags": ["frontend"], "allowed": [{"IPProtocol": "tcp"}]} lacks ports, so incomplete.Step 3: Verify other options
{"sourceRanges": ["0.0.0.0/0"], "targetTags": ["frontend"], "allowed": [{"IPProtocol": "tcp", "ports": [80]}]} allows all IPs (0.0.0.0/0), not restricted. {"sourceRanges": ["203.0.113.0/24"], "targetTags": ["backend"], "allowed": [{"IPProtocol": "udp", "ports": ["80"]}]} targets "backend" and uses UDP, both incorrect.Final Answer:
{"sourceRanges": ["203.0.113.0/24"], "targetTags": ["frontend"], "allowed": [{"IPProtocol": "tcp", "ports": ["80"]}]} -> Option BQuick Check:
Correct source, target, protocol, and port [OK]
- Using wrong IP range or all IPs
- Targeting wrong VM tags
- Missing ports in allowed protocols
- Using wrong protocol like UDP for HTTP
