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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
|
# -*- coding: utf-8 -*-
# pylint: disable=protected-access,too-many-branches
from __future__ import absolute_import
import collections
import importlib
import inspect
import json
import os
import pkgutil
import sys
from six import add_metaclass
if sys.version_info >= (3, 0):
from urllib.parse import unquote # pylint: disable=no-name-in-module,import-error
else:
from urllib import unquote # pylint: disable=no-name-in-module
# pylint: disable=wrong-import-position
import cherrypy
from .. import logger
from ..security import Scope, Permission
from ..tools import wraps, getargspec, TaskManager, get_request_body_params
from ..exceptions import ScopeNotValid, PermissionNotValid
from ..services.auth import AuthManager, JwtManager
class Controller(object):
def __init__(self, path, base_url=None, security_scope=None, secure=True):
if security_scope and not Scope.valid_scope(security_scope):
logger.debug("Invalid security scope name: %s\n Possible values: "
"%s", security_scope, Scope.all_scopes())
raise ScopeNotValid(security_scope)
self.path = path
self.base_url = base_url
self.security_scope = security_scope
self.secure = secure
if self.path and self.path[0] != "/":
self.path = "/" + self.path
if self.base_url is None:
self.base_url = ""
elif self.base_url == "/":
self.base_url = ""
if self.base_url == "" and self.path == "":
self.base_url = "/"
def __call__(self, cls):
cls._cp_controller_ = True
cls._cp_path_ = "{}{}".format(self.base_url, self.path)
cls._security_scope = self.security_scope
config = {
'tools.dashboard_exception_handler.on': True,
'tools.authenticate.on': self.secure,
}
if not hasattr(cls, '_cp_config'):
cls._cp_config = {}
cls._cp_config.update(config)
return cls
class ApiController(Controller):
def __init__(self, path, security_scope=None, secure=True):
super(ApiController, self).__init__(path, base_url="/api",
security_scope=security_scope,
secure=secure)
def __call__(self, cls):
cls = super(ApiController, self).__call__(cls)
cls._api_endpoint = True
return cls
class UiApiController(Controller):
def __init__(self, path, security_scope=None, secure=True):
super(UiApiController, self).__init__(path, base_url="/ui-api",
security_scope=security_scope,
secure=secure)
def Endpoint(method=None, path=None, path_params=None, query_params=None,
json_response=True, proxy=False, xml=False):
if method is None:
method = 'GET'
elif not isinstance(method, str) or \
method.upper() not in ['GET', 'POST', 'DELETE', 'PUT']:
raise TypeError("Possible values for method are: 'GET', 'POST', "
"'DELETE', or 'PUT'")
method = method.upper()
if method in ['GET', 'DELETE']:
if path_params is not None:
raise TypeError("path_params should not be used for {} "
"endpoints. All function params are considered"
" path parameters by default".format(method))
if path_params is None:
if method in ['POST', 'PUT']:
path_params = []
if query_params is None:
query_params = []
def _wrapper(func):
if method in ['POST', 'PUT']:
func_params = _get_function_params(func)
for param in func_params:
if param['name'] in path_params and not param['required']:
raise TypeError("path_params can only reference "
"non-optional function parameters")
if func.__name__ == '__call__' and path is None:
e_path = ""
else:
e_path = path
if e_path is not None:
e_path = e_path.strip()
if e_path and e_path[0] != "/":
e_path = "/" + e_path
elif e_path == "/":
e_path = ""
func._endpoint = {
'method': method,
'path': e_path,
'path_params': path_params,
'query_params': query_params,
'json_response': json_response,
'proxy': proxy,
'xml': xml
}
return func
return _wrapper
def Proxy(path=None):
if path is None:
path = ""
elif path == "/":
path = ""
path += "/{path:.*}"
return Endpoint(path=path, proxy=True)
def load_controllers():
# setting sys.path properly when not running under the mgr
controllers_dir = os.path.dirname(os.path.realpath(__file__))
dashboard_dir = os.path.dirname(controllers_dir)
mgr_dir = os.path.dirname(dashboard_dir)
logger.debug("LC: controllers_dir=%s", controllers_dir)
logger.debug("LC: dashboard_dir=%s", dashboard_dir)
logger.debug("LC: mgr_dir=%s", mgr_dir)
if mgr_dir not in sys.path:
sys.path.append(mgr_dir)
controllers = []
mods = [mod for _, mod, _ in pkgutil.iter_modules([controllers_dir])]
logger.debug("LC: mods=%s", mods)
for mod_name in mods:
mod = importlib.import_module('.controllers.{}'.format(mod_name),
package='dashboard')
for _, cls in mod.__dict__.items():
# Controllers MUST be derived from the class BaseController.
if inspect.isclass(cls) and issubclass(cls, BaseController) and \
hasattr(cls, '_cp_controller_'):
if cls._cp_path_.startswith(':'):
# invalid _cp_path_ value
logger.error("Invalid url prefix '%s' for controller '%s'",
cls._cp_path_, cls.__name__)
continue
controllers.append(cls)
return controllers
ENDPOINT_MAP = collections.defaultdict(list)
def generate_controller_routes(endpoint, mapper, base_url):
inst = endpoint.inst
ctrl_class = endpoint.ctrl
endp_base_url = None
if endpoint.proxy:
conditions = None
else:
conditions = dict(method=[endpoint.method])
endp_url = endpoint.url
if base_url == "/":
base_url = ""
if endp_url == "/" and base_url:
endp_url = ""
url = "{}{}".format(base_url, endp_url)
if '/' in url[len(base_url)+1:]:
endp_base_url = url[:len(base_url)+1+endp_url[1:].find('/')]
else:
endp_base_url = url
logger.debug("Mapped [%s] to %s:%s restricted to %s",
url, ctrl_class.__name__, endpoint.action,
endpoint.method)
ENDPOINT_MAP[endpoint.url].append(endpoint)
name = ctrl_class.__name__ + ":" + endpoint.action
mapper.connect(name, url, controller=inst, action=endpoint.action,
conditions=conditions)
# adding route with trailing slash
name += "/"
url += "/"
mapper.connect(name, url, controller=inst, action=endpoint.action,
conditions=conditions)
return endp_base_url
def generate_routes(url_prefix):
mapper = cherrypy.dispatch.RoutesDispatcher()
ctrls = load_controllers()
parent_urls = set()
endpoint_list = []
for ctrl in ctrls:
inst = ctrl()
for endpoint in ctrl.endpoints():
endpoint.inst = inst
endpoint_list.append(endpoint)
endpoint_list = sorted(endpoint_list, key=lambda e: e.url)
for endpoint in endpoint_list:
parent_urls.add(generate_controller_routes(endpoint, mapper,
"{}".format(url_prefix)))
logger.debug("list of parent paths: %s", parent_urls)
return mapper, parent_urls
def json_error_page(status, message, traceback, version):
cherrypy.response.headers['Content-Type'] = 'application/json'
return json.dumps(dict(status=status, detail=message, traceback=traceback,
version=version))
def _get_function_params(func):
"""
Retrieves the list of parameters declared in function.
Each parameter is represented as dict with keys:
* name (str): the name of the parameter
* required (bool): whether the parameter is required or not
* default (obj): the parameter's default value
"""
fspec = getargspec(func)
func_params = []
nd = len(fspec.args) if not fspec.defaults else -len(fspec.defaults)
for param in fspec.args[1:nd]:
func_params.append({'name': param, 'required': True})
if fspec.defaults:
for param, val in zip(fspec.args[nd:], fspec.defaults):
func_params.append({
'name': param,
'required': False,
'default': val
})
return func_params
class Task(object):
def __init__(self, name, metadata, wait_for=5.0, exception_handler=None):
self.name = name
if isinstance(metadata, list):
self.metadata = dict([(e[1:-1], e) for e in metadata])
else:
self.metadata = metadata
self.wait_for = wait_for
self.exception_handler = exception_handler
def _gen_arg_map(self, func, args, kwargs):
arg_map = {}
params = _get_function_params(func)
args = args[1:] # exclude self
for idx, param in enumerate(params):
if idx < len(args):
arg_map[param['name']] = args[idx]
else:
if param['name'] in kwargs:
arg_map[param['name']] = kwargs[param['name']]
else:
assert not param['required']
arg_map[param['name']] = param['default']
if param['name'] in arg_map:
# This is not a type error. We are using the index here.
arg_map[idx+1] = arg_map[param['name']]
return arg_map
def __call__(self, func):
@wraps(func)
def wrapper(*args, **kwargs):
arg_map = self._gen_arg_map(func, args, kwargs)
md = {}
for k, v in self.metadata.items():
if isinstance(v, str) and v and v[0] == '{' and v[-1] == '}':
param = v[1:-1]
try:
pos = int(param)
md[k] = arg_map[pos]
except ValueError:
md[k] = arg_map[v[1:-1]]
else:
md[k] = v
task = TaskManager.run(self.name, md, func, args, kwargs,
exception_handler=self.exception_handler)
try:
status, value = task.wait(self.wait_for)
except Exception as ex:
if task.ret_value:
# exception was handled by task.exception_handler
if 'status' in task.ret_value:
status = task.ret_value['status']
else:
status = getattr(ex, 'status', 500)
cherrypy.response.status = status
return task.ret_value
raise ex
if status == TaskManager.VALUE_EXECUTING:
cherrypy.response.status = 202
return {'name': self.name, 'metadata': md}
return value
return wrapper
class BaseController(object):
"""
Base class for all controllers providing API endpoints.
"""
class Endpoint(object):
"""
An instance of this class represents an endpoint.
"""
def __init__(self, ctrl, func):
self.ctrl = ctrl
self.inst = None
self.func = func
if not self.config['proxy']:
setattr(self.ctrl, func.__name__, self.function)
@property
def config(self):
func = self.func
while not hasattr(func, '_endpoint'):
if hasattr(func, "__wrapped__"):
func = func.__wrapped__
else:
return None
return func._endpoint
@property
def function(self):
return self.ctrl._request_wrapper(self.func, self.method,
self.config['json_response'],
self.config['xml'])
@property
def method(self):
return self.config['method']
@property
def proxy(self):
return self.config['proxy']
@property
def url(self):
if self.config['path'] is not None:
url = "{}{}".format(self.ctrl.get_path(), self.config['path'])
else:
url = "{}/{}".format(self.ctrl.get_path(), self.func.__name__)
ctrl_path_params = self.ctrl.get_path_param_names(
self.config['path'])
path_params = [p['name'] for p in self.path_params
if p['name'] not in ctrl_path_params]
path_params = ["{{{}}}".format(p) for p in path_params]
if path_params:
url += "/{}".format("/".join(path_params))
return url
@property
def action(self):
return self.func.__name__
@property
def path_params(self):
ctrl_path_params = self.ctrl.get_path_param_names(
self.config['path'])
func_params = _get_function_params(self.func)
if self.method in ['GET', 'DELETE']:
assert self.config['path_params'] is None
return [p for p in func_params if p['name'] in ctrl_path_params
or (p['name'] not in self.config['query_params']
and p['required'])]
# elif self.method in ['POST', 'PUT']:
return [p for p in func_params if p['name'] in ctrl_path_params
or p['name'] in self.config['path_params']]
@property
def query_params(self):
if self.method in ['GET', 'DELETE']:
func_params = _get_function_params(self.func)
path_params = [p['name'] for p in self.path_params]
return [p for p in func_params if p['name'] not in path_params]
# elif self.method in ['POST', 'PUT']:
func_params = _get_function_params(self.func)
return [p for p in func_params
if p['name'] in self.config['query_params']]
@property
def body_params(self):
func_params = _get_function_params(self.func)
path_params = [p['name'] for p in self.path_params]
query_params = [p['name'] for p in self.query_params]
return [p for p in func_params
if p['name'] not in path_params
and p['name'] not in query_params]
@property
def group(self):
return self.ctrl.__name__
@property
def is_api(self):
return hasattr(self.ctrl, '_api_endpoint')
@property
def is_secure(self):
return self.ctrl._cp_config['tools.authenticate.on']
def __repr__(self):
return "Endpoint({}, {}, {})".format(self.url, self.method,
self.action)
def __init__(self):
logger.info('Initializing controller: %s -> %s',
self.__class__.__name__, self._cp_path_)
def _has_permissions(self, permissions, scope=None):
if not self._cp_config['tools.authenticate.on']:
raise Exception("Cannot verify permission in non secured "
"controllers")
if not isinstance(permissions, list):
permissions = [permissions]
if scope is None:
scope = getattr(self, '_security_scope', None)
if scope is None:
raise Exception("Cannot verify permissions without scope security"
" defined")
username = JwtManager.LOCAL_USER.username
return AuthManager.authorize(username, scope, permissions)
@classmethod
def get_path_param_names(cls, path_extension=None):
if path_extension is None:
path_extension = ""
full_path = cls._cp_path_[1:] + path_extension
path_params = []
for step in full_path.split('/'):
param = None
if not step:
continue
if step[0] == ':':
param = step[1:]
elif step[0] == '{' and step[-1] == '}':
param, _, _ = step[1:-1].partition(':')
if param:
path_params.append(param)
return path_params
@classmethod
def get_path(cls):
return cls._cp_path_
@classmethod
def endpoints(cls):
"""
This method iterates over all the methods decorated with ``@endpoint``
and creates an Endpoint object for each one of the methods.
:return: A list of endpoint objects
:rtype: list[BaseController.Endpoint]
"""
result = []
for _, func in inspect.getmembers(cls, predicate=callable):
if hasattr(func, '_endpoint'):
result.append(cls.Endpoint(cls, func))
return result
@staticmethod
def _request_wrapper(func, method, json_response, xml): # pylint: disable=unused-argument
@wraps(func)
def inner(*args, **kwargs):
for key, value in kwargs.items():
# pylint: disable=undefined-variable
if (sys.version_info < (3, 0) and isinstance(value, unicode)) \
or isinstance(value, str):
kwargs[key] = unquote(value)
# Process method arguments.
params = get_request_body_params(cherrypy.request)
kwargs.update(params)
ret = func(*args, **kwargs)
if isinstance(ret, bytes):
ret = ret.decode('utf-8')
if xml:
cherrypy.response.headers['Content-Type'] = 'application/xml'
return ret.encode('utf8')
if json_response:
cherrypy.response.headers['Content-Type'] = 'application/json'
ret = json.dumps(ret).encode('utf8')
return ret
return inner
@property
def _request(self):
return self.Request(cherrypy.request)
class Request(object):
def __init__(self, cherrypy_req):
self._creq = cherrypy_req
@property
def scheme(self):
return self._creq.scheme
@property
def host(self):
base = self._creq.base
base = base[len(self.scheme)+3:]
return base[:base.find(":")] if ":" in base else base
@property
def port(self):
base = self._creq.base
base = base[len(self.scheme)+3:]
default_port = 443 if self.scheme == 'https' else 80
return int(base[base.find(":")+1:]) if ":" in base else default_port
@property
def path_info(self):
return self._creq.path_info
class RESTController(BaseController):
"""
Base class for providing a RESTful interface to a resource.
To use this class, simply derive a class from it and implement the methods
you want to support. The list of possible methods are:
* list()
* bulk_set(data)
* create(data)
* bulk_delete()
* get(key)
* set(data, key)
* delete(key)
Test with curl:
curl -H "Content-Type: application/json" -X POST \
-d '{"username":"xyz","password":"xyz"}' https://127.0.0.1:8443/foo
curl https://127.0.0.1:8443/foo
curl https://127.0.0.1:8443/foo/0
"""
# resource id parameter for using in get, set, and delete methods
# should be overridden by subclasses.
# to specify a composite id (two parameters) use '/'. e.g., "param1/param2".
# If subclasses don't override this property we try to infer the structure
# of the resourse ID.
RESOURCE_ID = None
_permission_map = {
'GET': Permission.READ,
'POST': Permission.CREATE,
'PUT': Permission.UPDATE,
'DELETE': Permission.DELETE
}
_method_mapping = collections.OrderedDict([
('list', {'method': 'GET', 'resource': False, 'status': 200}),
('create', {'method': 'POST', 'resource': False, 'status': 201}),
('bulk_set', {'method': 'PUT', 'resource': False, 'status': 200}),
('bulk_delete', {'method': 'DELETE', 'resource': False, 'status': 204}),
('get', {'method': 'GET', 'resource': True, 'status': 200}),
('delete', {'method': 'DELETE', 'resource': True, 'status': 204}),
('set', {'method': 'PUT', 'resource': True, 'status': 200})
])
@classmethod
def infer_resource_id(cls):
if cls.RESOURCE_ID is not None:
return cls.RESOURCE_ID.split('/')
for k, v in cls._method_mapping.items():
func = getattr(cls, k, None)
while hasattr(func, "__wrapped__"):
func = func.__wrapped__
if v['resource'] and func:
path_params = cls.get_path_param_names()
params = _get_function_params(func)
return [p['name'] for p in params
if p['required'] and p['name'] not in path_params]
return None
@classmethod
def endpoints(cls):
result = super(RESTController, cls).endpoints()
res_id_params = cls.infer_resource_id()
for _, func in inspect.getmembers(cls, predicate=callable):
no_resource_id_params = False
status = 200
method = None
query_params = None
path = ""
sec_permissions = hasattr(func, '_security_permissions')
permission = None
if func.__name__ in cls._method_mapping:
meth = cls._method_mapping[func.__name__]
if meth['resource']:
if not res_id_params:
no_resource_id_params = True
else:
path_params = ["{{{}}}".format(p) for p in res_id_params]
path += "/{}".format("/".join(path_params))
status = meth['status']
method = meth['method']
if not sec_permissions:
permission = cls._permission_map[method]
elif hasattr(func, "_collection_method_"):
if func._collection_method_['path']:
path = func._collection_method_['path']
else:
path = "/{}".format(func.__name__)
status = func._collection_method_['status']
method = func._collection_method_['method']
query_params = func._collection_method_['query_params']
if not sec_permissions:
permission = cls._permission_map[method]
elif hasattr(func, "_resource_method_"):
if not res_id_params:
no_resource_id_params = True
else:
path_params = ["{{{}}}".format(p) for p in res_id_params]
path += "/{}".format("/".join(path_params))
if func._resource_method_['path']:
path += func._resource_method_['path']
else:
path += "/{}".format(func.__name__)
status = func._resource_method_['status']
method = func._resource_method_['method']
query_params = func._resource_method_['query_params']
if not sec_permissions:
permission = cls._permission_map[method]
else:
continue
if no_resource_id_params:
raise TypeError("Could not infer the resource ID parameters for"
" method {} of controller {}. "
"Please specify the resource ID parameters "
"using the RESOURCE_ID class property"
.format(func.__name__, cls.__name__))
if method in ['GET', 'DELETE']:
params = _get_function_params(func)
if res_id_params is None:
res_id_params = []
if query_params is None:
query_params = [p['name'] for p in params
if p['name'] not in res_id_params]
func = cls._status_code_wrapper(func, status)
endp_func = Endpoint(method, path=path,
query_params=query_params)(func)
if permission:
_set_func_permissions(endp_func, [permission])
result.append(cls.Endpoint(cls, endp_func))
return result
@classmethod
def _status_code_wrapper(cls, func, status_code):
@wraps(func)
def wrapper(*vpath, **params):
cherrypy.response.status = status_code
return func(*vpath, **params)
return wrapper
@staticmethod
def Resource(method=None, path=None, status=None, query_params=None):
if not method:
method = 'GET'
if status is None:
status = 200
def _wrapper(func):
func._resource_method_ = {
'method': method,
'path': path,
'status': status,
'query_params': query_params
}
return func
return _wrapper
@staticmethod
def Collection(method=None, path=None, status=None, query_params=None):
if not method:
method = 'GET'
if status is None:
status = 200
def _wrapper(func):
func._collection_method_ = {
'method': method,
'path': path,
'status': status,
'query_params': query_params
}
return func
return _wrapper
# Role-based access permissions decorators
def _set_func_permissions(func, permissions):
if not isinstance(permissions, list):
permissions = [permissions]
for perm in permissions:
if not Permission.valid_permission(perm):
logger.debug("Invalid security permission: %s\n "
"Possible values: %s", perm,
Permission.all_permissions())
raise PermissionNotValid(perm)
if not hasattr(func, '_security_permissions'):
func._security_permissions = permissions
else:
permissions.extend(func._security_permissions)
func._security_permissions = list(set(permissions))
def ReadPermission(func):
_set_func_permissions(func, Permission.READ)
return func
def CreatePermission(func):
_set_func_permissions(func, Permission.CREATE)
return func
def DeletePermission(func):
_set_func_permissions(func, Permission.DELETE)
return func
def UpdatePermission(func):
_set_func_permissions(func, Permission.UPDATE)
return func
|