Site icon RegoLand

Rego playground

Rego is declarative in nature and was inspired by Datalog query language.  Writing queries in Rego takes some practice, but definitely worth learning. Rego supports structured document model such as JSON.

Before we start to dive into Rego, a basic knowledge of Open Policy Agent (OPA) will help us to understand the purpose of Rego and its applicability. 

OPA consists of three in-memory compartments:

  1. Data – a set of facts about the outside world that OPA refers to while making a decision. The data must be provided to OPA in JSON format.
  2. Query Input – triggers the decision computation. It specifies the question that OPA should decide upon. The query input must bet formatted in JSON.
  3. Policy specifies computational logic that for the given data and query input yields a policy decision – query result. The policy decision generated by the Policy Engine is in JSON document. 
Rego (pronounced as "ray-go") is OPA's policy language. Policies written in rego have access to Query Input and Data parts (as JSON values only) under global variables "input" and "data". 

The simplest way to start with Rego is to use Rego Playground. Rego Playground is a browser-based UI for composing, evaluating, and debugging Rego quires with access to the Policy, Data, and Query Input parts ( The ultimate experience while working with Rego comes from using IntelliJ Plugin. We will dedicate a separate post to the plugin.

Rego Playground: Rego basics example
## Policy
package rego.basic

# making sure that we have default value for the rule `hello` 
default hello = false

# define logic for the rule here 
hello {
    m := input.message
    m == "world"
// Input query
    "message": "world"
// Data
// Evaluated Policy Output
    "hello": true

Our rego.basic policy evaluates to { “hello” : true }. In line 10, we are accessing the Query Input. In line 11, we are comparing extracted value to the value “world”, if the values match then our policy output is { “hello” : true }, otherwise it will be evaluated to { “hello” : false }.

This is a very basic example, but I encourage you to spend some time with Rego Playground, trying to change the values of the Input query and introduce Data (you can access Data in similar way as Query Input but use “data.” prefix instead of “input.”).

Exit mobile version