כתיבת כללים בהתאמה אישית באמצעות Rego

במסמך הזה מוסבר איך לכתוב כללים בהתאמה אישית באמצעות שפת המדיניות Rego. אפשר להשתמש בכללים האלה בכלי לניהול עומס העבודה כדי להעריך את עומסי העבודה בהשוואה לשיטות המומלצות שהוגדרו לארגון.

מידע נוסף זמין במאמר מידע על כללים בהתאמה אישית בכלי לניהול עומס העבודה.

לפני שמתחילים

כתיבת כללים בהתאמה אישית באמצעות Rego

‫Google מספקת מאגר לדוגמה ב-GitHub עם קבוצה של כללים מוגדרים מראש שבהם אפשר להשתמש כדי להעריך את עומסי העבודה. הדוגמאות האלה כוללות כמה תרחישי שימוש. בוחרים כללים ממאגר הכללים או יוצרים קובץ כללים (.rego) שמתאר את דרישות ההערכה.

כלל בהתאמה אישית כולל את הקטעים הבאים:

  • מטא-נתונים. השדות הבאים מגדירים את המטא-נתונים של הכלל:

    • DETAILS: תיאור קצר של הכלל.
    • SEVERITY: ערך שמוגדר על ידי המשתמש ומגדיר את חומרת ההפרה של הכלל. לדוגמה, HIGH,‏ CRITICAL,‏ MEDIUM או LOW.
    • ASSET_TYPE: אחד מהנכסים הנתמכים. אפשר לעיין ברשימה של מקורות הנתונים הנתמכים.
    • TAGS: תג אחד או יותר לכלל. התגים האלה עוזרים לסנן את הכללים.
  • הצהרת חבילה. לדוגמה, templates.google.compute.instance.label.

  • ייבוא דוחות. לדוגמה, data.validator.google.lib as lib.

  • הגדרות של כללים: קבוצת הוראות שמגדירה את הכלל.

דוגמאות לכללים

כללי הדוגמה הבאים זמינים במאגר GitHub‏ GoogleCloudPlatform/workload-manager. אתם יכולים להעלות את הכללים האלה כמו שהם לקטגוריה שלכם ב-Cloud Storage ולהשתמש בהם כדי להריץ את ההערכות. לחלופין, אפשר לשנות את הכללים בהתאם למדיניות הארגון ואז להעלות את הקבצים לקטגוריה של Cloud Storage.

  • דוגמה 1: מוודאים שיש לפחות תווית אחת למכונות ה-VM.
  • דוגמה 2: מוודאים שעומס העבודה לא משתמש בחשבון השירות שמוגדר כברירת מחדל ב-Compute Engine.
  • דוגמה 3: מוודאת שמכונות וירטואליות בעומס העבודה לא משתמשות בכתובת IP חיצונית.

רשימה מלאה של כללי דוגמה שאפשר להשתמש בהם בכלי לניהול עומס העבודה זמינה במאגר GitHub‏ GoogleCloudPlatform/workload-manager.

דוגמה 1

הכלל מוודא שיש לפחות תג אחד למשאבי 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}
}

דוגמה 2

מוודא שעומס העבודה לא משתמש בחשבון השירות שמוגדר כברירת מחדל של 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)
}

דוגמה 3

מוודא שהמכונות הווירטואליות בעומס העבודה לא משתמשות בכתובת IP חיצונית.

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

העלאת הכלל לקטגוריה של Cloud Storage

אחרי שיוצרים את הקובץ .rego, מעלים אותו לקטגוריה של Cloud Storage. הרמה העליונה של הקטגוריה ב-Cloud Storage חייבת לכלול את התיקיות /lib ו-/rules:

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

המאמרים הבאים