If you need to manage firewall rules at scale for your organization, it is recommended that you use firewall policies. Firewall policies are a container for firewall rules that can be attached to supported GCP resources within the Resource Hierarchy. We’ll expand on firewall policies later in this article.
All firewall rules have a configurable priority
GCP firewall rules have a configurable priority associated with them. The configured priority determines which order rules are evaluated. As such, the principle of a rule being used based on “first hit, not best fit” applies.
Firewall rules matching is done by “first hit, not best fit”
The phrase First Hit, Not Best Fit is used to characterize and emphasize how the configuration of firewall priority on a rule is important.
Let’s create an academic example to demonstrate this. In this scenario, you were interested in denying network access to services from a contiguous half of a private subnetwork’s address space (192.168.1.0/25). Below is a simplified and egregiously configured list of firewall rules and their priority to attempt to meet that objective:
| Priority |
Action |
Direction |
Target |
Filter |
| 1500 |
allow |
INGRESS |
All Networks |
172.16.0.0/16 |
| 2500 |
allow |
INGRESS |
All Networks |
192.168.1.0/24 |
| 3500 |
deny |
INGRESS |
All Networks |
192.168.1.0/25 |
While rule 3500 is valid, it is superseded by rule 2500, which will permit those clients who you want to deny. Flipping the rule definitions of rules 2500 and 3500 will result in the desired behavior:
| Priority |
Action |
Direction |
Target |
Filter |
| 1500 |
allow |
INGRESS |
All Networks |
172.16.0.0/16 |
| 2500 |
deny |
INGRESS |
All Networks |
192.168.1.0/25 |
| 3500 |
allow |
INGRESS |
All Networks |
192.168.1.0/24 |
Contextual differences on firewall rule priority
Firewall rules in GCP will always have a priority associated with them. However, the constraints around how priorities are configured differ depending on usage context. See the table below which illustrates the differences between firewall rule and firewall policy usage contexts:
| Firewall Rule Attached To |
Highest Priority1 |
Lowest Priority1 |
Duplicate Rule Priority? |
| VPC Network Firewall |
0 |
65535 |
Allowed |
| GCP Firewall Policy |
0 |
2147483647 |
Denied |
Regarding duplicate firewall rules configured within a VPC Network Firewall, there is an order of operations that reconciles what takes precedence in the event a set of rules share a common priority.
Being mindful of these priority rules is not necessary for a firewall policy. This is because all rules defined within a firewall policy must have discrete priorities. This condenses the consideration for firewall rules order in a firewall policy down to “First hit, not best fit” when crafting rules for a firewall policy.
Additional details around GCP firewalling
In addition to using traditional CIDR notation for address ranges to define in rules, you may also use service accounts and tags (network tags on VMs or secure tags2).
Preferring service accounts and tags over CIDR-based rules provides an opportunity to filter traffic based on application usage context and makes porting or duplicating deployments needing firewall rules easier and more predictable.
For the ‘default’ VPC network that is created with a GCP project, there are a collection of predefined rules that are automatically applied and enforced. The rules permit all intra-network traffic within the VPC, as well as permissive rules allowing ICMP, SSH, and RDP access to GCP resources.
Implicit firewall rules
The following implicit firewall rules are catch-all default rules that are applied if none of the configured firewall rules match a traffic flow:
- Implied allow
egress rule
- Implied
deny ingress rule
This is a speculative side note here, however, it is assumed those implicit firewall rules, as well as the auto-configured rules applied to the default network, are provided as a courtesy to expedite GCP service usage and testing. As such, these defaults should not be considered appropriate or secure for sensitive environments.Understanding what these defaults are is important when trying to level set the desired security posture you’re trying to achieve to align with technical and non-technical requirements.
Google manages a collection of globally allowed, blocked, or limited traffic for GCP
With Google advocating for Shared Fate along with Shared Responsibility when operating in GCP, it’s important to understand, or at least have reference to, what GCP is always allowing, blocking, and limiting as part of its service provider offering.
Reviewing, understanding, and accepting the risks related to what a service provider’s operating constraints are is globally important to an organization and not solely security’s responsibility.
Firewall policies and management at scale
Let’s pivot to talking about firewall policies in GCP. So far, we’ve focused on attaching firewall rules to a VPC network. However, let’s say your organization’s scale is outstripping the administrative capabilities provided for managing firewall rules in a GCP project. Problems you’d be encountering include:
- Inconsistent application of common firewall rules across multiple projects in a GCP organization
- Delegating a level of autonomy to teams within your organization to self-manage firewall rules in your GCP projects without violating the organization’s desired firewall hardening posture
Enter firewall policies. Firewall policies are a logical container consisting of firewall rules. Those rules containers can then be attached to supported GCP resources within the hierarchy.
There are three types of firewall policies:
- Hierarchical Firewall Policies
- Network Firewall Policies
- Regional Firewall Policies
Fortunately, there are a lot of common traits across the different policy types. We’ll focus on those and then raise some key differences in policy types.
Common facts regarding GCP Firewall Policy Types
When creating a new firewall policy, the policy is not attached to any GCP resource by default. Only when the policy is attached to a supported resource, will the rules in the firewall policy be evaluated.
Rules evaluation for attached firewall policies follow the resource hierarchy
While firewall policy association constraints vary across the different firewall policy types, they all adhere to the GCP Resource Hierarchy for parent/child resource relationships. Having working knowledge of how the resource hierarchy is structured in GCP will lead to more impactful design and architectural choices throughout your management lifecycle on Google Cloud.
Firewall policies enable centralized rules management and governance
Firewall policies enable one policy to many resource relationships. These one-to-many relationships allow for consistent management and enforcement of firewall rules across your GCP organization.
Additionally, firewall policies introduce a new action, goto_next. When a rule is configured with this action, the rules processing is deferred to a child policy or rule downstream. This allows for a layered approach on what rules are globally enforced. This logical flow diagram illustrates a chain of firewall policies and rules mapped onto a generic GCP resource hierarchy: