為生產者/消費者模型設定頻內整合

網路安全整合服務會使用生產者-消費者模型檢查及監控資料。這項服務會使用頻內整合搭配封包攔截技術,將第三方網路裝置插入網路流量,並執行深度封包檢查。

本教學課程說明如何建立及設定生產者和消費者資源,以設定頻內整合。

目標

本教學課程說明如何在專案中完成下列工作:

  • 在同一個專案中,為生產者和消費者建立含有子網路的自訂 VPC 網路。
  • 對於生產者,請建立及設定生產者資源,例如內部直通式網路負載平衡器、攔截服務和攔截部署資源。
  • 對於消費者,請建立及設定用戶端和伺服器虛擬機器 (VM) 執行個體、消費者端點資源和防火牆政策,以攔截用戶端與伺服器之間的流量。
  • 測試連線,並確認網路流量封包是否遭到消費者端點資源攔截,以及是否由生產者端點的攔截資源檢查。

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. 為專案啟用 Compute Engine API
  7. 在要用於計費的 Google Cloud 專案中,啟用 Network Security API
  8. 確認您在專案層級具備Compute 網路管理員角色 (roles/compute.networkAdmin),且在機構層級具備安全設定檔管理員角色 (roles/networksecurity.securityProfileAdmin)。
  9. 如果您希望透過指令列操作,請先安裝 Google Cloud CLI。如需該工具的概念與安裝資訊,請參閱 gcloud CLI 總覽

    注意:如果您先前沒有執行過 Google Cloud CLI,請先執行 gcloud init,將 gcloud CLI 目錄初始化。

  10. 取得專案 ID。您需要這個 ID 才能建立攔截和端點資源。

    如果您使用 gcloud CLI,可以在這裡或稍後在相應的指令中,替換 PROJECT_ID

  11. 取得貴機構的 ID。您需要這個 ID 才能建立機構層級的資源,例如安全性設定檔和安全性設定檔群組。

    如果您使用 gcloud CLI,可以替換ORG_ID這裡的 ORG_ID,或稍後在各自的指令中替換。

建立製作人資源

在本節中,您將為製作人建立下列資源:

  • 具有子網路的自訂虛擬私有雲網路。
  • 非代管執行個體群組,其中包含執行通用網路虛擬化封裝 (GENEVE) 迴響伺服器的 VM 執行個體。
  • 具有後端服務和轉送規則的內部直通式網路負載平衡器。
  • 允許 Google Cloud 健康狀態檢查的防火牆規則。
  • 攔截部署項目群組和攔截部署項目。

建立自訂虛擬私有雲網路

在本節中,您將建立具有子網路的虛擬私有雲網路。

控制台

  1. 前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。

    前往「VPC networks」(虛擬私有雲網路)

  2. 按一下「建立虛擬私有雲網路」

  3. 在「Name」(名稱) 中輸入 producer-network

  4. 在「說明」中輸入 Producer VPC network

  5. 在「Subnets」(子網路) 區段中,執行下列操作:

    1. 在 [Subnet creation mode] (子網路建立模式) 中,選擇 [Custom]。
    2. 在「New subnet」(新的子網路) 區段中,輸入以下資訊:
      • Name (名稱):producer-subnet
      • Region (區域):us-west1
      • IP stack type (IP 堆疊類型):IPv4 (單一堆疊)
      • IPv4 範圍10.10.0.0/16
    3. 按一下 [完成]
  6. 點選「建立」

  7. 開啟 Google Cloud CLI,然後執行下列指令,增加虛擬私有雲網路的最大傳輸單元 (MTU)。

    gcloud compute networks update producer-network \
        --mtu=1768
    

    在指令中,您指定的 MTU 為 1768 個位元組,這是虛擬私有雲網路的預設 MTU (1460 個位元組) 和網路安全整合服務 GENEVE 封裝負載 (308 個位元組) 的總和。

gcloud

  1. 建立虛擬私有雲網路。

    gcloud compute networks create producer-network \
        --subnet-mode=custom \
        --mtu=1768 \
        --description="Producer VPC network"
    

    在指令中,您指定最大傳輸單位 (MTU) 為 1768 個位元組,這是虛擬私有雲網路的預設 MTU (1460 個位元組) 和網路安全整合服務 GENEVE 封裝負載 (308 個位元組) 的總和。

  2. 在虛擬私有雲網路中建立子網路。

    gcloud compute networks subnets create producer-subnet \
        --network=producer-network \
        --region=us-west1 \
        --range=10.10.0.0/16
    

建立非代管執行個體群組

在本節中,您將建立非代管執行個體群組。

控制台

  1. 前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。

    前往「Instance groups」(執行個體群組)

  2. 點選「建立執行個體群組」

  3. 按一下「New unmanaged instance group」(新增非代管的執行個體群組)

  4. 在「Name」(名稱) 中輸入 producer-instance-group

  5. 在「Location」(位置) 區段,為「Region」(區域) 選取 us-west1,然後為「Zone」(可用區) 選取 us-west1-b

  6. 在「Network」(網路) 中選取 producer-network

  7. 在「Subnetwork」(子網路) 中,選取 producer-subnet

  8. 點選「建立」

gcloud

