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
Rate limiting in Rego uses the default rule := value pattern to provide a fallback limit. This is the same pattern as default allow := false — just applied to a non-boolean rule. Multiple rule heads then return tier-specific values when conditions match.
default rule := valueDeclare the fallback with default, then add specific rule heads for each tier:
# Fallback for unknown tiers — same pattern as `default allow := false`
default user_limit := 10
# Specific values for known tiers
user_limit := 1000 if data.user_tiers[input.user] == "premium"
user_limit := 100 if data.user_tiers[input.user] == "standard"The default declaration ensures user_limit always has a value even when no tier matches. Do not use else := — Regal flags this with default-over-else.
# METADATA
# title: Rate Limiting Policies
# description: Enforces rate limits based on user tier with a safe default fallback
# authors:
# - API Security Team <api-security@example.com>
# custom:
# category: http-authorization
package httpapi.authz
import rego.v1
default allow := false
# METADATA
# title: Allow rate-limited requests
# description: Permits requests when the user is within their rate limit
# entrypoint: true
# custom:
# severity: MEDIUM
allow if {
not rate_limit_exceeded
}
rate_limit_exceeded if {
input.request_count >= user_limit
}
# Default fallback for unknown tiers
default user_limit := 10
user_limit := 1000 if data.user_tiers[input.user] == "premium"
user_limit := 100 if data.user_tiers[input.user] == "standard"Input:
{
"user": "bob",
"request_count": 120
}Data (data.user_tiers):
{
"alice": "premium",
"bob": "standard"
}Result: allow == false — bob is standard tier (limit 100), request_count 120 >= 100.
Per the Regal file-missing-test-suffix rule, test files must use a _test.rego suffix. Import the policy package and reference rules via the alias.
# authz_test.rego
package httpapi.authz_test
import rego.v1
import data.httpapi.authz # import the policy package under test
tiers := {"alice": "premium", "bob": "standard"}
# Premium user within limit
test_premium_user_allowed if {
authz.allow with input as {"user": "alice", "request_count": 999}
with data.user_tiers as tiers
}
# Standard user over limit
test_standard_user_denied if {
not authz.allow with input as {"user": "bob", "request_count": 101}
with data.user_tiers as tiers
}
# Unknown user gets default limit of 10
test_unknown_user_default_limit if {
not authz.allow with input as {"user": "unknown", "request_count": 11}
with data.user_tiers as tiers
}
# Unknown user within default limit
test_unknown_user_within_default if {
authz.allow with input as {"user": "unknown", "request_count": 5}
with data.user_tiers as tiers
}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