Rego를 사용하여 맞춤 규칙 작성

이 문서에서는 Rego 정책 언어를 사용하여 맞춤 규칙을 작성하는 방법을 설명합니다. 워크로드 관리자에서 이러한 규칙을 사용하여 조직에 정의된 권장사항에 따라 워크로드를 평가할 수 있습니다.

자세한 내용은 워크로드 관리자의 맞춤 규칙 정보를 참고하세요.

시작하기 전에

Rego를 사용하여 맞춤 규칙 작성

Google은 워크로드를 평가하는 데 사용할 수 있는 사전 정의된 규칙 집합이 포함된 샘플 GitHub 저장소를 제공합니다. 이 샘플은 여러 사용 사례를 다룹니다. 저장소에서 규칙을 선택하거나 평가 요구사항을 설명하는 규칙 (.rego) 파일을 만듭니다.

맞춤 규칙에는 다음 섹션이 있습니다.

  • 메타데이터. 다음 필드는 규칙 메타데이터를 정의합니다.

    • DETAILS: 규칙에 대한 간단한 설명입니다.
    • SEVERITY: 규칙 위반의 심각도를 정의하는 사용자 정의 값입니다. 예를 들면 HIGH, CRITICAL, MEDIUM, LOW입니다.
    • ASSET_TYPE: 지원되는 애셋 중 하나입니다. 지원되는 데이터 소스를 참고하세요.
    • TAGS: 규칙의 하나 이상의 태그입니다. 이러한 태그는 규칙을 필터링하는 데 도움이 됩니다.
  • 패키지 선언 예를 들면 templates.google.compute.instance.label입니다.

  • import 문 예를 들면 data.validator.google.lib as lib입니다.

  • 규칙 정의: 규칙을 정의하는 안내 모음입니다.

규칙 예

다음 샘플 규칙은 GoogleCloudPlatform/workload-manager GitHub 저장소에서 확인할 수 있습니다. 이러한 규칙을 있는 그대로 Cloud Storage 버킷에 업로드하고 이를 사용하여 평가를 실행할 수 있습니다. 또는 조직 정책에 따라 규칙을 수정한 후 파일을 Cloud Storage 버킷에 업로드합니다.

  • 예 1: VM에 라벨이 하나 이상 있는지 확인합니다.
  • 예 2: 워크로드가 Compute Engine 기본 서비스 계정을 사용하지 않도록 합니다.
  • 예 3: 워크로드의 VM이 외부 IP 주소를 사용하지 않도록 합니다.

Workload Manager에서 사용할 수 있는 샘플 규칙의 전체 목록은 GoogleCloudPlatform/workload-manager GitHub 저장소를 참고하세요.

예 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

워크로드의 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

다음 단계