本页面展示了分层防火墙政策实现的示例。本文假定您熟悉分层防火墙政策中所述的概念。
示例 1:允许探测器访问所有虚拟机
在此使用场景中,必须使用从特定 IP 地址 (10.100.0.1) 到特定目标端口 (123) 的探测来扫描和记录组织中的所有虚拟机实例。组织安全管理员确保没有网络管理员或其他安全管理员可以在组织中任何虚拟机实例上阻止该端口。
此示例假定未部署文件夹级防火墙政策。
下图介绍了此使用场景的配置设置。
在虚拟机中应用的有效政策
在此示例中,在评估层次结构中的规则后,有效的虚拟机防火墙政策如下。
入站连接
- 根据政策的规定,允许使用来源 IP 为 - 10.100.0.1和目标端口为- 123的入站连接。在匹配组织政策后,系统将允许探测连接,并且不会在层次结构中评估其他规则。
- 对于除来源 IP - 10.100.0.1和目标端口- 123之外的任何入站连接,不存在匹配项;因此,系统会应用 VPC 防火墙规则中的默认入站流量规则,并拒绝该连接。
出站连接
- 层次结构定义的规则中没有匹配的规则。因此,应用 VPC 防火墙规则中的默认出站规则,允许出站连接。
如何配置
- 创建防火墙政策以包含规则: - gcloud compute firewall-policies create \ --organization=123456789012 \ --short-name="example-firewall-policy" \ --description="rules that apply to all VMs in the organization"
- 将规则添加到防火墙政策: - gcloud compute firewall-policies rules create 1000 \ --action=allow \ --description="allow-scan-probe" \ --layer4-configs=tcp:123 \ --firewall-policy=example-firewall-policy \ --organization=123456789012 \ --src-ip-ranges=10.100.0.1/32
- 将防火墙政策与组织相关联: - gcloud compute firewall-policies associations create \ --firewall-policy=example-firewall-policy \ --organization=123456789012
示例 2:拒绝除特定端口之外的所有外部连接
在此使用场景中,防火墙政策会阻止来自外部互联网来源的所有连接(目标端口 80、443 和 22 上的连接除外)。无论防火墙规则处于 VPC 网络级层中的哪一层级,80、443 和 22 以外的任何端口上的入站互联网连接都将被屏蔽。对于端口 80、443 或 22 上的任何连接,政策都会将要在其各自的 VPC 网络中对这些端口强制执行的行为委托给 VPC 安全管理员。
下图介绍了此使用场景的配置设置。
在虚拟机中应用的有效政策
在此示例中,在评估层次结构中的规则后,有效的虚拟机防火墙政策如下。
入站连接
- 来自 - 10.0.0.0/8的任何入站连接都将与优先级最高的组织级规则- delegate-internal-traffic匹配,并绕过组织政策中的其他规则,以针对 VPC 网络级层配置的防火墙规则进行评估。在 VPC 防火墙规则中,允许来自- 10.2.0.0/16的连接,其余连接针对隐式入站规则(即- deny)进行评估。
- 如果入站连接的来源 IP 范围对于目标端口 - 22、- 80和- 443而言不是- 10.0.0.0/8,则会委托给下一级层,其中允许端口- 80和- 443,但不允许- 22。
- 屏蔽所有其他连接。 
出站连接
- 层次结构定义的规则中没有匹配的规则。因此,应用 VPC 防火墙规则中的默认出站规则,允许出站连接。
如何配置
- 创建防火墙政策以包含规则: - gcloud compute firewall-policies create \ --organization=123456789012 \ --short-name="example-firewall-policy" \ --description="rules that apply to all VMs in the organization"
- 添加规则,将内部连接委托给项目所有者: - gcloud compute firewall-policies rules create 1000 \ --action=goto_next \ --description="delegate-internal-traffic" \ --organization=123456789012 \ --firewall-policy="example-firewall-policy" \ --src-ip-ranges=10.0.0.0/8
- 添加规则,将端口 - 80/- 443/- 22外部连接规则委托给项目所有者:- gcloud compute firewall-policies rules create 2000 \ --action=goto_next \ --description="delegate-external-traffic-spec-ports" \ --src-ip-ranges=0.0.0.0/0 \ --layer4-configs=tcp:80,tcp:443,tcp:22 \ --organization=123456789012 \ --firewall-policy="example-firewall-policy"
- 添加规则以拒绝所有其他外部连接: - gcloud compute firewall-policies rules create 3000 \ --action=deny \ --description="block-other-external-traffic-spec-ports" \ --organization=123456789012 \ --firewall-policy="example-firewall-policy" \ --src-ip-ranges=0.0.0.0/0
- 将防火墙政策与组织相关联: - gcloud compute firewall-policies associations create \ --organization=123456789012 \ --firewall-policy="example-firewall-policy"
- 在项目中,添加防火墙规则以允许来自指定子网的内部连接: - gcloud compute firewall-rules create allow-internal-traffic \ --action=allow \ --priority=1000 \ --source-ranges=10.2.0.0/16
- 在项目中,添加防火墙规则以允许外部 TCP - 80/- 443连接:- gcloud compute firewall-rules create allow-external-traffic \ --action=allow \ --priority=2000 \ --rules=tcp:80,tcp:443
示例 3:拒绝来自特定 VPC 网络的出站连接
在此使用场景中,组织安全管理员不允许任何 VPC 网络中的出站连接(来自 VPC 网络 myvpc 中的连接除外)。管理员会将有关向公共服务器 203.0.113.1 开放出站流量的决策委托给 myvpc 安全管理员。
此示例假定未部署文件夹级防火墙政策。下图介绍了此使用场景的配置设置。
在虚拟机中应用的有效政策
在此示例中,在评估层次结构中的规则后,有效的虚拟机防火墙政策如下。
入站连接
- 层次结构定义的规则中没有匹配的规则。因此,应用 VPC 防火墙规则中的默认入站流量规则,拒绝入站流量连接。
出站连接
- 允许发往 - 203.0.113.1的所有出站连接;其余连接会被拒绝。目标为- 203.0.113.1的所有出站连接都与- delegate-egress-my-vpc规则匹配,并绕过组织政策中的其余规则。
- 然后,系统会根据 - myvpc中配置的防火墙规则评估出站流量连接。默认规则允许出站连接。组织级层政策中的- block-egress-traffic-sepc-ports规则会拒绝其余连接。
如何配置
- 创建防火墙政策以包含规则: - gcloud compute firewall-policies create \ --organization=123456789012 \ --short-name="example-firewall-policy" \ --description="rules that apply to all VMs in the organization"
- 添加规则以委托某些出站连接: - gcloud compute firewall-policies rules create 1000 \ --action=goto_next \ --description="delegate-egress-myvpc" \ --dest-ip-ranges=203.0.113.1/32 --direction=egress --organization=123456789012 \ --short-name="example-firewall-policy" \ --target-resources=projects/PROJECT_ID/networks/myvpc
- 添加规则以拒绝所有其他出站连接: - gcloud compute firewall-policies rules create 2000 \ --action=deny \ --description="block-egress-external-traffic-spec-ports" \ --direction=egress \ --dest-ip-ranges=0.0.0.0/0 \ --organization=123456789012 \ --short-name="example-firewall-policy"
- 将防火墙政策与组织相关联: - gcloud compute firewall-policies associations create \ --organization=123456789012 \ --short-name="example-firewall-policy"
示例 4:配置组织范围的规则和特定于文件夹的规则
在此使用场景中,安全管理员不允许与组织中的任何虚拟机建立入站连接,但添加到许可名单的范围内的虚拟机除外:203.0.113.0/24。管理员会将有关如何处理来自 203.0.113.0/24 的连接的进一步决策委托给文件夹级层的安全管理员。
有两个不同的文件夹:
- Folder1,该政策仅允许到后端虚拟机上的端口 80和443的连接,且阻止其余端口。
- Folder2,该政策强制要求 Folder2 中的虚拟机不阻止来自 IP 地址 203.0.113.1的流量的任何目标端口。Folder2 安全管理员将其他决策委托给 VPC 安全管理员,后者决定开放端口80、443和22,并拒绝其余端口。
下图介绍了此使用场景的配置设置。
在虚拟机中应用的有效政策
在此示例中,在评估层次结构中的规则后,有效的虚拟机防火墙政策如下。
对于属于 my-vpc 的虚拟机
- 允许来自 - 203.0.113.0/24并且目标端口 TCP- 80和- 443的所有入站连接。拒绝其他任何入站连接。
- 由于更高级别的防火墙政策规则中不存在匹配项,因此所有出站连接都按照所应用的 VPC 防火墙规则被接受。 
对于属于 vpc2 的虚拟机
- 允许来自 - 203.0.113.1的所有入站连接。除- 203.0.113.1之外的其他- 203.0.113.0/24来源的入站连接仅允许去往端口- 80、- 443和- 22。拒绝其他所有入站连接。
- 由于更高级别的防火墙政策规则中不存在匹配项,因此所有出站连接都按照所应用的 VPC 防火墙规则被接受。 
如何配置
- 创建防火墙政策以包含 Org_A 的规则: - gcloud compute firewall-policies create \ --organization=100000000000 \ --short-name="example-firewall-policy-org-a" \ --description="rules that apply to all VMs in the organization"
- 添加规则以将来自 - 203.0.113.0/24的入站流量委托给项目所有者:- gcloud compute firewall-policies rules create 1000 \ --action=goto_next \ --description="delegate-ingress" \ --organization=100000000000 \ --short-name="example-firewall-policy-org-a" \ --src-ip-ranges=203.0.113.0/24
- 添加规则以拒绝所有其他外部连接: - gcloud compute firewall-policies rules create 2000 \ --action=deny --description="block-ingress-external-traffic" --organization=100000000000 \ --short-name="example-firewall-policy-org-a" \ --src-ip-ranges=0.0.0.0/0
- 将防火墙政策与组织相关联: - gcloud compute firewall-policies associations create \ --organization=100000000000 \ --short-name="example-firewall-policy-org-a"
- 创建防火墙政策以包含 Folder1 的规则: - gcloud compute firewall-policies create \ --organization=100000000000 \ --short-name="example-firewall-policy-folder1" \ --description="rules that apply to all VMs under Folder1"
- 添加规则以允许所有 HTTP(S) 入站流量: - gcloud compute firewall-policies rules create 1000 \ --action=allow \ --description="allow-http-s-ingress" \ --layer4-configs=tcp:80,tcp:443 \ --organization=100000000000 \ --short-name="example-firewall-policy-folder1"
- 添加规则以拒绝所有其他端口或协议上的入站流量: - gcloud compute firewall-policies rules create 2000 \ --action=deny \ --description="block-ingress-external-traffic" \ --organization=100000000000 \ --short-name="example-firewall-policy-folder1" \ --src-ip-ranges=0.0.0.0/0
- 将防火墙政策与 Folder1 关联: - gcloud compute firewall-policies associations create \ --organization=100000000000 \ --short-name="example-firewall-policy-folder1" \ --folder=200000000000
- 创建防火墙政策以包含 Folder2 的规则: - gcloud compute firewall-policies create \ --organization=100000000000 \ --short-name="example-firewall-policy-folder2" \ --description="rules that apply to all VMs under Folder2"
- 添加规则以允许来自 - 203.0.113.1的入站流量:- gcloud compute firewall-policies rules create 1000 \ --action=allow \ --description="allow-vul-scan-ingress" \ --organization=100000000000 \ --short-name="example-firewall-policy-folder2" \ --src-ip-ranges=203.0.113.1/32
- 将防火墙政策与 Folder2 关联: - gcloud compute firewall-policies associations create \ --organization=100000000000 \ --short-name="example-firewall-policy-folder2" \ --folder=300000000000
- 添加防火墙规则以允许 HTTP(S) 连接入站流量: - gcloud compute firewall-rules create allow-internal-traffic \ --network=vpc2 \ --action=allow \ --rules=tcp:80,tcp:443,tcp:22
后续步骤
- 如需创建和修改分层防火墙政策和规则,请参阅使用分层防火墙政策。