建立及管理自訂限制

本頁面說明如何使用「組織政策服務」自訂限制條件,限制對下列 Google Cloud 資源執行的特定作業:

  • apikeys.googleapis.com/Key

如要進一步瞭解組織政策,請參閱「自訂組織政策」。

關於組織政策和限制

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

組織政策提供各種 Google Cloud 服務的內建代管限制。不過,如要更精細地自訂組織政策中受限的特定欄位,您也可以建立「自訂限制」,並用於組織政策。

政策繼承

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

優點

  • 成本管理:使用機構政策,限制貴機構可用的 VM 執行個體和磁碟大小與類型。您也可以限制用於 VM 執行個體的機器系列。
  • 安全性、法規遵循與管理:您可以按照下列方式使用自訂機構政策:
    • 如要強制執行安全規定,您可以在 VM 上要求特定的防火牆通訊埠規則。
    • 為支援硬體隔離或授權法規遵循,您可以要求特定專案或資料夾中的所有 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. 安裝 Google Cloud CLI。

  5. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  6. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  7. 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

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

  9. 安裝 Google Cloud CLI。

  10. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  11. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  12. 請確認您知道組織 ID

必要的角色

如要取得管理自訂組織政策所需的權限,請要求管理員授予組織資源的組織政策管理員 (roles/orgpolicy.policyAdmin) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

設定自訂限制

自訂限制是在 YAML 檔案中定義,其中包含您要強制執行組織政策的服務所支援的資源、方法、條件和動作。自訂限制的條件是使用一般運算語言 (CEL) 來定義。如要進一步瞭解如何使用 CEL 在自訂限制中建構條件,請參閱「建立及管理自訂限制」的 CEL 相關章節。

控制台

如要建立自訂限制,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Organization policies」(組織政策) 頁面。

    前往「Organization policies」(組織政策)

  2. 在專案選擇工具中,選取要設定組織政策的專案。
  3. 按一下「自訂限制」
  4. 在「顯示名稱」方塊中,輸入容易理解的限制名稱。這個名稱會顯示在錯誤訊息中,可用於識別和偵錯。請勿在顯示名稱中使用 PII 或機密資料,因為錯誤訊息可能會顯示這類名稱。這個欄位最多可包含 200 個半形字元。
  5. 在「Constraint ID」(限制 ID) 方塊中,輸入新自訂限制的名稱。自訂限制只能包含字母 (包括大寫和小寫) 或數字,例如 custom.disableGkeAutoUpgrade。這個欄位最多可包含 70 個字元,不含前置字元 (custom.),例如 organizations/123456789/customConstraints/custom。請勿在限制 ID 中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。
  6. 在「說明」方塊中,輸入使用者可理解的限制說明。違反政策時,系統會顯示這項說明做為錯誤訊息。請提供違反政策的詳細原因,以及如何解決問題。請勿在說明中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。這個欄位最多可輸入 2000 個字元。
  7. 在「Resource type」方塊中,選取包含要限制物件和欄位的 Google Cloud REST 資源名稱,例如 container.googleapis.com/NodePool。大多數資源類型最多支援 20 項自訂限制。如果您嘗試建立更多自訂限制,作業會失敗。
  8. 在「強制執行方法」下方,選取要對 REST 「CREATE」方法強制執行限制,還是對「CREATE」和「UPDATE」方法都強制執行限制。如果您使用 UPDATE 方法,對違反限制的資源強制執行限制,除非變更可解決違規問題,否則組織政策會封鎖對該資源的變更。
  9. 並非所有 Google Cloud 服務都支援這兩種方法。如要查看各項服務支援的方法,請在「 支援自訂限制的服務」中找出該服務。

  10. 如要定義條件,請按一下「編輯條件」
    1. 在「Add condition」(新增條件) 面板中,建立參照支援服務資源的 CEL 條件,例如 resource.management.autoUpgrade == false。這個欄位最多可輸入 1000 個字元。如要進一步瞭解如何使用 CEL,請參閱「 一般運算語言」。如要進一步瞭解自訂限制中可使用的服務資源,請參閱「 自訂限制支援的服務」。
    2. 按一下 [儲存]
  11. 在「動作」下方,選取符合條件時要允許或拒絕評估方法。
  12. 如果條件評估結果為 true,系統會禁止建立或更新資源。

    允許動作是指只有在條件評估為 true 時,才允許建立或更新資源的作業。除了條件中明確列出的情況外,所有其他情況都會遭到封鎖。

  13. 按一下「建立限制」
  14. 在每個欄位中輸入值後,右側會顯示這個自訂限制的對等 YAML 設定。

