-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate.rego
72 lines (54 loc) · 1.9 KB
/
generate.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
package generate
import future.keywords.in
# policy
default allow := false
allow {
some role in data.users[input.user]
some permission in data.roles[role]
permission.action == input.action
permission.resource == input.resource
}
# generation
range_upto_random_n(keys, n) := range {
range := numbers.range(1, rand.intn(sprintf("key-%s", [concat("-", keys)]), n))
}
user_ids := [u | u := sprintf("user%d", [numbers.range(1, input.users)[_]])]
role_ids := [u | u := sprintf("role%d", [numbers.range(1, input.roles)[_]])]
action_ids := [u | u := sprintf("action%d", [numbers.range(1, input.actions)[_]])]
resource_ids := [u | u := sprintf("resource%d", [numbers.range(1, input.resources)[_]])]
users[user_id] := roles {
some user_id in user_ids
roles := {r |
some i in range_upto_random_n(["user", user_id], input.max_roles_per_user)
r := role_ids[rand.intn(sprintf("%s%d", [user_id, i]), count(role_ids))]
}
}
roles[role_id] := permission {
some role_id in role_ids
permission := {p |
some i in range_upto_random_n(["role", role_id], input.max_capabilities_per_role)
action_id := action_ids[rand.intn(sprintf("%s%d-action", [role_id, i]), count(action_ids))]
resource_id := resource_ids[rand.intn(sprintf("%s%d-resource", [role_id, i]), count(resource_ids))]
p := {
"action": action_id,
"resource": resource_id,
}
}
}
queries := [q |
some i in numbers.range(1, input.queries)
user_id := user_ids[rand.intn(sprintf("%d-user", [i]), count(user_ids))]
action_id := action_ids[rand.intn(sprintf("%d-action", [i]), count(action_ids))]
resource_id := resource_ids[rand.intn(sprintf("%d-resource", [i]), count(resource_ids))]
allowed := allow with input as {"user": user_id, "action": action_id, "resource": resource_id}
with data.users as users
with data.roles as roles
q := {
"input": {
"user": user_id,
"action": action_id,
"resource": resource_id,
},
"expected": allowed,
}
]