使用自訂機構政策管理 VPC 資源

Google Cloud 機構政策可讓您透過程式以集中方式控管機構資源。身為機構政策管理員,您可以定義機構政策,也就是一組稱為限制的限制,適用於 Google Cloud 資源階層中的Google Cloud 資源和這些資源的子系。您可以在機構、資料夾或專案層級強制執行機構政策。

機構政策為各種Google Cloud 服務提供預先定義的限制。不過,如果您想進一步控管貴機構政策中受限制的特定欄位,並且進一步細分這些欄位,也可以建立自訂限制,並在自訂機構政策中使用這些自訂限制。

政策繼承

根據預設,機構政策會由您強制執行政策的資源子項繼承。舉例來說,如果您對資料夾套用政策, Google Cloud 就會對資料夾中的所有專案套用該政策。如要進一步瞭解這項行為和如何變更,請參閱「階層評估規則」。

虛擬私有雲支援的資源

針對 VPC,您可以針對下列資源和欄位設定自訂限制。

  • Network (網路):compute.googleapis.com/Network
    • 名稱:resource.name
    • 說明:resource.description
    • 模式:resource.autoCreateSubnetworks
    • MTU 設定:resource.mtu
    • 防火牆執行順序:resource.networkFirewallPolicyEnforcementOrder
    • 內部 IPv6 設定:resource.enableUlaInternalIpv6
    • 內部 IPv6 範圍:resource.internalIpv6Range
    • 轉送模式:resource.routingConfig.routingMode
  • 子網路compute.googleapis.com/Subnetwork
    • 名稱:resource.name
    • 說明:resource.description
    • 網路:resource.network
    • IP CIDR 範圍:resource.ipCidrRange
    • 次要 IP 位址範圍:resource.secondaryIpRanges[]
      • 範圍名稱:resource.secondaryIpRanges[].rangeName
      • IP CIDR 範圍:resource.secondaryIpRanges[].ipCidrRange
    • 堆疊類型:resource.stackType
    • IPv6 存取權類型:resource.ipv6AccessType
    • 私人 Google 存取權設定:resource.privateIpGoogleAccess
    • 子網路用途:resource.purpose
    • 角色:resource.role
    • 虛擬私有雲流量記錄:
      • 記錄設定:resource.logConfig.enable
      • 匯總時間間隔:resource.logConfig.aggregationInterval
      • 取樣率:resource.logConfig.flowSampling
      • 中繼資料:resource.logConfig.metadata
      • 中繼資料欄位:resource.logConfig.metadataFields[]
      • 篩選運算式:resource.logConfig.filterExpr
  • 路線compute.googleapis.com/Route
    • 名稱:resource.name
    • 說明:resource.description
    • 網路:resource.network
    • 標記:resource.tags
    • 目的地範圍:resource.destRange
    • 優先順序:resource.priority
    • 下一個躍點執行個體:resource.nextHopInstance
    • 下一個躍點的 IP 位址:resource.nextHopIp
    • 下一個躍點閘道:resource.nextHopGateway
    • 下一個躍點內部直通式網路負載平衡器:resource.nextHopIlb
    • 下一個躍點 VPN 通道:resource.nextHopVpnTunnel
  • Packet Mirroringcompute.googleapis.com/PacketMirroring
    • 名稱:resource.name
    • 說明:resource.description
    • 聯播網網址:resource.network.url
    • 優先順序:resource.priority
    • 收集器內部負載平衡器網址:resource.collectorIlb.url
    • 鏡像資源:
      • 子網路網址:resource.mirroredResources.subnetworks[].url
      • 執行個體網址:resource.mirroredResources.instances[].url
      • 標記:resource.mirroredResources.tags[]
    • 篩選器:
      • CIDR 範圍:resource.filter.cidrRanges[]
      • IP 通訊協定:resource.filter.IPProtocols[]
      • 方向:resource.filter.direction
    • 啟用:resource.enable

事前準備

  • 如果尚未設定,請先設定驗證機制。「驗證」是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,以便對 Compute Engine 進行驗證:

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

    1. After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    2. Set a default region and zone.
    3. REST

      如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

        After installing the Google Cloud CLI, initialize it by running the following command:

        gcloud init

        If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      詳情請參閱 Google Cloud 驗證說明文件中的「驗證以使用 REST」。

必要的角色

如要取得管理 VPC 資源的機構政策所需的權限,請要求管理員授予您下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

這些預先定義的角色包含管理 VPC 資源的組織政策所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:

所需權限

必須具備下列權限,才能管理 VPC 資源的機構政策:

  • orgpolicy.constraints.list
  • orgpolicy.policies.create
  • orgpolicy.policies.delete
  • orgpolicy.policies.list
  • orgpolicy.policies.update
  • orgpolicy.policy.get
  • orgpolicy.policy.set

您或許還可透過自訂角色或其他預先定義的角色取得這些權限。

設定自訂限制

