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

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 规则),并且默认情况下所有入站流量都会被拒绝,因此客户端实例将无法访问相应的 Web 服务器。为了验证请求是否会超时,请打开一个新窗口,启动与 fwpolicy-vpc1-client 实例的 SSH 会话,并尝试对网络服务器执行 curl 命令:

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

添加允许网络流量的规则:

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 网络关联,允许网络流量的规则也不再应用于网络服务器,因为标记未添加到实例:

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

向特定代码和用户授予 Tag User 角色。使用权限参考页面查看每个预定义角色包含的权限。

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. 恭喜!

恭喜,您已成功使用“标记”配置成功配置并验证了全局网络防火墙政策。