本頁面說明如何使用 Google Cloud 組織政策服務自訂限制條件,限制對下列 Cloud Storage for Firebase 資源執行的特定作業:
firebasestorage.googleapis.com/DefaultBucket
如要進一步瞭解組織政策,請參閱「自訂組織政策」。
關於組織政策和限制
Google Cloud組織政策服務可讓您透過程式輔助,集中控管組織的資源。組織政策管理員可以定義組織政策,也就是一組稱為「限制」的限制條件,適用於Google Cloud資源和這些資源在Google Cloud資源階層中的子系。您可以在組織、資料夾或專案層級強制執行組織政策。
組織政策提供各種Google Cloud服務的內建代管限制。不過,如要更精細地自訂組織政策中受限的特定欄位,您也可以建立「自訂限制」,並用於組織政策。
政策繼承
根據預設,您強制執行政策的資源子系會繼承組織政策。舉例來說,如果您對資料夾強制執行政策,Google Cloud會對該資料夾中的所有專案強制執行政策。如要進一步瞭解這項行為以及如何變更,請參閱這篇文章。
此外,由於 Cloud Storage for Firebase 會將檔案儲存在基礎 Cloud Storage bucket 中,因此對 Cloud Storage 資源強制執行的任何組織政策或自訂限制,也適用於 Cloud Storage for Firebase bucket。
優點
- 安全性與管理:集中控管組織的 Cloud Storage bucket 佈建作業,確保符合資料落地和成本管理防護措施。
- 精細的屬性控管:根據
storageClass或location等特定資源屬性,限制預設 bucket 的建立。
限制
使用自訂組織政策搭配 Cloud Storage for Firebase 時,須遵守下列限制:
- 不溯及既往:新強制執行的自訂限制不適用於現有資源。除非現有的預設 bucket 有更新,否則不會進行評估。如要找出可能違反限制的現有資源,可以使用模擬測試模式測試政策。
- 存取控管限制:自訂限制無法限制 bucket 的存取控管清單 (ACL) 或 IAM 政策。
事前準備
在 Firebase 控制台中,選取或建立 Firebase 專案。
確認專案屬於某個組織,且您知道組織 ID。
如要使用指令列設定及強制執行自訂限制,請安裝 Google Cloud CLI (
gcloud)。自訂組織政策無法透過 Firebase CLI 管理。
必要的角色
如要取得管理自訂組織政策所需的權限,請要求管理員授予您組織的組織政策管理員 (roles/orgpolicy.policyAdmin) IAM 角色。
Cloud Storage for Firebase 項支援的資源
下表列出自訂限制中可參照的 Cloud Storage for Firebase 資源。您也可以參考Cloud Storage支援的資源,因為這些資源的自訂限制也適用於 Cloud Storage for Firebase bucket。
| 資源 | 支援的欄位 |
|---|---|
firebasestorage.googleapis.com/DefaultBucket |
|
設定自訂限制
自訂限制是在 YAML 檔案中定義,其中包含您要強制執行組織政策的服務所支援的資源、方法、條件和動作。自訂限制的條件是使用一般運算語言 (CEL) 定義。如要進一步瞭解如何使用 CEL 在自訂限制中建構條件,請參閱「建立及管理自訂限制」的 CEL 相關章節。
控制台
如要建立自訂限制,請按照下列步驟操作:
- 前往 Google Cloud 控制台的「Organization policies」(組織政策) 頁面。
- 在專案挑選器中,選取要設定組織政策的專案。
- 按一下「 自訂限制」。
- 在「Display name」(顯示名稱) 方塊中,輸入人類可讀的限制名稱。這個名稱會用於錯誤訊息,可用於識別和偵錯。請勿在顯示名稱中使用個人識別資訊 (PII) 或機密資料,因為錯誤訊息可能會顯示這類資訊。這個欄位最多可輸入 200 個字元。
- 在「Constraint ID」(限制 ID) 方塊中,輸入新自訂限制的 ID。自訂限制只能包含英文字母 (包括大小寫) 或數字,例如
custom.restrictStorageClass。這個欄位最多可包含 70 個字元,不含前置字元 (custom.),例如organizations/123456789/customConstraints/custom.restrictStorageClass。請勿在限制 ID 中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。 - 在「Description」(說明) 方塊中,輸入人類可讀的限制說明。違反政策時,系統會將這段說明做為錯誤訊息顯示。請詳細說明違反政策的原因和解決方法。請勿在說明中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。這個欄位最多可輸入 2000 個字元。
- 在「Resource type」(資源類型) 方塊中,選取包含要限制的物件和欄位的 REST 資源名稱,例如
firebasestorage.googleapis.com/DefaultBucket。大多數資源類型最多支援 20 項自訂限制。如果嘗試建立更多自訂限制,作業會失敗。 - 在「Enforcement method」(強制執行方法) 下方,選取要對 REST
CREATE方法,還是同時對CREATE和UPDATE方法強制執行限制。(注意:DefaultBucket僅支援CREATE)。 - 如要定義條件,請按一下「編輯條件」圖示 。
- 在「Add condition」(新增條件) 面板中,建立參照支援服務資源的 CEL 條件,例如
resource.storageClass == 'STANDARD'。這個欄位最多可輸入 1000 個字元。如要進一步瞭解 CEL 的用法,請參閱「一般運算語言」。 - 按一下「儲存」。
- 在「Add condition」(新增條件) 面板中,建立參照支援服務資源的 CEL 條件,例如
- 在「Action」下方,選取是否要在符合條件時允許或拒絕評估的方法。
「拒絕」動作是指如果條件評估結果為 true,系統就會禁止建立或更新資源。
「允許」動作代表只有在條件評估為 true 時,才允許建立或更新資源。除了條件中明確列出的情況外,其他所有情況都會遭到封鎖。
- 按一下「Create constraint」(建立限制)。
gcloud
- 如要建立自訂限制,請使用下列格式建立 YAML 檔案:
name: organizations/ORGANIZATION_ID/customConstraints/custom.restrictStorageClass resourceTypes: firebasestorage.googleapis.com/DefaultBucket methodTypes: - CREATE condition: "resource.storageClass == 'STANDARD'" actionType: DENY displayName: Restrict Standard Storage Class description: Deny creating buckets with STANDARD storage class.
- 為新的自訂限制建立 YAML 檔案後,您必須加以設定,才能用於組織的組織政策。如要設定自訂限制,請使用
gcloud org-policies set-custom-constraint指令:gcloud org-policies set-custom-constraint CONSTRAINT_PATH
請將
CONSTRAINT_PATH替換成自訂限制檔案的完整路徑,例如:/home/user/customconstraint.yaml。 - 如要確認是否存在自訂限制,請使用
gcloud org-policies list-custom-constraints指令:gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
強制執行自訂組織政策
如要強制執行限制,請建立參照該限制的組織政策,然後將該政策套用至 Google Cloud 資源。
控制台
- 前往 Google Cloud 控制台的「Organization policies」(組織政策) 頁面。
- 在專案挑選器中,選取要設定組織政策的專案。
- 在「Organization policies」(組織政策) 頁面的清單中選取限制,即可查看該限制的「Policy details」(政策詳細資料) 頁面。
- 如要為這項資源設定組織政策,請按一下「Manage policy」(管理政策)。
- 在「Edit policy」(編輯政策) 頁面,選取「Override parent's policy」(覆寫上層政策)。
- 點選「新增規則」。
- 在「Enforcement」(強制執行) 部分,選取是否要強制執行這項組織政策。
- 如要在模擬測試模式下強制執行組織政策,請按一下「Set dry run policy」(設定模擬測試政策)。詳情請參閱「測試組織政策」。
- 確認組織政策在模擬測試模式下運作正常後,請按一下「Set policy」(設定政策),設定正式政策。
gcloud
- 如要建立含有布林值規則的組織政策,請建立參照限制的政策 YAML 檔案:
name: projects/PROJECT_ID/policies/custom.restrictStorageClass spec: rules: - enforce: true dryRunSpec: rules: - enforce: true
- 如要在模擬測試模式強制執行組織政策,請執行下列指令並加上
dryRunSpec旗標:gcloud org-policies set-policy POLICY_PATH --update-mask=dryRunSpec
- 確認模擬測試模式中的組織政策運作正常後,請使用
org-policies set-policy指令和spec旗標設定正式政策:gcloud org-policies set-policy POLICY_PATH --update-mask=spec
測試自訂組織政策
下列範例會建立自訂限制和政策,要求所有新建立的預設 bucket 避免使用 STANDARD 儲存空間級別。您可以嘗試執行政策應禁止的操作,藉此測試政策。
建立限制
將下列檔案儲存為
constraint-restrict-storage-class.yaml:name: organizations/ORGANIZATION_ID/customConstraints/custom.restrictStorageClass resourceTypes: - firebasestorage.googleapis.com/DefaultBucket methodTypes: - CREATE condition: "resource.storageClass == 'STANDARD'" actionType: DENY displayName: Restrict Standard Storage Class description: Deny creating buckets with STANDARD storage class.請將
ORGANIZATION_ID替換成組織 ID。套用限制:
gcloud org-policies set-custom-constraint ~/constraint-restrict-storage-class.yaml確認限制是否存在:
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID輸出結果會與下列內容相似:
CUSTOM_CONSTRAINT ACTION_TYPE METHOD_TYPES RESOURCE_TYPES DISPLAY_NAME custom.restrictStorageClass DENY CREATE firebasestorage.googleapis.com/DefaultBucket Restrict Standard Storage Class ...
建立政策
將下列檔案儲存為
policy-restrict-storage-class.yaml:name: projects/PROJECT_ID/policies/custom.restrictStorageClass spec: rules: - enforce: true將
PROJECT_ID替換為您的專案 ID。套用政策:
gcloud org-policies set-policy ~/policy-restrict-storage-class.yaml確認政策是否存在:
gcloud org-policies list --project=PROJECT_ID輸出結果會與下列內容相似:
CONSTRAINT LIST_POLICY BOOLEAN_POLICY ETAG custom.restrictStorageClass - SET CIqktscGELiZn6cC-
套用政策後,請等待約兩分鐘,Google Cloud就會開始強制執行政策。
測試政策
嘗試使用 Cloud Storage for Firebase REST API 建立預設 bucket,但不指定儲存空間級別 (預設為 STANDARD):
PROJECT_ID="PROJECT_ID"
TOKEN=$(gcloud auth print-access-token)
curl -X POST "https://firebasestorage.googleapis.com/v1beta/projects/${PROJECT_ID}/defaultBucket" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json" \
-d '{"location": "US-EAST1"}'
要求失敗,並顯示類似以下的錯誤:
FAILED_PRECONDITION: Operation denied by org policy: ["customConstraints/custom.restrictStorageClass": "Deny creating buckets with STANDARD storage class."].
- '@type': type.googleapis.com/google.rpc.ErrorInfo
domain: iam.googleapis.com
metadata:
customConstraints: customConstraints/custom.restrictStorageClass
reason: CUSTOM_ORG_POLICY_VIOLATION
常見用途的自訂組織政策範例
下表提供一些常見自訂限制的語法範例。
| 說明 | 限制語法 |
|---|---|
| 限制建立儲存空間級別為 Standard Storage 的預設 bucket |
name: organizations/ORGANIZATION_ID/customConstraints/custom.restrictStorageClass resourceTypes: - firebasestorage.googleapis.com/DefaultBucket methodTypes: - CREATE condition: "resource.storageClass == 'STANDARD'" actionType: DENY displayName: Restrict Standard Storage Class description: Deny creating buckets with STANDARD storage class. |
| 預設 bucket 必須建立在特定位置 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.requireDefaultBucketLocation resourceTypes: - firebasestorage.googleapis.com/DefaultBucket methodTypes: - CREATE condition: "resource.location != 'US-EAST1'" actionType: DENY displayName: Require US-EAST1 Location description: Deny creating default buckets outside the US-EAST1 location. |