將 Secure Web Proxy 部署為明確 Proxy

本快速入門導覽課程說明如何部署及測試 Secure Web Proxy 執行個體。這些步驟說明如何在明確轉送模式下部署 Secure Web Proxy,做為明確 Proxy。

在明確轉送模式下,Secure Web Proxy 執行個體可以部署為 Private Service Connect 服務。或者,您也可以在下一個躍點轉送模式中部署 Secure Web Proxy。

以明確轉送模式部署 Secure Web Proxy 的步驟如下:

  1. 建立 Secure Web Proxy 政策。
  2. 建立 Secure Web Proxy 規則,並新增至政策。
  3. 在明確路由模式下設定 Secure Web Proxy 執行個體。
  4. 從 Linux 虛擬機器 (VM) 執行個體測試連線能力,確認 Proxy 和政策運作正常。
以明確 Proxy 轉送模式部署 Secure Web Proxy。
以明確 Proxy 轉送模式部署 Secure Web Proxy (按一下可放大)。

事前準備

  1. 完成初始設定步驟。

  2. 選用:如要執行本文件指定的 gcloud 指令列範例,請在下列任一開發環境中安裝 Google Cloud CLI:

    Cloud Shell

    如要使用已設定 gcloud CLI 的線上終端機,請啟用 Cloud Shell。

    頁面底部會開啟 Cloud Shell 工作階段,並顯示指令列提示。工作階段可能要幾秒鐘的時間才能初始化。

    如果您先前已安裝 gcloud CLI,請執行 gcloud components update 指令,確認您使用的是最新版本。

    本機殼層

    如要使用本機開發環境,請按照下列步驟操作:

    1. 安裝 Google Cloud CLI
    2. 初始化 gcloud CLI
  3. 建立或選取現有 Google Cloud 專案。

    如果您不打算保留在這項程序中建立的資源,建議您建立新專案,而不要選取現有專案。完成本快速入門導覽課程的步驟後,您可以刪除專案,移除與專案相關聯的所有資源。

    控制台

    在 Google Cloud 控制台的專案選取器頁面中,建立 Google Cloud 專案或選取現有專案。

    前往專案選取器

    Cloud Shell

    1. 建立 Google Cloud 專案。

      gcloud projects create PROJECT_ID
      

      將 PROJECT_ID 替換為您選擇的專案 ID。

    2. 選取您建立的 Google Cloud 專案。

      gcloud config set project PROJECT_ID
      
  4. 建立 Linux VM 執行個體。

    gcloud compute instances create swp-test-vm \
        --subnet=default \
        --zone=ZONE \
        --image-project=debian-cloud \
        --image-family=debian-11
    

    將 ZONE 替換為測試 VM 執行個體的可用區。

    Compute Engine 會將 Compute 執行個體管理員角色 (roles/compute.instanceAdmin) 授予建立 VM 執行個體的使用者。Compute Engine 也會將該使用者新增至 sudo 群組。

  5. 建立防火牆規則。

    gcloud compute firewall-rules create default-allow-ssh \
        --direction=INGRESS \
        --priority=1000 \
        --network=default \
        --action=ALLOW \
        --rules=tcp:22 \
        --source-ranges=0.0.0.0/0
    

必要的角色

請確認您具備下列角色,才能完成本教學課程中的工作:

如要瞭解詳情,請參考下列資源:

建立 Secure Web Proxy 政策

本節說明建立 Secure Web Proxy 政策的步驟。接著,這項政策可做為容器,用於管理通過 Secure Web Proxy 的流量的所有安全性規則。建立政策後,您可以建立規則並新增至政策。

控制台

  1. 前往 Google Cloud 控制台的「SWP Policies」(SWP 政策) 頁面。

    前往「SWP Policies」(SWP 政策) 頁面

  2. 按一下「 建立政策」。

  3. 在「Name」(名稱) 欄位中,輸入政策名稱,例如 myswppolicy。

  4. 在「Description」(說明) 欄位中輸入政策說明,例如 My new swp policy。

  5. 在「Regions」(區域) 部分,選取要建立政策的區域,例如 us-central1。

  6. 如要為政策建立規則,請按一下「Add rule」(新增規則)。詳情請參閱「建立 Secure Web Proxy 規則」一節。

  7. 點選「建立」。

