Sty -OPA - Rego : Expressions
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Single Value : There are a number of design princples that we can put in place when we design Rego, But here are the top three.
- Syntax : In terms is Rego syntax we have designed it to mirror those real world polices that you find in PDFs , E-Mails,
So the idea of Rego statement is that it is a simple IF Statements, Something are true if the other things are true . For example , allow this request if this user is actually alice
2. Semantics : The second design principle is more symantic. The idea here was the hierarchical data JSON/YAML and so on are pervasive so we wanted Rego to support that hierarchical data in a first class way and so
3. Algorithm : OPA coould automatically optimize performance so the policy can often focus on correctness and make retain able polices
Rego Overview
one you write those rules you organize those rules into policies. Policies here is just a set of Rules that have a hierarchical name.
Rego also supports a set .Set is just an unordered collection of distinguished values
Variables are mutable so do not assign them twice or you might end up having some kind of errors. Example you get a compile error.
- Arrays use the square brackets
- Sets use the curly brackets
- Object uses the curly bracket with key value pairs separated by a colon .
There are couple of global variables that OPA takes care of .
- Input : When a service is asking OPA for policy decision , it will hand over some chunk of JSON. That chunk of data when you are writing policy you reference it via input. Input is a global variable storing the JSON object given to OPA.
- Data : Data is also a global variable, remember OPA can also be given arbitrary external data as long as that data is in JSON . So here whatever data is given to OPA can be reference using data keyword
Once you have a variable whether it is managed by OPA once you have a variable whether it is managed by OPA , input or data or you assigned your self then you need to be able to inspect internals those of those variables
For inspecting internals of an object we use Brackets so we use - obj[] , in here in our case obj["users"]
is going to look up the field "user" with in obj variable which is going to Alice
You can use variables with Brackets as well assign the variable "key" to the string "user "
key := "user"
obj[key]
The above condition is completely equivalent of
obj["user"]
As you might expect we also use in Rego brackets to inspect the internals of arrays and by the way all arrays are 0 indexed so if you write
arr[2] which is going to get 0, 1, 2 --2 in this case "banana" in the above slide
and again you can use variable in place
index := 2
arr[index]
Most interestingly we use Brackets to inspect sets
And as you may expect Brackets call be applied repeatedly in this example
obj["path"][0]
Like in many languages Rego uses brackets to inspect objects unlike any other language you can use brackets for any composite objects
Rego Also supports Dot . Dots are only used with objects
Dot is simply shorthand for bracket so every time Rego sees x.y it internally translates into x["y"]
Dot can only be used only when key is alpha-numeric
In such cases where the value is not found such as x and y in the above case which does have a value, The error is not thrown it says "undefined"
NOT turns undefined into True
Rule Evaluation
Comments
Post a Comment