gcloud compute instance-groups unmanaged create producer-instance-group \
    --zone=us-west1-b

設定負載平衡器元件

在本節中,您將建立內部直通式網路負載平衡器的元件,包括後端服務和轉送規則。

控制台

開始設定

  1. 前往 Google Cloud 控制台的「Load balancing」(負載平衡) 頁面。

    前往「Load balancing」(負載平衡)

  2. 點選「建立負載平衡器」
  3. 在「負載平衡器類型」部分,選取「網路負載平衡器 (TCP/UDP/SSL)」,然後點選「下一步」
  4. 在「Proxy or passthrough」(直通或使用 Proxy) 部分,選取「Passthrough load balancer」(直通式負載平衡器),然後點選「Next」(下一步)
  5. 在「公開或內部」部分,選取「內部」,然後點選「下一步」
  6. 按一下 [設定]

基本設定

在「建立內部直通式網路負載平衡器」頁面中,輸入下列資訊:

  • 負載平衡器名稱producer-ilb
  • Region (區域):us-west1
  • Network (網路):producer-network

設定後端

  1. 按一下「後端設定」
  2. 在「通訊協定」部分選取「UDP」。
  3. 在「健康狀態檢查」清單中,選取「建立健康狀態檢查」,輸入下列資訊,然後按一下「建立」
    • 名稱:producer-health-check
    • 範圍區域
    • 通訊埠: 80
    • Proxy 通訊協定: NONE
  4. 在「Backends」(後端) 的「New Backend」(新增後端) 區段中,選取「IP stack type」(IP 堆疊類型) 做為「IPv4 (single-stack)」(IPv4 (單一堆疊))
  5. 在「Instance group」(執行個體群組) 中,選取 producer-instance-group 執行個體群組,然後按一下「Done」(完成)
  6. 繼續操作之前,請先驗證「Backend configuration」(後端設定) 旁是否有藍色勾號。

設定前端

在「New Frontend IP and port」(新增的前端 IP 和通訊埠) 區段中,執行下列操作:

  1. 在「Name」(名稱) 中輸入 producer-ilb-fr
  2. 在「Subnetwork」(子網路) 中,選取 producer-subnet
  3. 在「Ports」(通訊埠) 中選取「Single」(單個),然後在「Port number」(通訊埠編號) 中輸入 6081
  4. 繼續操作之前,請先驗證「Frontend configuration」(前端設定) 旁是否有藍色勾號。

檢查設定

  1. 按一下「檢查並完成」
  2. 查看負載平衡器設定。
  3. 點選「建立」

gcloud

  1. 建立地區健康狀態檢查。

    gcloud compute health-checks create tcp producer-health-check \
        --region=us-west1 \
        --port=80
    
  2. 建立後端服務。

    gcloud compute backend-services create producer-backend-service \
        --protocol=UDP \
        --region=us-west1 \
        --health-checks=producer-health-check \
        --health-checks-region=us-west1 \
        --load-balancing-scheme=INTERNAL
    
  3. 為後端服務建立轉送規則。

    gcloud compute forwarding-rules create producer-ilb-fr \
        --backend-service=producer-backend-service \
        --region=us-west1 \
        --network=producer-network \
        --subnet=producer-subnet \
        --ip-protocol=UDP \
        --load-balancing-scheme=INTERNAL \
        --ports=6081
    

建立 VM 執行個體並新增至執行個體群組

在本節中,您會建立 VM 執行個體,並使用開機指令碼設定 GENEVE 封裝封包的回應伺服器。

建立 VM 執行個體前,請先取得內部直通式網路負載平衡器的轉送規則 IP 位址和子網路的閘道。您需要啟動指令碼的 IP 位址。

控制台

取得內部直通式網路負載平衡器的 IP 位址

  1. 前往 Google Cloud 控制台的「Load balancing」(負載平衡) 頁面。

    前往「Load balancing」(負載平衡)

  2. 按一下 producer-ilb 負載平衡器。

  3. 在「Frontend」(前端) 部分中,記下「IP:ports」(IP:通訊埠) 欄中的轉送規則 IP 位址。

取得子網路閘道的 IP 位址

  1. 前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。

    前往「VPC networks」(虛擬私有雲網路)

  2. 按一下 producer-network 虛擬私有雲網路。

  3. 按一下「子網路」分頁標籤。

  4. 在「Subnets」(子網路) 區段中,記下「Gateway」(閘道) 欄中的閘道 IP 位址。

