skip to content

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 pageshow

explore

questions

page 2 of 2

A Rego complete rule ran green for months and now errors on one plan. Why?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Most 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.

open as a page

Your Rego secrets rule has denied nothing in six weeks of evaluation — how do you find out why?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Silence 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.

open as a page

Your Rego deny rule names only the first container missing resource limits — how do you fix it?

level: seniorimportance: should knowfreq 36%

basics

~20 s

The 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.

open as a page

How do you use OPA to list every database violating a backup rule held in an external inventory?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Compile 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.

open as a page

Why does a Rego test whose only assertion is deny with input as bad_pod always pass?

level: seniorimportance: should knowfreq 45%

basics

~20 s

A 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.

open as a page

What does opa check --strict reject in a .rego file that plain opa check accepts?

level: middleimportance: nice to knowfreq 34%

basics

~20 s

Strict 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.

open as a page

In Rego, how do you assert on a JSON blob that arrives as a string field?

level: middleimportance: nice to knowfreq 34%

basics

~20 s

Decode 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.

open as a page

In opa eval, how do --explain=notes and --explain=fails differ?

level: middleimportance: nice to knowfreq 34%

basics

~20 s

Both 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.

open as a page

What lets OPA index the rules of a Rego document, and which body shapes defeat it?

level: middleimportance: nice to knowfreq 31%

basics

~20 s

OPA 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.

open as a page

An auditor asks which licence list your Rego policy enforced six months ago. How do you answer?

level: seniorimportance: nice to knowfreq 32%

basics

~20 s

You 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.

open as a page

Why does OPA reject a Rego helper that calls itself to walk a base-image chain?

level: seniorimportance: nice to knowfreq 29%

basics

~10 s

Rego 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.

open as a page

You own a shared Rego policy library for 40 teams — standardize on deny sets or one allow decision?

level: principalimportance: nice to knowfreq 27%

basics

~20 s

Standardize 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.

open as a page

When is owning a Rego-to-SQL residual translation layer worth it over hand-writing the compliance query?

level: principalimportance: nice to knowfreq 24%

basics

~20 s

It 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.

open as a page

showing 31–43 of 43