gcloud

  1. 如要建立自訂限制,請使用下列格式建立 YAML 檔案:
  2. name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
    resourceTypes: RESOURCE_NAME
    methodTypes:
      - CREATE
    - UPDATE
    condition: "CONDITION" actionType: ACTION displayName: DISPLAY_NAME description: DESCRIPTION

    請替換下列項目:

    • ORGANIZATION_ID:您的機構 ID,例如 123456789
    • CONSTRAINT_NAME:新自訂限制的名稱。自訂限制只能包含字母 (包括大寫和小寫) 或數字,例如 custom.allowOnlyStartsWithKeyName。這個欄位最多可包含 70 個字元。
    • RESOURCE_NAME:內含要限制的物件和欄位的 Google Cloud資源完整名稱,例如:apikeys.googleapis.com/Key
    • CONDITION:針對支援服務資源表示法所撰寫的 CEL 條件。這個欄位最多可輸入 1000 個半形字元。例如:"resource.displayName.startsWith('key-')"
    • 如要進一步瞭解可編寫條件的資源,請參閱「支援的資源」。

    • ACTION:符合 condition 時採取的動作。可能的值為 ALLOWDENY
    • 允許動作是指如果條件評估結果為 true,系統就會允許建立或更新資源的作業。這也表示系統會封鎖條件中明確列出的情況以外的所有其他情況。

      拒絕動作表示如果條件評估結果為 true,系統會封鎖建立或更新資源的作業。

    • DISPLAY_NAME:容易記得的限制名稱。這個欄位最多可包含 200 個半形字元。
    • DESCRIPTION:違反政策時,會以錯誤訊息形式顯示且易於理解的限制說明。這個欄位最多可輸入 2000 個字元。
  3. 為新的自訂限制建立 YAML 檔案後,您必須加以設定,才能用於組織的組織政策。如要設定自訂限制,請使用 gcloud org-policies set-custom-constraint 指令:
  4. gcloud org-policies set-custom-constraint CONSTRAINT_PATH

    請將 CONSTRAINT_PATH 替換為自訂限制檔案的完整路徑,例如:/home/user/customconstraint.yaml

    這項作業完成後,自訂限制會顯示在 Google Cloud 組織政策清單中,供組織政策使用。

  5. 如要確認是否存在自訂限制,請使用 gcloud org-policies list-custom-constraints 指令:
  6. gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID

    請將 ORGANIZATION_ID 替換成組織資源的 ID。

    詳情請參閱「 查看組織政策」。

強制執行自訂組織政策

如要強制執行限制,請建立參照該限制的組織政策,然後將組織政策套用至 Google Cloud 資源。