建立 VM 執行個體

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

    1. 點選「建立執行個體」
    2. 在「Name」(名稱) 中輸入 producer-instance
    3. 在「Region」(區域) 中選取 us-west1
    4. 在「Zone」(可用區) 中選取 us-west1-b
    5. 按一下「網路」,然後設定下列欄位:

      在「Network interfaces」(網路介面) 區段中,按一下 default 並編輯網路介面。

      • Network (網路):producer-network
      • Subnet (子網路):producer-subnet
      • 外部 IPv4 位址
    6. 點選「進階」,並在「開機指令碼」中輸入下列指令碼:

      #!/bin/bash
      # Enable IP forwarding.
      echo 1 > /proc/sys/net/ipv4/ip_forward
      
      # Log incoming packets from the gateway IP and the GENEVE 6081
      # port.
      iptables -t nat -A PREROUTING -p udp -s GATEWAY_IP/32 --dport 6081 -j LOG --log-prefix="[NSI INTERCEPT] "
      
      # Apply DNAT (Destination NAT) on incoming packets from the gateway IP
      # and the GENEVE 6081 port.
      # Set the destination IP to the gateway IP so the packet is rerouted
      # back to the sender.
      iptables -t nat -A PREROUTING -p udp -s GATEWAY_IP/32 --dport 6081 -j DNAT --to-destination GATEWAY_IP
      
      # Apply SNAT (Source NAT) on outgoing packets that have the GENEVE 6081
      # port and source IP that equals the gateway IP.
      # Set the source IP to the internal load balancer IP which
      # effectively swaps the IPs of the original packet.
      iptables -t nat -A POSTROUTING -p udp -s GATEWAY_IP/32 --dport 6081 -j SNAT --to-source LOAD_BALANCER_IP
      
      # Spin up a simple server for health checks on port 80.
      nohup python3 -u -m http.server 80 &
      

      更改下列內容:

      • GATEWAY_IP:子網路閘道的 IP 位址。
      • LOAD_BALANCER_IP:負載平衡器轉送規則的 IP 位址。
    7. 點選「建立」

將 VM 執行個體新增至執行個體群組

  1. 前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。

    前往「Instance groups」(執行個體群組)

  2. 按一下「producer-instance-group」。

  3. 按一下 [編輯]

  4. 在「VM instances」(VM 執行個體) 區段,從「Select VMs」(選取 VM) 清單中選取 producer-instance VM。

  5. 按一下 [儲存]

gcloud

  1. 取得負載平衡器轉送規則的 IP 位址。

    ILB_IP=$(gcloud compute forwarding-rules describe producer-ilb-fr \
        --region=us-west1 \
        --format="get(IPAddress)")
    
  2. 取得子網路閘道的 IP 位址。

    GW_IP=$(gcloud compute networks subnets describe producer-subnet \
        --region=us-west1 \
        --format="get(gatewayAddress)")
    
  3. 建立 VM 執行個體。

    gcloud compute instances create producer-instance \
        --image-project=debian-cloud \
        --image-family=debian-11 \
        --machine-type=e2-micro \
        --zone=us-west1-b \
        --network-interface="subnet=producer-subnet,no-address" \
        --metadata=startup-script='#!/bin/bash
        # Enable IP forwarding.
        echo 1 > /proc/sys/net/ipv4/ip_forward
    
        # Log incoming packets from the gateway IP and the GENEVE 6081 port.
        iptables -t nat -A PREROUTING -p udp -s '"$GW_IP"'/32 --dport 6081 -j LOG --log-prefix="[NSI INTERCEPT] "
    
        # Apply DNAT (Destination NAT) on incoming packets from the gateway
        # IP and the GENEVE 6081 port.
        # Set the destination IP to the gateway IP so the packet is rerouted
        # back to the sender.
        iptables -t nat -A PREROUTING -p udp -s '"$GW_IP"'/32 --dport 6081 -j DNAT --to-destination '"$GW_IP"'
    
        # Apply SNAT (Source NAT) on outgoing packets that have the GENEVE
        # 6081 port and source IP that equals the gateway IP.
        # Set the source IP to the internal load balancer IP which
        # effectively swaps the IPs of the original packet.
        iptables -t nat -A POSTROUTING -p udp -s '"$GW_IP"'/32 --dport 6081 -j SNAT --to-source '"$ILB_IP"'
    
        # Spin up a simple server for health checks on port 80.
        nohup python3 -u -m http.server 80 &'
    
  4. 將 VM 執行個體新增至執行個體群組。

    gcloud compute instance-groups unmanaged add-instances producer-instance-group \
        --instances=producer-instance \
        --zone=us-west1-b
    
  5. 將執行個體群組新增至後端服務。

    gcloud compute backend-services add-backend producer-backend-service \
        --region=us-west1 \
        --instance-group=producer-instance-group \
        --instance-group-zone=us-west1-b
    

建立防火牆政策並新增防火牆規則

在本節中,您會建立防火牆政策並新增防火牆規則,允許透過 Identity-Aware Proxy (IAP) 建立 UDP 連線、 Google Cloud 健康狀態檢查和 SSH 連線至 Producer VM 執行個體。

