Rego を使用してカスタムルールを作成する

このドキュメントでは、Rego ポリシー言語を使用してカスタムルールを作成する方法について説明します。Workload Manager でこれらのルールを使用して、組織に定義されたベスト プラクティスと照らし合わせてワークロードを評価できます。

詳細については、Workload Manager のカスタムルールについてをご覧ください。

始める前に

Rego を使用してカスタムルールを作成する

Google は、ワークロードの評価に使用できる一連の事前定義ルールを含むサンプル GitHub リポジトリを提供しています。これらのサンプルは複数のユースケースをカバーしています。リポジトリからルールを選択するか、評価要件を記述するルール(.rego)ファイルを作成します。

カスタムルールには次のセクションがあります。

  • メタデータ。次のフィールドはルールのメタデータを定義します。

    • DETAILS: ルールの簡単な説明。
    • SEVERITY: ルール違反の重大度を定義するユーザー定義値。たとえば、HIGHCRITICALMEDIUMLOW です。
    • ASSET_TYPE: サポートされているアセットのいずれか。サポートされるデータソースをご覧ください。
    • TAGS: ルールの 1 つ以上のタグ。これらのタグは、ルールのフィルタに役立ちます。
  • パッケージ宣言。例: templates.google.compute.instance.label

  • インポート ステートメント。例: data.validator.google.lib as lib

  • ルール定義。ルールを定義する一連の手順。

ルールの例

次のサンプルルールは、GitHub リポジトリ GoogleCloudPlatform/workload-manager で入手できます。これらのルールをそのまま Cloud Storage バケットにアップロードして、評価の実行に使用できます。または、組織のポリシーに従ってルールを変更し、ファイルを Cloud Storage バケットにアップロードします。

  • 例 1: VM に少なくとも 1 つのラベルがあることを確認します。
  • 例 2: ワークロードで Compute Engine のデフォルトのサービス アカウントが使用されないようにします。
  • 例 3: ワークロード内の VM が外部 IP アドレスを使用しないようにします。

Workload Manager で使用できるサンプルルールの一覧については、GitHub の GoogleCloudPlatform/workload-manager リポジトリをご覧ください。

例 1

Compute Engine リソースに少なくとも 1 つのタグがあることを確認します。

# 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

ワークロード内の VM が外部 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

次のステップ