Rego is the declarative policy language used by Open Policy Agent (OPA). This tile covers writing and testing Rego policies for Kubernetes admission control, Terraform and infrastructure-as-code plan validation, Docker container authorization, HTTP API authorization, RBAC and role-based access control, data filtering, metadata annotations with opa inspect, and OPA policy testing with opa test.
99
Quality
Pending
Does it follow best practices?
Impact
99%
1.19xAverage score across 31 eval scenarios
Pending
The risk profile of this skill
This document covers Regal rules related to set, object, and array comprehensions.
Relevant Regal rules:
comprehension-term-assignment — do not assign a value inside a comprehension just to use it as the term; use the expression directlyuse-object-keys — use object.keys(obj) instead of {k | some k in obj} to get object keysuse-some-for-output-vars — declare output variables with some in comprehension bodiesUse the expression directly as the comprehension term rather than assigning it to a variable first.
# CORRECT: use expression directly as the term
names := {container.name | some container in input.spec.containers}
# CORRECT: multi-field object comprehension
resources := {name: limit |
some container in input.spec.containers
name := container.name
limit := container.resources.limits.cpu
}# WRONG: assigning to variable just to use as term (comprehension-term-assignment violation)
names := {name | some container in input.spec.containers; name := container.name}Use object.keys() to get the keys of an object.
# CORRECT: use object.keys()
required_fields := {"name", "email", "role"}
provided_fields := object.keys(input.user)
missing := required_fields - provided_fields# WRONG: comprehension to get keys (use-object-keys violation)
provided_fields := {k | some k in input.user}Declare output variables with some in comprehension bodies.
# CORRECT: some for output variable
active_users := {user |
some user in input.users
user.active == true
}
# CORRECT: some for key iteration
tag_names := {key | some key, _ in input.resource.tags}package terraform.compliance
import rego.v1
# Use object.keys() — not a comprehension
provided_tags := object.keys(input.resource.tags)
required_tags := {"environment", "owner", "cost_center"}
missing_tags := required_tags - provided_tags
# Use expression directly as term — no intermediate variable
allowed_regions := {"us-east-1", "us-west-2", "eu-west-1"}
deny contains msg if {
count(missing_tags) > 0
msg := sprintf("missing required tags: %v", [missing_tags])
}
deny contains msg if {
input.resource.region != null
not input.resource.region in allowed_regions
msg := sprintf("region '%s' is not allowed", [input.resource.region])
}package terraform.compliance_test
import rego.v1
import data.terraform.compliance
test_missing_tags if {
some msg in compliance.deny
contains(msg, "missing required tags")
} with input as {
"resource": {
"tags": {"environment": "prod"},
"region": "us-east-1"
}
}
test_all_tags_present if {
count(compliance.deny) == 0
} with input as {
"resource": {
"tags": {"environment": "prod", "owner": "team-a", "cost_center": "123"},
"region": "us-east-1"
}
}docs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10
scenario-11
scenario-12
scenario-13
scenario-14
scenario-15
scenario-16
scenario-17
scenario-18
scenario-19
scenario-20
scenario-21
scenario-22
scenario-23
scenario-24
scenario-25
scenario-26
scenario-27
scenario-28
scenario-29
scenario-30
scenario-31