分层防火墙政策示例

本页面展示了分层防火墙政策实现的示例。本文假定您熟悉分层防火墙政策中所述的概念。

示例 1:允许探测器访问所有虚拟机

在此使用场景中,必须使用从特定 IP 地址 (10.100.0.1) 到特定目标端口 (123) 的探测来扫描和记录组织中的所有虚拟机实例。组织安全管理员确保没有网络管理员或其他安全管理员可以在组织中任何虚拟机实例上阻止该端口。

此示例假定未部署文件夹级防火墙政策。

下图介绍了此使用场景的配置设置。

允许探测器访问所有虚拟机
允许探测器访问所有虚拟机

在虚拟机中应用的有效政策

在此示例中,在评估层次结构中的规则后,有效的虚拟机防火墙政策如下。

入站连接

  • 根据政策的规定,允许使用来源 IP 为 10.100.0.1 和目标端口为 123 的入站连接。在匹配组织政策后,系统将允许探测连接,并且不会在层次结构中评估其他规则。

  • 对于除来源 IP 10.100.0.1 和目标端口 123 之外的任何入站连接,就没有匹配项;因此,应用 VPC 防火墙规则中的默认入站规则,拒绝连接。

出站连接

  • 层次结构定义的规则中没有匹配的规则。因此,应用 VPC 防火墙规则中的默认出站规则,允许出站连接。

如何配置

  1. 创建防火墙政策以包含规则:

    gcloud compute firewall-policies create \
         --organization=123456789012 \
         --short-name="example-firewall-policy" \
         --description="rules that apply to all VMs in the organization"
    
  2. 将规则添加到防火墙政策:

    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
  3. 将防火墙政策与组织节点相关联:

    gcloud compute firewall-policies associations create \
        --firewall-policy=example-firewall-policy \
        --organization=123456789012

示例 2:拒绝除特定端口之外的所有外部连接

在此使用场景中,防火墙政策会阻止来自外部互联网来源的所有连接(目标端口 8044322 上的连接除外)。无论防火墙规则处于 VPC 网络级层中的哪一层级,8044322 以外的任何端口上的入站互联网连接都将被屏蔽。对于端口 8044322 上的任何连接,政策都会将要在其各自的 VPC 网络中强制执行这些端口的行为委派给 VPC 安全管理员。

下图介绍了此使用场景的配置设置。

拒绝除特定目标端口之外的所有外部连接
拒绝除特定目标端口之外的所有外部连接

在虚拟机中应用的有效政策

在此示例中,在评估层次结构中的规则后,有效的虚拟机防火墙政策如下。

入站连接

  • 来自 10.0.0.0/8 的任何入站连接都将与优先级最高的组织级规则 delegate-internal-traffic 匹配,并绕过组织政策中的其他规则,以针对 VPC 网络级层配置的防火墙规则进行评估。在 VPC 防火墙规则中,允许来自 10.2.0.0/16 的连接,其余连接针对隐式入站规则(即 deny)进行评估。

  • 如果入站连接的来源 IP 地址范围对于目标端口 2280443 而言不是 10.0.0.0/8,则会委托给下一级,其中端口 80443 会被接受,但 22 会被拒绝。

  • 屏蔽所有其他连接。

出站连接

  • 层次结构定义的规则中没有匹配的规则。因此,应用 VPC 防火墙规则中的默认出站规则,允许出站连接。

如何配置

  1. 创建防火墙政策以包含规则:

    gcloud compute firewall-policies create \
        --organization=123456789012 \
        --short-name="example-firewall-policy" \
        --description="rules that apply to all VMs in the organization"
    
  2. 添加规则,将内部连接委托给项目所有者:

    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
    
  3. 添加规则,将端口 80/443/22 外部连接规则委托给项目所有者:

    gcloud compute firewall-policies rules create 2000 \
        --action=goto_next \
        --description="delegate-external-traffic-spec-ports" \
        --layer4-configs=tcp:80,tcp:443,tcp:22 \
        --organization=123456789012 \
        --firewall-policy="example-firewall-policy"
  4. 添加规则以拒绝所有其他外部连接:

    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
    
  5. 将防火墙政策与组织节点相关联:

    gcloud compute firewall-policies associations create \
        --organization=123456789012 \
        --firewall-policy="example-firewall-policy"
  6. 在项目中,添加防火墙规则以允许来自指定子网的内部连接:

    gcloud compute firewall-rules create allow-internal-traffic \
        --action=allow \
        --priority=1000 \
        --source-ranges=10.2.0.0/16
    
  7. 在项目中,添加防火墙规则以允许外部 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 规则会拒绝其余连接。

