撰寫自訂雲端控管機制的規則

建立自訂雲端控制項時,您可以使用一般運算語言 (CEL) 運算式建立規則,評估掃描資源的屬性。

運算式可以是單一值檢查,也可以是更複雜的複合運算式,檢查多個值或條件。無論如何,運算式都必須解析為布林值 false,才能觸發發現結果。

評估資源屬性的 CEL 運算式必須符合下列規則:

  • 您在 CEL 運算式中指定的屬性,必須是掃描資源的屬性,如資源類型的 API 定義中所述。

  • CEL 運算式中的所有列舉都必須以字串表示。舉例來說,下列是 cloudkms.googleapis.com/CryptoKeyVersion 資源類型的有效運算式:

    resource.state = "PENDING_GENERATION"
  • 您在 condition 屬性中定義的 CEL 運算式結果必須是布林值。只有在結果為 false 時,才會觸發發現項目。

如要進一步瞭解 CEL,請參閱下列文章:

CEL 運算式範例

下表列出一些可用於評估資源屬性的 CEL 運算式。

資源類型 說明 CEL 運算式
cloudkms.googleapis.com/CryptoKey 檢查 Cloud KMS 金鑰輪替週期 has(resource.rotationPeriod) && resource.rotationPeriod < duration('60h')
compute.googleapis.com/Network 將虛擬私有雲對等互連規則與網路對等互連項目相符 resource.selfLink.matches('https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/default') || resource.peerings.exists(p, p.network.matches('https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/shared$'))
cloudfunctions.googleapis.com/CloudFunction 僅允許 Cloud Run 函式的內部輸入流量 has(resource.ingressSettings) && resource.ingressSettings.matches('ALLOW_INTERNAL_ONLY')
compute.googleapis.com/Instance 資源名稱符合模式 resource.name.matches('^gcp-vm-(linux|windows)-v\\\\d+$')
serviceusage.googleapis.com/Service 僅允許啟用儲存空間相關 API resource.state == 'ENABLED' && !( resource.name.matches('storage-api.googleapis.com') || resource.name.matches('bigquery-json.googleapis.com') || resource.name.matches('bigquery.googleapis.com') || resource.name.matches('sql-component.googleapis.com') || resource.name.matches('spanner.googleapis.com'))
sqladmin.googleapis.com/Instance 只允許許可清單中的公開 IP 位址 (resource.instanceType == 'CLOUD_SQL_INSTANCE' && resource.backendType == 'SECOND_GEN' && resource.settings.ipConfiguration.ipv4Enabled ) && (resource.ipAddresses.all(ip, ip.type != 'PRIMARY' || ip.ipAddress.matches('IP_ADDRESS')))
dataproc.googleapis.com/Cluster 檢查 Dataproc 叢集中的專案 ID 是否包含子字串 testingdevelopment has(resource.projectId) && !resource.projectId.contains('testing') || !resource.projectId.contains('development')