-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.rego
269 lines (227 loc) · 7.45 KB
/
common.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
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
# Action to method mappings
scope_method := {"acl:Read": ["GET"], "acl:Append": ["POST", "PATCH"], "acl:Write": ["POST", "PUT", "DELETE", "PATCH"], "acl:Control": ["CONTROL"]}
# Extract Bearer Token
bearer_token = t {
v := authorization
startswith(v, "Bearer ")
t := substring(v, count("Bearer "), -1)
}
# Helper to get the token payload
token = {"payload": payload, "header": header} {
[header, payload, signature] := io.jwt.decode(bearer_token)
}
# Valid Issuers
valid_iss = split(opa.runtime()["env"]["VALID_ISSUERS"], ";")
# API URI
api_uri = opa.runtime()["env"]["AUTH_API_URI"]
# Audience
aud = split(opa.runtime()["env"]["VALID_AUDIENCE"], ";")
# Token issuer
issuer = token.payload.iss
# Get token metadata
metadata = http.send({
"url": concat("", [issuer, "/.well-known/openid-configuration"]),
"method": "GET",
"force_cache": true,
"force_cache_duration_seconds": 86400
}).body
# Get endpoints for jws and the token
jwks_endpoint := metadata.jwks_uri
token_endpoint := metadata.token_endpoint
# jwks_request for validation
jwks_request(url) = http.send({
"url": url,
"method": "GET",
"force_cache": true,
"force_cache_duration_seconds": 3600
})
# Policies pull method
# policies = http.send({"method": "get", "url": api_uri, "headers": {"accept": "text/rego", "fiware-service": request.tenant, "fiware-servicepath": request.service_path}}).body
# Policies push method
policies = data[request.tenant]["policies"]
# The user/subject
subject := p {
p := token.payload.email
}
# Fiware service
fiware_service := p {
p := input.attributes.request.http.headers["fiware-service"]
}
# Fiware servicepath
fiware_servicepath := p {
p := input.attributes.request.http.headers["fiware-servicepath"]
}
# Request data
request = {"user":subject, "action": method, "resource":path, "tenant":fiware_service, "service_path":fiware_servicepath}
# Compute link
default header_link = ""
# Auth defaults to false
default allow = {
"allowed": false,
}
# Auth main rule
allow = response {
response := {
"allowed": check_policy,
"response_headers_to_add": {"Link": header_link}
}
}
# Check policy when accessing resources
check_policy {
current_path := split(request.resource, "/")
current_path[2] != "policies"
user_permitted(request)
}
# Check for token validity
is_token_valid {
now = time.now_ns() / 1000000000
token.payload.exp >= now
jwks = json.marshal(jwks_request(jwks_endpoint).body.keys[_])
io.jwt.verify_rs256(bearer_token, jwks)
some i, a in aud
valid_audience(a)
issuer = valid_iss[_]
}
# Test for valid audiences in token
valid_audience(aud_entry) {
token.payload.azp = aud_entry
}
# Token valid when testing (default is false)
testing = false
is_token_valid {
testing
}
# Check if service path in policy is equal to the request path
service_path_matches_policy(entry_path, request_path) {
entry_path == request_path
}
# Check if service path in policy is the wildcard, thus matching any path
service_path_matches_policy(entry_path, request_path) {
entry_path == "*"
}
# Check if service path in policy is equal to the request path, and matching any
# subpath as well
service_path_matches_default_policy(entry_path, request_path) {
split_entry_path := split(entry_path, "/")
split_request_path := split(request_path, "/")
not arrays_dont_have_same_value(split_entry_path, split_request_path)
}
# Check if service path in policy is equal to the request path, with / matching
# all subpaths
service_path_matches_default_policy(entry_path, request_path) {
entry_path == "/"
}
arrays_dont_have_same_value(a, b) {
some i, _ in a
a[i] != b[i]
}
method_matches_action(entry, request) {
scope_method[entry.action][_] == request.action
}
default user_permitted(request) = false
# User permissions
user_permitted(request) {
is_token_valid
entry := policies.user_permissions[request.user][_]
method_matches_action(entry, request)
path_matches_policy(entry, request)
entry.tenant == request.tenant
service_path_matches_policy(entry.service_path, request.service_path)
}
# Default User permissions
user_permitted(request) {
is_token_valid
entry := policies.default_user_permissions[request.user][_]
method_matches_action(entry, request)
path_matches_policy(entry, request)
entry.tenant == request.tenant
service_path_matches_default_policy(entry.service_path, request.service_path)
}
# Group permissions
user_permitted(request) {
is_token_valid
token.payload.tenants[request.tenant]
entry := policies.group_permissions[token.payload.tenants[request.tenant].groups[_]][_]
method_matches_action(entry, request)
path_matches_policy(entry, request)
entry.tenant == request.tenant
service_path_matches_policy(entry.service_path, request.service_path)
}
# Default Group permissions
user_permitted(request) {
is_token_valid
token.payload.tenants[request.tenant]
entry := policies.default_group_permissions[token.payload.tenants[request.tenant].groups[_]][_]
method_matches_action(entry, request)
path_matches_policy(entry, request)
entry.tenant == request.tenant
service_path_matches_default_policy(entry.service_path, request.service_path)
}
# Role permissions
user_permitted(request) {
is_token_valid
token.payload.tenants[request.tenant]
entry := policies.role_permissions[token.payload.tenants[request.tenant].roles[_]][_]
method_matches_action(entry, request)
path_matches_policy(entry, request)
entry.tenant == request.tenant
service_path_matches_policy(entry.service_path, request.service_path)
}
# Default Role permissions
user_permitted(request) {
is_token_valid
token.payload.tenants[request.tenant]
entry := policies.default_role_permissions[token.payload.tenants[tenant_i].roles[_]][_]
method_matches_action(entry, request)
path_matches_policy(entry, request)
entry.tenant == request.tenant
service_path_matches_default_policy(entry.service_path, request.service_path)
}
# AuthenticatedAgent special permission
user_permitted(request) {
is_token_valid
some role
entry := policies.role_permissions[role][_]
role == "AuthenticatedAgent"
method_matches_action(entry, request)
path_matches_policy(entry, request)
entry.tenant == request.tenant
service_path_matches_policy(entry.service_path, request.service_path)
}
# Default AuthenticatedAgent special permission
user_permitted(request) {
is_token_valid
some role
entry := policies.default_role_permissions[role][_]
role == "AuthenticatedAgent"
method_matches_action(entry, request)
path_matches_policy(entry, request)
entry.tenant == request.tenant
service_path_matches_default_policy(entry.service_path, request.service_path)
}
# Agent special permission
user_permitted(request) {
some role
entry := policies.role_permissions[role][_]
role == "Agent"
method_matches_action(entry, request)
path_matches_policy(entry, request)
entry.tenant == request.tenant
service_path_matches_policy(entry.service_path, request.service_path)
}
# Default Agent special permission
user_permitted(request) {
some role
entry := policies.default_role_permissions[role][_]
role == "Agent"
method_matches_action(entry, request)
path_matches_policy(entry, request)
entry.tenant == request.tenant
service_path_matches_default_policy(entry.service_path, request.service_path)
}