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

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

機構政策提供各種Google Cloud 服務的預先定義限制。不過,如要進一步自訂組織政策中受限制的特定欄位,也可以建立自訂限制,並在自訂組織政策中使用這些限制。

政策繼承

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

虛擬私有雲支援的資源

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

  • 網路compute.googleapis.com/Network
    • 名稱:resource.name
    • 說明:resource.description
    • 模式:resource.autoCreateSubnetworks
    • MTU 設定:resource.mtu
    • 防火牆強制執行順序:resource.networkFirewallPolicyEnforcementOrder
    • 內部 IPv6 設定:resource.enableUlaInternalIpv6
    • 內部 IPv6 範圍:resource.internalIpv6Range
    • 轉送模式:resource.routingConfig.routingMode
  • Subnetwork (子網路):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
  • 封包鏡像compute.googleapis.com/PacketMirroring
    • 名稱:resource.name
    • 說明:resource.description
    • 電視網網址:resource.network.url
    • 優先順序:resource.priority
    • 收集器 ILB 網址: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. 安裝 Google Cloud CLI。 安裝完成後,執行下列指令初始化 Google Cloud CLI:

      gcloud init

      如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

    2. Set a default region and zone.

    REST

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

      安裝 Google Cloud CLI。 安裝完成後,執行下列指令初始化 Google Cloud CLI:

      gcloud init

      如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

    詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using 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. 選取頁面頂端的「專案挑選器」

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

  4. 按一下「自訂限制」

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

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

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

  8. 在「Resource type」方塊中,選取包含要限制物件和欄位的 Google Cloud REST 資源名稱。例如: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. 在「編輯政策」頁面中,選取「覆寫上層政策」
  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

要求以 IP 位址指定 next-hop-ilb 路由

要求使用 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 方法強制執行。

後續步驟