含標記的全域網路防火牆政策

1. 簡介

網路防火牆政策

防火牆是安全雲端環境的基本構成元素。我們先前是在機構和資料夾層級推出防火牆政策,同時讓虛擬私有雲防火牆保持不變。這個版本將把防火牆政策結構擴展至虛擬私有雲層級,並針對現行的防火牆政策支援做出多項強化措施,讓 Google Cloud 資源階層結構提供統一的防火牆支援,並為使用者提供一致的使用者體驗,以安全、有彈性且可擴充的方式管理防火牆政策控制項。

網路防火牆政策可做為防火牆規則的容器使用。在政策與虛擬私有雲網路建立關聯之前,網路防火牆政策中定義的規則不會在任何位置強制執行。每個虛擬私有雲網路都只能有一項相關聯的網路防火牆政策。網路防火牆政策支援防火牆規則中受 IAM 規範的標記 (或只有標記),這類標記會取代目前的網路標記,並可用於為工作負載提供身分。

跨網路共用網路防火牆政策,並與受 IAM 控管的標記整合,可大幅簡化防火牆的設定與管理作業。

隨著網路防火牆政策推出,Google Cloud 的防火牆政策現在包含以下元件:

  1. 階層式防火牆政策
  2. 虛擬私有雲防火牆規則
  3. 網路防火牆政策 ( 全域區域性)

階層式防火牆政策支援資源階層內的機構和資料夾節點,虛擬私有雲防火牆規則和網路防火牆政策則會在虛擬私有雲層級套用。虛擬私有雲防火牆規則和網路防火牆政策的一大差異在於,虛擬私有雲防火牆規則只能套用至單一虛擬私有雲網路,網路防火牆政策則可連結至單一虛擬私有雲或一組虛擬私有雲,並具備批次更新等其他好處。

最後,每個虛擬私有雲網路都有默示防火牆規則

  • 輸出規則,其動作允許,目的地為 0.0.0.0/0
  • 輸入規則的動作遭拒,來源為 0.0.0.0/0

根據預設,強制執行順序如下圖所示:

abae4597af782b2b.png

請注意,您可以交換虛擬私有雲防火牆規則和全域網路防火牆政策之間的強制執行順序。客戶隨時可以使用 gcloud 指令指定強制執行順序。

標記

新的整合至網路防火牆政策規則的標記,是在 Google Cloud 資源階層的機構層級定義的鍵/值組合資源。顧名思義,這類標記包含 IAM 存取權控管,指定誰可以對標記執行動作。例如,允許某個主體指定哪些主體可以指派值給標記,以及哪些主體可將標記附加至資源。將標記套用至資源後,網路防火牆規則即可使用該標記來允許及拒絕流量。

標記會遵循 Google Cloud 的繼承資源模型,也就是說,標記及其值會從父項向下傳遞。因此,如果您在同一處建立標記,就能供資源階層結構中的其他資料夾和專案使用。如要進一步瞭解代碼和存取權限制,請前往這個頁面

標記不應與網路標記混淆,後者是可新增至 Compute Engine 執行個體的字串;這些執行個體會與執行個體相關聯,並在執行個體停用時消失。虛擬私有雲防火牆規則可能包含網路標記,但這類標記不視為雲端資源,因此不受 IAM 存取權控管限制。

請注意,本文件中會交替使用標記和受 IAM 控管的標記。

建構項目

本程式碼研究室分為兩個部分:第一部分示範使用單一虛擬私有雲網路的網路防火牆政策和標記;第二部分則會說明在對等互連的虛擬私有雲網路之間使用標記,如下圖所示。因此,本程式碼研究室需要一項專案,並建立多個虛擬私有雲網路。

b9acf9823df8be04.jpeg

課程內容

  • 如何建立網路防火牆政策
  • 如何搭配網路防火牆政策建立及使用標記
  • 如何透過虛擬私有雲網路對等互連使用標記

軟硬體需求

  • Google Cloud 專案
  • 具備部署執行個體與設定網路元件的知識
  • 虛擬私有雲防火牆設定知識

2. 事前準備

建立/更新變數

本程式碼研究室會使用 $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. 建立虛擬私有雲網路和子網路

虛擬私人雲端網路

建立 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 用戶端和網路伺服器執行個體:

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

由於未定義任何虛擬私有雲防火牆規則 (如本節開頭所述,設定 IAP 時應建立的允許 SSH 規則除外),而且根據預設,系統會拒絕所有輸入流量,因此用戶端執行個體無法存取各個網路伺服器。為了驗證要求是否會逾時,請開啟新視窗,並啟動對 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 定義虛擬私有雲防火牆規則:

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 網路伺服器:

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 中說明現有的網路防火牆政策規則,確認已成功套用標記,並回報為 effectiveIVE

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

驗證即使網路防火牆政策仍與虛擬私有雲網路相關聯,允許網路流量的規則也不會再套用至網路伺服器,因為標記未新增至執行個體:

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. 透過虛擬私有雲網路對等互連使用 IAM 控管標記

在 Cloud Shell 中建立新的虛擬私有雲、子網路和用戶端,並設定網路之間的虛擬私有雲網路對等互連:

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

雖然標記是整個機構的物件,但標記金鑰已連結至特定虛擬私有雲,因此無法套用至不同網路中的執行個體。因此,您必須建立適用於 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

更新現有的防火牆規則,允許對等互連虛擬私有雲網路中的標記:

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

最後,移除虛擬私有雲網路對等互連、子網路和虛擬私有雲網路:

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

恭喜!您已成功使用標記設定,成功設定並驗證全域網路防火牆政策。