Cloud Shell

  1. 使用文字編輯器建立 policy.yaml 檔案。

  2. 在 policy.yaml 檔案中新增下列程式碼:

      description: basic Secure Web Proxy policy
      name: projects/PROJECT_ID/locations/REGION/gatewaySecurityPolicies/myswppolicy
    

    更改下列內容:

    • PROJECT_ID:專案 ID
    • REGION:建立政策的區域,例如 us-central1
  3. 建立 Secure Web Proxy 政策。

    gcloud network-security gateway-security-policies import myswppolicy \
        --source=policy.yaml \
        --location=REGION
    

建立 Secure Web Proxy 規則

本節詳細說明建立 Secure Web Proxy 規則的步驟。規則可協助定義網站流量的實際存取控管機制。

在先前建立的政策中,您可以定義 allow-wikipedia-org 等規則。這些規則會指定比對網路工作階段的條件 (例如比對 www.wikipedia.org 等特定主機),然後允許或拒絕網站流量。

控制台

  1. 前往 Google Cloud 控制台的「SWP Policies」(SWP 政策) 頁面。

    前往「SWP Policies」(SWP 政策) 頁面

  2. 按一下政策名稱,例如 myswppolicy。

  3. 按一下「 新增規則」。

  4. 針對每項規則,執行下列操作:

    1. 在「Priority」(優先順序) 部分,輸入規則的評估順序 (數字)。系統會從最高到最低優先順序評估規則,其中 0 是最高優先順序。

    2. 在「Name」(名稱) 欄位中輸入規則名稱,例如 allow-wikipedia-org。

    3. 在「Description」(說明) 欄位中輸入規則說明,例如 Allow wikipedia.org。

    4. 在「動作」部分,選取下列任一選項:

      • 允許:允許符合規則的連線要求。
      • 拒絕:拒絕符合規則的連線要求。
    5. 在「Status」(狀態) 欄位中,選取下列其中一個規則強制執行選項:

      • 啟用:在 Secure Web Proxy 執行個體上強制執行規則。
      • 停用:不對 Secure Web Proxy 執行個體強制執行規則。
    6. 在「Session Match」(工作階段比對) 部分,指定比對工作階段的條件,例如 host() == 'www.wikipedia.org'。

      如要進一步瞭解 SessionMatcher 的語法,請參閱「CEL 比對器語言參考資料」。

    7. 在「Application Match」(應用程式比對) 部分,指定比對要求的條件。由於我們尚未啟用 TLS 檢查規則,因此要求只能比對 HTTP 流量。

      如要進一步瞭解如何比對 TCP 流量,請參閱「設定 TCP Proxy 規則」。

    8. 點選「建立」。

  5. 如要新增其他規則,請按一下「新增規則」。

Cloud Shell

  1. 使用文字編輯器建立 rule.yaml 檔案。

  2. 將下列程式碼新增至 rule.yaml 檔案。如要進一步瞭解 SessionMatcher 的語法,請參閱「CEL 比對器語言參考資料」。

    name: projects/PROJECT_ID/locations/REGION/gatewaySecurityPolicies/policy1/rules/RULE_NAME
    description: Allow wikipedia.org
    enabled: true
    priority: 1
    basicProfile: ALLOW
    sessionMatcher: host() == 'www.wikipedia.org'
    

    更改下列內容:

    • PROJECT_ID:專案 ID
    • REGION:政策的區域
    • RULE_NAME:規則名稱,例如 allow-wikipedia-org。
  3. 建立安全性政策規則。

    gcloud network-security gateway-security-policies rules import allow-wikipedia-org \
        --source=rule.yaml \
        --location=REGION \
        --gateway-security-policy=policy1
    

設定網路 Proxy

本節說明如何以明確轉送模式部署 Secure Web Proxy,做為明確 Proxy。此外,您也會瞭解如何將先前建立的政策和規則與 Secure Web Proxy 執行個體建立關聯,確保所有通過該執行個體的流量都受到定義的存取控管機制規範。