控制台

  1. 前往 Google Cloud 控制台的「Organization policies」(組織政策) 頁面。

    前往「Organization policies」(組織政策)

  2. 在專案選擇工具中,選取要設定組織政策的專案。
  3. 在「Organization policies」(組織政策) 頁面的清單中選取限制,即可查看該限制的「Policy details」(政策詳細資料) 頁面。
  4. 如要為這項資源設定組織政策,請按一下「Manage policy」(管理政策)
  5. 在「Edit policy」(編輯政策) 頁面,選取「Override parent's policy」(覆寫上層政策)
  6. 按一下「Add a rule」(新增規則)
  7. 在「強制執行」部分,選取是否要強制執行這項機構政策。
  8. 選用:如要根據標記設定組織政策的條件,請按一下「Add condition」(新增條件)。請注意,如果為組織政策新增條件式規則,您必須至少新增一項無條件規則,否則無法儲存政策。詳情請參閱「 使用標記設定組織政策範圍」。
  9. 按一下「Test changes」(測試變更),模擬組織政策的影響。詳情請參閱「 使用 Policy Simulator 測試組織政策變更」。
  10. 如要在模擬測試模式下強制執行組織政策,請按一下「設定模擬測試政策」。詳情請參閱「 測試組織政策」。
  11. 確認試營運模式中的機構政策運作正常後,請按一下「設定政策」,設定正式政策。

gcloud

  1. 如要建立含有布林值規則的機構政策,請建立參照限制的政策 YAML 檔案:
  2. name: projects/PROJECT_ID/policies/CONSTRAINT_NAME
    spec:
      rules:
      - enforce: true
    
    dryRunSpec:
      rules:
      - enforce: true

    請替換下列項目:

    • PROJECT_ID:要強制執行限制的專案。
    • CONSTRAINT_NAME:要為自訂限制定義的名稱,例如:custom.allowOnlyStartsWithKeyName
  3. 如要以模擬測試模式強制執行組織政策,請執行下列指令並加上 dryRunSpec 旗標:
  4. gcloud org-policies set-policy POLICY_PATH --update-mask=dryRunSpec

    請將 POLICY_PATH 替換為組織政策 YAML 檔案的完整路徑。政策最多需要 15 分鐘才會生效。

  5. 確認模擬測試模式中的機構政策能發揮預期效果後,請使用 org-policies set-policy 指令和 spec 旗標設定正式政策:
  6. gcloud org-policies set-policy POLICY_PATH --update-mask=spec

    請將 POLICY_PATH 替換為組織政策 YAML 檔案的完整路徑。政策最多需要 15 分鐘才會生效。

測試自訂組織政策

下列範例會建立自訂限制和政策,規定特定專案中所有新 API 金鑰的顯示名稱都必須以 key- 開頭。

開始之前,請先瞭解下列事項:

  • 組織 ID
  • 專案 ID

建立限制

  1. 將下列檔案儲存為 constraint-allow-only-starts-with-key.yaml

    name: organizations/ORGANIZATION_ID/customConstraints/custom.allowOnlyStartsWithKeyName
    resourceTypes:
    - apikeys.googleapis.com/Key
    methodTypes:
    - CREATE
    condition: "resource.displayName.startsWith('key-')"
    actionType: ALLOW
    displayName: Only new API Keys with the display name starting with 'key-' are allowed.
    description: Restrict all other key creation with the display name starting with 'key-'.
    

    這項限制定義指出,如果每個新 API 金鑰的顯示名稱不是以 key- 開頭,系統就會拒絕作業。

  2. 套用限制:

    gcloud org-policies set-custom-constraint ~/constraint-allow-only-starts-with-key.yaml
    
  3. 確認限制是否存在:

    gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
    

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

    CUSTOM_CONSTRAINT                       ACTION_TYPE  METHOD_TYPES   RESOURCE_TYPES                     DISPLAY_NAME
    custom.allowOnlyStartsWithKeyName       ALLOW        CREATE         apikeys.googleapis.com/Key         Only new API Keys with the display name starting with 'key-' are allowed.
    ...
    

建立政策

  1. 將下列檔案儲存為 policy-allow-only-starts-with-key.yaml

    name: projects/PROJECT_ID/policies/custom.allowOnlyStartsWithKeyName
    spec:
      rules:
      - enforce: true
    

    PROJECT_ID 替換為專案 ID。

  2. 套用政策:

    gcloud org-policies set-policy ~/policy-allow-only-starts-with-key.yaml
    
  3. 確認政策是否存在:

    gcloud org-policies list --project=PROJECT_ID
    

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

    CONSTRAINT                        LIST_POLICY    BOOLEAN_POLICY    ETAG
    custom.allowOnlyStartsWithKeyName -              SET               COCsm5QGENiXi2E=
    

