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 metadata annotations on policies and rules.
Relevant Regal rules:
missing-metadata — modules and rules should have metadata annotationsdetached-metadata — metadata comments must be immediately above the rule with no blank lines betweenno-defined-entrypoint — at least one rule should be annotated with entrypoint: trueEvery module must have a package-level annotation block using the # METADATA comment syntax.
# METADATA
# title: API Authorization Policy
# description: Enforces role-based access control for the REST API
# authors:
# - Platform Security Team
# related_resources:
# - ref: https://www.openpolicyagent.org/docs/latest/
# schemas:
# - input: schema.input
package api.authz
import rego.v1Mark the primary decision rule as the entrypoint.
# METADATA
# title: Allow
# description: Grants access when the user's role has the required permission
# entrypoint: true
default allow := false
allow if {
user_role := data.user_roles[input.user]
required_permission in data.role_permissions[user_role]
}The # METADATA block must immediately precede the rule it annotates — no blank lines in between.
# CORRECT: annotation immediately above the rule
# METADATA
# title: Deny
# description: Collects policy violation messages
deny contains msg if {
not input.user
msg := "user is required"
}# WRONG: blank line between annotation and rule (detached-metadata violation)
# METADATA
# title: Deny
deny contains msg if { # blank line above detaches the annotation
not input.user
msg := "user is required"
}# METADATA
# title: RBAC Authorization
# description: |
# Role-based access control for API endpoints.
# Users have roles; roles have permissions; allow when the user's role
# grants the required permission.
# authors:
# - Platform Team
# schemas:
# - input: schema["input.json"]
package rbac.authz
import rego.v1
# METADATA
# title: Allow
# description: Grants access when user role has the required permission
# entrypoint: true
default allow := false
allow if {
user_role := data.user_roles[input.user]
required_permission in data.role_permissions[user_role]
}
# METADATA
# title: User Role
# description: Returns the role assigned to the requesting user
user_role := role if {
role := data.user_roles[input.user]
}
# METADATA
# title: Required Permission
# description: Derives the permission key from method and path
required_permission := permission if {
action := lower(input.method)
resource := trim_prefix(input.path, "/api/")
permission := sprintf("%s:%s", [resource, action])
}Annotations do not affect runtime behavior — test the rules as normal.
package rbac.authz_test
import rego.v1
import data.rbac.authz
test_allow_engineer_read if {
authz.allow
with input as {"user": "alice", "method": "GET", "path": "/api/reports"}
with data.user_roles as {"alice": "engineer"}
with data.role_permissions as {"engineer": ["reports:get"]}
}
test_deny_engineer_write if {
not authz.allow
with input as {"user": "alice", "method": "POST", "path": "/api/reports"}
with data.user_roles as {"alice": "engineer"}
with data.role_permissions as {"engineer": ["reports:get"]}
}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