控制台

  1. 前往 Google Cloud 控制台的「Web Proxies」(網路 Proxy) 頁面。

    前往「Web Proxies」(網路 Proxy)

  2. 按一下「 建立安全網路 Proxy」。

  3. 在「Name」(名稱) 欄位中,輸入網路 Proxy 的名稱,例如 myswp。

  4. 在「Description」(說明) 欄位中,輸入網路 Proxy 的說明,例如 My new swp。

  5. 在「轉送模式」部分,選取「明確」選項。

  6. 在「Regions」(區域) 部分,選取要建立網路 Proxy 的區域,例如 us-central1。

  7. 在「Network」(網路) 部分,選取要建立網路 Proxy 的網路。

  8. 在「Subnetwork」(子網路) 部分,選取您先前在初始設定步驟中建立的虛擬私有雲子網路。

  9. 選用:在「Web proxy IP address」(網路 Proxy IP 位址) 欄位中,輸入 Secure Web Proxy IP 位址。

    您可以輸入安全網路 Proxy IP 位址範圍內的 IP 位址,該範圍位於您在上一個步驟中建立的子網路。如未輸入 IP 位址,Secure Web Proxy 執行個體會自動從所選子網路中選擇 IP 位址。

  10. 在「Ports」(通訊埠) 中,輸入您要網路 Proxy 監聽的通訊埠編號,例如 80 或 443。

  1. 在「Certificate」(憑證) 部分,選取要用來建立網路 Proxy 的憑證。

  2. 在「Associated policy」(相關聯政策) 部分,選取您建立的政策,將網路 Proxy 與該政策建立關聯。

  3. 點選「建立」。

Cloud Shell

  1. 使用文字編輯器建立 gateway.yaml 檔案。

  2. 在 gateway.yaml 檔案中新增下列程式碼:

    name: projects/PROJECT_ID/locations/REGION/gateways/myswp
    type: SECURE_WEB_GATEWAY
    addresses: ["IP_ADDRESS"]
    ports: [443]
    gatewaySecurityPolicy: projects/PROJECT_ID/locations/REGION/gatewaySecurityPolicies/policy1
    network: projects/PROJECT_ID/global/networks/NETWORK
    subnetwork: projects/PROJECT_ID/regions/REGION/subnetworks/SUBNETWORK
    routingMode: EXPLICIT_ROUTING_MODE
    

    選用:如要允許其他 Google Cloud區域的用戶端應用程式連線至 Proxy,請在建立 Secure Web Proxy 執行個體時啟用全域存取權。

    在 gateway.yaml 檔案中,新增 allowGlobalAccess 欄位並設為 true。

    重要事項:只有在建立 Secure Web Proxy 執行個體時,才能啟用 allowGlobalAccess。這項設定之後無法調整。

    name: projects/PROJECT_ID/locations/REGION/gateways/swp1
    type: SECURE_WEB_GATEWAY
    addresses: ["IP_ADDRESS"]
    ports: [443]
    gatewaySecurityPolicy: projects/PROJECT_ID/locations/REGION/gatewaySecurityPolicies/policy1
    network: projects/PROJECT_ID/global/networks/NETWORK
    subnetwork: projects/PROJECT_ID/regions/REGION/subnetworks/SUBNETWORK
    routingMode: EXPLICIT_ROUTING_MODE
    allowGlobalAccess: true
    

    更改下列內容:

    • PROJECT_ID:專案 ID
    • REGION:Secure Web Proxy 執行個體的建立區域
    • IP_ADDRESS:Secure Web Proxy 執行個體的 IP 位址
    • NETWORK:Secure Web Proxy 執行個體的網路
    • SUBNETWORK:Secure Web Proxy 執行個體的子網路。您必須使用先前在初始設定步驟中建立的虛擬私有雲子網路。
  3. 根據 gateway.yaml 檔案建立 Secure Web Proxy 執行個體。

    gcloud network-services gateways import myswp \
        --source=gateway.yaml \
        --location=REGION
    

    Secure Web Proxy 執行個體可能需要幾分鐘才能部署完成。

測試連線能力

本節重點在於測試 Linux VM 的連線能力,確認新部署的 Secure Web Proxy、相關政策和規則運作正常。

  1. 連線至先前佈建的 VM。

    gcloud compute ssh swp-test-vm \
        --zone=ZONE
    

    將 ZONE 替換為測試 VM 執行個體的可用區。

  2. 測試 Secure Web Proxy 執行個體。

    curl -s -o /dev/null -w "%{http_code}\\n" -x IP_ADDRESS:443 https://www.wikipedia.org
    

    將 IP_ADDRESS 替換為 Secure Web Proxy 執行個體的 IP 位址。

    這個指令會輸出從 www.wikipedia.org 傳回的 HTTP 狀態碼。如果指令成功,狀態碼會是 200。不過,如果 Proxy 有問題,指令會傳回 000 狀態碼,表示連線錯誤。如要查看詳細錯誤訊息,請在指令中加入 -v 選項。

