Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d90bd4c

Browse files
committed
chore: use now mandatory 'if' for v1 rego rules
1 parent 97cd00d commit d90bd4c

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

coderd/rbac/policy.rego

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package authz
2-
import future.keywords
2+
import rego.v1
3+
34
# A great playground: https://play.openpolicyagent.org/
45
# Helpful cli commands to debug.
56
# opa eval --format=pretty 'data.authz.allow' -d policy.rego -i input.json
@@ -29,12 +30,12 @@ import future.keywords
2930

3031
# bool_flip lets you assign a value to an inverted bool.
3132
# You cannot do 'x := !false', but you can do 'x := bool_flip(false)'
32-
bool_flip(b) = flipped {
33+
bool_flip(b) = flipped if {
3334
b
3435
flipped = false
3536
}
3637

37-
bool_flip(b) = flipped {
38+
bool_flip(b) = flipped if {
3839
not b
3940
flipped = true
4041
}
@@ -43,17 +44,17 @@ bool_flip(b) = flipped {
4344
# -1: {false, true} or {false}
4445
# 0: {}
4546
# 1: {true}
46-
number(set) = c {
47+
number(set) = c if {
4748
count(set) == 0
4849
c := 0
4950
}
5051

51-
number(set) = c {
52+
number(set) = c if {
5253
false in set
5354
c := -1
5455
}
5556

56-
number(set) = c {
57+
number(set) = c if {
5758
not false in set
5859
set[_]
5960
c := 1
@@ -67,7 +68,7 @@ site := site_allow(input.subject.roles)
6768
default scope_site := 0
6869
scope_site := site_allow([input.subject.scope])
6970

70-
site_allow(roles) := num {
71+
site_allow(roles) := num if {
7172
# allow is a set of boolean values without duplicates.
7273
allow := { x |
7374
# Iterate over all site permissions in all roles
@@ -102,7 +103,7 @@ scope_org := org_allow([input.scope])
102103
# The reason we calculate this for all orgs, and not just the input.object.org_owner
103104
# is that sometimes the input.object.org_owner is unknown. In those cases
104105
# we have a list of org_ids that can we use in a SQL 'WHERE' clause.
105-
org_allow_set(roles) := allow_set {
106+
org_allow_set(roles) := allow_set if {
106107
allow_set := { id: num |
107108
id := org_members[_]
108109
set := { x |
@@ -115,7 +116,7 @@ org_allow_set(roles) := allow_set {
115116
}
116117
}
117118

118-
org_allow(roles) := num {
119+
org_allow(roles) := num if {
119120
# If the object has "any_org" set to true, then use the other
120121
# org_allow block.
121122
not input.object.any_org
@@ -135,7 +136,7 @@ org_allow(roles) := num {
135136
# This is useful for UI elements when we want to conclude, "Can the user create
136137
# a new template in any organization?"
137138
# It is easier than iterating over every organization the user is apart of.
138-
org_allow(roles) := num {
139+
org_allow(roles) := num if {
139140
input.object.any_org # if this is false, this code block is not used
140141
allow := org_allow_set(roles)
141142

@@ -159,24 +160,24 @@ org_allow(roles) := num {
159160

160161
# 'org_mem' is set to true if the user is an org member
161162
# If 'any_org' is set to true, use the other block to determine org membership.
162-
org_mem := true {
163+
org_mem := true if {
163164
not input.object.any_org
164165
input.object.org_owner != ""
165166
input.object.org_owner in org_members
166167
}
167168

168-
org_mem := true {
169+
org_mem := true if {
169170
input.object.any_org
170171
count(org_members) > 0
171172
}
172173

173-
org_ok {
174+
org_ok if {
174175
org_mem
175176
}
176177

177178
# If the object has no organization, then the user is also considered part of
178179
# the non-existent org.
179-
org_ok {
180+
org_ok if {
180181
input.object.org_owner == ""
181182
not input.object.any_org
182183
}
@@ -188,7 +189,7 @@ user := user_allow(input.subject.roles)
188189
default user_scope := 0
189190
scope_user := user_allow([input.scope])
190191

191-
user_allow(roles) := num {
192+
user_allow(roles) := num if {
192193
input.object.owner != ""
193194
input.subject.id = input.object.owner
194195
allow := { x |
@@ -202,11 +203,11 @@ user_allow(roles) := num {
202203

203204
# Scope allow_list is a list of resource IDs explicitly allowed by the scope.
204205
# If the list is '*', then all resources are allowed.
205-
scope_allow_list {
206+
scope_allow_list if {
206207
"*" in input.subject.scope.allow_list
207208
}
208209

209-
scope_allow_list {
210+
scope_allow_list if {
210211
# If the wildcard is listed in the allow_list, we do not care about the
211212
# object.id. This line is included to prevent partial compilations from
212213
# ever needing to include the object.id.
@@ -226,16 +227,16 @@ scope_allow_list {
226227
# Allow query:
227228
# data.authz.role_allow = true data.authz.scope_allow = true
228229

229-
role_allow {
230+
role_allow if {
230231
site = 1
231232
}
232233

233-
role_allow {
234+
role_allow if {
234235
not site = -1
235236
org = 1
236237
}
237238

238-
role_allow {
239+
role_allow if {
239240
not site = -1
240241
not org = -1
241242
# If we are not a member of an org, and the object has an org, then we are
@@ -244,18 +245,18 @@ role_allow {
244245
user = 1
245246
}
246247

247-
scope_allow {
248+
scope_allow if {
248249
scope_allow_list
249250
scope_site = 1
250251
}
251252

252-
scope_allow {
253+
scope_allow if {
253254
scope_allow_list
254255
not scope_site = -1
255256
scope_org = 1
256257
}
257258

258-
scope_allow {
259+
scope_allow if {
259260
scope_allow_list
260261
not scope_site = -1
261262
not scope_org = -1
@@ -266,15 +267,15 @@ scope_allow {
266267
}
267268

268269
# ACL for users
269-
acl_allow {
270+
acl_allow if {
270271
# Should you have to be a member of the org too?
271272
perms := input.object.acl_user_list[input.subject.id]
272273
# Either the input action or wildcard
273274
[input.action, "*"][_] in perms
274275
}
275276

276277
# ACL for groups
277-
acl_allow {
278+
acl_allow if {
278279
# If there is no organization owner, the object cannot be owned by an
279280
# org_scoped team.
280281
org_mem
@@ -285,7 +286,7 @@ acl_allow {
285286
}
286287

287288
# ACL for 'all_users' special group
288-
acl_allow {
289+
acl_allow if {
289290
org_mem
290291
perms := input.object.acl_group_list[input.object.org_owner]
291292
[input.action, "*"][_] in perms
@@ -296,13 +297,13 @@ acl_allow {
296297
# The role or the ACL must allow the action. Scopes can be used to limit,
297298
# so scope_allow must always be true.
298299

299-
allow {
300+
allow if {
300301
role_allow
301302
scope_allow
302303
}
303304

304305
# ACL list must also have the scope_allow to pass
305-
allow {
306+
allow if {
306307
acl_allow
307308
scope_allow
308309
}

0 commit comments

Comments
 (0)