Scrivere regole personalizzate utilizzando Rego

Questo documento descrive come scrivere regole personalizzate utilizzando il linguaggio dei criteri Rego. Puoi utilizzare queste regole in Workload Manager per valutare i tuoi carichi di lavoro in base alle best practice definite per la tua organizzazione.

Per saperne di più, consulta Informazioni sulle regole personalizzate in Workload Manager.

Prima di iniziare

Scrivere regole personalizzate utilizzando Rego

Google fornisce un repository GitHub di esempio con un insieme di regole predefinite che puoi utilizzare per valutare i tuoi carichi di lavoro. Questi esempi coprono più casi d'uso. Seleziona le regole dal repository o crea un file di regole (.rego) che descriva i tuoi requisiti di valutazione.

Una regola personalizzata è composta dalle seguenti sezioni:

  • Metadati. I seguenti campi definiscono i metadati della regola:

    • DETAILS: una breve descrizione della regola.
    • SEVERITY: un valore definito dall'utente che definisce la gravità della violazione della regola. Ad esempio, HIGH, CRITICAL, MEDIUM o LOW.
    • ASSET_TYPE: uno degli asset supportati. Consulta Origini dati supportate.
    • TAGS: uno o più tag per la regola. Questi tag consentono di filtrare le regole.
  • Dichiarazione del pacco. Ad esempio, templates.google.compute.instance.label.

  • Comandi di importazione. Ad esempio, data.validator.google.lib as lib.

  • Definizioni delle regole: un insieme di istruzioni che definisce la regola.

Regole di esempio

Le seguenti regole di esempio sono disponibili nel repository GitHub GoogleCloudPlatform/workload-manager. Puoi caricare queste regole così come sono nel tuo bucket Cloud Storage e utilizzarlo per eseguire le valutazioni. In alternativa, modifica le regole in base ai criteri della tua organizzazione e poi carica i file in un bucket Cloud Storage.

  • Esempio 1: assicurati che esista almeno un'etichetta per le tue VM.
  • Esempio 2: assicurati che il tuo carico di lavoro non utilizzi l'account di servizio predefinito di Compute Engine.
  • Esempio 3: assicurati che le VM nel tuo carico di lavoro non utilizzino un indirizzo IP esterno.

Per un elenco completo di regole di esempio che puoi utilizzare in Workload Manager, consulta il repository GitHub GoogleCloudPlatform/workload-manager.

Esempio 1

Garantisce che sia presente almeno un tag per le risorse Compute Engine.

# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

########################################################################
# DETAILS:  MUST have atleast one tag
# SEVERITY: Medium
# ASSET_TYPE: compute.googleapis.com/Instance
# TAGS: Tags, Cost, Management, Compute Engine
########################################################################

package google.compute.instance.tags

import data.validator.google.lib as lib
import data.validator.google.lib.parameters as gparam
import future.keywords

asset := input.asset

params:= lib.get_default(gparam.global_parameters,"compute",{})

deny [{"msg": message, "details": metadata}] {

	# Check if resource is in exempt list
	exempt_list := lib.get_default(params, "exemptions", [])
	exempt := {asset.name} & {ex | ex := exempt_list[_]}
	not count(exempt) != 0

	tags := lib.get_default(asset.resource.data, "tags", {"items": []})
	count(tags.items) == 0

	message:="Compute resource is missing tags. Ensure appropriate tags are applied."

	metadata:={"name": asset.name}
}

Esempio 2

Assicurati che il tuo carico di lavoro non utilizzi l'account di servizio predefinito di Compute Engine

# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

########################################################################
# DETAILS:  MUST NOT use default service account
# SEVERITY: Medium
# ASSET_TYPE: compute.googleapis.com/Instance
# TAGS: Defaults, Management, Compute Engine
########################################################################

package google.compute.defaultserviceAccount

import data.validator.google.lib as lib
import data.validator.google.lib.parameters as gparam
import future.keywords

asset := input.asset

input_enriched := object.union({"resource": {"data": {"serviceAccounts": []}}}, asset)

params := lib.get_default(gparam.global_parameters, "compute", {})

deny[{
	"msg": "Disallowed default service account",
	"details": {"name": asset.name},
}] {

	account = input_enriched.resource.data.serviceAccounts[_]
	endswith(account.email, params.default_sa)
}

Esempio 3

Assicurati che le VM nel tuo carico di lavoro non utilizzino un indirizzo IP esterno.

# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

########################################################################
# DETAILS:  Ensure VMs dont have External IP
# SEVERITY: High
# ASSET_TYPE: compute.googleapis.com/Instance
# TAGS: Security, Network, Compute Engine, External IP, VM, Virtual Machine
########################################################################

package google.compute.instance.approved.external.ip

import data.validator.google.lib as lib
import data.validator.google.lib.parameters as gparam
import future.keywords

asset := input.asset

params := lib.get_default(gparam.global_parameters, "compute", {})

deny [{"msg": message, "details": metadata}] {

	# Check if resource is in exempt list
	exempt_list := lib.get_default(params, "exemptions", [])
	exempt := {asset.name} & {ex | ex := exempt_list[_]}
	not count(exempt) != 0

	# Find network access config block w/ external IP
	instance := asset.resource.data
	access_config := instance.networkInterfaces[_].accessConfigs
	count(access_config) > 0

	message := sprintf("%v : VM Instance has external IP. current config: %v",[asset.name, access_config])
	metadata := {"name": asset.name}
}

Carica la regola in un bucket Cloud Storage

Dopo aver creato il file .rego, caricalo in un bucket Cloud Storage. Il livello superiore del bucket Cloud Storage deve includere le cartelle /lib e /rules:

  • lib
    • parameters.rego
    • utils.rego
  • /rules
    • rule_name1.rego
    • rule_name2.rego

Passaggi successivi