-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathumbrella_client.py
301 lines (234 loc) · 10.6 KB
/
umbrella_client.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# -*- coding: utf-8 -*-
# Copyright (c) 2018 CoNWeT Lab., Universidad Politécnica de Madrid
# This file is part of BAE Umbrella service plugin.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import json
import requests
import urllib
from datetime import datetime
from urlparse import urljoin, urlparse, parse_qs
from django.core.exceptions import PermissionDenied
from wstore.asset_manager.resource_plugins.plugin_error import PluginError
class UmbrellaClient(object):
def __init__(self, server, token, key):
self._server = server
self._token = token
self._key = key
self._accounting_processor = {
'api call': self._process_call_accounting
}
def _make_request(self, path, method, **kwargs):
url = urljoin(self._server, path)
try:
resp = method(url, **kwargs)
except requests.ConnectionError:
raise PermissionDenied('Invalid resource: API Umbrella server is not responding')
if resp.status_code == 404:
raise PluginError('The provided Umbrella resource does not exist')
elif resp.status_code != 200:
raise PluginError('Umbrella gives an error accessing the provided resource')
return resp
def _get_request(self, path):
resp = self._make_request(path, requests.get, headers={
'X-Api-Key': self._key,
'X-Admin-Auth-Token': self._token
}, verify=False)
return resp.json()
def _put_request(self, path, body):
self._make_request(path, requests.get, json=body, headers={
'X-Api-Key': self._key,
'X-Admin-Auth-Token': self._token
}, verify=False)
def _post_request(self, path, body):
resp = self._make_request(path, requests.post, data=body, headers={
'X-Api-Key': self._key,
'X-Admin-Auth-Token': self._token
}, verify=False)
return resp.json()
def _paginate_data(self, url, err_msg, page_processor):
page_len = 100
start = 0
processed = False
matching_elem = None
while not processed:
result = self._get_request(url + '&start={}&length={}'.format(start, page_len))
# There is no remaining elements
if not len(result['data']):
raise PluginError(err_msg)
for elem in result['data']:
processed = page_processor(elem)
# The page element has been found
if processed:
matching_elem = elem
break
start += page_len
return matching_elem
def validate_service(self, path):
err_msg = 'The provided asset is not supported. ' \
'Only services protected by API Umbrella are supported'
# Split the path of the service
paths = [p for p in path.split('/') if p != '']
if not len(paths):
# API umbrella resources include a path for matching the service
raise PluginError(err_msg)
# Make paginated requests to API umbrella looking for the provided paths
url = '/api-umbrella/v1/apis.json?search[value]={}&search[regex]=false'.format(paths[0])
def page_processor(api):
front_path = [p for p in api['frontend_prefixes'].split('/') if p != '']
return len(front_path) <= len(paths) and front_path == paths[:len(front_path)]
matching_elem = self._paginate_data(url, err_msg, page_processor)
# If the API is configured to accept access tokens from an external IDP save its external id
app_id = None
if 'idp_app_id' in matching_elem['settings'] and len(matching_elem['settings']['idp_app_id']):
app_id = matching_elem['settings']['idp_app_id']
return app_id
def check_role(self, role):
# Check that the provided role already exists, in order to avoid users creating new roles
# using this service
existing_roles = self._get_request('api-umbrella/v1/user_roles')
for existing_role in existing_roles['user_roles']:
if existing_role['id'] == role:
break
else:
raise PluginError('The role {} does not exist in API Umbrella instance'.format(role))
def _get_user_model(self, email):
# Search users using the email field
url = '/api-umbrella/v1/users?search[value]={}'.format(email)
err_msg = 'There is not any user registered in Umbrella instance with email: {}'.format(email)
def page_processor(user_result):
return user_result['email'] == email
matching_user = self._paginate_data(url, err_msg, page_processor)
user_id = matching_user['id']
# Get user model
return self._get_request('/api-umbrella/v1/users/{}'.format(user_id))
def _filter_roles(self, user_model, role):
new_roles = []
if user_model['user']['roles'] is not None:
# Parse existing roles
new_roles = [user_role for user_role in user_model['user']['roles'] if user_role != role]
return new_roles
def grant_permission(self, user, role):
self.check_role(role)
user_model = self._get_user_model(user.email)
# Update user roles
new_roles = self._filter_roles(user_model, role).append(role)
user_model['user']['roles'] = new_roles
self._put_request('/api-umbrella/v1/users/{}'.format(user_model['user']['id']), user_model)
def revoke_permission(self, user, role):
self.check_role(role)
user_model = self._get_user_model(user.email)
user_model['user']['roles'] = self._filter_roles(user_model, role)
self._put_request('/api-umbrella/v1/users/{}'.format(user_model['user']['id']), user_model)
def _get_rule(self, field, value, operator='equal'):
return {
'id': field,
'field': field,
'type': 'string',
'input': 'text',
'operator': operator,
'value': value
}
def _get_null_rule(self, field):
return {
'id': field,
'field': field,
'type': 'string',
'input': 'select',
'operator': 'is_null',
'value': None
}
def _paginate_accounting(self, params, accounting, accounting_aggregator, unit):
page_len = 500
start = 0
processed = False
current_date = None
current_value = 0
while not processed:
params['start'] = start
params['length'] = page_len
result = self._post_request('/api-umbrella/v1/analytics/logs.json', params)
# There is no remaining elements
if not len(result['data']):
processed = True
for elem in result['data']:
# Process log timestamp (Which includes milliseconds)
date = datetime.utcfromtimestamp(elem['request_at']/1000.0)
day = date.date()
if current_date is None:
# New day to be aggregated
current_date = day
# If new day is higher save the accounting info
if day > current_date:
accounting.append({
'unit': unit,
'value': current_value,
'date': unicode(current_date.isoformat()) + 'T00:00:00Z'
})
# Set current day and reset value
current_date = day
current_value = 0
current_value += accounting_aggregator(elem)
start += page_len
# Save last info
if current_value > 0:
accounting.append({
'unit': unit,
'value': current_value,
'date': unicode(current_date.isoformat()) + 'T00:00:00Z'
})
def _process_call_accounting(self, params, parsed_url, extra_qs):
def list_equal_elems(list1, list2):
intersect = set(list2).intersection(list1)
return len(list1) == len(list2) == len(intersect)
accounting = []
def call_aggregator(elem):
account = 1
# Filter query strings during aggregation to enable changing the order and
# included extra params when enabled
if len(parsed_url.query):
parsed_elem_qs = parse_qs(elem['request_url_query'])
url_qs = parse_qs(parsed_url.query)
# Check that all the query strings of the asset URL are included
for key, value in url_qs.iteritems():
if key not in parsed_elem_qs or not list_equal_elems(value, parsed_elem_qs[key]):
account = 0
break
else:
# If not extra qs allowed and included in the log element, the call do not match
if not extra_qs and len(parsed_elem_qs) != len(url_qs):
account = 0
return account
self._paginate_accounting(params, accounting, call_aggregator, 'api call')
return accounting
def get_drilldown_by_service(self, email, service, path_allowed, extra_qs, start_at, end_at, unit):
parsed_url = urlparse(service)
rules = [
self._get_null_rule('gatekeeper_denied_code'),
self._get_rule('user_email', email)
]
# Include path rule depending on whether subpath requests are allowed
path_operator = 'equal' if not path_allowed else 'begins_with'
rules.append(self._get_rule('request_path', parsed_url.path, operator=path_operator))
query = {
'condition': 'AND',
'rules': rules,
'valid': True
}
params = {
'start_at': start_at,
'end_at': end_at,
'query': json.dumps(query)
}
return self._accounting_processor[unit](params, parsed_url, extra_qs)