Menulis aturan kustom menggunakan Rego

Dokumen ini menjelaskan cara menulis aturan kustom menggunakan bahasa kebijakan Rego. Anda dapat menggunakan aturan ini di Workload Manager untuk mengevaluasi beban kerja Anda berdasarkan praktik terbaik yang ditentukan untuk organisasi Anda.

Untuk mengetahui informasi selengkapnya, lihat Tentang aturan kustom di Workload Manager.

Sebelum memulai

Menulis aturan kustom menggunakan Rego

Google menyediakan contoh repositori GitHub dengan serangkaian aturan standar yang dapat Anda gunakan untuk mengevaluasi beban kerja. Contoh ini mencakup beberapa kasus penggunaan. Pilih aturan dari repositori atau buat file aturan (.rego) yang menjelaskan persyaratan evaluasi Anda.

Aturan kustom memiliki bagian berikut:

  • Metadata. Kolom berikut menentukan metadata aturan:

    • DETAILS: deskripsi singkat untuk aturan.
    • SEVERITY: nilai buatan pengguna yang menentukan tingkat keparahan pelanggaran aturan. Misalnya, HIGH, CRITICAL, MEDIUM, or LOW.
    • ASSET_TYPE: salah satu aset yang didukung. Lihat Sumber data yang didukung.
    • TAGS: satu atau beberapa tag untuk aturan. Tag ini membantu memfilter aturan.
  • Deklarasi paket. Contoh, templates.google.compute.instance.label.

  • Mengimpor laporan. Contoh, data.validator.google.lib as lib.

  • Definisi aturan. serangkaian petunjuk yang menentukan aturan.

Contoh aturan

Contoh aturan berikut tersedia di repositori GitHub GoogleCloudPlatform/workload-manager. Anda dapat mengupload aturan ini apa adanya ke bucket Cloud Storage dan menggunakannya untuk menjalankan evaluasi. Atau, ubah aturan sesuai kebijakan organisasi Anda, lalu upload file ke bucket Cloud Storage.

  • Contoh 1: memastikan bahwa ada setidaknya satu label untuk VM Anda.
  • Contoh 2: memastikan bahwa beban kerja Anda tidak menggunakan akun layanan default Compute Engine.
  • Contoh 3: memastikan bahwa VM dalam beban kerja Anda tidak menggunakan alamat IP eksternal.

Untuk mengetahui daftar lengkap contoh aturan yang dapat Anda gunakan di Pengelola Beban Kerja, lihat repositori GitHub GoogleCloudPlatform/workload-manager.

Contoh 1

Memastikan bahwa ada minimal satu tag untuk resource 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}
}

Contoh 2

Memastikan bahwa beban kerja Anda tidak menggunakan akun layanan default 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)
}

Contoh 3

Memastikan bahwa VM dalam beban kerja Anda tidak menggunakan alamat IP eksternal.

# 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}
}

Mengupload aturan ke bucket Cloud Storage

Setelah membuat file .rego, upload file tersebut ke bucket Cloud Storage. Tingkat atas bucket Cloud Storage Anda harus menyertakan folder /lib dan /rules:

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

Langkah selanjutnya