Rego
Rego is the declarative policy language of Open Policy Agent (OPA), a CNCF graduated project. Rego rules compute decisions as JSON-compatible documents from the input document and the data document. This skill pins OPA v1.21.1 and its documentation, and helps the agent write, test, package and query Rego policies that follow the documented language and OPA interfaces.
Follow the workflow below step by step. Every rule here comes from a source in Sources. OPA pages have no section numbers, so each rule cites the page and heading it comes from, for example (Policy Language, Default Keyword). When a rule and the pinned source disagree, the source wins; when the source has a newer revision than the pin, follow the refresh steps.
Inputs (fill in, or ask before starting)
- Role: policy author, reviewer, test author, bundle producer (
opa build), or decision caller (an app that queries OPA). - Decision shape: a boolean
allow, a set ofdenymessages, or a structured object. This decides the rule kinds you use. - Runtime: the OPA version of every consumer that loads the policy (OPA server, sidecar,
opa eval, Go SDK, Wasm). Opt-in syntax such asand/oror$"..."strings needs a recent enough consumer. - Target version: Rego v1 (OPA 1.x) is current and the default, at OPA v1.21.1. Rego v0 (OPA 0.x) is legacy: read it and upgrade from it, never author it. Rego v2 import (unreleased) is a preview (posture: track): never emit
import rego.v2. Seereferences/versions.md. - Revision: the pinned revision in Sources, unless the user names another.
- Sources: when refreshing this skill or when a rule looks out of date, check the OPA releases page for a newer release and read its CHANGELOG entry, re-read every URL in Sources, and update the pins.
Invariants
- Rule bodies use
if; multi-value rules usecontains. In Rego v1 a rule with a body withoutifis a compile error, and a partial set rule needscontains(Upgrading to v1.0, Enforce use ofifandcontainskeywords). - Undefined is not false. A rule whose body never succeeds is undefined, and so is any expression that refers to it, including
!=(Policy Language, The Basics). Give every decision rule adefault(Policy Language, Default Keyword). A deny rule written with!=or a positive check on a missinginputfield never fires, so write deny conditions asnot <required condition>. - Legacy
notfails on undefined sub-expressions. Withoutimport future.keywords.not,not "x" in input.missingfails the whole rule instead of succeeding. Import it (OPA v1.17.0 or later) in modules whosenotguards a denial (Rego Keyword: not, Improved Negation Semantics). - A complete rule has one value. Two definitions of a complete rule or function that produce different values are a conflict error (Policy Language, Complete Definitions; Functions).
- Bodies are AND, rule definitions are OR. Expressions in a body must all hold; rules with the same name are unioned (Policy Language, The Basics; Incremental Definitions).
- Variables are existential.
x := xs[_]; x != "a"holds if some element differs. Express FOR ALL withevery, or withnotover a helper rule (Policy Language, Universal Quantification). - Safety. Every variable in a rule head, in a negated expression, or used as a built-in input must be bound by a non-negated expression in the same rule (Policy Language, Variables; Negation; Built-in Functions).
- Use
:=and==, not=, unless you need unification.:=declares a local and may not be repeated or used before assignment;==requires both sides to be bound (Policy Language, Assignment; Best Practices for Equality and Assignment). inputanddataare reserved. No rule or variable may be namedinputordata; duplicate or shadowing imports are compile errors (Upgrading to v1.0,inputanddatakeywords are reserved; Prohibit duplicate imports).- Built-in errors are undefined by default. A failing built-in call evaluates to undefined and does not halt evaluation unless strict built-in errors are on (Policy Language, Built-in Functions, Errors).
- Negating
everyis forbidden. Writesome x in xs; not p(x)instead (Policy Language, Every Keyword). andandorproduce no value and needimport future.keywords.and/or;rego.v1does not enable them (Rego Keywords: and, or, Enabling; Expressions, not values).- An undefined decision returns 200 with no
result.POST /v1/data/{path}answers an undefined document with HTTP 200 and omitsresult; callers must treat a missingresultas no decision (REST API, Get a Document (with Input)). - Bundles own their roots. Without
rootsin.manifest, the bundle owns all ofdata; policies and data in a bundle must sit under its roots (Bundles, Bundle File Format; Multiple Sources of Policy and Data). - Signed bundles activate only after verification. OPA activates a signed bundle only if
.signatures.jsonverifies with a key configured out of band (Bundles, Signing).
Workflow
- Pick the version. Write Rego v1. If the code has rule bodies without
if,p[x] { ... }set rules, orimport rego.v1, it is Rego v0 or written for both: plan the upgrade (step 9). ->references/versions.md✓ The target is Rego v1, and the OPA version of every consumer is known. - Lay out modules and packages. One
packageper module, imports at the top, policy atdata.<package path>. Put tests in a<package>_testpackage. ->references/language.md✓ Every module has exactly one package, no unused or duplicate imports, and package paths match the decision paths callers query. - Design the decision. Choose deny-by-default:
default allow := falsewithallow ifrules, or adeny contains msg ifset withallow if count(deny) == 0. Return structured decisions as an object rule. ->references/authorization-patterns.md✓ Every decision rule queried by a caller has adefault, and no path yields allow wheninputis missing a field. - Write the rules. Use the right rule kind (complete, partial set, partial object, function,
elsechain),some ... infor iteration,infor membership,everyfor FOR ALL, comprehensions for aggregation. ->references/language.md✓opa checkpasses, and no complete rule can produce two values. - Use built-ins and data deliberately. Use only built-ins in the reference index, and keep external data in
data(bundles or the Data API) rather thanhttp.sendon the hot path. ->references/builtins-and-data.md✓ Every built-in exists in the target OPA's capabilities, and failing built-ins cannot turn into allow. - Test. Write
test_rules that mockinput,dataand functions withwith; cover allow and deny for each rule; runopa testwith coverage. ->references/testing-and-tooling.md✓opa test . -vpasses,--fail-on-emptyis set in CI, and coverage meets the agreed--threshold. - Check, format and tune. Run
opa fmt,opa check --strict, andopa check --capabilitiesfor the oldest consumer. Apply the performance basics where latency matters. ->references/testing-and-tooling.md✓opa fmt --failandopa check --strictpass, and hot rules use indexable statements. - Package and serve. Build a bundle with
opa build, declareroots, sign it when consumers verify, and query the decision throughPOST /v1/data/<path>. ->references/builtins-and-data.md✓ The manifest hasrevisionandroots, and the caller denies on a missingresultor any non-200 response. - Upgrade (only when asked). Follow the Rego v0 to Rego v1 steps: upgrade bundle producers before consumers, run
opa check --v0-v1, thenopa fmt --write --v0-v1, then remove the v0 imports once every consumer is OPA 1.x. ->references/versions.md✓opa checkpasses without--v0-compatible, and the tests give the same results as before.
Verify before done
- Every rule with a body has
if, and every multi-value rule hascontains(Upgrading to v1.0). - Every decision rule a caller queries has a
default; the default for an authorization decision denies (Policy Language, Default Keyword). - No FOR ALL is written as
x := xs[_]; x != v(Policy Language, Universal Quantification). - Evaluating every decision with
input{}denies; deny conditions usenotrather than!=, and modules that negate composite expressions importfuture.keywords.not(Rego Keyword: not). -
opa fmt --fail,opa check --strictandopa test --fail-on-emptypass (CLI Reference; Policy Language, Strict Mode). -
opa check --capabilitiespasses for the oldest consumer when opt-in syntax or newer built-ins are used (CLI Reference, check). - Tests cover both outcomes of each
allowanddenyrule, and mocks usewith(Policy Testing). - The caller treats a missing
result, an error, or a timeout as deny (REST API, Data API). - Bundle
.manifestdeclaresroots, and signed bundles have a verification key configured (Bundles). - Nothing emits
import rego.v2(preview, posture track).
Reference index
references/versions.md: Rego v1, Rego v0 and therego.v2preview, what OPA 1.0 changed, the producer-before-consumer upgrade and theopa check/opa fmtupgrade commands. Load for steps 1 and 9.references/language.md: modules, packages, imports, values, references, every rule kind, the keywords and operators, comprehensions,with, metadata annotations, and common mistakes. Load for steps 2 and 4.references/authorization-patterns.md: deny-by-default, deny sets, RBAC overdata, structured decisions,elseordering, and decision-call handling. Load for step 3.references/builtins-and-data.md: the base and virtual document model, verified built-ins with the OPA version that introduced them, built-in errors, the Data and Policy APIs, bundles, manifests and signing. Load for steps 5 and 8.references/testing-and-tooling.md:opa test, test results, parameterized tests, mocking, coverage,opa check, strict mode,opa fmt, capabilities, and performance basics. Load for steps 6 and 7.
Related skills
cedarfor the Cedar policy language:npx skills add ScaleDockHQ/scaledock-skills --skill cedar.openfgafor relationship-based authorization models:npx skills add ScaleDockHQ/scaledock-skills --skill openfga.celfor the Common Expression Language:npx skills add ScaleDockHQ/scaledock-skills --skill cel.authzenfor the AuthZEN authorization API between enforcement and decision points:npx skills add ScaleDockHQ/scaledock-skills --skill authzen.
Sources
Status uses the publishing body's own maturity term. Checked is the date the source was last read.
- OPA Policy Language: Documentation (latest), docs source at
mainas of 2026-10-05, checked 2026-10-05. - OPA Policy Reference: Documentation (latest), grammar and reserved names as of 2026-10-05, checked 2026-10-05.
- Rego Keyword: import: Documentation (latest), includes the unreleased
rego.v2section, checked 2026-10-05. - Rego Keyword: not: Documentation (latest), improved negation semantics since OPA v1.17.0, checked 2026-10-05.
- Rego Keywords: and, or: Documentation (latest), keywords since OPA v1.20.0, checked 2026-10-05.
- Rego Built-ins: Documentation (latest), generated from built-in metadata, checked 2026-10-05.
- OPA built-in metadata: Released, v1.21.1, checked 2026-10-05.
- OPA capabilities: Released, v1.21.1 (features
rego_v1,template_strings,keywords_in_refs; future keywordsand,not,or), checked 2026-10-05. - OPA Policy Testing: Documentation (latest), checked 2026-10-05.
- Upgrading to v1.0: Documentation (latest), checked 2026-10-05.
- v0 Backwards Compatibility: Documentation (latest), checked 2026-10-05.
- OPA REST API: Documentation (latest), Data API with
rule_labelssince OPA v1.21.0, checked 2026-10-05. - OPA Bundles: Documentation (latest), manifest schema v1, checked 2026-10-05.
- OPA Policy Performance: Documentation (latest), checked 2026-10-05.
- OPA CLI Reference: Documentation (latest), generated from the
opacommand help, checked 2026-10-05. - OPA Philosophy: The OPA Document Model: Documentation (latest), checked 2026-10-05.
- OPA releases: Released, v1.21.1 (2026-09-29) latest; v1.0.0 (2024-12-20); v0.70.0 (2024-10-31) last 0.x, checked 2026-10-05.
- OPA CHANGELOG: Changelog,
mainas of 2026-10-05 (Unreleased section addsimport rego.v2), checked 2026-10-05. Preview posture: track.