Rego Language
You will learn to write and test Rego: how a rule resolves over the input and data documents, why deny-by-default is idiomatic, and how opa test proves it. Interviewers ask you to sketch a deny rule.
on this pageshowhide
explore
- How a Rule Resolves14 questions
- Rules and Documents3 questions
- Unification and Binding4 questions
- Undefined Is Not False4 questions
- Seeing Inside a Rule3 questions
- Rules Over Real Input19 questions
- Proving the Rule10 questions
- Tests as Ordinary Rules3 questions
- Coverage and Hot Paths3 questions
- Partial Evaluation4 questions
questions
page 2 of 2A Rego complete rule ran green for months and now errors on one plan. Why?
basics
~20 sMost likely the input finally contained two matching resources. A complete rule may produce only one value, so a body that iterates and succeeds twice with different values raises an evaluation conflict and the whole query fails.
Your Rego secrets rule has denied nothing in six weeks of evaluation — how do you find out why?
basics
~20 sSilence is not evidence: a clean estate and a broken rule produce identical output. Replay a violating fixture and a sample of real objects offline — if the bad fixture also passes, the rule's body is undefined.
Your Rego deny rule names only the first container missing resource limits — how do you fix it?
basics
~20 sThe comprehension collapses every offender into one array, and indexing it at [0] picks a single name. Move the iteration into the rule body so each binding produces its own set entry, and iterate the container field names so init and ephemeral containers are covered too.
How do you use OPA to list every database violating a backup rule held in an external inventory?
basics
~20 sCompile the rule with the resource set declared unknown, then translate the residual conditions into the inventory's own query language and run them there. OPA produces the filter; the inventory produces the list. Never stream a million records through the engine.
Why does a Rego test whose only assertion is deny with input as bad_pod always pass?
basics
~20 sA partial set rule such as deny always produces a document, empty when nothing matched, and in Rego every value except false is truthy. So the expression succeeds whether or not the rule fired, and the test asserts nothing.
What does opa check --strict reject in a .rego file that plain opa check accepts?
basics
~20 sStrict mode promotes lint-grade problems to compile errors: duplicate imports, unused imports, unused local assignments, using input or data as a variable or rule name, and deprecated built-in aliases. Plain opa check only reports genuine parse and compile errors.
In Rego, how do you assert on a JSON blob that arrives as a string field?
basics
~20 sDecode it first: json.unmarshal turns the string into a value you can reference, and yaml.unmarshal does the same for embedded YAML. Until you decode, traversal sees one opaque string and finds none of the fields inside it.
In opa eval, how do --explain=notes and --explain=fails differ?
basics
~20 sBoth filter the same evaluation trace. notes shows only what the policy itself said — print and trace output. fails shows only the expressions that did not hold, which is how you find where an undefined rule stopped.
What lets OPA index the rules of a Rego document, and which body shapes defeat it?
basics
~20 sOPA indexes rules that share a name using equality tests between a plain reference into input or data and a constant, so a query only evaluates the rules whose key matches. Negations, comprehensions, walks and helper calls cannot be indexed.
An auditor asks which licence list your Rego policy enforced six months ago. How do you answer?
basics
~20 sYou can answer only if the fact was pinned. A licence list shipped in the policy's data document has a version you can name and replay; one fetched by http.send at decision time was never captured.
Why does OPA reject a Rego helper that calls itself to walk a base-image chain?
basics
~10 sRego forbids recursion. The compiler builds a dependency graph over rules and functions and rejects any cycle, so the helper never loads. Use a transitive-closure built-in, or resolve the chain before evaluation.
You own a shared Rego policy library for 40 teams — standardize on deny sets or one allow decision?
basics
~20 sStandardize on deny sets for almost everything, because they are additive, tool-native and produce usable messages, then buy back the fail-closed property in the harness. Reserve an explicit allow decision for the few gates where a silently absent rule is unacceptable.
When is owning a Rego-to-SQL residual translation layer worth it over hand-writing the compliance query?
basics
~20 sIt pays when the same rules must be reported over several data stores and also enforced elsewhere, so one policy stays the single source of truth. For one stable rule over one store, a hand-written query is cheaper and easier to defend.
showing 31–43 of 43