套用政策後,請等待約兩分鐘, Google Cloud 就會開始強制執行政策。

測試政策

請嘗試在專案中建立顯示名稱為 not-key 的新 API 金鑰:

gcloud services api-keys create --display-name=not-key

輸出內容如下:

Operation denied by org policy: ["customConstraints/custom.allowOnlyStartsWithKeyName": "Only new API Keys with the display name starting with 'key-' are allowed."]

常見用途的自訂組織政策範例

下表提供一些常見自訂限制的語法範例。

說明 限制語法
只能使用顯示名稱開頭為「key-」的新 API 金鑰。
    name: organizations/ORGANIZATION_ID/customConstraints/custom.allowOnlyStartsWithKeyName
    resourceTypes:
    - apikeys.googleapis.com/Key
    methodTypes:
    - CREATE
    condition: "resource.displayName.startsWith('key-')"
    actionType: ALLOW
    displayName: Only new API Keys with the display name starting with 'key-' are allowed.
    description: Restrict all other key creation with the display name starting with 'key-'.
    
系統只允許建立/更新繫結至「test-sa@test-project.iam.gserviceaccount.com」的 API 金鑰。
    name: organizations/ORGANIZATION_ID/customConstraints/custom.denyTestServiceAccountBound
    resourceTypes:
    - apikeys.googleapis.com/Key
    methodTypes:
    - CREATE
    - UPDATE
    condition: "resource.serviceAccountEmail != 'test-sa@test-project.iam.gserviceaccount.com'"
    actionType: ALLOW
    displayName: Only API Keys bound to 'test-sa@test-project.iam.gserviceaccount.com' are allowed.
    description: Restrict all other key creation or update to only be bound to the test service account 'test-sa@test-project.iam.gserviceaccount.com'.
    
您只能建立允許清單中 IP 為「1.1.1.1」的新 API 金鑰。
    name: organizations/ORGANIZATION_ID/customConstraints/custom.allowOnlyIP1111
    resourceTypes:
    - apikeys.googleapis.com/Key
    methodTypes:
    - CREATE
    condition: "'1.1.1.1' in resource.restrictions.serverKeyRestrictions.allowedIps"
    actionType: ALLOW
    displayName: Only new API Keys with the IP '1.1.1.1' in the allowlist can be created.
    description: Restrict all other key creation with the IP '1.1.1.1' in the allowlist.
    
拒絕以 myservice.googleapis.com 服務為目標的 API 金鑰更新。
    name: organizations/ORGANIZATION_ID/customConstraints/custom.denyUpdateMyService
    resourceTypes:
    - apikeys.googleapis.com/Key
    methodTypes:
    - UPDATE
    condition: "resource.restrictions.apiTargets.exists(target, target.service == 'myservice.googleapis.com')"
    actionType: DENY
    displayName: Deny API Keys update targeting the myservice.googleapis.com service.
    description: Block myservice.googleapis.com service API Keys update.
    

API 金鑰支援的資源

下表列出可在自訂限制中參照的 API 金鑰資源。

資源 欄位
apikeys.googleapis.com/Key resource.displayName
resource.restrictions.androidKeyRestrictions.allowedApplications.packageName
resource.restrictions.androidKeyRestrictions.allowedApplications.sha1Fingerprint
resource.restrictions.apiTargets.methods
resource.restrictions.apiTargets.service
resource.restrictions.browserKeyRestrictions.allowedReferrers
resource.restrictions.iosKeyRestrictions.allowedBundleIds
resource.restrictions.serverKeyRestrictions.allowedIps
resource.serviceAccountEmail

後續步驟