控制台

  1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

    前往「Firewall policies」(防火牆政策)

  2. 按一下「建立防火牆政策」

  3. 在「Name」(名稱) 欄位中,輸入 producer-firewall-policy

  4. 在「部署範圍」部分選取「全域」,然後按一下「繼續」

  5. 為政策建立下列規則。

    允許從虛擬私有雲閘道 IP 位址建立與 GENEVE 通訊埠的 UDP 連線

    1. 點選「建立防火牆規則」,並設定下列欄位:
      • Priority (優先順序):100
      • Direction of traffic (流量方向):「Ingress」(輸入)
      • 「Action on match」(相符時執行的動作)允許
      • 「Source filters」(來源篩選器) >「IP ranges」(IP 範圍)GATEWAY_IP
      • 「通訊協定和通訊埠」:選取「指定的通訊協定和通訊埠」,勾選「UDP」核取方塊,並在「通訊埠」中指定 6081
    2. 點選「建立」

    GATEWAY_IP 替換為子網路閘道的 IP 位址。

    允許 Google Cloud 健康狀態檢查

    1. 點選「建立防火牆規則」,並設定下列欄位:
      • Priority (優先順序):101
      • Direction of traffic (流量方向):「Ingress」(輸入)
      • 「Action on match」(相符時執行的動作)允許
      • IP 範圍35.191.0.0/16130.211.0.0/22
      • 通訊協定和通訊埠:選取「指定的通訊協定和通訊埠」,勾選「TCP」核取方塊,然後在「通訊埠」中指定 80
    2. 點選「建立」

    允許透過 Identity-Aware Proxy 連線至生產者 VM 執行個體的 SSH

    1. 點選「建立防火牆規則」,並設定下列欄位:
      • Priority (優先順序):102
      • Direction of traffic (流量方向):「Ingress」(輸入)
      • 「Action on match」(相符時執行的動作)允許
      • IP 範圍35.235.240.0/20
      • 通訊協定和通訊埠:選取「指定的通訊協定和通訊埠」,勾選「TCP」核取方塊,然後在「通訊埠」中指定 22
    2. 點選「建立」
  6. 如要將政策與聯播網建立關聯,請按一下「繼續」,然後按一下「建立關聯」

  7. 勾選 producer-network 核取方塊。

  8. 按一下「繼續」

  9. 點選「建立」

gcloud

  1. 建立全域網路防火牆政策。

    gcloud compute network-firewall-policies create producer-firewall-policy \
        --global
    
  2. 將防火牆政策與生產端網路建立關聯。

    gcloud compute network-firewall-policies associations create \
        --name=producer-firewall-policy-assoc \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --network=producer-network
    
  3. 建立防火牆規則,允許從 VPC 閘道 IP 位址建立與 GENEVE 連接埠的 UDP 連線。

    gcloud compute network-firewall-policies rules create 100 \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=udp:6081 \
        --src-ip-ranges=$GW_IP/32
    
  4. 建立防火牆規則,允許 Google Cloud 健康狀態檢查。

    gcloud compute network-firewall-policies rules create 101 \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=tcp:80 \
        --src-ip-ranges=35.191.0.0/16,130.211.0.0/22
    
  5. 建立防火牆規則,允許透過 Identity-Aware Proxy 與 Producer VM 執行個體建立 SSH 連線。

    gcloud compute network-firewall-policies rules create 102 \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=tcp:22 \
        --src-ip-ranges=35.235.240.0/20
    

建立生產端攔截資源

在本節中,您將建立攔截部署項目群組和攔截部署項目。

控制台

  1. 前往 Google Cloud 控制台的「Deployment groups」(部署群組) 頁面。

    前往「Deployment groups」(部署群組)

  2. 按一下「建立部署群組」

  3. 在「Name」(名稱) 中輸入 producer-deployment-group

  4. 在「Network」(網路) 中選取 producer-network

  5. 在「用途」部分,選取「NSI in-band」

  6. 在「Intercept deployments」(攔截部署作業) 區段中,按一下「Create intercept deployment」(建立攔截部署作業),指定下列欄位,然後按一下「Create」(建立)

    • 名稱producer-deployment
    • 區域us-west1
    • 區域us-west1-b
    • 內部負載平衡器producer-ilb
  7. 點選「建立」

gcloud

  1. 建立攔截部署作業群組。

    gcloud network-security intercept-deployment-groups create producer-deployment-group \
        --location=global \
        --network=projects/PROJECT_ID/global/networks/producer-network \
        --no-async
    

    PROJECT_ID 替換為專案 ID。

  2. 建立攔截部署作業。

    gcloud network-security intercept-deployments create producer-deployment \
        --location=us-west1-b \
        --forwarding-rule=producer-ilb-fr \
        --forwarding-rule-location=us-west1 \
        --intercept-deployment-group=projects/PROJECT_ID/locations/global/interceptDeploymentGroups/producer-deployment-group \
        --no-async
    

    PROJECT_ID 替換為專案 ID。

建立消費者資源

在本節中,您將為消費者建立下列資源:

  • 具有子網路的自訂虛擬私有雲網路。
  • 伺服器和用戶端 VM。
  • 防火牆政策和攔截流量的規則。
  • 攔截端點群組和攔截端點群組關聯。
  • 安全性設定檔和安全性設定檔群組。

建立自訂虛擬私有雲網路

在本節中,您將建立具有子網路的虛擬私有雲網路。

控制台

  1. 前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。

    前往「VPC networks」(虛擬私有雲網路)

  2. 按一下「建立虛擬私有雲網路」

  3. 在「Name」(名稱) 中輸入 consumer-network

  4. 在「說明」中輸入 Consumer VPC network

  5. 在「Subnets」(子網路) 區段中,執行下列操作:

    1. 將「Subnet creation mode」(子網路建立模式) 設為 [Custom] (自訂)
    2. 在「New subnet」(新的子網路) 區段中,輸入以下資訊:
      • Name (名稱):consumer-subnet
      • Region (區域):us-west1
      • IP stack type (IP 堆疊類型):IPv4 (單一堆疊)
      • IPv4 範圍10.10.0.0/16
    3. 按一下 [完成]
  6. 點選「建立」

