使用自訂限制管理 Cloud Storage 資源

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

本頁說明如何設定自訂限制,對 Cloud Storage 資源強制執行政策。

如要在正式環境強制執行新限制前進行測試,請使用政策模擬器

政策繼承

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

定價

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

限制

  • 您只能使用 Google Cloud 控制台或 Google Cloud CLI,為 Cloud Storage 資源設定自訂限制。

  • 自訂限制只能針對 Cloud Storage 資源的 CREATEUPDATE 方法強制執行。

  • 新強制執行的自訂限制不會自動套用至現有資源。您必須更新現有資源,限制才會生效。

    如要找出需要更新的現有資源,可以強制執行模擬測試組織政策

  • 自訂限制無法用於限制物件或值區的 ACL 或 IAM 政策。

Cloud Storage 支援的資源

如果是 Cloud Storage,您可以對下列資源設定自訂限制:

必要的角色

如要瞭解管理自訂限制的機構政策時需要哪些角色,請參閱「必要角色」一節。

除了管理機構政策,您可能也想測試您建立的自訂限制。如要測試自訂限制,建議您使用最寬鬆的預先定義或自訂角色,其中包含測試特定限制所需的權限。如要查看必要權限和角色,請參閱 Cloud Storage 的角色權限

設定自訂限制

控制台

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

    前往「機構政策」

  2. 選取頁面頂端的「專案挑選器」

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

  4. 按一下「自訂限制」

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

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

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

  8. 在「Resource type」欄位中,選取包含要限制物件和欄位的 Google Cloud REST 資源名稱。例如:storage.googleapis.com/Bucket

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

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

    1. 在「新增條件」面板中,建立參照支援服務資源的 CEL 條件,例如 resource.versioning.enabled == true。這個欄位的長度上限為 1000 個字元。

    2. 按一下 [儲存]

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

  12. 按一下「建立限制」

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

gcloud

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

name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- RESOURCE_NAME
methodTypes:
- METHOD1
- METHOD2
condition: "CONDITION"
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION

更改下列內容:

  • ORGANIZATION_ID:您的機構 ID,例如 123456789

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

  • RESOURCE_NAME:Google Cloud REST 資源的完整名稱,其中包含您要限制的物件和欄位。例如:storage.googleapis.com/Bucket

  • METHOD1,METHOD2:要強制執行限制的 RESTful 方法清單。可以是 CREATECREATEUPDATE

  • CONDITION:參照支援服務資源的 CEL 條件,例如 "resource.versioning.enabled == true"。這個欄位的長度上限為 1000 個字元。如要進一步瞭解如何使用 CEL,請參閱「一般運算語言」。

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

  • DISPLAY_NAME:限制條件的易記名稱。這個欄位的長度上限為 200 個字元。

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

為新的自訂限制建立 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。 詳情請參閱「查看機構政策」。

如果要求成功,輸出結果會與下列內容相似:

CUSTOM_CONSTRAINT             ACTION_TYPE  METHOD_TYPES   RESOURCE_TYPES                     DISPLAY_NAME
custom.uniformBucketLevelAccess  DENY         CREATE,UPDATE  storage.googleapis.com/Bucket  Enable object versioning

如要進一步瞭解如何設定及管理自訂限制,請參閱「建立及管理自訂限制」一文。

強制執行限制

如要強制執行限制,請建立參照該限制的機構政策,然後將該政策套用至 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.uniformBucketLevelAccess

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

    gcloud org-policies set-policy POLICY_PATH
    

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

如果要求違反自訂限制而遭到拒絕,就會失敗並顯示 412 錯誤:CUSTOM_ORGPOLICY_CONSTRAINT_FAILED

範例:建立限制,強制在 Bucket 上使用客戶自行管理的加密金鑰