自訂限制是由您要強制執行機構政策的服務支援的資源、方法、條件和動作所定義。您可以使用一般運算語言 (CEL) 定義自訂限制的條件。如要進一步瞭解如何使用 CEL 在自訂限制中建立條件,請參閱「建立及管理自訂限制」一文的 CEL 相關部分。

您可以建立自訂限制,並使用 Google Cloud 控制台或 Google Cloud CLI 將其設為組織政策的使用限制。

控制台

  1. 在 Google Cloud 控制台中,前往「Organization policies」(機構政策) 頁面。

    前往「機構政策」

  2. 選取頁面頂端的「Project picker」

  3. 在「專案選擇工具」中,選取要設定機構政策的資源。

  4. 按一下 「自訂限制」

  5. 在「顯示名稱」方塊中,輸入限制的易懂名稱。這個欄位的長度上限為 200 個半形字元。請勿在限制名稱中使用 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。

  6. 在「Constraint ID」方塊中,輸入新自訂限制條件的名稱。自訂限制的開頭必須為 custom.,且只能包含大寫或小寫英文字母或數字,例如 custom.createCustomNetworks。這個欄位的長度上限為 70 個半形字元,不含前置字元 (例如 organizations/123456789/customConstraints/custom.)。

  7. 在「說明」方塊中,輸入限制的易讀說明,以便在違反政策時以錯誤訊息形式顯示。這個欄位的長度上限為 2,000 個半形字元。

  8. 在「Resource type」方塊中,選取包含要限制的物件和欄位的 Google CloudREST 資源名稱。例如:compute.googleapis.com/Network

  9. 在「強制執行方法」下方,選取是否要在 REST CREATE 方法上強制執行限制。

  10. 如要定義條件,請按一下 編輯條件

    1. 在「Add condition」面板中,建立參照支援服務資源的 CEL 條件。這個欄位的長度上限為 1,000 個半形字元。

    2. 按一下 [儲存]

  11. 在「動作」下方,選取在符合上述條件時,要允許或拒絕評估方法。

  12. 按一下「建立限制條件」

在每個欄位中輸入值後,這個自訂限制的對等 YAML 設定會顯示在右側。

gcloud

如要使用 Google Cloud CLI 建立自訂限制,請建立自訂限制的 YAML 檔案:

name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes: compute.googleapis.com/RESOURCE_NAME
methodTypes:
- CREATE
condition: "CONDITION"
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION

更改下列內容:

  • ORGANIZATION_ID:貴機構 ID,例如 123456789

  • CONSTRAINT_NAME:您要為新自訂限制設定的名稱。自訂限制的開頭必須為 custom.,且只能包含大寫或小寫英文字母或數字,例如 custom.createCustomNetworks。這個欄位的長度上限為 70 個半形字元,不含前置字元 (例如 organizations/123456789/customConstraints/custom.)。

  • RESOURCE_NAME:包含您要限制的物件和欄位的 Compute Engine API REST 資源名稱 (而非 URI)。例如:Network

  • CONDITION:針對支援的服務資源表示法編寫的 CEL 條件。這個欄位的長度上限為 1000 個半形字元。如要進一步瞭解可用於寫入條件的資源,請參閱「支援的資源」。

  • ACTION:如果符合 condition,則採取的動作。這個欄位可以是 ALLOWDENY

  • DISPLAY_NAME:限制的易讀名稱。這個欄位的長度上限為 200 個半形字元。

  • DESCRIPTION:在違反政策時,要以錯誤訊息形式顯示的限制說明。這個欄位的長度上限為 2,000 個半形字元。

如要進一步瞭解如何建立自訂限制,請參閱「定義自訂限制」。

為新自訂限制建立 YAML 檔案後,您必須設定該檔案,以便在貴機構中使用機構政策。如要設定自訂限制條件,請使用 gcloud org-policies set-custom-constraint 指令:
gcloud org-policies set-custom-constraint CONSTRAINT_PATH
CONSTRAINT_PATH 替換為自訂限制檔案的完整路徑。例如:/home/user/customconstraint.yaml。完成後,自訂限制會列入 Google Cloud 機構政策清單,成為機構政策。如要確認自訂限制是否存在,請使用 gcloud org-policies list-custom-constraints 指令:
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
ORGANIZATION_ID 替換為機構資源的 ID。詳情請參閱「查看組織政策」。

強制執行自訂限制

您可以建立參照限制的機構政策,然後將該機構政策套用至 Google Cloud 資源,藉此強制執行限制。

控制台

  1. 在 Google Cloud 控制台中,前往「Organization policies」(機構政策) 頁面。

    前往「機構政策」

  2. 在專案選擇工具中,選取要設定機構政策的專案。
  3. 在「Organization policies」頁面的清單中選取限制條件,即可查看該限制條件的「Policy details」頁面。
  4. 如要設定這項資源的機構政策,請按一下「管理政策」
  5. 在「Edit policy」頁面中,選取「Override parent's policy」
  6. 按一下「新增規則」
  7. 在「Enforcement」(強制執行) 部分,選取是否要啟用這項機構政策的強制執行機制。
  8. 選用:如要讓機構政策依標記而定,請按一下「新增條件」。請注意,如果您在組織政策中新增條件規則,則必須至少新增一項無條件規則,否則無法儲存政策。詳情請參閱「設定含有標記的組織政策」。
  9. 按一下「測試變更」,模擬機構政策的效果。舊版受管理限制條件無法進行政策模擬。詳情請參閱「 使用 Policy Simulator 測試組織政策變更」。
  10. 如要完成並套用機構政策,請按一下「設定政策」。這項政策最多需要 15 分鐘才會生效。