gcloud

  1. 建立消費者虛擬私有雲網路。

    gcloud compute networks create consumer-network \
        --subnet-mode=custom \
        --description="Consumer VPC network"
    
  2. 在虛擬私有雲網路中建立子網路。

    gcloud compute networks subnets create consumer-subnet \
        --network=consumer-network \
        --region=us-west1 \
        --range=10.10.0.0/16
    

建立伺服器和用戶端 VM

在本節中,您將建立伺服器和用戶端 VM。

控制台

建立伺服器 VM

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 點選「建立執行個體」

  3. 將「Name」(名稱) 設定為 consumer-server-vm

  4. 將「區域」設為 us-west1

  5. 將「Zone」(區域) 設為 us-west1-b

  6. 將「Machine type」(機型) 設為「e2-micro」

  7. 按一下「網路」,然後設定下列欄位:

  8. 在「Network interfaces」(網路介面) 區段中,按一下 default 並編輯網路介面:

    • Network (網路):consumer-network
    • Subnet (子網路):consumer-subnet
    • 外部 IPv4 位址
  9. 按一下「進階」,並在「開機指令碼」中輸入下列指令碼:

    echo success > /tmp/connection_test && nohup python3 -u -m http.server --directory /tmp 8000
    
  10. 點選「建立」

建立用戶端 VM

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 點選「建立執行個體」

  3. 將「Name」(名稱) 設定為 consumer-client-vm

  4. 將「區域」設為 us-west1

  5. 將「Zone」(區域) 設為 us-west1-b

  6. 將「Machine type」(機型) 設為「e2-micro」

  7. 按一下「網路」,然後設定下列欄位:

  8. 在「Network interfaces」(網路介面) 區段中,按一下 default 並編輯網路介面:

    • Network (網路):consumer-network
    • Subnet (子網路):consumer-subnet
    • 外部 IPv4 位址
  9. 點選「建立」

gcloud

  1. 建立伺服器 VM。

    gcloud compute instances create consumer-server-vm \
        --image-project=debian-cloud \
        --image-family=debian-11 \
        --machine-type=e2-micro \
        --zone=us-west1-b \
        --network-interface="subnet=consumer-subnet,no-address" \
        --metadata=startup-script="echo success > /tmp/connection_test && nohup python3 -u -m http.server --directory /tmp 8000"
    
  2. 建立用戶端 VM。

    gcloud compute instances create consumer-client-vm \
        --image-project=debian-cloud \
        --image-family=debian-11 \
        --machine-type=e2-micro \
        --zone=us-west1-b \
        --network-interface="subnet=consumer-subnet,no-address"
    

建立防火牆政策,允許 SSH 存取

在本節中,您將建立防火牆政策,並新增防火牆規則,允許透過 SSH 存取取用者 VM。

控制台

  1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

    前往「Firewall policies」(防火牆政策)

  2. 按一下「建立防火牆政策」

  3. 在「Name」(名稱) 欄位中,輸入 consumer-firewall-policy

  4. 在「部署範圍」部分選取「全域」,然後按一下「繼續」

  5. 點選「建立防火牆規則」,設定下列欄位,然後點選「建立」

    • Priority (優先順序):100
    • Direction of traffic (流量方向):「Ingress」(輸入)
    • 「Action on match」(相符時執行的動作)允許
    • 「Source filters」(來源篩選器) >「IP ranges」(IP 範圍)35.235.240.0/20
    • 通訊協定和通訊埠:選取「指定的通訊協定和通訊埠」,勾選「TCP」核取方塊,然後在「通訊埠」中指定 22

    IPv4 範圍「35.235.240.0/20」包含 Identity-Aware Proxy 用於 TCP 轉送的所有 IP 位址。詳情請參閱「準備專案以進行 TCP 轉送」。

  6. 如要將政策與聯播網建立關聯,請按一下「繼續」,然後按一下「建立關聯」

  7. 勾選 consumer-network 核取方塊。

  8. 按一下「繼續」

  9. 點選「建立」

gcloud

  1. 建立全域網路防火牆政策。

    gcloud compute network-firewall-policies create consumer-firewall-policy \
        --global
    
  2. 將防火牆政策與消費者網路建立關聯。

    gcloud compute network-firewall-policies associations create \
        --name=consumer-firewall-policy-assoc \
        --firewall-policy=consumer-firewall-policy \
        --global-firewall-policy \
        --network=consumer-network
    
  3. 建立 SSH 允許規則,允許透過 Identity-Aware Proxy 與用戶端 VM 執行個體建立 SSH 連線。

    gcloud compute network-firewall-policies rules create 101 \
        --firewall-policy=consumer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=tcp:22 \
        --src-ip-ranges=35.235.240.0/20
    

    IPv4 範圍「35.235.240.0/20」包含 Identity-Aware Proxy (IAP) 用於 TCP 轉送的所有 IP 位址。詳情請參閱「準備專案以進行 TCP 轉送」。

建立消費者端點群組

在本節中,您將建立攔截端點群組和攔截端點群組關聯。

