-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathexample-conditions.rego
131 lines (119 loc) · 2.73 KB
/
example-conditions.rego
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package example
import rego.v1
policies := [
{
"subjects": ["group:users"],
"resource": ["posts"],
"action": "GET",
"condition": [
{
"field": "author",
"ref": "user",
"operator": "equal",
},
{
"field": "id",
"value": "post1",
"operator": "equal",
},
],
},
{
"subjects": ["group:admin"],
"resource": ["posts"],
"action": "GET",
"condition": [{
"field": "clearance",
"ref": "level",
"value": 9,
"operator": "greater_than",
}],
},
]
### Entry point to the policy.
### Example with conditions being interpreted.
allow if {
some p in policies
p.action = input.method
p.resource = input.path
some r in data.elastic[p.resource[0]]
all_conditions_true(p, r)
}
all_conditions_true(p, r) if {
not any_condition_false(p, r)
}
any_condition_false(p, r) if {
some c in p.condition
not eval(r, c.operator, c)
}
### Equality operator
# OPA Query: "bob" = data.elastic.posts[_].author; "post1" = data.elastic.posts[_].id
# ES Query: {name:author value:bob boost:<nil> queryName:TermQuery}
eval(r, "equal", c) if {
r[c.field] == input[c.ref] # regal ignore:external-reference
}
# OPA Query: "bob" = data.elastic.posts[_].author; "post1" = data.elastic.posts[_].id
# ES Query: {name:id value:post1 boost:<nil> queryName:TermQuery}
eval(r, "equal", c) if {
r[c.field] == c.value
}
### Greater Than operator
# OPA Query: gt(data.elastic.posts[_].clearance, 9)
# ES Query: {name:clearance from:9 to:<nil> timeZone: includeLower:false
# includeUpper:true boost:<nil> queryName: format: relation:}
eval(r, "greater_than", c) if {
r[c.field] > c.value
}
### Sample Input to OPA.
# {
# "method": "GET",
# "path": ["posts"],
# "user": "bob"
# }
### Sample Output from Elasticsearch.
# {
# "result": [
# {
# "id": "post1",
# "author": "bob",
# "message": "My first post",
# "department": "dev",
# "email": "[email protected]",
# "clearance": 2,
# "action": "read",
# "resource": "",
# "conditions": [],
# "likes": [],
# "followers": [],
# "stats": []
# },
# {
# "id": "post5",
# "author": "ben",
# "message": "Hii from Ben",
# "department": "ceo",
# "email": "[email protected]",
# "clearance": 10,
# "action": "read",
# "resource": "",
# "conditions": [],
# "likes": [],
# "followers": [],
# "stats": []
# },
# {
# "id": "post8",
# "author": "ben",
# "message": "This is OPA's time",
# "department": "ceo",
# "email": "[email protected]",
# "clearance": 10,
# "action": "read",
# "resource": "",
# "conditions": [],
# "likes": [],
# "followers": [],
# "stats": []
# }
# ]
# }