带有标记的全球网络防火墙政策

1. 简介

网络防火墙政策

防火墙是构建安全云环境的基础组件。之前,我们在组织级和文件夹级引入了防火墙政策,而 VPC 防火墙保持不变。在此版本中,我们将将防火墙政策结构扩展到 VPC 级别,并对当前的防火墙政策支持进行了多项增强,以便在 Google Cloud 的资源层次结构中提供统一的防火墙支持,并让用户能够以安全、灵活且可扩缩的方式一致地管理防火墙政策控制。

网络防火墙政策充当防火墙规则的容器。在网络防火墙政策与 VPC 网络关联之前,系统不会在任何位置强制执行该政策中定义的规则。每个 VPC 网络可以有一个与之关联的网络防火墙政策。网络防火墙政策支持在防火墙规则中使用 IAM 治理的标记(或简称“标记”),这些标记会取代当前的网络标记,可用于向工作负载提供身份。

在多个网络中共享网络防火墙政策以及与 IAM 治理的标记集成,可大大简化防火墙的配置和管理。

随着网络防火墙政策的推出,Google Cloud 的防火墙政策现在由以下组件组成:

  1. 分层防火墙政策
  2. VPC 防火墙规则
  3. 网络防火墙政策(全球区域

资源层次结构中的组织和文件夹节点支持分层防火墙政策,而 VPC 防火墙规则和网络防火墙政策则在 VPC 级别应用。VPC 防火墙规则和网络防火墙政策之间的一个重大区别是,VPC 防火墙规则只能应用于单个 VPC 网络,而网络防火墙政策可以附加到单个 VPC 或一组 VPC,还具有批量更新等其他优势。

最后,我们还有每个 VPC 网络附带的隐式防火墙规则

  • 操作为“允许”且目的地为 0.0.0.0/0 的出站规则
  • 操作为“拒绝”且来源为 0.0.0.0/0 的入站规则

默认情况下,违规处置顺序如下图所示:

abae4597af782b2b.png

请注意,VPC 防火墙规则和全球网络防火墙政策之间的强制执行顺序可以互换。客户可以随时使用 gcloud 命令指定违规处置命令。

标签

新的集成到网络防火墙政策规则中的标记是在 Google Cloud 资源层次结构的组织级层定义的键值对资源。顾名思义,此类标记包含一个 IAM 访问权限控件,用于指定谁可以对标记执行什么操作。例如,通过 IAM 权限,您可以指定哪些主账号可为标记分配值,以及哪些主账号可将标记附加到资源。将标记应用于资源后,网络防火墙规则可以使用该标记来允许和拒绝流量。

标记遵循 Google Cloud 的继承资源模型,这意味着标记及其值会从其父级沿层次结构向下传递。因此,标记可以在一个位置创建,然后由整个资源层次结构中的其他文件夹和项目使用。如需详细了解代码和访问权限限制,请访问此页面

不应将标记与网络标记混淆,后者是可以添加到 Compute Engine 实例中的字符串;它们与实例相关联,并会在实例停用后消失。VPC 防火墙规则可能包含网络标记,但由于它们不被视为云资源,因此不受 IAM 访问权限控制的约束。

请注意,本文档中“标记”和“受 IAM 管控的标记”是互换使用的。

构建内容

本 Codelab 分为两部分:第一部分演示了使用单个 VPC 网络的网络防火墙政策和标记,第二部分将展示如何根据下图在对等互连的 VPC 网络中使用标记。因此,此 Codelab 需要一个项目,并且能够创建多个 VPC 网络。

b9acf9823df8be04.jpeg

学习内容

  • 如何创建网络防火墙政策
  • 如何在网络防火墙政策中创建和使用标记
  • 如何通过 VPC 网络对等互连使用标记

所需条件

  • Google Cloud 项目
  • 了解如何部署实例和配置网络组件
  • VPC 防火墙配置知识

2. 准备工作

创建/更新变量

此 Codelab 使用 $variables 来帮助在 Cloud Shell 中实现 gcloud 配置。

在 Cloud Shell 中,执行以下操作:

gcloud config set project [project-id]
export project_id=`gcloud config list --format="value(core.project)"`
export org_id=[org]
export region=us-central1
export zone=us-central1-a
export prefix=fwpolicy

3. 创建 VPC 网络和子网

VPC 网络

创建 fwpolicy-vpc1

gcloud compute networks create $prefix-vpc1 --subnet-mode=custom 

子网

在所选区域中创建相应的子网:

gcloud compute networks subnets create $prefix-vpc1-subnet \
   --range=10.0.0.0/24 --network=$prefix-vpc1 --region=$region

Cloud NAT

fwpolicy-pc1 创建 Cloud Router 路由器和 Cloud NAT 网关:

gcloud compute routers create $prefix-vpc1-cr \
  --region=$region --network=$prefix-vpc1

gcloud compute routers nats create $prefix-vpc1-cloudnat \
   --router=$prefix-vpc1-cr --router-region=$region \
   --auto-allocate-nat-external-ips \
   --nat-all-subnet-ip-ranges

4. 创建实例

创建一条防火墙规则,允许来自 IAP 范围的 SSH 入站流量(如果尚未在 IAP 设置中定义):

gcloud compute firewall-rules create allow-ssh-ingress-from-iap-vpc1 \
  --direction=INGRESS \
  --action=allow \
  --network=$prefix-vpc1 \
  --rules=tcp:22 \
  --source-ranges=35.235.240.0/20

创建 fwpolicy-vpc1 客户端和 Web 服务器实例:

gcloud compute instances create $prefix-vpc1-www \
   --subnet=$prefix-vpc1-subnet --no-address --zone $zone \
   --metadata startup-script='#! /bin/bash
apt-get update
apt-get install apache2 -y
a2ensite default-ssl
a2enmod ssl
# Read VM network configuration:
md_vm="http://169.254.169.254/computeMetadata/v1/instance/"
vm_hostname="$(curl $md_vm/name -H "Metadata-Flavor:Google" )"
filter="{print \$NF}"
vm_network="$(curl $md_vm/network-interfaces/0/network \
-H "Metadata-Flavor:Google" | awk -F/ "${filter}")"
vm_zone="$(curl $md_vm/zone \
-H "Metadata-Flavor:Google" | awk -F/ "${filter}")"
# Apache configuration:
echo "Page on $vm_hostname in network $vm_network zone $vm_zone" | \
tee /var/www/html/index.html
systemctl restart apache2'

gcloud compute instances create $prefix-vpc1-client \
    --subnet=$prefix-vpc1-subnet --no-address --zone $zone

由于未定义任何 VPC 防火墙规则(除了应在配置 IAP 时根据本部分开头部分创建的允许 SSH 规则),并且默认情况下所有入站流量都会被拒绝,因此客户端实例将无法访问相应的网站服务器。为了验证请求是否会超时,请打开一个新窗口,并发起与 fwpolicy-vpc1-client 实例的 SSH 会话,然后尝试 curl 该 Web 服务器:

user@fwpolicy-vpc1-client$ curl fwpolicy-vpc1-www --connect-timeout 2

预期输出:

curl: (28) Connection timed out after 2001 milliseconds

(可选)通过 Cloud Shell 验证是否未为 fwpolicy-vpc1 定义任何 VPC 防火墙规则:

gcloud compute firewall-rules list --filter="network:$prefix-vpc1"

5. 全球网络防火墙政策

创建全球网络防火墙政策:

gcloud compute network-firewall-policies create \
   $prefix-example --description \
   "firewall-policy-description" --global

添加一条允许 Web 流量的规则:

gcloud compute network-firewall-policies rules create 500 \
    --action allow \
    --description "allow-web" \
    --layer4-configs tcp:80,tcp:443 \
    --firewall-policy $prefix-example \
    --src-ip-ranges 10.0.0.0/16 \
    --global-firewall-policy --enable-logging

描述网络防火墙政策,并验证规则是否已成功added

gcloud compute network-firewall-policies describe \
    $prefix-example --global

预期输出(向上滚动到输出开头;请注意,系统还会显示隐式规则):

creationTimestamp: '2022-09-23T12:46:53.677-07:00'
description: "firewall-policy-description"
fingerprint: Np1Rup09Amc=
id: '7021772628738421698'
kind: compute#firewallPolicy
name: fwpolicy-example
ruleTupleCount: 13
rules:
- action: allow
  description: allow-web
  direction: INGRESS
  disabled: false
  enableLogging: true
  kind: compute#firewallPolicyRule
  match:
    layer4Configs:
    - ipProtocol: tcp
      ports:
      - '80'
    - ipProtocol: tcp
      ports:
      - '443'
    srcIpRanges:
    - 10.0.0.0/16
  priority: 500
  ruleTupleCount: 5
...

将网络防火墙政策与 fwpolicy-vpc1 相关联:

gcloud compute network-firewall-policies associations create \
     --firewall-policy $prefix-example \
     --network $prefix-vpc1 \
     --name $prefix-vpc1-association \
     --global-firewall-policy

验证该规则是否已成功应用到 fwpolicy-vpc1 网络:

gcloud compute networks get-effective-firewalls $prefix-vpc1

预期输出(请注意,如果有优先级更高的分层防火墙政策,相关规则会显示在顶部):

TYPE                     FIREWALL_POLICY_NAME     PRIORITY    ACTION     DIRECTION  IP_RANGES
network-firewall-policy  fwpolicy-example      500         ALLOW      INGRESS    10.0.0.0/16
network-firewall-policy  fwpolicy-example      2147483645  GOTO_NEXT  INGRESS    ::/0
network-firewall-policy  fwpolicy-example      2147483647  GOTO_NEXT  INGRESS    0.0.0.0/0
network-firewall-policy  fwpolicy-example      2147483644  GOTO_NEXT  EGRESS     ::/0
network-firewall-policy  fwpolicy-example      2147483646  GOTO_NEXT  EGRESS     0.0.0.0/0

验证是否已成功将其应用到 fwpolicy-vpc1 Web 服务器:

gcloud compute instances network-interfaces \
   get-effective-firewalls $prefix-vpc1-www --zone $zone

预期输出与上一个命令(fwpolicy-vpc1 有效防火墙)类似:

TYPE                     FIREWALL_POLICY_NAME     PRIORITY    ACTION     DIRECTION  IP_RANGES
network-firewall-policy  fwpolicy-example      500         ALLOW      INGRESS    10.0.0.0/16
network-firewall-policy  fwpolicy-example      2147483645  GOTO_NEXT  INGRESS    ::/0
network-firewall-policy  fwpolicy-example      2147483647  GOTO_NEXT  INGRESS    0.0.0.0/0
network-firewall-policy  fwpolicy-example      2147483644  GOTO_NEXT  EGRESS     ::/0
network-firewall-policy  fwpolicy-example      2147483646  GOTO_NEXT  EGRESS     0.0.0.0/0

切换回 vpc1-client SSH 会话,然后再次尝试 curl(请注意,以下命令假定使用 fwpolicy 作为前缀;如果改用其他名称,请相应地调整 curl 命令):

user@vpc1-client$ curl fwpolicy-vpc1-www --connect-timeout 2
Page on vpc1-www in network vpc1 zone us-central1-a

在 Cloud Shell 中,验证网络防火墙政策是否已应用于 fwpolicy-vpc1

gcloud compute network-firewall-policies describe \
   $prefix-example --global

预期输出(向上滚动到输出开头):

---
associations:
- attachmentTarget: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/fwpolicy-vpc1
  name: fwpolicy-vpc1-association
...

6. IAM 治理的标记

标记是一种可以关联到组织、文件夹或项目的键值对。如需了解详情,请参阅创建和管理代码以及所需权限

通过 tagAdmin 角色,您可以创建新标记、更新和删除现有标记。组织管理员可以授予此角色。在 Cloud Shell 中,更新 IAM 政策以向用户添加 tagAdmin 角色。您可以使用权限参考文档页面查看每个预定义角色包含哪些权限。

gcloud organizations add-iam-policy-binding $org_id \
  --member user:[user@example.com] --role roles/resourcemanager.tagAdmin

运行以下命令,验证哪些用户拥有 resourcemanager.tagAdmin 角色:

gcloud organizations get-iam-policy $org_id --flatten=bindings \
  --filter=bindings.role:roles/resourcemanager.tagAdmin

创建新的代码键:

gcloud resource-manager tags keys create tags-vpc1 \
   --parent organizations/$org_id \
   --purpose GCE_FIREWALL \
   --purpose-data network=$project_id/$prefix-vpc1

预期输出:

Waiting for TagKey [tags-vpc1] to be created...done.                                                                                                                
createTime: '2022-09-23T20:49:01.162228Z'
etag: PwvmFuHO4wK1y6c5Ut2n5w==
name: tagKeys/622132302133
namespacedName: ORGANIZATION_ID/tags-vpc1
parent: organizations/ORGANIZATION_ID
purpose: GCE_FIREWALL
purposeData:
  network: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/6749205358365096383
shortName: tags-vpc1
updateTime: '2022-09-23T20:49:03.873776Z'

创建新的代码值:

gcloud resource-manager tags values create web-servers \
   --parent=$org_id/tags-vpc1

gcloud resource-manager tags values create web-clients \
   --parent=$org_id/tags-vpc1

验证是否已成功创建代码值:

gcloud resource-manager tags values list \
   --parent=$org_id/tags-vpc1

预期输出:

NAME                    SHORT_NAME   DESCRIPTION
tagValues/349564376683  web-servers
tagValues/780363571446  web-clients

在 Cloud Shell 中,描述现有的网络防火墙政策规则,以确认未使用标记:

gcloud compute network-firewall-policies rules describe 500 \
    --firewall-policy $prefix-example \
    --global-firewall-policy

预期输出:

---
action: allow
description: allow-web
direction: INGRESS
disabled: false
enableLogging: true
kind: compute#firewallPolicyRule
match:
  layer4Configs:
  - ipProtocol: tcp
    ports:
    - '80'
  - ipProtocol: tcp
    ports:
    - '443'
  srcIpRanges:
  - 10.0.0.0/16
priority: 500
ruleTupleCount: 5

在 Cloud Shell 中,更新规则以仅允许来自 vpc1-tags/web-clients 标记键的流量,并在具有 vpc1-tags/web-servers 标记键的实例上安装该规则。

gcloud compute network-firewall-policies rules update 500 \
    --firewall-policy $prefix-example \
    --src-secure-tags $org_id/tags-vpc1/web-clients \
    --target-secure-tags $org_id/tags-vpc1/web-servers \
    --global-firewall-policy

在 Cloud Shell 中,描述现有的网络防火墙政策规则,以确认标记已成功应用且报告为有效

gcloud compute network-firewall-policies rules describe 500 \
    --firewall-policy $prefix-example \
    --global-firewall-policy

预期输出:

---
action: allow
description: allow-web
direction: INGRESS
disabled: false
enableLogging: false
kind: compute#firewallPolicyRule
match:
  layer4Configs:
  - ipProtocol: tcp
    ports:
    - '80'
  - ipProtocol: tcp
    ports:
    - '443'
  srcIpRanges:
  - 10.0.0.0/16
  srcSecureTags:
  - name: tagValues/479619031616
    state: EFFECTIVE
priority: 500
ruleTupleCount: 7
targetSecureTags:
- name: tagValues/230424970229
  state: EFFECTIVE

在 Cloud Shell 中,我们来验证该规则是否已应用于 vpc1

gcloud compute networks get-effective-firewalls $prefix-vpc1

预期输出:

network-firewall-policy  fwpolicy-example      500         ALLOW      INGRESS    10.0.0.0/16
network-firewall-policy  fwpolicy-example      2147483645  GOTO_NEXT  INGRESS    ::/0
network-firewall-policy  fwpolicy-example      2147483647  GOTO_NEXT  INGRESS    0.0.0.0/0
network-firewall-policy  fwpolicy-example      2147483644  GOTO_NEXT  EGRESS     ::/0
network-firewall-policy  fwpolicy-example      2147483646  GOTO_NEXT  EGRESS     0.0.0.0/0

验证即使网络防火墙政策仍与 VPC 网络相关联,但允许 Web 流量的规则已不再应用于 Web 服务器,因为未将标记添加到实例:

gcloud compute instances network-interfaces \
   get-effective-firewalls $prefix-vpc1-www --zone $zone

预期输出(请注意,优先级为 500 的防火墙规则不会显示):

network-firewall-policy  fwpolicy-example      2147483645  GOTO_NEXT  INGRESS    ::/0
network-firewall-policy  fwpolicy-example      2147483647  GOTO_NEXT  INGRESS    0.0.0.0/0
network-firewall-policy  fwpolicy-example      2147483644  GOTO_NEXT  EGRESS     ::/0
network-firewall-policy  fwpolicy-example      2147483646  GOTO_NEXT  EGRESS     0.0.0.0/0

向特定代码和用户授予“代码用户”角色。您可以使用权限参考文档页面查看每个预定义角色包含哪些权限。

gcloud resource-manager tags keys add-iam-policy-binding \
  $org_id/tags-vpc1 \
  --member user:[email] --role roles/resourcemanager.tagUser

gcloud projects add-iam-policy-binding $project_id \
  --member user:[email] --role roles/resourcemanager.tagUser

验证该角色是否已成功添加:

gcloud resource-manager tags keys get-iam-policy $org_id/tags-vpc1

gcloud projects get-iam-policy $project_id --flatten=bindings \
   --filter=bindings.role:roles/resourcemanager.tagUser

预期输出:

bindings:
- members:
  - user:[user]
  role: roles/resourcemanager.tagUser
...

将代码应用于 fwpolicy-vpc1-www 实例:

gcloud resource-manager tags bindings create \
  --location $zone \
  --tag-value $org_id/tags-vpc1/web-servers \
  --parent \
//compute.googleapis.com/projects/$project_id/zones/$zone/instances/$prefix-vpc1-www

预期输出:

Waiting for TagBinding for parent [//compute.googleapis.com/projects/PROJECT_ID/zones/us-central1-a/instances/38369703403698502] and tag value [tagValues/34
9564376683] to be created with [operations/rctb.us-central1-a.6144808968019372877]...done.                                                                            
done: true
metadata:
  '@type': type.googleapis.com/google.cloud.resourcemanager.v3.CreateTagBindingMetadata
name: operations/rctb.us-central1-a.6144808968019372877
response:
  '@type': type.googleapis.com/google.cloud.resourcemanager.v3.TagBinding
  name: tagBindings/%2F%2Fcompute.googleapis.com%2Fprojects%2FPROJECT_NUMBER%2Fzones%2Fus-central1-a%2Finstances%2F38369703403698502/tagValues/349564376683
  parent: //compute.googleapis.com/projects/PROJECT_NUMBER/zones/us-central1-a/instances/38369703403698502
  tagValue: tagValues/349564376683

验证绑定:

gcloud resource-manager tags bindings list --location $zone --effective --parent //compute.googleapis.com/projects/$project_id/zones/$zone/instances/$prefix-vpc1-www 

预期输出:

namespacedTagKey: ORGANIZATION_ID/tags-vpc1
namespacedTagValue: ORGANIZATION_ID/tags-vpc1/web-servers
tagKey: tagKeys/622132302133
tagValue: tagValues/349564376683

再次验证有效的防火墙规则:

gcloud compute instances network-interfaces \
   get-effective-firewalls $prefix-vpc1-www --zone $zone

预期输出:

network-firewall-policy  fwpolicy-example      490         ALLOW      INGRESS    10.0.0.0/16
network-firewall-policy  fwpolicy-example      2147483645  GOTO_NEXT  INGRESS    ::/0
network-firewall-policy  fwpolicy-example      2147483647  GOTO_NEXT  INGRESS    0.0.0.0/0
network-firewall-policy  fwpolicy-example      2147483644  GOTO_NEXT  EGRESS     ::/0
network-firewall-policy  fwpolicy-example      2147483646  GOTO_NEXT  EGRESS     0.0.0.0/0

切换回 fwpolicy-vpc1-client SSH 会话标签页,然后尝试 curl:

user@fwpolicy-vpc1-client$ curl fwpolicy-vpc1-www --connect-timeout 2

您能连接上吗?

如需验证这一点,请通过 Cloud Shell 更新规则,移除来源 CIDR 条件。

gcloud compute network-firewall-policies rules update 500 \
    --firewall-policy $prefix-example \
    --src-ip-ranges "" \
    --global-firewall-policy

gcloud compute network-firewall-policies rules describe 500 \
    --firewall-policy $prefix-example \
    --global-firewall-policy

action: allow
description: allow-web
direction: INGRESS
disabled: false
enableLogging: false
kind: compute#firewallPolicyRule
match:
  layer4Configs:
  - ipProtocol: tcp
    ports:
    - '80'
  - ipProtocol: tcp
    ports:
    - '443'
  srcSecureTags:
  - name: tagValues/479619031616
    state: EFFECTIVE
priority: 490
ruleTupleCount: 7
targetSecureTags:
- name: tagValues/230424970229
  state: EFFECTIVE

切换回 fwpolicy-vpc1-client SSH 会话标签页,然后重试:

user@fwpolicy-vpc1-client$ curl fwpolicy-vpc1-www --connect-timeout 2

由于未将标记添加到 fwpolicy-vpc1-client,因此这次连接应该会超时。在 Cloud Shell 中添加该角色,然后重试。

gcloud resource-manager tags bindings create \
  --location $zone \
  --tag-value $org_id/tags-vpc1/web-clients \
  --parent \
//compute.googleapis.com/projects/$project_id/zones/$zone/instances/$prefix-vpc1-client

切换回 fwpolicy-vpc1-client SSH 会话标签页,然后重试,这次应该会成功。

user@fwpolicy-vpc1-client$ curl fwpolicy-vpc1-www --connect-timeout 2

7. 通过 VPC 网络对等互连使用 IAM 治理的标记

在 Cloud Shell 中,创建新的 VPC、子网和客户端,并在这些网络之间设置 VPC 网络对等互连:

gcloud compute networks create $prefix-vpc2 --subnet-mode=custom 

gcloud compute networks subnets create $prefix-vpc2-subnet \
   --range=10.0.1.0/24 --network=$prefix-vpc2 --region=$region

gcloud compute instances create $prefix-vpc2-client \
   --subnet=$prefix-vpc2-subnet --no-address --zone $zone

gcloud compute networks peerings create vpc1-to-vpc2 \
   --network=$prefix-vpc1 \
   --peer-project $project_id \
   --peer-network $prefix-vpc2

gcloud compute networks peerings create vpc2-to-vpc1 \
    --network=$prefix-vpc2 \
    --peer-project $project_id \
    --peer-network $prefix-vpc1

创建一条防火墙规则,允许来自 IAP 范围的 SSH 入站流量(如果尚未在 IAP 设置中定义):

gcloud compute firewall-rules create allow-ssh-ingress-from-iap-vpc2 \
  --direction=INGRESS \
  --action=allow \
  --network=$prefix-vpc2 \
  --rules=tcp:22 \
  --source-ranges=35.235.240.0/20

虽然标记是组织级对象,但标记键会与特定 VPC 相关联,因此无法应用于不同网络中的实例。因此,您需要创建适用于 vpc2 的新标记键和值:

gcloud resource-manager tags keys create tags-vpc2 \
   --parent organizations/$org_id \
   --purpose GCE_FIREWALL \
   --purpose-data network=$project_id/$prefix-vpc2

gcloud resource-manager tags values create web-clients \
   --parent=$org_id/tags-vpc2

将新标记应用于 fwpolicy-vpc2-client 实例:

gcloud resource-manager tags bindings create \
  --location $zone \
  --tag-value $org_id/tags-vpc2/web-clients \
  --parent \
//compute.googleapis.com/projects/$project_id/zones/$zone/instances/$prefix-vpc2-client

(可选)列出 fwpolicy-vpc2-client 的绑定:

gcloud resource-manager tags bindings list --location $zone --effective --parent //compute.googleapis.com/projects/$project_id/zones/$zone/instances/$prefix-vpc2-client

预期输出:

namespacedTagKey: ORGANIZATION_ID/tags-vpc2
namespacedTagValue: ORGANIZATION_ID/tags-vpc2/web-clients
tagKey: tagKeys/916316350251
tagValue: tagValues/633150043992

在 Cloud Shell 中,描述现有的网络防火墙政策规则,以确认未使用新标记:

gcloud compute network-firewall-policies rules describe 500 \
    --firewall-policy $prefix-example \
    --global-firewall-policy

预期输出:

---
action: allow
description: allow-web
direction: INGRESS
disabled: false
enableLogging: true
kind: compute#firewallPolicyRule
match:
  layer4Configs:
  - ipProtocol: tcp
    ports:
    - '80'
  - ipProtocol: tcp
    ports:
    - '443'
  srcSecureTags:
  - name: tagValues/479619031616
    state: EFFECTIVE
priority: 500
ruleTupleCount: 6
targetSecureTags:
- name: tagValues/230424970229
  state: EFFECTIVE

更新现有防火墙规则,以允许来自对等 VPC 网络的标记:

gcloud compute network-firewall-policies rules update 500 \
    --firewall-policy $prefix-example \
    --src-secure-tags $org_id/tags-vpc1/web-clients,$org_id/tags-vpc2/web-clients \
    --global-firewall-policy

描述防火墙规则,确保其已成功应用并报告为有效

gcloud compute network-firewall-policies rules describe 500 \
    --firewall-policy $prefix-example \
    --global-firewall-policy

预期输出:

---
action: allow
description: allow-web
direction: INGRESS
disabled: false
enableLogging: false
kind: compute#firewallPolicyRule
match:
  layer4Configs:
  - ipProtocol: tcp
    ports:
    - '80'
  - ipProtocol: tcp
    ports:
    - '443'
  srcSecureTags:
  - name: tagValues/479619031616
    state: EFFECTIVE
  - name: tagValues/633150043992
    state: EFFECTIVE
priority: 500
ruleTupleCount: 7
targetSecureTags:
- name: tagValues/230424970229
  state: EFFECTIVE

通过以下 gcloud 命令查找 fwpolicy-vpc1-www 的 IP:

gcloud compute instances list --filter=vpc1-www

通过 SSH 连接到 fwpolicy-vpc2-client,然后尝试 curl fwpolicy-vpc1 的 IP:

user@fwpolicy-vpc2-client$ curl [fwpolicy-vpc1-www_IP] --connect-timeout 2

您应该能够连接到 fwpolicy-vpc1-www 服务器。请参阅下一部分,了解清理步骤。

8. 清理步骤

在 Cloud Shell 中,移除实例、Cloud NAT 和 Cloud Router:

gcloud -q compute instances delete $prefix-vpc2-client --zone=$zone

gcloud -q compute instances delete $prefix-vpc1-client --zone=$zone

gcloud -q compute instances delete $prefix-vpc1-www --zone=$zone

gcloud -q compute routers nats delete $prefix-vpc1-cloudnat \
--router=$prefix-vpc1-cr --router-region=$region

gcloud -q compute routers delete $prefix-vpc1-cr --region=$region

移除全球网络防火墙政策和标记:

gcloud -q resource-manager tags values delete \
   $org_id/tags-vpc2/web-clients

gcloud -q resource-manager tags keys delete $org_id/tags-vpc2

gcloud -q resource-manager tags values delete \
   $org_id/tags-vpc1/web-servers

gcloud -q resource-manager tags values delete \
   $org_id/tags-vpc1/web-clients

gcloud -q resource-manager tags keys delete $org_id/tags-vpc1

gcloud -q compute network-firewall-policies associations delete \
     --firewall-policy $prefix-example \
     --name $prefix-vpc1-association \
     --global-firewall-policy

gcloud -q compute network-firewall-policies delete \
   $prefix-example --global

gcloud -q compute firewall-rules delete allow-ssh-ingress-from-iap-vpc1

gcloud -q compute firewall-rules delete allow-ssh-ingress-from-iap-vpc2

如果更改了 tagAdmin 和 tagUsers 角色,请执行以下步骤:

gcloud organizations remove-iam-policy-binding $org_id \
  --member user:[email] --role roles/resourcemanager.tagAdmin

gcloud organizations remove-iam-policy-binding $org_id \
  --member user:[email] --role roles/resourcemanager.tagUser

最后,移除 VPC 网络对等互连、子网和 VPC 网络:

gcloud -q compute networks peerings delete vpc1-to-vpc2 \
    --network $prefix-vpc1

gcloud -q compute networks peerings delete vpc2-to-vpc1 \
    --network $prefix-vpc2

gcloud -q compute networks subnets delete $prefix-vpc1-subnet \
    --region $region

gcloud -q compute networks subnets delete $prefix-vpc2-subnet \
    --region $region

gcloud -q compute networks delete $prefix-vpc1

gcloud -q compute networks delete $prefix-vpc2

9. 恭喜!

恭喜!您已成功配置并验证了使用标记配置的全球网络防火墙政策。