我們並未在本頁提及的範例中設定 TLS 檢查。如要進一步瞭解如何設定 Secure Web Proxy 以進行 TLS 檢查,然後測試 Secure Web Proxy 執行個體,請參閱「啟用 TLS 檢查」。

清除所用資源

為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。

刪除 swp1 Secure Web Proxy 執行個體

控制台

  1. 前往 Google Cloud 控制台的「Web Proxies」(網路 Proxy) 頁面。您可以查看所有網路 Proxy 的清單,或只查看特定網路中可用的網路 Proxy。

    前往「Web Proxies」(網路 Proxy)

  2. 選取要刪除的網路 Proxy。這裡的網路 Proxy 名稱為 swp1。

  3. 點選「刪除」,然後再次點按「刪除」確認操作。

Cloud Shell

gcloud network-services gateways delete swp1 \
    --location=REGION

將 REGION 替換為 Secure Web Proxy 執行個體的區域。

刪除 Cloud NAT 閘道

控制台

  1. 前往 Google Cloud 控制台的「Cloud NAT」頁面。

    前往 Cloud NAT

  2. 選取為 Secure Web Proxy 執行個體自動生成的 Cloud NAT 閘道。Cloud NAT 閘道通常命名為 swg-autogen-nat。

  3. 點選「刪除」,然後再次點按「刪除」確認操作。

Cloud Shell

  1. 選用:如要找出自動生成的 Cloud Router 名稱,請列出區域中的路由器。

    gcloud compute routers list \
        --region=REGION \
        --filter="name:(swg-autogen-router-*)" \
        --format="value(name)"
    

    將 REGION 替換為 Secure Web Proxy 執行個體的區域。

  2. 刪除 Cloud NAT 閘道。

    gcloud compute routers nats delete NAT_NAME \
        --router=ROUTER_NAME \
        --region=REGION
    

    更改下列內容:

    • NAT_NAME:Cloud NAT 閘道的名稱,通常是 swg-autogen-nat
    • ROUTER_NAME:與 Cloud NAT 閘道相關聯的 Cloud Router 名稱。路由器名稱通常開頭為 swg-autogen-router-。
    • REGION:Cloud NAT 閘道所在的區域

刪除 Cloud Router

控制台

  1. 前往 Google Cloud 控制台的「Cloud Router」頁面。

    前往 Cloud Router

  2. 選取為 Secure Web Proxy 執行個體自動生成的 Cloud Router。路由器名稱通常以 swg-autogen-router- 開頭。

  3. 點選「刪除」,然後再次點按「刪除」確認操作。

Cloud Shell

gcloud compute routers delete ROUTER_NAME \
    --region=REGION

更改下列內容:

  • ROUTER_NAME:Cloud Router 的名稱
  • REGION:Cloud Router 的區域

刪除 allow-wikipedia-org 規則

控制台

  1. 前往 Google Cloud 控制台的「Web Proxies」(網路 Proxy) 頁面。您可以查看所有網路 Proxy 的清單,或只查看特定網路中可用的網路 Proxy。

    前往「Web Proxies」(網路 Proxy)

  2. 按一下政策。在本例中,政策名稱為 policy1。

  3. 選取要刪除的規則。這裡的規則名稱是 allow-wikipedia-org。

  4. 點選「刪除」,然後再次點按「刪除」確認操作。

Cloud Shell

gcloud network-security gateway-security-policies rules delete allow-wikipedia-org \
    --location=REGION \
    --gateway-security-policy=policy1

將 REGION 替換為政策的區域。

刪除 policy1 Secure Web Proxy 政策

控制台

  1. 前往 Google Cloud 控制台的「Web Proxies」(網路 Proxy) 頁面。您可以查看所有網路 Proxy 的清單,或只查看特定網路中可用的網路 Proxy。

    前往「Web Proxies」(網路 Proxy)

  2. 選取要刪除的政策。這裡的政策名稱是 policy1。

  3. 點選「刪除」,然後再次點按「刪除」確認操作。

Cloud Shell

gcloud network-security gateway-security-policies delete policy1 \
    --location=REGION

將 REGION 替換為政策的區域。

刪除 swp-test-vm Linux VM 執行個體

控制台

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

    前往 VM 執行個體

  2. 選取要刪除的執行個體。其中 swp-test-vm 是 Linux VM 執行個體的名稱。

  3. 點選「刪除」。

Cloud Shell

gcloud compute instances delete swp-test-vm

後續步驟