控制台

  1. 前往 Google Cloud 控制台的「Endpoint groups」(端點群組) 頁面。

    前往端點群組

  2. 按一下「建立端點群組」

  3. 在「Name」(名稱) 中輸入 consumer-endpoint-group

  4. 在「部署群組」部分,從「選取專案」選單中選取「選取目前專案」

  5. 在「Intercept deployment group」(攔截部署群組) 部分,選取 producer-deployment-group

  6. 按一下「繼續」

  7. 按一下「新增端點群組關聯」

  8. 在「Project」(專案) 部分,選取目前的專案。

  9. 在「Network」(網路) 中選取 consumer-network

  10. 點選「建立」

gcloud

  1. 建立攔截端點群組。

    gcloud network-security intercept-endpoint-groups create consumer-endpoint-group \
        --location=global \
        --intercept-deployment-group=projects/PROJECT_ID/locations/global/interceptDeploymentGroups/producer-deployment-group \
        --no-async
    

    PROJECT_ID 替換為專案 ID。

  2. 建立攔截端點群組關聯。

    gcloud network-security intercept-endpoint-group-associations create consumer-endpoint-group-association \
        --location=global \
        --intercept-endpoint-group=projects/PROJECT_ID/locations/global/interceptEndpointGroups/consumer-endpoint-group \
        --network=consumer-network \
        --no-async
    

    PROJECT_ID 替換為專案 ID。

建立安全性設定檔和安全性設定檔群組

建立安全性設定檔群組和自訂安全性設定檔,攔截流量。

控制台

建立安全性設定檔

  1. 前往 Google Cloud 控制台的「Security profiles」(安全性設定檔) 頁面。

    前往「安全性設定檔」

  2. 在專案選擇工具中選取機構。

  3. 在「安全性設定檔」分頁中,按一下「建立設定檔」

  4. 在「Name」(名稱) 中輸入 consumer-security-profile

  5. 在「安全設定檔用途」部分,選取「NSI 頻帶內」

  6. 在「Project」(專案) 部分,選取目前的專案。

  7. 在「Intercept endpoint group」(攔截端點群組) 部分,選取 consumer-endpoint-group

  8. 點選「建立」

建立安全性設定檔群組

  1. 前往 Google Cloud 控制台的「Security profile groups」(安全性設定檔群組) 頁面。

    前往「安全性設定檔群組」

  2. 在專案選擇工具中選取機構。

  3. 在「安全性設定檔群組」分頁中,按一下「建立設定檔群組」

  4. 在「Name」(名稱) 中輸入 consumer-security-profile-group

  5. 在「Security profile group purpose」部分,選取「NSI in-band」

  6. 在「自訂攔截設定檔」部分,選取 consumer-security-profile

  7. 點選「建立」

gcloud

  1. 建立攔截安全性設定檔。

    gcloud network-security security-profiles custom-intercept create consumer-security-profile \
        --location=global \
        --organization=ORG_ID \
        --intercept-endpoint-group=projects/PROJECT_ID/locations/global/interceptEndpointGroups/consumer-endpoint-group \
        --no-async
    

    更改下列內容:

    • ORG_ID:貴機構的 ID。 安全性設定檔是組織層級的資源。如要建立這些設定,您必須具備組織層級的安全性設定檔管理員角色 (networksecurity.securityProfileAdmin)。
    • PROJECT_ID:專案 ID。
  2. 建立攔截安全性設定檔群組。

    gcloud network-security security-profile-groups create consumer-security-profile-group \
        --location=global \
        --organization=ORG_ID \
        --custom-intercept-profile=consumer-security-profile \
        --billing-project=PROJECT_ID \
        --no-async
    

    更改下列內容:

    • ORG_ID:貴機構的 ID。 安全性設定檔群組是組織層級的資源。如要建立這些設定檔,您必須具備機構層級的安全性設定檔管理員角色 (networksecurity.securityProfileAdmin)。
    • PROJECT_ID:專案 ID。

建立防火牆政策來攔截流量

在本節中,您將建立防火牆政策和防火牆規則,以攔截流量。

控制台

  1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

    前往「Firewall policies」(防火牆政策)

  2. 按一下「consumer-firewall-policy」。

  3. 點選「建立防火牆規則」,並設定下列欄位:

    • Priority (優先順序):100
    • Direction of traffic (流量方向):「Ingress」(輸入)
    • 相符時執行的動作進行 L7 檢查
    • 目的NSI 頻內
    • 安全性設定檔群組consumer-security-profile-group
    • IP 範圍10.10.0.0/16
    • 通訊協定和通訊埠:選取「指定的通訊協定和通訊埠」,勾選「TCP」核取方塊,然後在「通訊埠」中指定 8000
  4. 點選「建立」

gcloud

  1. 新增防火牆規則,攔截伺服器 VM 上 TCP 通訊埠 8000 的流量。

    gcloud compute network-firewall-policies rules create 100 \
        --firewall-policy=consumer-firewall-policy \
        --global-firewall-policy \
        --action=apply_security_profile_group \
        --security-profile-group=organizations/ORG_ID/locations/global/securityProfileGroups/consumer-security-profile-group \
        --direction=INGRESS \
        --layer4-configs=tcp:8000 \
        --src-ip-ranges=10.10.0.0/16
    

    ORG_ID 替換為您的機構 ID。