gcloud

  1. 建立 enforceCMEK.yaml 限制檔案,並加入下列資訊:

    name: organizations/ORGANIZATION_ID/customConstraints/custom.customerManagedEncryptionKeys
    resource_types: storage.googleapis.com/Bucket
    method_types: CREATE UPDATE
    condition: "has(resource.encryption.defaultKmsKeyName)"
    action_type: ALLOW
    display_name: Enforce Cloud KMS key
    description: When this constraint is enforced, newly created buckets and newly updated buckets must be encrypted with a
    Cloud KMS key. The Cloud KMS key on existing buckets can be updated but not deleted.
  2. 設定自訂限制。

    gcloud org-policies set-custom-constraint enforceCMEK.yaml
    
  3. 建立包含下列資訊的 enforceCMEK-policy.yaml 政策檔案。

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

    PROJECT_ID 替換為您的專案 ID。

    在本範例中,我們是在專案層級強制執行這項限制,但您也可以在機構或資料夾層級設定這項限制。

  4. 強制執行政策。

    gcloud org-policies set-policy enforceCMEK-policy.yaml
    

常見用途的自訂限制範例

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

用途 語法
值區保留政策的期限必須在指定時間範圍內
      name: organizations/ORGANIZATION_ID/customConstraints/custom.retentionPolicy
      method_types:
CREATE
UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.retentionPolicy.retentionPeriod not in [3600, 2678400]"
      action_type: DENY
      display_name: Bucket retention policy is either 3,600 seconds or 2,678,400 seconds
      description: Newly created buckets and newly updated buckets must have a
      retention policy that's either 3,600 seconds or 2,678,400 seconds.
值區必須啟用物件版本管理功能
      name: organizations/ORGANIZATION_ID/customConstraints/custom.enforceBucketVersioning
      method_types:
CREATE
UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.versioning.enabled == true"
      action_type: ALLOW
      display_name: Buckets must have Object Versioning enabled
      description: Newly created buckets and newly updated buckets must have Object Versioning enabled.
bucket 名稱必須使用特定規則運算式
      name: organizations/ORGANIZATION_ID/customConstraints/custom.bucketName
      method_types:
CREATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.name.matches('^[a-zA-Z]+$')"
      action_type: ALLOW
      display_name: Bucket names must match the specified regular expression
      description: Newly created buckets must have a name that matches the
      specified regular expression. Only letters are allowed in the bucket name.
值區無法啟用 Bucket Lock
      name: organizations/ORGANIZATION_ID/customConstraints/custom.prohibitBucketLock
      method_types:
CREATE
UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.retentionPolicy.isLocked == true"
      action_type: DENY
      display_name: Prohibit the use of Bucket Lock
      description: Newly created buckets and newly updated buckets cannot have
      Bucket Lock enabled.
值區無法啟用 Object Retention Lock
      name: organizations/ORGANIZATION_ID/customConstraints/custom.prohibitObjectRetentionLock
      method_types:
CREATE
UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.objectRetention.mode == 'Enabled'"
      action_type: DENY
      display_name: Objects cannot have retention configurations
      description: Newly created buckets and newly updated buckets cannot have
      Object Retention Lock enabled.
位於 USEU 多區域的值區必須將保留期限設為 86,400 秒
      name: organizations/ORGANIZATION_ID/customConstraints/custom.locationRetentionPolicy
      method_types:
CREATE
UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "(resource.location.startsWith('US') || resource.location.startsWith('EU')) && resource.retentionPolicy.retentionPeriod != 86400"
      action_type: DENY
      display_name: All buckets in US and EU must have a retention policy of 86,400 seconds
      description: Newly created buckets and newly updated buckets located in
      US and EU regions must have a retention policy of 86,400 seconds.
Bucket 必須有標籤1
      name: organizations/ORGANIZATION_ID/customConstraints/custom.labels
      method_types:
CREATE
UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "'my_annotations.data.source' in resource.labels && resource.labels['my_annotations.data.source'] in ['SOURCE_IMAGES','SOURCE_TEXT','SOURCE_VIDEOS']"
      action_type: ALLOW
      display_name: Buckets must have a label classifying the contents of the bucket
      description: Newly created buckets and newly updated buckets must have the
      label my_annotations.data.source with the SOURCE_IMAGES, SOURCE_TEXT, or
      SOURCE_VIDEOS key.
值區必須位於雙區域
      name: organizations/ORGANIZATION_ID/customConstraints/custom.dualRegionUS
      method_types:
CREATE
UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "'US-EAST1' in resource.customPlacementConfig.dataLocations && 'US-EAST4' in resource.customPlacementConfig.dataLocations"
      action_type: ALLOW
      display_name: Buckets must be located in a dual-region
      description: Newly created buckets and newly updated buckets must be located in a dual-region
      composed of the us-east1 and us-east4 regions.
值區無法使用舊版儲存空間類別
      name: organizations/ORGANIZATION_ID/customConstraints/custom.disableLegacyStorageClass
      method_types:
CREATE
UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.storageClass in ['STANDARD', 'NEARLINE', 'COLDLINE', 'ARCHIVE']"
      action_type: ALLOW
      display_name: Buckets cannot use legacy storage classes
      description: Newly created buckets and newly updated buckets must use
      Standard storage, Nearline storage, Coldline storage, or Archive storage.
值區 IP 篩選功能必須限制來自所有公開網際網路的要求
      name: organizations/ORGANIZATION_ID/customConstraints/custom.IpFilter
      method_types:
CREATE
      resource_types: storage.googleapis.com/Bucket
      condition: "!has(resource.ipFilter) || (resource.ipFilter.mode == 'Disabled' || resource.ipFilter.publicNetworkSource.allowedIpCidrRanges.size() > 0)"
      action_type: DENY
      display_name: Bucket IP filter rules must restrict all the public network
      description: Newly created buckets must have IP filtering and IP filtering rules must restrict all public network resources. 

1 指定不存在的 bucket 標籤鍵會傳回 BAD_CONDITION 錯誤。這項錯誤會導致系統無法在 bucket 上執行 CREATEUPDATE 方法。為避免發生這項錯誤,請一律先使用 "my_annotations.data.source" in resource.labels 檢查標籤鍵是否存在。

條件的運算式欄位

下表列出可用於建立條件的運算式欄位。條件是以一般運算語言 (CEL) 編寫。 請注意,這些運算式欄位的值會區分大小寫。

如要瞭解下列運算式欄位,以及可指定的值,請參閱 JSON API 的 Bucket 資源表示法

運算式欄位 值類型
billing.requesterPays bool
cors list
cors.maxAgeSeconds int
cors.method list
cors.origin list
cors.responseHeader list
customPlacementConfig.dataLocations1 list
defaultEventBasedHold bool
encryption.defaultKmsKeyName string
iamConfiguration.publicAccessPrevention string
iamConfiguration.uniformBucketLevelAccess.enabled bool
labels map
lifecycle.rule list
lifecycle.rule.action.storageClass1 string
lifecycle.rule.action.type string
lifecycle.rule.condition.age int
lifecycle.rule.condition.createdBefore string
lifecycle.rule.condition.customTimeBefore string
lifecycle.rule.condition.daysSinceCustomTime int
lifecycle.rule.condition.daysSinceNoncurrentTime int
lifecycle.rule.condition.isLive bool
lifecycle.rule.condition.matchesPrefix list
lifecycle.rule.condition.matchesStorageClass list
lifecycle.rule.condition.matchesSuffix list
lifecycle.rule.condition.noncurrentTimeBefore string
lifecycle.rule.condition.numNewerVersions int
location1 string
locationType string
logging.logBucket string
logging.logObjectPrefix string
ipFilter.mode string
ipFilter.publicNetworkSource object
ipFilter.publicNetworkSource.allowedIpCidrRanges list
ipFilter.vpcNetworkSources list
ipFilter.vpcNetworkSources.network string
ipFilter.vpcNetworkSources.allowedIpCidrRanges list
objectRetention.mode string
name string
projectNumber2 string
retentionPolicy.isLocked3 bool
retentionPolicy.retentionPeriod int
rpo string
softDeletePolicy.retentionDurationSeconds int
storageClass1 string
versioning.enabled bool
website.mainPageSuffix string
website.notFoundPage string

1 這個欄位的值必須以大寫字母填寫。

2 這個欄位已淘汰。

3 這個欄位只能用於禁止使用 Bucket Lock,無法強制執行 Bucket Lock。

注意事項

不建議在自訂限制條件中使用bucket 標籤。請改用標記,這類標記只能由具備必要 IAM 角色的使用者設定,且控管比標籤更嚴格。