-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanubis_management_api_policy.rego
278 lines (254 loc) · 9.41 KB
/
anubis_management_api_policy.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package envoy.authz
import future.keywords.in
import input.attributes.request.http.method as method
import input.attributes.request.http.path as path
import input.attributes.request.http.headers.authorization as authorization
import input.parsed_body as parsed_body
match_policy_id_or_wildcard(e, policy_id) {
e.id == policy_id
}
match_policy_id_or_wildcard(e, policy_id) {
e.resource == "*"
}
# Check policy when trying to get all policies (API will filter the ones for the given user)
check_policy {
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
request.action in ["GET"]
current_path[3] == ""
}
check_policy {
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
request.action in ["GET"]
not current_path[3]
}
check_policy {
current_path := split(path, "/")
current_path[1] == "v1"
current_path[2] == "policies"
method in ["GET"]
current_path[3] == "me"
}
# Check policy when trying to get an entity acl resource
check_policy {
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
request.action in ["GET", "PUT", "PATCH", "DELETE"]
policy_id := current_path[3]
e := policies[_][_][_]
match_policy_id_or_wildcard(e, policy_id)
e.resource_type == "entity"
control_request = {"user":request.user, "action": "CONTROL", "resource":concat("", ["/v2/entities/",e.resource]), "tenant":request.tenant, "service_path":request.service_path}
user_permitted(control_request)
}
# Check policy when trying to create a new entity acl resource
check_policy {
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
request.action == "POST"
parsed_body.resource_type == "entity"
control_request = {"user":request.user, "action": "CONTROL", "resource":concat("", ["/v2/entities/",parsed_body.access_to]), "tenant":request.tenant, "service_path":request.service_path}
user_permitted(control_request)
}
# Check policy when trying to get an entity type acl resource
check_policy {
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
request.action in ["GET", "PUT", "PATCH", "DELETE"]
policy_id := current_path[3]
e := policies[_][_][_]
match_policy_id_or_wildcard(e, policy_id)
e.resource_type == "entity_type"
control_request = {"user":request.user, "action": "CONTROL", "resource":concat("", ["/v2/types/",e.resource]), "tenant":request.tenant, "service_path":request.service_path}
user_permitted(control_request)
}
# Check policy when trying to access a new entity type acl resource
check_policy {
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
request.action == "POST"
parsed_body.resource_type == "entity_type"
control_request = {"user":request.user, "action": "CONTROL", "resource":concat("", ["/v2/types/",parsed_body.access_to]), "tenant":request.tenant, "service_path":request.service_path}
user_permitted(control_request)
}
# Check policy when trying to get an entity type acl resource
check_policy {
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
request.action in ["GET", "PUT", "PATCH", "DELETE"]
policy_id := current_path[3]
e := policies[_][_][_]
match_policy_id_or_wildcard(e, policy_id)
e.resource_type == "subscriptions"
control_request = {"user":request.user, "action": "CONTROL", "resource":concat("", ["/v2/subscription/",e.resource]), "tenant":request.tenant, "service_path":request.service_path}
user_permitted(control_request)
}
# Check policy when trying to access a new subscription acl resource
check_policy {
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
request.action == "POST"
parsed_body.resource_type == "subscriptions"
control_request = {"user":request.user, "action": "CONTROL", "resource":concat("", ["/v2/subscriptions/",parsed_body.access_to]), "tenant":request.tenant, "service_path":request.service_path}
user_permitted(control_request)
}
######################################################################################################
# Checks if the policy has the wildcard asterisks, thus matching paths to any policy or all
path_matches_policy(entry, request) {
entry.resource == "*"
entry.resource_type == "policy"
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
}
# Checks if the policy is a default
path_matches_policy(entry, request) {
entry.resource == "default"
entry.resource_type == "policy"
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
}
# Checks if the policy targetted by this policy matches the path
path_matches_policy(entry, request) {
entry.resource_type == "policy"
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "policies"
current_path[3] == entry.resource
}
# Set the header link for the policies
header_link = link {
current_path := split(path, "/")
current_path[2] == "policies"
not current_path[3] == ""
link := sprintf("<%s/me?resource=%s&&type=%s>; rel=\"acl\"", [api_uri,current_path[3],"policy"])
}
header_link = link {
current_path := split(path, "/")
current_path[2] == "policies"
current_path[3] == ""
link := sprintf("<%s/me?resource=%s&&type=%s>; rel=\"acl\"", [api_uri,"*","policy"])
}
header_link = link {
current_path := split(path, "/")
current_path[2] == "policies"
not current_path[3]
link := sprintf("<%s/me?resource=%s&&type=%s>; rel=\"acl\"", [api_uri,"*","policy"])
}
# Checks if the policy has the wildcard asterisks, thus matching paths to any tenant or all
path_matches_policy(entry, request) {
entry.resource == "*"
entry.resource_type == "tenant"
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "tenants"
}
# Checks if the policy is a default
path_matches_policy(entry, request) {
entry.resource == "default"
entry.resource_type == "tenant"
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "tenants"
}
# Checks if the tenant in the policy matches the path
path_matches_policy(entry, request) {
entry.resource_type == "tenant"
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "tenants"
current_path[3] == entry.resource
}
# Set the header link for the tenants
header_link = link {
current_path := split(request.resource, "/")
current_path[2] == "tenants"
current_path[3]
not current_path[3] == ""
not current_path[4]
link := sprintf("<%s/me?resource=%s&&type=%s>; rel=\"acl\"", [api_uri,current_path[3],"tenant"])
}
header_link = link {
current_path := split(request.resource, "/")
current_path[2] == "tenants"
current_path[3] == ""
link := sprintf("<%s/me?resource=%s&&type=%s>; rel=\"acl\"", [api_uri,"*","tenant"])
}
header_link = link {
current_path := split(request.resource, "/")
current_path[2] == "tenants"
not current_path[3]
link := sprintf("<%s/me?resource=%s&&type=%s>; rel=\"acl\"", [api_uri,"*","tenant"])
}
# Checks if the policy has the wildcard asterisks, thus matching paths to any tenant or all
path_matches_policy(entry, request) {
entry.resource == "*"
entry.resource_type == "service_path"
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "tenants"
current_path[3] == entry.tenant
current_path[4] == "service_paths"
}
# Checks if the policy is a default
path_matches_policy(entry, request) {
entry.resource == "default"
entry.resource_type == "service_path"
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "tenants"
current_path[3] == entry.tenant
current_path[4] == "service_paths"
}
# Checks if the tenant in the policy matches the path
path_matches_policy(entry, request) {
entry.resource_type == "service_path"
current_path := split(request.resource, "/")
current_path[1] == "v1"
current_path[2] == "tenants"
current_path[3] == entry.tenant
current_path[4] == "service_paths"
split_request_path := split(entry.resource, "/")
not arrays_dont_have_same_value(array.slice(current_path, 5, count(current_path)), array.slice(split_request_path, 1, count(split_request_path)))
}
# Set the header link for the service paths
header_link = link {
current_path := split(request.resource, "/")
current_path[2] == "tenants"
current_path[3]
not current_path[3] == ""
current_path[4] == "service_paths"
current_path[5]
not current_path[5] == ""
current_service_path_array := array.concat([""],array.slice(current_path, 5, count(current_path)))
current_service_path := concat("/",current_service_path_array)
link := sprintf("<%s/me?resource=%s&&type=%s>; rel=\"acl\"", [api_uri,current_service_path,"service_path"])
}
header_link = link {
current_path := split(request.resource, "/")
current_path[2] == "tenants"
current_path[3]
not current_path[3] == ""
current_path[4] == "service_paths"
not current_path[5]
link := sprintf("<%s/me?resource=%s&&type=%s>; rel=\"acl\"", [api_uri,"*","service_path"])
}
header_link = link {
current_path := split(request.resource, "/")
current_path[2] == "tenants"
current_path[3]
not current_path[3] == ""
current_path[4] == "service_paths"
current_path[5] == ""
link := sprintf("<%s/me?resource=%s&&type=%s>; rel=\"acl\"", [api_uri,"*","service_path"])
}