]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/restful/decorators.py
bump version to 15.2.11-pve1
[ceph.git] / ceph / src / pybind / mgr / restful / decorators.py
CommitLineData
11fdf7f2
TL
1from __future__ import absolute_import
2
31f18b77
FG
3from pecan import request, response
4from base64 import b64decode
5from functools import wraps
6
7import traceback
8
11fdf7f2 9from . import context
31f18b77
FG
10
11
12# Handle authorization
13def auth(f):
14 @wraps(f)
15 def decorated(*args, **kwargs):
16 if not request.authorization:
17 response.status = 401
18 response.headers['WWW-Authenticate'] = 'Basic realm="Login Required"'
19 return {'message': 'auth: No HTTP username/password'}
20
11fdf7f2 21 username, password = b64decode(request.authorization[1]).decode('utf-8').split(':')
31f18b77
FG
22
23 # Check that the username exists
11fdf7f2 24 if username not in context.instance.keys:
31f18b77
FG
25 response.status = 401
26 response.headers['WWW-Authenticate'] = 'Basic realm="Login Required"'
27 return {'message': 'auth: No such user'}
28
29 # Check the password
11fdf7f2 30 if context.instance.keys[username] != password:
31f18b77
FG
31 response.status = 401
32 response.headers['WWW-Authenticate'] = 'Basic realm="Login Required"'
33 return {'message': 'auth: Incorrect password'}
34
35 return f(*args, **kwargs)
36 return decorated
37
38
39# Helper function to lock the function
40def lock(f):
41 @wraps(f)
42 def decorated(*args, **kwargs):
11fdf7f2 43 with context.instance.requests_lock:
31f18b77
FG
44 return f(*args, **kwargs)
45 return decorated
46
47
48# Support ?page=N argument
49def paginate(f):
50 @wraps(f)
51 def decorated(*args, **kwargs):
52 _out = f(*args, **kwargs)
53
54 # Do not modify anything without a specific request
55 if not 'page' in kwargs:
56 return _out
57
58 # A pass-through for errors, etc
59 if not isinstance(_out, list):
60 return _out
61
62 # Parse the page argument
63 _page = kwargs['page']
64 try:
65 _page = int(_page)
66 except ValueError:
67 response.status = 500
68 return {'message': 'The requested page is not an integer'}
69
70 # Raise _page so that 0 is the first page and -1 is the last
71 _page += 1
72
73 if _page > 0:
74 _page *= 100
75 else:
76 _page = len(_out) - (_page*100)
77
78 return _out[_page - 100: _page]
79 return decorated