gcloud

如要建立包含布林值規則的機構政策,請建立參照限制的政策 YAML 檔案:

      name: projects/PROJECT_ID/policies/CONSTRAINT_NAME
      spec:
        rules:
        - enforce: true
    

取代下列內容:

  • PROJECT_ID:您要強制執行限制的專案。
  • CONSTRAINT_NAME:您為自訂限制定義的名稱。例如:custom.createCustomNetworks

如要強制執行包含限制的機構政策,請執行下列指令:

    gcloud org-policies set-policy POLICY_PATH
    

POLICY_PATH 替換為機構政策 YAML 檔案的完整路徑。這項政策最多需要 15 分鐘才會生效。

範例:建立限制網路只能使用自訂模式的限制

gcloud

  1. 請使用下列資訊建立 onlyCustomNetwork.yaml 限制檔案。將 ORGANIZATION_ID 替換為您的機構 ID。

    name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlyCustomNetwork
    resourceTypes: compute.googleapis.com/Network
    condition: "resource.autoCreateSubnetworks == false"
    actionType: ALLOW
    methodTypes: CREATE
    displayName: Restrict creation of networks to custom mode networks
    description: Only custom mode networks allowed.
  2. 設定自訂限制。

    gcloud org-policies set-custom-constraint onlyCustomNetwork.yaml
    
  3. 請使用下列資訊建立 onlyCustomNetwork-policy.yaml 政策檔案。在這個範例中,我們會在專案層級強制執行這項限制,但您也可以在機構或資料夾層級設定這項限制。將 PROJECT_ID 替換為您的專案 ID。

          name: projects/PROJECT_ID/policies/custom.onlyCustomNetwork
          spec:
            rules:
    enforce: true
  4. 強制執行政策。

    gcloud org-policies set-policy onlyCustomNetwork-policy.yaml
    
  5. 嘗試建立自動模式虛擬私有雲網路,以測試限制條件。

    gcloud compute networks create vpc1
        --project=PROJECT_ID \
        --subnet-mode=auto
    

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

    ERROR: (gcloud.compute.networks.create) Could not fetch resource:
    - Operation denied by custom org policy: [customConstraints/custom.createOnlyCustomNetwork] : Only custom mode networks allowed.
    

常見用途的範例自訂限制

以下各節將提供一些實用的自訂限制語法。

要求網路必須具備內部 IPv6 範圍

name: organizations/ORGANIZATION_ID/customConstraints/custom.networkInternalIpv6Range
resourceTypes: compute.googleapis.com/Network
condition: "resource.enableUlaInternalIpv6 == true"
actionType: ALLOW
methodTypes: CREATE
displayName: Require networks to have an internal IPv6 range
description: Networks must have a ULA internal IPv6 range configured

要求子網路使用 10.0.0.0/8 中的範圍

name: organizations/ORGANIZATION_ID/customConstraints/custom.subnetRangeUse10Slash8
resourceTypes: compute.googleapis.com/Subnetwork
condition: "resource.ipCidrRange.startsWith('10.')"
actionType: ALLOW
methodTypes: CREATE
displayName: Require subnets to use ranges in 10.0.0.0/8
description: Subnetwork's primary IPv4 range must come from 10.0.0.0/8

要求 next-hop-ilb 路由必須使用 IP 位址指定

要求使用 next-hop-ilb 的路徑必須使用 IP 位址,而非轉送規則資源名稱來指定。

name: organizations/ORGANIZATION_ID/customConstraints/custom.routeNextHopIlbByIpAddress
resourceTypes: compute.googleapis.com/Route
condition: "!resource.nextHopIlb.contains('forwardingRules')"
actionType: ALLOW
methodTypes: CREATE
displayName: Require defining next-hop-ilb by IP address
description: Next hops that are an internal load balancer must be specified by IP address instead of resource name.

要求封包鏡像僅鏡像 TCP 流量

name: organizations/ORGANIZATION_ID/customConstraints/custom.packetMirroringTcpFilter
resourceTypes: compute.googleapis.com/PacketMirroring
condition: "resource.filter.IPProtocols.size() == 1 && resource.filter.IPProtocols[0] == 'tcp'"
actionType: ALLOW
methodTypes:
  - CREATE
  - UPDATE
displayName: Require policies to mirror TCP protocol only.
description: Packet mirroring must mirror all TCP traffic and no other protocols.

定價

機構政策服務 (包括預先定義和自訂機構政策) 均免費提供。

限制

  • 自訂限制只會套用至路徑資源CREATE 方法。

後續步驟