為虛擬私有雲網路設定 VPC Service Controls perimeter

瞭解如何使用 VPC Service Controls 設定 service perimeter。本教學課程會使用防火牆、Private Service Connect 和 DNS 設定等網路設定,這些設定是有效使用 VPC Service Controls perimeter 的必要條件。本教學課程接著會示範如何允許或拒絕服務,以及如何為特定服務的許可清單建立精細的例外狀況。

目標

  • 設定 VPC Service Controls perimeter,並搭配額外的網路控制項,減少資料竊取路徑。
  • 允許或拒絕來自 perimeter 內部或外部的要求,存取 perimeter 內部的服務。
  • 允許或拒絕來自 perimeter 內部的要求,存取 perimeter 外部的服務。
  • 同時使用「限制資源服務使用情形」組織政策和 VPC Service Controls。

費用

本教學課程使用下列 Google Cloud計費元件:

您可以使用 Pricing Calculator 根據預測用量估算費用。

完成本教學課程後,您可以刪除建立的資源以避免繼續計費。詳情請參閱「清除所用資源」一節。

事前準備

  1. 本教學課程需要貴組織的專案。如果還沒有 Google Cloud 組織,請參閱「建立及管理組織」一文。

  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 (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. Enable the Compute Engine, Access Context Manager, and Cloud DNS APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

  6. Make sure that you have the following role or roles on the organization: Access Context Manager Admin, Organization Policy Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the organization.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往 IAM

    2. 選取所需組織。
    3. 點按「授予存取權」圖示
    4. 在「New principals」(新主體) 欄位中,輸入您的使用者 ID。 這通常是指 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「Add another role」(新增其他角色),然後新增其他角色。
    7. 按一下「Save」(儲存)
    8. Make sure that you have the following role or roles on the project: Compute Admin, DNS Administrator, IAP-Secured Tunnel User, Service Account User, Service Directory Editor

      Check for the roles

      1. In the Google Cloud console, go to the IAM page.

        Go to IAM
      2. Select the project.
      3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

      4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

      Grant the roles

      1. In the Google Cloud console, go to the IAM page.

        前往 IAM

      2. 選取所需專案。
      3. 點按「授予存取權」圖示
      4. 在「New principals」(新主體) 欄位中,輸入您的使用者 ID。 這通常是指 Google 帳戶的電子郵件地址。

      5. 在「Select a role」(選取角色) 清單中,選取角色。
      6. 如要授予其他角色,請按一下 「Add another role」(新增其他角色),然後新增其他角色。
      7. 按一下「Save」(儲存)
      8. 設定 VPC Service Controls perimeter

        如要為虛擬私有雲網路實作 VPC Service Controls perimeter,您必須實作網路控制項,拒絕流向外部服務的流量。以下各節將詳細說明您必須在 perimeter 內的虛擬私有雲網路中實作的網路設定,以及 perimeter 設定範例。

        準備虛擬私有雲網路

        在本節中,您將設定虛擬私有雲網路的 Google API 和服務非公開連線,以減少網際網路的網路輸出路徑。

        1. 在 Cloud Shell 中設定變數:

          gcloud config set project PROJECT_ID
          gcloud config set compute/region REGION
          gcloud config set compute/zone ZONE

          更改下列內容:

          • PROJECT_ID:您要在當中建立資源的專案 ID
          • REGION:靠近您所在位置的地區,例如 us-central1
          • ZONE:靠近您所在位置的區域,例如 us-central1-a
        2. 建立虛擬私有雲網路和子網路,並啟用 Private Google Access:

          gcloud compute networks create restricted-vpc --subnet-mode=custom
          gcloud compute networks subnets create restricted-subnet \
          --range=10.0.0.0/24 \
          --network=restricted-vpc \
          --enable-private-ip-google-access
        3. 建立 Private Service Connect 端點和轉送規則,並設為使用 vpc-sc 套件

          gcloud compute addresses create restricted-psc-endpoint \
          --global \
          --purpose=PRIVATE_SERVICE_CONNECT \
          --addresses=10.0.1.1 \
          --network=restricted-vpc
          
          gcloud compute forwarding-rules create restrictedpsc \
          --global \
          --network=restricted-vpc \
          --address=restricted-psc-endpoint \
          --target-google-apis-bundle=vpc-sc
        4. 設定 Cloud DNS 伺服器政策,將 Google Cloud API 的查詢重新導向至 Private Service Connect 端點:

          gcloud dns managed-zones create restricted-dns-zone \
            --description="Private DNS Zone to map Google API queries to the Private Service Connect endpoint for Google APIs" \
            --dns-name="googleapis.com." \
            --networks=restricted-vpc \
            --visibility=private
          
          gcloud dns record-sets create googleapis.com  \
          --rrdatas=10.0.1.1 \
          --type=A \
          --ttl=300 \
          --zone=restricted-dns-zone
          
          gcloud dns record-sets create *.googleapis.com  \
          --rrdatas="googleapis.com." \
          --type=CNAME \
          --ttl=300 \
          --zone=restricted-dns-zone
        5. 設定低優先順序的防火牆規則,拒絕所有輸出流量:

          gcloud compute firewall-rules create deny-all-egress \
          --priority=65534 \
          --direction=egress \
          --network=restricted-vpc \
          --action=DENY \
          --rules=all \
          --destination-ranges=0.0.0.0/0
        6. 設定優先順序較高的防火牆規則,允許流量導向 Private Service Connect 端點使用的 IP 位址:

          gcloud compute firewall-rules create allow-psc-for-google-apis \
          --priority=1000 \
          --direction=egress \
          --network=restricted-vpc \
          --action=ALLOW \
          --rules=tcp:443 \
          --destination-ranges=10.0.1.1

          這些防火牆規則會廣泛拒絕輸出流量,然後選擇性地允許輸出至 Private Service Connect 端點。這項設定會拒絕導向預設網域的輸出流量。根據預設,這些網域通常可透過 Private Google Access 和隱含防火牆規則連線。

        建立 VPC Service Controls perimeter

        在本節中,您將建立 VPC Service Controls perimeter。

        1. 在 Cloud Shell 中建立存取權政策,這是建立 VPC Service Controls perimeter 的必要條件:

          gcloud access-context-manager policies create \
          --organization=ORGANIZATION_ID --title "Access policy at organization node"

          輸出結果會與下列內容相似:

          "Create request issued
          Waiting for operation [operations/accessPolicies/123456789/create/123456789] to complete...done."
          

          組織節點只能有一個存取權政策容器。如果貴組織已建立政策,輸出結果會與下列內容相似:

          "ALREADY_EXISTS: Policy already exists with parent ContainerKey{containerId=organizations/123456789012, numericId=123456789012}"
          

          如果看到這則訊息,請繼續下一個步驟。

        2. 建立 VPC Service Controls perimeter,限制 Cloud Storage 和 Compute Engine 服務。

          export POLICY_ID=$(gcloud access-context-manager policies list \
          --organization=ORGANIZATION_ID \
          --format="value(name)")
          
          gcloud access-context-manager perimeters create demo_perimeter \
          --title="demo_perimeter" \
          --resources=projects/$(gcloud projects describe PROJECT_ID --format="value(projectNumber)") \
          --restricted-services="storage.googleapis.com,compute.googleapis.com" \
          --enable-vpc-accessible-services \
          --policy=$POLICY_ID \
          --vpc-allowed-services="RESTRICTED-SERVICES"

        確認允許從 perimeter 外部流量存取的服務

        下列各節將說明 VPC Service Controls perimeter 如何允許或拒絕 perimeter 外的存取要求,以及如何透過設定存取層級和輸入政策,選擇性允許服務輸入。

        如要模擬來自 perimeter 外部的流量,請在 Cloud Shell 中執行指令。Cloud Shell 是專案和 perimeter 以外的資源。即使要求具備足夠的 Identity and Access Management 權限,perimeter 仍可允許或拒絕要求。

        本教學課程使用 Compute Engine API、Cloud Storage API 和 Cloud Resource Manager API,但同樣的概念也適用於其他服務。

        確認 perimeter 拒絕受限服務的外部流量

        在本節中,您將驗證 perimeter 是否拒絕受限服務的外部流量。

        架構圖:VPC Service Controls perimeter 如何拒絕存取受限制的服務

        上圖說明已授權的用戶端如何遭拒存取您設為受限服務 perimeter 內的服務,但可存取您未設定為受限的服務。

        在下列步驟中,您將使用 Cloud Shell 嘗試在虛擬私有雲網路中建立 VM,但由於 VPC Service Controls perimeter 的設定,因此會失敗,藉此驗證這個概念。

        1. 在 Cloud Shell 中執行下列指令,在虛擬私有雲網路內建立 VM。

          gcloud compute instances create demo-vm \
              --machine-type=e2-micro \
              --subnet=restricted-subnet \
              --scopes=https://www.googleapis.com/auth/cloud-platform \
              --no-address

          輸出結果會與下列內容相似:

          "ERROR: (gcloud.compute.instances.create) Could not fetch resource:
          - Request is prohibited by organization's policy."
          

          要求失敗,因為 Cloud Shell 超出 perimeter 範圍,且 Compute Engine 已設定 --restricted-services 旗標。

        2. 在 Cloud Shell 中執行下列指令,存取未在 --restricted-services 旗標中設定的 Resource Manager 服務。

          gcloud projects describe PROJECT_ID

          成功的回應會傳回專案的詳細資料。這項回應表示 perimeter 允許外部流量存取 Cloud Resource Manager API。

          您已成功示範如何讓 perimeter 拒絕外部流量存取 --restricted-services 中設定的服務,以及允許外部流量存取未在 --restricted-services 設定的服務。

        以下各節將介紹例外模式,以便存取 perimeter 內的受限服務。

        確認存取層級允許 perimeter 例外狀況

        在本節中,您將驗證存取層級是否允許 perimeter 例外狀況。如要為外部流量建立例外狀況,允許存取 perimeter 內的所有受限服務,且不需要為每個服務或其他屬性建立精細的例外狀況,存取層級就非常實用。

        架構圖:存取層級如何為 VPC Service Controls perimeter 內的所有服務授予例外狀況

        上圖說明存取層級如何允許授權用戶端存取 perimeter 內的所有受限服務。

        在下列步驟中,您將建立存取層級,並向 Compute Engine 服務提出成功要求,藉此驗證這個概念。即使您將 Compute Engine 設為受限,系統仍允許這項要求。

        1. 在 Cloud Shell 中,建立說明存取層級設定的 YAML 檔案,並將其套用至 perimeter。這個範例會為您目前用來執行本教學課程的使用者身分建立存取層級。

          export USERNAME=$(gcloud config list account --format "value(core.account)")
          
          cat <<EOF > user_spec.yaml
          - members:
            - user:$USERNAME
          EOF
          
          gcloud access-context-manager levels create single_user_level \
          --title="single-user access level" \
          --basic-level-spec=user_spec.yaml \
          --policy=$POLICY_ID
          
          gcloud access-context-manager perimeters update demo_perimeter \
          --add-access-levels=single_user_level \
          --policy=$POLICY_ID
        2. 在 Cloud Shell 中再次執行下列指令,嘗試建立 VM:

          gcloud compute instances create demo-vm \
          --machine-type=e2-micro \
          --subnet=restricted-subnet \
          --scopes=https://www.googleapis.com/auth/cloud-platform \
          --no-address

          這次要求成功。您的 perimeter 會禁止外部流量存取受限服務,但您設定的存取層級允許例外狀況。

        確認輸入政策允許對 perimeter 進行精細的例外處理

        在本節中,您將驗證輸入政策是否允許對 perimeter 設定精細例外狀況。相較於粗略存取層級,細微輸入政策可設定流量來源的其他屬性,並允許存取個別服務或方法。

        架構圖:輸入政策如何允許精細例外狀況,存取 perimeter 內的指定服務

        上圖說明輸入政策如何允許授權用戶端僅存取 perimeter 內的指定服務,而不允許存取其他受限服務。

        在下列步驟中,您將把存取層級替換為僅允許授權用戶端存取 Compute Engine 服務,但不允許存取其他受限服務的輸入政策,藉此驗證這個概念。

        1. 在 Cloud Shell 分頁中執行下列指令,移除存取層級。

          gcloud access-context-manager perimeters update demo_perimeter \
          --policy=$POLICY_ID \
          --clear-access-levels
        2. 在 Cloud Shell 分頁中,建立輸入政策,只允許使用者身分輸入 Compute Engine 服務,並將政策套用至 perimeter。

          cat <<EOF > ingress_spec.yaml
          - ingressFrom:
              identities:
              - user:$USERNAME
              sources:
              - accessLevel: '*'
            ingressTo:
              operations:
              - methodSelectors:
                - method: '*'
                serviceName: compute.googleapis.com
              resources:
              - '*'
          EOF
          
          gcloud access-context-manager perimeters update demo_perimeter \
          --set-ingress-policies=ingress_spec.yaml \
          --policy=$POLICY_ID
        3. 在 Cloud Shell 分頁中執行下列指令,在 perimeter 內建立 Cloud Storage bucket。

          gcloud storage buckets create gs://PROJECT_ID-01

          輸出結果會與下列內容相似:

          "ERROR: (gcloud.storage.buckets.create) HTTPError 403: Request is prohibited by organization's policy."
          

          Cloud Shell 是 perimeter 外的用戶端,因此 VPC Service Controls perimeter 會禁止 Cloud Shell 與 perimeter 內的受限服務通訊。

        4. 在 Cloud Shell 分頁中,執行下列指令,向 perimeter 內的 Compute Engine 服務提出要求。

          gcloud compute instances describe demo-vm --zone=ZONE

          成功的回應會傳回 demo-vm 的詳細資料。這項回應表示 perimeter 允許外部流量,只要符合輸入政策的條件,即可存取 Compute Engine 服務。

        確認允許從 perimeter 內部流量存取的服務

        下列各節將說明 VPC Service Controls perimeter 如何允許或拒絕從 perimeter 內部對服務提出的要求,以及如何透過輸出政策選擇性允許輸出至外部服務。

        為示範 perimeter 內外流量的差異,下列各節將同時使用 perimeter 外的 Cloud Shell,以及您在 perimeter 內建立的 Compute Engine 執行個體。您在安全殼層內對 perimeter 內的 Compute Engine Commands 執行個體執行的指令,會使用附加服務帳戶的身分,而從 perimeter 外部 Cloud Shell 執行的指令會使用您自己的身分。按照本教學課程的建議設定操作時,即使要求具備足夠的 IAM 權限,perimeter 仍會允許或拒絕要求。

        本教學課程使用 Compute Engine API、Cloud Storage API 和 Cloud Resource Manager API,但同樣的概念也適用於其他服務。

        確認 perimeter 允許 perimeter 內部受限服務的內部流量

        在本節中,如果服務也設為可透過虛擬私有雲存取的服務,您將驗證 perimeter 是否允許來自 perimeter 內部網路端點的流量。

        架構圖:如何設定 vpc-accessible-services,藉此從內部網路端點連線至服務

        上圖說明 perimeter 如何允許 perimeter 內的網路端點流量,連往您也設定為可透過虛擬私有雲存取的受限服務。如果服務未設為可透過虛擬私有雲存取的服務,就無法從 perimeter 內的網路端點連線。

        在下列步驟中,您將建立與 perimeter 內部 Compute Engine 執行個體的 SSH 連線,然後向服務發出要求,藉此驗證這個概念。

        1. 在 Cloud Shell 中建立防火牆規則,允許 SSH 流量連往虛擬私有雲網路,方法是允許來自 35.235.240.0/20 IP 位址範圍的輸入流量,該範圍是由 IAP for TCP forwarding 服務使用:

          gcloud compute firewall-rules create demo-allow-ssh \
          --direction=INGRESS \
          --priority=1000 \
          --network=restricted-vpc \
          --action=ALLOW \
          --rules=tcp:22 \
          --source-ranges=35.235.240.0/20 
        2. 啟動這個執行個體的 SSH 工作階段:

          gcloud compute ssh demo-vm --zone=ZONE

          確認命令列提示已變更為顯示執行個體的主機名稱,藉此驗證您是否已成功連線至 demo-vm 執行個體:

          username@demo-vm:~$
          

          如果先前的指令失敗,您可能會看到類似下列內容的錯誤訊息:

          "[/usr/bin/ssh] exited with return code [255]"
          

          在這種情況下,Compute Engine 執行個體可能尚未完成啟動,請稍後再試。

        3. 在 perimeter 內的 SSH 工作階段中,使用可透過虛擬私有雲存取的服務的 Google Cloud 服務,驗證 perimeter 內部允許的服務。舉例來說,請嘗試使用 Compute Engine 服務的任何指令。

          gcloud compute instances describe demo-vm --zone=ZONE

          成功的回應會傳回 demo-vm 的詳細資料。這項回應表示 perimeter 允許內部流量存取 Compute Engine API。

        4. 在 perimeter 內的 SSH 工作階段中,確認未納入可透過虛擬私有雲存取的服務允許清單中的服務,是否不允許從 VM 存取。舉例來說,下列指令會使用 Resource Manager 服務,但該服務並未納入可透過虛擬私有雲存取的服務許可清單中。

          gcloud projects describe PROJECT_ID

          輸出結果會與下列內容相似:

          "ERROR: (gcloud.projects.list) PERMISSION_DENIED: Request is prohibited by organization's policy."
          

          Compute Engine 執行個體和其他網路端點只能要求將服務納入可透過虛擬私有雲存取的服務允許清單中。不過,來自 perimeter 外的無伺服器資源或服務流量可能會要求該服務。如要禁止專案使用某項服務,請參閱受限服務資源使用政策

        確認 perimeter 拒絕 perimeter 外部受限服務的內部流量

        在本節中,您將確認 perimeter 會封鎖 perimeter 內部服務與 perimeter 外部 Google Cloud 服務之間的通訊。

        架構圖:VPC Service Controls perimeter 如何拒絕 perimeter 內流量存取 perimeter 外的受限服務

        上圖說明如何避免內部流量與 perimeter 外的受限服務通訊。

        在下列步驟中,您將嘗試把內部流量傳送至 perimeter 內的受限服務,以及 perimeter 外的受限服務,藉此驗證這個概念。

        1. 在 perimeter 內的 SSH 工作階段中,執行下列指令,在 perimeter 內建立儲存空間 bucket。這項指令之所以能夠運作,是因為 Cloud Storage 服務已納入 restricted-servicesaccessible-services

          gcloud storage buckets create gs://PROJECT_ID-02

          成功的回應會建立儲存空間 bucket。這項回應表示 perimeter 允許內部流量連線至 Cloud Storage 服務。

        2. 在 perimeter 內的 SSH 工作階段中,執行下列指令,從 perimeter 外的 bucket 讀取資料。這個公開 bucket 允許 allUsers 的唯讀權限,但 perimeter 會拒絕從 perimeter 內傳送至 perimeter 外受限服務的流量。

          gcloud storage cat gs://solutions-public-assets/vpcsc-tutorial/helloworld.txt

          輸出結果會與下列內容相似:

          "ERROR: (gcloud.storage.objects.describe) HTTPError 403: Request is prohibited
          by organization's policy."
          

          這項回應顯示您可以在 perimeter 內使用受限服務,但 perimeter 內的資源無法與 perimeter 外的受限服務通訊。

        確認輸出政策允許 perimeter 例外狀況

        在本節中,您將驗證輸出政策是否允許 perimeter 例外狀況。

        架構圖:輸出政策如何允許特定例外狀況,存取 perimeter 外的受限服務

        上圖說明透過輸出政策授予狹隘例外狀況時,如何允許內部流量與特定外部資源通訊。

        在後續步驟中,您將建立輸出政策,然後存取輸出政策允許的 perimeter 以外的公開 Cloud Storage bucket,藉此驗證這個概念。

        1. 在 Cloud Shell 中點按「開啟新分頁」圖示 ,開啟新的 Cloud Shell 工作階段。在後續步驟中,您會在第一個分頁 (內含 perimeter 內的 SSH 工作階段) 和第二個分頁 (perimeter 外的 Cloud Shell,指令列提示以 username@cloudshell 開頭) 之間切換。

        2. 在 Cloud Shell 分頁中,建立輸出政策,允許從 demo-vm 的附加服務帳戶身分輸出,方法是使用 google.storage.objects.get 方法,輸出至外部專案中的公開 bucket。請使用輸出政策更新 perimeter。

          export POLICY_ID=$(gcloud access-context-manager policies list \
          --organization=ORGANIZATION_ID \
          --format="value(name)")
          
          export SERVICE_ACCOUNT_EMAIL=$(gcloud compute instances describe demo-vm \
          --zone=ZONE \
          --format="value(serviceAccounts.email)")
          cat <<EOF > egress_spec.yaml
          - egressFrom:
              identities:
                - serviceAccount:$SERVICE_ACCOUNT_EMAIL
            egressTo:
              operations:
              - methodSelectors:
                - method: 'google.storage.objects.get'
                serviceName: storage.googleapis.com
              resources:
              - projects/950403849117
          EOF
          
          gcloud access-context-manager perimeters update demo_perimeter \
          --set-egress-policies=egress_spec.yaml \
          --policy=$POLICY_ID
        3. 返回 SSH 工作階段的分頁,該工作階段位於 perimeter 內的 VM,且指令列提示以 username@demo-vm 開頭。

        4. 在 perimeter 內的 SSH 工作階段中,再次向 Cloud Storage bucket 提出要求,並確認要求是否正常運作。

          gcloud storage cat gs://solutions-public-assets/vpcsc-tutorial/helloworld.txt

          輸出結果會與下列內容相似:

          "Hello world!
          This is a sample file in Cloud Storage that is viewable to allUsers."
          

          這項回應表示您的 perimeter 和輸出政策允許特定身分從內部流量存取特定 Cloud Storage bucket。

        5. 在 perimeter 內的 SSH 工作階段中,您也可以測試輸出政策例外狀況未允許的其他方法。舉例來說,下列指令需要 google.storage.buckets.list 權限,但 perimeter 拒絕授予這項權限。

          gcloud storage ls gs://solutions-public-assets/vpcsc-tutorial/*

          輸出結果會與下列內容相似:

          "ERROR: (gcloud.storage.cp) Request is prohibited by organization's policy."
          

          這項回應顯示,perimeter 會拒絕內部流量列出外部 bucket 中的物件,表示輸出政策僅允許明確指定的方法。

        如要進一步瞭解在 service perimeter 外部共用資料的常見模式,請參閱「透過輸入和輸出規則保護資料交換作業」一文。

        (選用) 設定受限服務資源使用政策

        您可能也有內部或法規遵循方面的需求,只允許在環境內使用個別核准的 API。在這種情況下,您也可以設定受限服務資源用量組織政策服務。在專案中套用組織政策,即可限制可在該專案中建立的服務。不過,組織政策不會禁止這項專案中的服務與其他專案中的服務通訊。相較之下,VPC Service Controls 可讓您定義 perimeter,防止與 perimeter 外的服務通訊。

        舉例來說,如果您定義的組織政策允許 Compute Engine,但拒絕專案中的 Cloud Storage,則該專案中的 VM 無法在專案中建立 Cloud Storage bucket。不過,VM 可以向其他專案中的 Cloud Storage bucket 提出要求,因此仍有可能透過 Cloud Storage 服務外洩資料。以下步驟說明如何實作及測試這個情境:

        1. 切換到 Cloud Shell 分頁,指令列提示會以 username@cloudshell 開頭。
        2. 在 Cloud Shell 分頁中,建立 YAML 檔案來描述組織政策服務,該服務只允許使用 Compute Engine 服務,並拒絕所有其他服務,然後將其套用至專案。

          cat <<EOF > allowed_services_policy.yaml
          constraint: constraints/gcp.restrictServiceUsage
          listPolicy:
            allowedValues:
            - compute.googleapis.com
            inheritFromParent: true
          EOF
          
          gcloud resource-manager org-policies set-policy allowed_services_policy.yaml \
          --project=PROJECT_ID
        3. 返回 SSH 工作階段的分頁,該工作階段位於 perimeter 內的 VM,且指令列提示以 username@demo-vm 開頭。

        4. 在 perimeter 內的 SSH 工作階段中,執行下列指令,查看您先前在這項專案中建立的相同儲存空間 bucket。

          gcloud storage buckets describe gs://PROJECT_ID

          輸出結果會與下列內容相似:

          "ERROR: (gcloud.storage.buckets.create) HTTPError 403: Request is disallowed by organization's constraints/gcp.restrictServiceUsage constraint for 'projects/123456789' attempting to use service 'storage.googleapis.com'."
          

          這項回應表示無論 perimeter 的設定為何,組織政策服務都會拒絕專案內的 Cloud Storage 服務。

        5. 在 perimeter 內的 SSH 工作階段中,執行下列指令,查看 perimeter 外的儲存空間 bucket,該 bucket 受到輸出政策允許。

          gcloud storage cat gs://solutions-public-assets/vpcsc-tutorial/helloworld.txt

          輸出結果會與下列內容相似:

          "Hello world!
          This is a sample file in Cloud Storage that is viewable to allUsers."
          

          成功的回應會傳回外部儲存空間 bucket 中的 helloworld.txt 內容。這項回應表示您的 perimeter 和輸出政策允許內部流量在特定條件下存取外部儲存空間 bucket,但無論 perimeter 設定為何,組織政策服務都會拒絕專案中的 Cloud Storage 服務。如果 perimeter 允許,即使受限服務資源用量組織政策服務不允許,仍可能使用專案外部的服務進行外洩。

          如要拒絕與 perimeter 外的 Cloud Storage 或其他 Google 服務通訊,單獨使用受限服務資源用量組織政策服務並不夠,您必須設定 VPC Service Controls perimeter。VPC Service Controls 可減少資料竊取路徑,而受限服務資源用量是法規遵循控制項,可防止在環境中建立未經核准的服務。搭配使用這些控制項,即可封鎖一系列資料竊取路徑,並在環境中選擇性允許核准的服務供內部使用。

        清除所用資源

      9. In the Google Cloud console, go to the Manage resources page.

        Go to Manage resources

      10. In the project list, select the project that you want to delete, and then click Delete.
      11. In the dialog, type the project ID, and then click Shut down to delete the project.
      12. 雖然 VPC Service Controls perimeter 不會產生任何額外費用,但仍應清除,以免組織中出現雜亂和未使用的資源。

        1. 在 Google Cloud 控制台頂端的專案選取器中,選取您在本教學課程中使用的組織。
        2. 在 Google Cloud 控制台中,前往「VPC Service Controls」頁面。

          前往 VPC Service Controls

        3. 在 perimeter 清單下方,選取要刪除的 perimeter,然後按一下「Delete」(刪除)

        4. 在對話方塊中,再次按一下「Delete」(刪除) 確認刪除。

        後續步驟