測試連線

在本節中,您會將一些網路流量從消費者端用戶端 VM 執行個體傳送至消費者端伺服器 VM 執行個體,然後檢查生產者端 VM 執行個體的記錄,確認流量是否遭到攔截。

  1. 執行下列指令,透過 SSH 連線至消費者端用戶端 VM 執行個體,並將要求傳送至消費者端伺服器 VM 執行個體。

    gcloud compute ssh consumer-client-vm \
        --tunnel-through-iap \
        --zone=us-west1-b \
        --command="curl -m 3 -s http://consumer-server-vm:8000/connection_test || echo fail"
    

    執行先前的指令時,你會看到 success 訊息。 表示流量是從用戶端傳送至伺服器。

  2. 執行下列指令,檢查 Producer VM 執行個體的記錄。

    gcloud compute ssh producer-instance \
        --tunnel-through-iap \
        --zone=us-west1-b \
        --command="cat /var/log/syslog | grep 'NSI INTERCEPT'"
    

    您會看到類似 [NSI INTERCEPT] IN=ens4 OUT= MAC=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx SRC=10.10.0.1 DST=10.10.0.2 LEN=136 TOS=0x00 PREC=0x00 TTL=64 ID=0 DF PROTO=UDP SPT=45554 DPT=6081 LEN=116 的訊息。這表示用戶端與伺服器之間的流量遭到生產者 VM 執行個體攔截。

清除所用資源

為避免因為本教學課程所用資源,導致系統向 Google Cloud 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。

刪除消費者資源

  1. 將防火牆政策與取用者網路取消關聯,然後刪除防火牆政策。

    控制台

    1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

      前往「Firewall policies」(防火牆政策)

    2. 按一下 consumer-firewall-policy 政策。

    3. 按一下「關聯項目」分頁標籤。

    4. 勾選「consumer-network 關聯」核取方塊。

    5. 按一下「移除關聯」。

    6. 按一下 [移除]

    7. 按一下頁面頂端的 [Delete] (刪除)

    8. 再按一下 [刪除] 加以確認。

    gcloud

    1. 移除關聯。

      gcloud compute network-firewall-policies associations delete \
          --name=consumer-firewall-policy-assoc \
          --firewall-policy=consumer-firewall-policy \
          --global-firewall-policy
      
    2. 刪除防火牆政策。

      gcloud compute network-firewall-policies delete consumer-firewall-policy \
          --global
      
  2. 刪除安全性設定檔群組。

    控制台

    1. 前往 Google Cloud 控制台的「Security profiles」(安全性設定檔) 頁面。

      前往「安全性設定檔」

    2. 在專案選擇工具中選取機構。

    3. 在「安全性設定檔群組」分頁中,選取要刪除的consumer-security-profile-group安全性設定檔群組,然後按一下「刪除」

    4. 再按一下 [刪除] 加以確認。

    gcloud

    gcloud network-security security-profile-groups delete consumer-security-profile-group \
        --location=global \
        --organization=ORG_ID \
        --billing-project=PROJECT_ID \
        --quiet
    

    更改下列內容:

    • ORG_ID:貴機構的 ID
    • PROJECT_ID:專案 ID
  3. 刪除安全性設定檔。

    控制台

    1. 前往 Google Cloud 控制台的「Security profiles」(安全性設定檔) 頁面。

      前往「安全性設定檔」

    2. 在專案選擇工具中選取機構。

    3. 在「安全性設定檔」分頁中,選取 consumer-security-profile 安全性設定檔,然後按一下「刪除」

    4. 再按一下 [刪除] 加以確認。

    gcloud

    gcloud network-security security-profiles custom-intercept delete consumer-security-profile \
        --location=global \
        --organization=ORG_ID \
        --billing-project=PROJECT_ID \
        --quiet
    

    更改下列內容:

    • ORG_ID:貴機構的 ID
    • PROJECT_ID:專案 ID
  4. 刪除攔截端點群組關聯。

    控制台

    1. 前往 Google Cloud 控制台的「Endpoint groups」(端點群組) 頁面。

      前往端點群組

    2. 按一下「consumer-endpoint-group」。

    3. 選取 consumer-network 關聯,然後按一下「刪除」

    4. 再按一下 [刪除] 加以確認。

    gcloud

    gcloud network-security intercept-endpoint-group-associations delete consumer-endpoint-group-association \
        --location=global \
        --no-async
    
  5. 刪除攔截端點群組。

    控制台

    1. 前往 Google Cloud 控制台的「Endpoint groups」(端點群組) 頁面。

      前往端點群組

    2. 選取 consumer-endpoint-group,然後按一下「刪除」

    3. 再按一下 [刪除] 加以確認。

    gcloud

    gcloud network-security intercept-endpoint-groups delete consumer-endpoint-group \
        --location=global \
        --no-async
    
  6. 刪除伺服器和用戶端 VM。

    控制台

    1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

      前往 VM 執行個體

    2. 勾選 consumer-client-vmconsumer-server-vm 的核取方塊,然後按一下「刪除」

    3. 再按一下 [刪除] 加以確認。

    gcloud

    1. 刪除用戶端 VM。

      gcloud compute instances delete consumer-client-vm \
          --zone=us-west1-b \
          --quiet
      
    2. 刪除伺服器 VM。

      gcloud compute instances delete consumer-server-vm \
          --zone=us-west1-b \
          --quiet
      
  7. 刪除消費者網路和子網路。

    控制台

    1. 前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。

      前往「VPC networks」(虛擬私有雲網路)

    2. 按一下「consumer-network」。

    3. 在「子網路」分頁中,選取 consumer-subnet 的核取方塊,然後按一下「刪除」

    4. 再按一下 [刪除] 加以確認。

    5. 按一下「刪除虛擬私有雲網路」

    6. 在文字欄位中輸入 consumer-network,然後按一下「刪除」

    gcloud

    1. 刪除子網路。

      gcloud compute networks subnets delete consumer-subnet \
          --region=us-west1 \
          --quiet
      
    2. 刪除虛擬私有雲網路。

      gcloud compute networks delete consumer-network \
          --quiet
      

