本頁面說明如何使用「組織政策服務」自訂限制條件,限制對下列 Google Cloud 資源執行的特定作業:
compute.googleapis.com/Diskcompute.googleapis.com/Imagecompute.googleapis.com/Instancecompute.googleapis.com/InstanceGroup
如要進一步瞭解組織政策,請參閱「自訂組織政策」。
關於組織政策和限制
Google Cloud 組織政策服務可讓您透過程式輔助,集中控管組織的資源。組織政策管理員可以定義組織政策,也就是一組稱為「限制」的限制條件,適用於Google Cloud 資源和這些資源在Google Cloud 資源階層中的子系。您可以在組織、資料夾或專案層級,強制執行組織政策。
組織政策提供各種 Google Cloud 服務的內建代管限制。不過,如要更精細地自訂組織政策中受限的特定欄位,您也可以建立「自訂限制」,並用於組織政策。
政策繼承
根據預設,您強制執行政策的資源子系會繼承組織政策。舉例來說,如果您對資料夾強制執行政策, Google Cloud 會對該資料夾中的所有專案強制執行政策。如要進一步瞭解這項行為及變更方式,請參閱「階層評估規則」。
優點
- 成本管理:使用自訂組織政策,限制機構可用的虛擬機器 (VM) 執行個體和磁碟大小與類型。您也可以限制 VM 執行個體使用的機器家族
- 安全性、法規遵循和管理:您可以透過自訂機構政策,強制執行下列政策:
- 如要強制執行安全規定,您可以在 VM 上要求特定的防火牆通訊埠規則。
- 為支援硬體隔離或授權法規遵循,您可以要求特定專案或資料夾中的所有 VM 都必須在單一用戶群節點上執行。
- 如要控管自動化指令碼,可以使用自訂機構政策,確認標籤符合指定運算式。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init - 請確認您知道組織 ID。
必要的角色
如要取得管理 Compute Engine 資源組織政策所需的權限,請要求管理員授予您下列 IAM 角色:
- 機構資源的機構政策管理員 (
roles/orgpolicy.policyAdmin) -
如要測試限制:
在專案中,Compute 執行個體管理員 (v1) (
roles/compute.instanceAdmin.v1)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
這些預先定義的角色具備管理 Compute Engine 資源組織政策所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:
所需權限
如要管理 Compute Engine 資源的組織政策,必須具備下列權限:
-
orgpolicy.constraints.list -
orgpolicy.policies.create -
orgpolicy.policies.delete -
orgpolicy.policies.list -
orgpolicy.policies.update -
orgpolicy.policy.get -
orgpolicy.policy.set -
如要測試限制:
- 專案的
compute.instances.create權限 - 如要使用自訂映像檔建立 VM,請按一下映像檔上的
compute.images.useReadOnly。 - 如要使用快照建立 VM,請按一下快照上的
compute.snapshots.useReadOnly - 如要使用執行個體範本建立 VM,請按一下執行個體範本上的
compute.instanceTemplates.useReadOnly - 如要將舊版網路指派給 VM:專案的
compute.networks.use權限 - 如要為 VM 指定靜態 IP 位址:專案的
compute.addresses.use權限 - 使用舊版網路時,如要將外部 IP 位址指派給 VM,請在專案中啟用
compute.networks.useExternalIp - 為 VM 指定子網路:專案或所選子網路的
compute.subnetworks.use - 使用虛擬私有雲網路時,如要將外部 IP 位址指派給 VM,請在專案或所選子網路上擁有
compute.subnetworks.useExternalIp權限 - 如要為專案中的 VM 設定 VM 執行個體中繼資料,請執行下列指令:
compute.instances.setMetadata - 如要為 VM 設定標記:
compute.instances.setTags在 VM 上 - 如要為 VM 設定標籤,請在 VM 上執行下列指令:
compute.instances.setLabels - 如要設定 VM 使用的服務帳戶,請在 VM 上執行
compute.instances.setServiceAccount - 如要為 VM 建立新磁碟,請取得專案的
compute.disks.create權限 - 如要以唯讀或讀寫模式連接現有磁碟,請對磁碟擁有
compute.disks.use權限 - 如要以唯讀模式連接現有磁碟,請對磁碟擁有
compute.disks.useReadOnly權限
- 專案的
Compute Engine 支援的資源
針對 Compute Engine,您可以對下列資源和欄位設定自訂限制。
| 資源 | 欄位 |
|---|---|
| compute.googleapis.com/Disk |
resource.enableConfidentialCompute
|
resource.licenseCodes
| |
resource.licenses
| |
resource.sizeGb
| |
resource.sourceImage
| |
resource.type
| |
| compute.googleapis.com/Image |
resource.rawDisk.source
|
| compute.googleapis.com/Instance |
resource.advancedMachineFeatures.enableNestedVirtualization
|
resource.advancedMachineFeatures.performanceMonitoringUnit
| |
resource.advancedMachineFeatures.threadsPerCore
| |
resource.canIpForward
| |
resource.confidentialInstanceConfig.confidentialInstanceType
| |
resource.confidentialInstanceConfig.enableConfidentialCompute
| |
resource.deletionProtection
| |
resource.guestAccelerators.acceleratorCount
| |
resource.guestAccelerators.acceleratorType
| |
resource.labels
| |
resource.machineType
| |
resource.minCpuPlatform
| |
resource.name
| |
resource.networkInterfaces.accessConfigs.name
| |
resource.networkInterfaces.accessConfigs.natIP
| |
resource.networkInterfaces.network
| |
resource.networkInterfaces.networkAttachment
| |
resource.networkInterfaces.subnetwork
| |
resource.privateIpv6GoogleAccess
| |
resource.resourceStatus.effectiveInstanceMetadata.blockProjectSshKeysMetadataValue
| |
resource.resourceStatus.effectiveInstanceMetadata.enableGuestAttributesMetadataValue
| |
resource.resourceStatus.effectiveInstanceMetadata.enableOsconfigMetadataValue
| |
resource.resourceStatus.effectiveInstanceMetadata.enableOsInventoryMetadataValue
| |
resource.resourceStatus.effectiveInstanceMetadata.enableOsloginMetadataValue
| |
resource.resourceStatus.effectiveInstanceMetadata.serialPortEnableMetadataValue
| |
resource.resourceStatus.effectiveInstanceMetadata.serialPortLoggingEnableMetadataValue
| |
resource.resourceStatus.effectiveInstanceMetadata.vmDnsSettingMetadataValue
| |
resource.scheduling.nodeAffinities.key
| |
resource.scheduling.nodeAffinities.operator
| |
resource.scheduling.nodeAffinities.values
| |
resource.selfLink
| |
resource.shieldedInstanceConfig.enableIntegrityMonitoring
| |
resource.shieldedInstanceConfig.enableSecureBoot
| |
resource.shieldedInstanceConfig.enableVtpm
| |
resource.zone
| |
| compute.googleapis.com/InstanceGroup |
resource.description
|
resource.name
| |
resource.namedPorts.name
| |
resource.namedPorts.port
|
設定自訂限制
自訂限制是在 YAML 檔案中定義,其中包含您要強制執行組織政策的服務所支援的資源、方法、條件和動作。自訂限制的條件是使用一般運算語言 (CEL) 來定義。如要進一步瞭解如何使用 CEL 在自訂限制中建構條件,請參閱「建立及管理自訂限制」的 CEL 相關章節。
控制台
如要建立自訂限制,請按照下列步驟操作:
- 前往 Google Cloud 控制台的「Organization policies」(組織政策) 頁面。
- 在專案選擇工具中,選取要設定組織政策的專案。
- 按一下「自訂限制」。
- 在「顯示名稱」方塊中,輸入容易理解的限制名稱。這個名稱會顯示在錯誤訊息中,可用於識別和偵錯。請勿在顯示名稱中使用個人識別資訊 (PII) 或私密資料,因為錯誤訊息可能會顯示這類名稱。這個欄位最多可包含 200 個半形字元。
-
在「Constraint ID」(限制 ID) 方塊中,輸入新自訂限制的 ID。自訂限制只能包含字母 (包括大寫和小寫) 或數字,例如
custom.createOnlyN2DVMs。這個欄位最多可包含 70 個字元,不含前置字元 (custom.),例如organizations/123456789/customConstraints/custom。請勿在限制 ID 中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。 - 在「說明」方塊中,輸入使用者可理解的限制說明。違反政策時,系統會顯示這項說明做為錯誤訊息。請提供違反政策的詳細原因,以及如何解決問題。請勿在說明中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。這個欄位最多可輸入 2000 個字元。
-
在「Resource type」方塊中,選取包含要限制物件和欄位的 Google Cloud REST 資源名稱,例如
container.googleapis.com/NodePool。大多數資源類型最多支援 20 項自訂限制。如果您嘗試建立更多自訂限制,作業會失敗。 -
在「強制執行方式」下方,選取要對 REST
CREATE方法強制執行限制,還是同時對CREATE和UPDATE方法強制執行限制。如果您在違反限制的資源上,使用UPDATE方法強制執行限制,除非變更可解決違規問題,否則組織政策會封鎖對該資源的變更。 - 如要定義條件,請按一下「編輯條件」。
-
在「Add condition」(新增條件) 面板中,建立參照支援服務資源的 CEL 條件,例如
resource.management.autoUpgrade == false。這個欄位最多可輸入 1000 個字元。如要進一步瞭解如何使用 CEL,請參閱「 一般運算語言」。如要進一步瞭解自訂限制中可使用的服務資源,請參閱「 自訂限制支援的服務」。 - 按一下 [儲存]。
- 在「動作」下方,選取符合條件時要允許或拒絕評估方法。
- 按一下「建立限制」。
如要查看各項服務支援的方法,請在「 支援自訂限制的服務」中找出該服務。
如果條件評估結果為 true,系統會禁止建立或更新資源。
允許動作是指只有在條件評估為 true 時,才允許建立或更新資源的作業。除了條件中明確列出的情況外,其他所有情況都會遭到封鎖。
在每個欄位中輸入值後,右側會顯示這個自訂限制的對等 YAML 設定。
gcloud
- 如要建立自訂限制,請使用下列格式建立 YAML 檔案:
-
ORGANIZATION_ID:您的機構 ID,例如123456789。 -
CONSTRAINT_NAME:新自訂限制的名稱。自訂限制只能包含字母 (包括大寫和小寫) 或數字,例如custom.createOnlyN2DVMs。這個欄位最多可包含 70 個字元,不含前置字元 (custom.),例如organizations/123456789/customConstraints/custom。請勿在限制 ID 中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。 -
RESOURCE_NAME:內含要限制的物件和欄位的 Google Cloud資源完整名稱,例如:compute.googleapis.com/Instance。大多數資源類型最多支援 20 項自訂限制。如果您嘗試建立更多自訂限制,作業會失敗。 -
methodTypes:強制執行限制的 REST 方法。可以是CREATE,也可以是CREATE和UPDATE。如果您在違反限制的資源上使用UPDATE方法強制執行限制,除非變更可解決違規問題,否則組織政策會封鎖對該資源的變更。 -
CONDITION:針對支援服務資源表示法所撰寫的 CEL 條件。這個欄位最多可輸入 1000 個字元。例如:"resource.machineType.contains('/machineTypes/n2d')"。 -
ACTION:符合condition時採取的動作。可能的值為ALLOW和DENY。 -
DISPLAY_NAME:人類可讀的限制條件名稱。這個名稱會顯示在錯誤訊息中,可用於識別和偵錯。請勿在顯示名稱中使用 PII 或機密資料,因為錯誤訊息可能會顯示這類名稱。這個欄位最多可輸入 200 個半形字元。 -
DESCRIPTION:違反政策時,會以錯誤訊息形式顯示且易於理解的限制說明。這個欄位最多可輸入 2000 個字元。 -
為新的自訂限制建立 YAML 檔案後,您必須加以設定,才能用於組織的組織政策。如要設定自訂限制條件,請使用
gcloud org-policies set-custom-constraint指令: -
如要驗證是否存在自訂限制條件,請使用
gcloud org-policies list-custom-constraints指令:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME resourceTypes: RESOURCE_NAME methodTypes: - CREATE
- UPDATE condition: "CONDITION" actionType: ACTION displayName: DISPLAY_NAME description: DESCRIPTION
請替換下列項目:
如要查看各項服務支援的方法,請在「 支援自訂限制的服務」中找出該服務。
如要進一步瞭解可編寫條件的資源,請參閱「支援的資源」。
允許動作是指如果條件評估結果為 true,系統就會允許建立或更新資源的作業。這也表示系統會封鎖條件中明確列出的情況以外的所有其他情況。
拒絕動作表示如果條件評估結果為 true,系統會封鎖建立或更新資源的作業。
gcloud org-policies set-custom-constraint CONSTRAINT_PATH
請將 CONSTRAINT_PATH 替換成自訂限制檔案的完整路徑。例如:/home/user/customconstraint.yaml。
這項作業完成後,自訂限制會顯示在 Google Cloud 組織政策清單中,供組織政策使用。
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
請將 ORGANIZATION_ID 替換成組織資源的 ID。
詳情請參閱「 查看組織政策」。
強制執行自訂組織政策
如要強制執行限制,請建立參照該限制的組織政策,然後將組織政策套用至 Google Cloud 資源。控制台
- 前往 Google Cloud 控制台的「Organization policies」(組織政策) 頁面。
- 在專案選擇工具中,選取要設定組織政策的專案。
- 在「Organization policies」(組織政策) 頁面的清單中選取限制,即可查看該限制的「Policy details」(政策詳細資料) 頁面。
- 如要為這項資源設定組織政策,請按一下「Manage policy」(管理政策)。
- 在「Edit policy」(編輯政策) 頁面,選取「Override parent's policy」(覆寫上層政策)。
- 按一下「Add a rule」(新增規則)。
- 在「強制執行」部分,選取是否要強制執行這項機構政策。
- 選用:如要根據標記設定組織政策的條件,請按一下「Add condition」(新增條件)。請注意,如果為組織政策新增條件式規則,您必須至少新增一項無條件規則,否則無法儲存政策。詳情請參閱「 使用標記設定組織政策範圍」。
- 按一下「Test changes」(測試變更),模擬組織政策的影響。詳情請參閱「 使用 Policy Simulator 測試組織政策變更」。
- 如要在模擬測試模式下強制執行組織政策,請按一下「設定模擬測試政策」。詳情請參閱「 測試組織政策」。
- 確認試營運模式中的機構政策運作正常後,請按一下「設定政策」,設定正式政策。
gcloud
- 如要建立含有布林規則的組織政策,請建立參照限制的政策 YAML 檔案:
-
PROJECT_ID:要強制執行限制的專案。 -
CONSTRAINT_NAME:要為自訂限制定義的名稱,例如:custom.createOnlyN2DVMs。 -
如要以模擬測試模式強制執行組織政策,請執行下列指令並加上
dryRunSpec旗標: -
確認模擬測試模式中的機構政策能發揮預期效果後,請使用
org-policies set-policy指令和spec旗標設定正式政策:
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true dryRunSpec: rules: - enforce: true
請替換下列項目:
gcloud org-policies set-policy POLICY_PATH --update-mask=dryRunSpec
請將 POLICY_PATH 替換為組織政策 YAML 檔案的完整路徑。政策最多需要 15 分鐘才會生效。
gcloud org-policies set-policy POLICY_PATH --update-mask=spec
請將 POLICY_PATH 替換為組織政策 YAML 檔案的完整路徑。政策最多需要 15 分鐘才會生效。
測試自訂組織政策
下列範例會建立自訂限制,將 VM 限制為使用 N2D 機型。
建立自訂限制條件
如要定義自訂限制條件,請建立名為
onlyN2DVMs.yaml的檔案。name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlyN2DVMs resourceTypes: compute.googleapis.com/Instance condition: "resource.machineType.contains('/machineTypes/n2d')" actionType: ALLOW methodTypes: CREATE displayName: Only N2D VMs allowed description: Restrict all VMs created to only use N2D machine types.
請將 ORGANIZATION_ID 替換成組織 ID。
套用自訂限制條件。
gcloud org-policies set-custom-constraint onlyN2DVMs.yaml
建立機構政策
如要定義組織政策,請建立名為
onlyN2DVMs-policy.yaml的檔案。在本範例中,我們是在專案層級強制執行這項限制,但您也可以在機構或資料夾層級設定這項限制。name: projects/PROJECT_ID/policies/custom.createOnlyN2DVMs spec: rules: – enforce: true
將
PROJECT_ID替換為專案 ID。強制執行機構政策。
gcloud org-policies set-policy onlyN2DVMs-policy.yaml
測試政策
如要測試限制條件,請嘗試建立使用非 N2D 機型的 VM。
gcloud compute instances create my-test-instance \ --project=PROJECT_ID \ --zone=us-central1-c \ --machine-type=e2-medium系統不允許這項作業,輸出內容類似如下:
ERROR: (gcloud.compute.instances.create) Could not fetch resource: – Operation denied by custom org policies: [customConstraints/custom.createOnlyN2DVMs]: Restrict all VMs created to only use N2D machine types.
常見用途的自訂組織政策範例
下表提供一些常見自訂限制的語法範例。
| 說明 | 限制語法 |
|---|---|
永久磁碟類型必須為「極端永久磁碟 (pd-extreme)」 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.createDisksPDExtremeOnly resourceTypes: compute.googleapis.com/Disk condition: "resource.type.contains('pd-extreme')" actionType: ALLOW methodTypes: CREATE displayName: Create pd-extreme disks only description: Only the extreme persistent disk type is allowed to be created. |
| 磁碟大小不得超過 250 GB | name: organizations/ORGANIZATION_ID/customConstraints/custom.createDisksLessThan250GB resourceTypes: compute.googleapis.com/Disk condition: "resource.sizeGb <= 250" actionType: ALLOW methodTypes: CREATE displayName: Disks size maximum is 250 GB description: Restrict the boot disk size to 250 GB or less for all VMs. |
來源圖片只能來自 Cloud Storage test_bucket |
name: organizations/ORGANIZATION_ID/customConstraints/custom.createDisksfromStoragebucket resourceTypes: compute.googleapis.com/Image condition: "resource.rawDisk.source.contains('storage.googleapis.com/test_bucket/')" actionType: ALLOW methodTypes: CREATE displayName: Source image must be from Cloud Storage test_bucket only description: Source images used in this project must be imported from the Cloud Storage test_bucket. |
VM 必須有標籤,且索引鍵設為 cost center |
name: organizations/ORGANIZATION_ID/customConstraints/custom.createVMWithLabel resourceTypes: compute.googleapis.com/Instance condition: "'cost_center' in resource.labels" actionType: ALLOW methodTypes: CREATE displayName: 'cost_center' label required description: Requires that all VMs created must have a 'cost_center' label that can be used for tracking and billing purposes. |
VM 必須有標籤,且鍵設為 cost center,值設為 eCommerce |
name: organizations/ORGANIZATION_ID/customConstraints/custom.createECommerceVMOnly resourceTypes: compute.googleapis.com/Instance condition: "'cost_center' in resource.labels and resource.labels['cost_center'] == 'eCommerce'" actionType: ALLOW methodTypes: CREATE displayName: Label (cost_center/eCommerce) required description: Label required and Key/value must be cost_center/eCommerce. |
| VM 必須使用 N2D 機型 | name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlyN2DVMs resourceTypes: compute.googleapis.com/Instance condition: "resource.machineType.contains('/machineTypes/n2d')" actionType: ALLOW methodTypes: CREATE displayName: Only N2D VMs allowed description: Restrict all VMs created to only use N2D machine types. |
VM 必須使用機型 e2-highmem-8 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlyE2highmem8 resourceTypes: compute.googleapis.com/Instance condition: "resource.machineType.endsWith('-e2-highmem-8')" actionType: ALLOW methodTypes: CREATE displayName: Only "e2-highmem-8" VMs allowed description: Restrict all VMs created to only use the E2 high-memory machine types that have 8 vCPUs. |
| 確保 VM 排定在「foo」節點群組上 | name: organizations/ORGANIZATION_ID/customConstraints/custom.createOnlySTVM resourceTypes: compute.googleapis.com/Instance condition: "resource.scheduling.nodeAffinities.exists(n, n.key == 'foo')" actionType: ALLOW methodTypes: CREATE displayName: Only VMs scheduled on node group "foo" allowed description: Restrict all VMs created to use the node group "foo". |
強制執行必要的 Resource Manager 標記
部分 Compute Engine 資源也支援 GOVERN_TAGS 類型限制,可對 Compute Engine 資源強制執行 Resource Manager 標記。詳情請參閱「使用組織政策強制執行必要標記」。
定價
組織政策服務 (包括預先定義和自訂組織政策) 免費提供。