Before you begin
- Be familiar with Rego policy language.
Write custom rules using Rego
Google provides a sample GitHub repository with a set of predefined rules that
you can use to evaluate your workloads. These samples cover multiple use cases.
Select rules from the repository or create a rule (.rego
) file that describes
your evaluation requirements.
A custom rule has the following sections:
Metadata. The following fields define the rule metadata:
DETAILS
: a short description for the rule.SEVERITY
: a user-defined value that defines the severity of violation of the rule. For example,HIGH
,CRITICAL
,MEDIUM
, orLOW
.ASSET_TYPE
: one of the supported assets. See Supported data sources.TAGS
: one or more tags for the rule. These tags help filter the rules.
Package declaration. For example,
templates.google.compute.instance.label
.Import statements. For example,
data.validator.google.lib as lib
.Rule definitions. a set of instructions that defines the rule.
Example rules
The following sample rules are available in the GoogleCloudPlatform/workload-manager GitHub repository. You can upload these rules as they are to your Cloud Storage bucket and use it to run your evaluations. Alternatively, modify the rules as per your organization policies and then upload the files to a Cloud Storage bucket.
- Example 1: ensures that there is at least one label for your VMs.
- Example 2: ensures that your workload does not use the Compute Engine default service account.
- Example 3: ensures that VMs in your workload don't use an external IP address.
For a full list of sample rules that you can use in Workload Manager, see the GoogleCloudPlatform/workload-manager GitHub repository.
Example 1
Ensures that there is at least one label for the VMs in your workload.
Example 2
Ensures that your workload does not use the Compute Engine default service account
Example 3
Ensures that VMs in your workload don't use an external IP address.
Upload the rule to a Cloud Storage bucket
After you create the .rego
file, upload it a Cloud Storage bucket. The
top level of your Cloud Storage bucket must include the /lib
and /rules
folders:
lib
parameters.rego
utils.rego
/rules
rule_name1.rego
rule_name2.rego