刪除製作人資源

  1. 將防火牆政策與生產者網路取消關聯,然後刪除防火牆政策。

    控制台

    1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

      前往「Firewall policies」(防火牆政策)

    2. 按一下 producer-firewall-policy 政策。

    3. 按一下「關聯項目」分頁標籤。

    4. 勾選「producer-network 關聯」核取方塊。

    5. 按一下「移除關聯」。

    6. 按一下 [移除]

    7. 按一下頁面頂端的 [Delete] (刪除)

    8. 再按一下 [刪除] 加以確認。

    gcloud

    1. 移除關聯。

      gcloud compute network-firewall-policies associations delete \
          --name=producer-firewall-policy-assoc \
          --firewall-policy=producer-firewall-policy \
          --global-firewall-policy
      
    2. 刪除防火牆政策。

      gcloud compute network-firewall-policies delete producer-firewall-policy \
          --global
      
  2. 刪除攔截部署作業和攔截部署作業群組。

    控制台

    1. 前往 Google Cloud 控制台的「Deployment groups」(部署群組) 頁面。

      前往「Deployment groups」(部署群組)

    2. 按一下「producer-deployment-group」。

    3. 刪除 producer-deployment

      1. 選取 producer-deployment 核取方塊,然後按一下「刪除」
      2. 再按一下 [刪除] 加以確認。
    4. 點選頁面頂端的 [Delete] (刪除) 按鈕。

    5. 再按一下 [刪除] 加以確認。

    gcloud

    1. 刪除攔截部署項目。

      gcloud network-security intercept-deployments delete producer-deployment \
          --location=us-west1-b \
          --no-async
      
    2. 刪除攔截部署作業群組。

      gcloud network-security intercept-deployment-groups delete producer-deployment-group \
          --location=global \
          --no-async
      
  3. 刪除負載平衡器資源。

    控制台

    1. 前往 Google Cloud 控制台的「Load balancing」(負載平衡) 頁面。

      前往「Load balancing」(負載平衡)

    2. 勾選 producer-ilb 核取方塊。

    3. 點選頁面頂端的 [Delete] (刪除) 按鈕。

    4. 選取 producer-health-check 核取方塊,然後按一下「Delete load balancer and the selected resources」(刪除負載平衡器和所選資源)

    gcloud

    1. 刪除轉送規則。

      gcloud compute forwarding-rules delete producer-ilb-fr \
          --region=us-west1 \
          --quiet
      
    2. 移除後端服務。

      gcloud compute backend-services delete producer-backend-service \
          --region=us-west1 \
          --quiet
      
    3. 刪除健康狀態檢查。

      gcloud compute health-checks delete producer-health-check \
          --region=us-west1 \
          --quiet
      
  4. 刪除 Producer VM 執行個體。

    控制台

    1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

      前往 VM 執行個體

    2. 勾選 producer-instance 的核取方塊,然後點選「Delete」(刪除)

    3. 再按一下 [刪除] 加以確認。

    gcloud

    gcloud compute instances delete producer-instance \
        --zone=us-west1-b \
        --quiet
    
  5. 刪除執行個體群組。

    控制台

    1. 前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。

      前往「Instance groups」(執行個體群組)

    2. 勾選 producer-instance-group 核取方塊。

    3. 點選「刪除」。

    4. 按一下確認視窗中的 [Delete]

    gcloud

    gcloud compute instance-groups unmanaged delete producer-instance-group \
        --zone=us-west1-b \
        --quiet
    
  6. 刪除製作人網路和子網路。

    控制台

    1. 前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。

      前往「VPC networks」(虛擬私有雲網路)

    2. 按一下「producer-network」。

    3. 在「子網路」分頁中,選取 producer-subnet 的核取方塊,然後按一下「刪除」

    4. 再按一下 [刪除] 加以確認。

    5. 按一下「刪除虛擬私有雲網路」

    6. 在文字欄位中輸入 producer-network,然後按一下「刪除」

    gcloud

    1. 刪除子網路。

      gcloud compute networks subnets delete producer-subnet \
          --region=us-west1 \
          --quiet
      
    2. 刪除網路。

      gcloud compute networks delete producer-network \
          --quiet
      

後續步驟