如何配置

  1. 创建防火墙政策以包含规则:

    gcloud compute firewall-policies create \
        --organization=123456789012 \
        --short-name="example-firewall-policy" \
        --description="rules that apply to all VMs in the organization"
    
  2. 添加规则以授予某些出站流量连接:

    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
    
  3. 添加规则以拒绝所有其他出站连接:

    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"
  4. 将防火墙政策与组织节点相关联:

    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,该政策仅允许到后端虚拟机上的端口 80443 的连接,且阻止其余端口。
  • Folder2,该政策强制要求 Folder2 中的虚拟机不阻止来自 IP 地址 203.0.113.1 的流量的任何目标端口。Folder2 安全管理员将其他决策委托给 VPC 安全管理员,该管理员决定打开端口 8044322,并拒绝其余端口。

下图介绍了此使用场景的配置设置。

组织范围的规则和特定于文件夹的规则
组织范围的规则和特定于文件夹的规则

在虚拟机中应用的有效政策

在此示例中,在评估层次结构中的规则后,有效的虚拟机防火墙政策如下。

对于属于 my-vpc 的虚拟机

  • 允许来自 203.0.113.0/24 并且目标端口 TCP 80443 的所有入站连接。拒绝其他任何入站连接。

  • 由于更高级别的防火墙政策规则没有匹配项,因此所有出站连接都按照所应用的 VPC 防火墙规则被接受。

对于属于 vpc2 的虚拟机

  • 允许来自 203.0.113.1 的所有入站连接。除 203.0.113.1 之外的其他 203.0.113.0/24 来源的入站连接仅允许去往端口 8044322。拒绝其他所有入站连接。

  • 由于更高级别的防火墙政策规则没有匹配项,因此所有出站连接都按照所应用的 VPC 防火墙规则被接受。

如何配置

  1. 创建防火墙政策以包含 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"
    
  2. 添加规则以将 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
    
  3. 添加规则以拒绝所有其他外部连接:

    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
    
  4. 将防火墙政策与组织节点相关联:

    gcloud compute firewall-policies associations create \
        --organization=100000000000 \
        --short-name="example-firewall-policy-org-a"
  5. 创建防火墙政策以包含 Folder1 的规则:

    gcloud compute firewall-policies create \
        --organization=100000000000 \
        --short-name="example-firewall-policy-folder1" \
        --description="rules that apply to all VMs under Folder1"
    
  6. 添加规则以允许所有 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"
  7. 添加规则以拒绝所有其他端口或协议上的入站流量:

    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
  8. 将防火墙政策与 Folder1 关联:

    gcloud compute firewall-policies associations create \
        --organization=100000000000 \
        --short-name="example-firewall-policy-folder1" \
        --folder=200000000000
  9. 创建防火墙政策以包含 Folder2 的规则:

    gcloud compute firewall-policies create \
        --organization=100000000000 \
        --short-name="example-firewall-policy-folder2" \
        --description="rules that apply to all VMs under Folder2"
  10. 添加规则以允许来自 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
  11. 将防火墙政策与 Folder2 关联:

    gcloud compute firewall-policies associations create \
        --organization=100000000000 \
        --short-name="example-firewall-policy-folder2" \
        --folder=300000000000
  12. 添加防火墙规则以允许 HTTP(S) 连接入站流量:

    gcloud compute firewall-rules create allow-internal-traffic \
        --action=allow \
        --rules=tcp:80,tcp:443,tcp:22
    

后续步骤