Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 3 | import os |
| 4 | import sys |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 5 | import dbus |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 6 | import dbus.exceptions |
| 7 | import json |
| 8 | import logging |
| 9 | from xml.etree import ElementTree |
| 10 | from rocket import Rocket |
| 11 | from bottle import Bottle, abort, request, response, JSONPlugin, HTTPError |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame^] | 12 | import obmc.utils.misc |
| 13 | import obmc.utils.pathtree |
| 14 | from obmc.dbuslib.introspection import IntrospectionNodeParser |
| 15 | import obmc.mapper |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 16 | import spwd |
| 17 | import grp |
| 18 | import crypt |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 19 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 20 | DBUS_UNKNOWN_INTERFACE = 'org.freedesktop.UnknownInterface' |
| 21 | DBUS_UNKNOWN_METHOD = 'org.freedesktop.DBus.Error.UnknownMethod' |
| 22 | DBUS_INVALID_ARGS = 'org.freedesktop.DBus.Error.InvalidArgs' |
Brad Bishop | d457892 | 2015-12-02 11:10:36 -0500 | [diff] [blame] | 23 | DBUS_TYPE_ERROR = 'org.freedesktop.DBus.Python.TypeError' |
Brad Bishop | aac521c | 2015-11-25 09:16:35 -0500 | [diff] [blame] | 24 | DELETE_IFACE = 'org.openbmc.Object.Delete' |
Brad Bishop | 9ee57c4 | 2015-11-03 14:59:29 -0500 | [diff] [blame] | 25 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 26 | _4034_msg = "The specified %s cannot be %s: '%s'" |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 27 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 28 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 29 | def valid_user(session, *a, **kw): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 30 | ''' Authorization plugin callback that checks |
| 31 | that the user is logged in. ''' |
| 32 | if session is None: |
| 33 | abort(403, 'Login required') |
| 34 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 35 | |
| 36 | class UserInGroup: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 37 | ''' Authorization plugin callback that checks that the user is logged in |
| 38 | and a member of a group. ''' |
| 39 | def __init__(self, group): |
| 40 | self.group = group |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 41 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 42 | def __call__(self, session, *a, **kw): |
| 43 | valid_user(session, *a, **kw) |
| 44 | res = False |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 45 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 46 | try: |
| 47 | res = session['user'] in grp.getgrnam(self.group)[3] |
| 48 | except KeyError: |
| 49 | pass |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 50 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 51 | if not res: |
| 52 | abort(403, 'Insufficient access') |
| 53 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 54 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 55 | def find_case_insensitive(value, lst): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 56 | return next((x for x in lst if x.lower() == value.lower()), None) |
| 57 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 58 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 59 | def makelist(data): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 60 | if isinstance(data, list): |
| 61 | return data |
| 62 | elif data: |
| 63 | return [data] |
| 64 | else: |
| 65 | return [] |
| 66 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 67 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 68 | class RouteHandler(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 69 | _require_auth = makelist(valid_user) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 70 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 71 | def __init__(self, app, bus, verbs, rules): |
| 72 | self.app = app |
| 73 | self.bus = bus |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame^] | 74 | self.mapper = obmc.mapper.Mapper(bus) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 75 | self._verbs = makelist(verbs) |
| 76 | self._rules = rules |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 77 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 78 | def _setup(self, **kw): |
| 79 | request.route_data = {} |
| 80 | if request.method in self._verbs: |
| 81 | return self.setup(**kw) |
| 82 | else: |
| 83 | self.find(**kw) |
| 84 | raise HTTPError( |
| 85 | 405, "Method not allowed.", Allow=','.join(self._verbs)) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 86 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 87 | def __call__(self, **kw): |
| 88 | return getattr(self, 'do_' + request.method.lower())(**kw) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 89 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 90 | def install(self): |
| 91 | self.app.route( |
| 92 | self._rules, callback=self, |
| 93 | method=['GET', 'PUT', 'PATCH', 'POST', 'DELETE']) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 94 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 95 | @staticmethod |
| 96 | def try_mapper_call(f, callback=None, **kw): |
| 97 | try: |
| 98 | return f(**kw) |
| 99 | except dbus.exceptions.DBusException, e: |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame^] | 100 | if e.get_dbus_name() != obmc.mapper.MAPPER_NOT_FOUND: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 101 | raise |
| 102 | if callback is None: |
| 103 | def callback(e, **kw): |
| 104 | abort(404, str(e)) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 105 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 106 | callback(e, **kw) |
| 107 | |
| 108 | @staticmethod |
| 109 | def try_properties_interface(f, *a): |
| 110 | try: |
| 111 | return f(*a) |
| 112 | except dbus.exceptions.DBusException, e: |
| 113 | if DBUS_UNKNOWN_INTERFACE in e.get_dbus_message(): |
| 114 | # interface doesn't have any properties |
| 115 | return None |
| 116 | if DBUS_UNKNOWN_METHOD == e.get_dbus_name(): |
| 117 | # properties interface not implemented at all |
| 118 | return None |
| 119 | raise |
| 120 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 121 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 122 | class DirectoryHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 123 | verbs = 'GET' |
| 124 | rules = '<path:path>/' |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 125 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 126 | def __init__(self, app, bus): |
| 127 | super(DirectoryHandler, self).__init__( |
| 128 | app, bus, self.verbs, self.rules) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 129 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 130 | def find(self, path='/'): |
| 131 | return self.try_mapper_call( |
| 132 | self.mapper.get_subtree_paths, path=path, depth=1) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 133 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 134 | def setup(self, path='/'): |
| 135 | request.route_data['map'] = self.find(path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 136 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 137 | def do_get(self, path='/'): |
| 138 | return request.route_data['map'] |
| 139 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 140 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 141 | class ListNamesHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 142 | verbs = 'GET' |
| 143 | rules = ['/list', '<path:path>/list'] |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 144 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 145 | def __init__(self, app, bus): |
| 146 | super(ListNamesHandler, self).__init__( |
| 147 | app, bus, self.verbs, self.rules) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 148 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 149 | def find(self, path='/'): |
| 150 | return self.try_mapper_call( |
| 151 | self.mapper.get_subtree, path=path).keys() |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 152 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 153 | def setup(self, path='/'): |
| 154 | request.route_data['map'] = self.find(path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 155 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 156 | def do_get(self, path='/'): |
| 157 | return request.route_data['map'] |
| 158 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 159 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 160 | class ListHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 161 | verbs = 'GET' |
| 162 | rules = ['/enumerate', '<path:path>/enumerate'] |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 163 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 164 | def __init__(self, app, bus): |
| 165 | super(ListHandler, self).__init__( |
| 166 | app, bus, self.verbs, self.rules) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 167 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 168 | def find(self, path='/'): |
| 169 | return self.try_mapper_call( |
| 170 | self.mapper.get_subtree, path=path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 171 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 172 | def setup(self, path='/'): |
| 173 | request.route_data['map'] = self.find(path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 174 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 175 | def do_get(self, path='/'): |
| 176 | objs = {} |
| 177 | mapper_data = request.route_data['map'] |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame^] | 178 | tree = obmc.utils.pathreee.PathTree() |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 179 | for x, y in mapper_data.iteritems(): |
| 180 | tree[x] = y |
Brad Bishop | 936f5fe | 2015-11-03 15:10:11 -0500 | [diff] [blame] | 181 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 182 | try: |
| 183 | # Check to see if the root path implements |
| 184 | # enumerate in addition to any sub tree |
| 185 | # objects. |
| 186 | root = self.try_mapper_call( |
| 187 | self.mapper.get_object, path=path) |
| 188 | mapper_data[path] = root |
| 189 | except: |
| 190 | pass |
Brad Bishop | 936f5fe | 2015-11-03 15:10:11 -0500 | [diff] [blame] | 191 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 192 | have_enumerate = [ |
| 193 | (x[0], self.enumerate_capable(*x)) |
| 194 | for x in mapper_data.iteritems() if self.enumerate_capable(*x)] |
Brad Bishop | 936f5fe | 2015-11-03 15:10:11 -0500 | [diff] [blame] | 195 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 196 | for x, y in have_enumerate: |
| 197 | objs.update(self.call_enumerate(x, y)) |
| 198 | tmp = tree[x] |
| 199 | # remove the subtree |
| 200 | del tree[x] |
| 201 | # add the new leaf back since enumerate results don't |
| 202 | # include the object enumerate is being invoked on |
| 203 | tree[x] = tmp |
Brad Bishop | 936f5fe | 2015-11-03 15:10:11 -0500 | [diff] [blame] | 204 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 205 | # make dbus calls for any remaining objects |
| 206 | for x, y in tree.dataitems(): |
| 207 | objs[x] = self.app.instance_handler.do_get(x) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 208 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 209 | return objs |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 210 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 211 | @staticmethod |
| 212 | def enumerate_capable(path, bus_data): |
| 213 | busses = [] |
| 214 | for name, ifaces in bus_data.iteritems(): |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame^] | 215 | if obmc.mapper.ENUMERATE_IFACE in ifaces: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 216 | busses.append(name) |
| 217 | return busses |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 218 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 219 | def call_enumerate(self, path, busses): |
| 220 | objs = {} |
| 221 | for b in busses: |
| 222 | obj = self.bus.get_object(b, path, introspect=False) |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame^] | 223 | iface = dbus.Interface(obj, obmc.mapper.ENUMERATE_IFACE) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 224 | objs.update(iface.enumerate()) |
| 225 | return objs |
| 226 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 227 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 228 | class MethodHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 229 | verbs = 'POST' |
| 230 | rules = '<path:path>/action/<method>' |
| 231 | request_type = list |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 232 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 233 | def __init__(self, app, bus): |
| 234 | super(MethodHandler, self).__init__( |
| 235 | app, bus, self.verbs, self.rules) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 236 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 237 | def find(self, path, method): |
| 238 | busses = self.try_mapper_call( |
| 239 | self.mapper.get_object, path=path) |
| 240 | for items in busses.iteritems(): |
| 241 | m = self.find_method_on_bus(path, method, *items) |
| 242 | if m: |
| 243 | return m |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 244 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 245 | abort(404, _4034_msg % ('method', 'found', method)) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 246 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 247 | def setup(self, path, method): |
| 248 | request.route_data['method'] = self.find(path, method) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 249 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 250 | def do_post(self, path, method): |
| 251 | try: |
| 252 | if request.parameter_list: |
| 253 | return request.route_data['method'](*request.parameter_list) |
| 254 | else: |
| 255 | return request.route_data['method']() |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 256 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 257 | except dbus.exceptions.DBusException, e: |
| 258 | if e.get_dbus_name() == DBUS_INVALID_ARGS: |
| 259 | abort(400, str(e)) |
| 260 | if e.get_dbus_name() == DBUS_TYPE_ERROR: |
| 261 | abort(400, str(e)) |
| 262 | raise |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 263 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 264 | @staticmethod |
| 265 | def find_method_in_interface(method, obj, interface, methods): |
| 266 | if methods is None: |
| 267 | return None |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 268 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 269 | method = find_case_insensitive(method, methods.keys()) |
| 270 | if method is not None: |
| 271 | iface = dbus.Interface(obj, interface) |
| 272 | return iface.get_dbus_method(method) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 273 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 274 | def find_method_on_bus(self, path, method, bus, interfaces): |
| 275 | obj = self.bus.get_object(bus, path, introspect=False) |
| 276 | iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE) |
| 277 | data = iface.Introspect() |
| 278 | parser = IntrospectionNodeParser( |
| 279 | ElementTree.fromstring(data), |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame^] | 280 | intf_match=obmc.utils.misc.ListMatch(interfaces)) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 281 | for x, y in parser.get_interfaces().iteritems(): |
| 282 | m = self.find_method_in_interface( |
| 283 | method, obj, x, y.get('method')) |
| 284 | if m: |
| 285 | return m |
| 286 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 287 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 288 | class PropertyHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 289 | verbs = ['PUT', 'GET'] |
| 290 | rules = '<path:path>/attr/<prop>' |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 291 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 292 | def __init__(self, app, bus): |
| 293 | super(PropertyHandler, self).__init__( |
| 294 | app, bus, self.verbs, self.rules) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 295 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 296 | def find(self, path, prop): |
| 297 | self.app.instance_handler.setup(path) |
| 298 | obj = self.app.instance_handler.do_get(path) |
| 299 | try: |
| 300 | obj[prop] |
| 301 | except KeyError, e: |
| 302 | if request.method == 'PUT': |
| 303 | abort(403, _4034_msg % ('property', 'created', str(e))) |
| 304 | else: |
| 305 | abort(404, _4034_msg % ('property', 'found', str(e))) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 306 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 307 | return {path: obj} |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 308 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 309 | def setup(self, path, prop): |
| 310 | request.route_data['obj'] = self.find(path, prop) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 311 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 312 | def do_get(self, path, prop): |
| 313 | return request.route_data['obj'][path][prop] |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 314 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 315 | def do_put(self, path, prop, value=None): |
| 316 | if value is None: |
| 317 | value = request.parameter_list |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 318 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 319 | prop, iface, properties_iface = self.get_host_interface( |
| 320 | path, prop, request.route_data['map'][path]) |
| 321 | try: |
| 322 | properties_iface.Set(iface, prop, value) |
| 323 | except ValueError, e: |
| 324 | abort(400, str(e)) |
| 325 | except dbus.exceptions.DBusException, e: |
| 326 | if e.get_dbus_name() == DBUS_INVALID_ARGS: |
| 327 | abort(403, str(e)) |
| 328 | raise |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 329 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 330 | def get_host_interface(self, path, prop, bus_info): |
| 331 | for bus, interfaces in bus_info.iteritems(): |
| 332 | obj = self.bus.get_object(bus, path, introspect=True) |
| 333 | properties_iface = dbus.Interface( |
| 334 | obj, dbus_interface=dbus.PROPERTIES_IFACE) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 335 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 336 | info = self.get_host_interface_on_bus( |
| 337 | path, prop, properties_iface, bus, interfaces) |
| 338 | if info is not None: |
| 339 | prop, iface = info |
| 340 | return prop, iface, properties_iface |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 341 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 342 | def get_host_interface_on_bus(self, path, prop, iface, bus, interfaces): |
| 343 | for i in interfaces: |
| 344 | properties = self.try_properties_interface(iface.GetAll, i) |
| 345 | if properties is None: |
| 346 | continue |
| 347 | prop = find_case_insensitive(prop, properties.keys()) |
| 348 | if prop is None: |
| 349 | continue |
| 350 | return prop, i |
| 351 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 352 | |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 353 | class SchemaHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 354 | verbs = ['GET'] |
| 355 | rules = '<path:path>/schema' |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 356 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 357 | def __init__(self, app, bus): |
| 358 | super(SchemaHandler, self).__init__( |
| 359 | app, bus, self.verbs, self.rules) |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 360 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 361 | def find(self, path): |
| 362 | return self.try_mapper_call( |
| 363 | self.mapper.get_object, |
| 364 | path=path) |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 365 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 366 | def setup(self, path): |
| 367 | request.route_data['map'] = self.find(path) |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 368 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 369 | def do_get(self, path): |
| 370 | schema = {} |
| 371 | for x in request.route_data['map'].iterkeys(): |
| 372 | obj = self.bus.get_object(x, path, introspect=False) |
| 373 | iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE) |
| 374 | data = iface.Introspect() |
| 375 | parser = IntrospectionNodeParser( |
| 376 | ElementTree.fromstring(data)) |
| 377 | for x, y in parser.get_interfaces().iteritems(): |
| 378 | schema[x] = y |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 379 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 380 | return schema |
| 381 | |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 382 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 383 | class InstanceHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 384 | verbs = ['GET', 'PUT', 'DELETE'] |
| 385 | rules = '<path:path>' |
| 386 | request_type = dict |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 387 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 388 | def __init__(self, app, bus): |
| 389 | super(InstanceHandler, self).__init__( |
| 390 | app, bus, self.verbs, self.rules) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 391 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 392 | def find(self, path, callback=None): |
| 393 | return {path: self.try_mapper_call( |
| 394 | self.mapper.get_object, |
| 395 | callback, |
| 396 | path=path)} |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 397 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 398 | def setup(self, path): |
| 399 | callback = None |
| 400 | if request.method == 'PUT': |
| 401 | def callback(e, **kw): |
| 402 | abort(403, _4034_msg % ('resource', 'created', path)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 403 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 404 | if request.route_data.get('map') is None: |
| 405 | request.route_data['map'] = self.find(path, callback) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 406 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 407 | def do_get(self, path): |
| 408 | properties = {} |
| 409 | for item in request.route_data['map'][path].iteritems(): |
| 410 | properties.update(self.get_properties_on_bus( |
| 411 | path, *item)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 412 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 413 | return properties |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 414 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 415 | @staticmethod |
| 416 | def get_properties_on_iface(properties_iface, iface): |
| 417 | properties = InstanceHandler.try_properties_interface( |
| 418 | properties_iface.GetAll, iface) |
| 419 | if properties is None: |
| 420 | return {} |
| 421 | return properties |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 422 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 423 | def get_properties_on_bus(self, path, bus, interfaces): |
| 424 | properties = {} |
| 425 | obj = self.bus.get_object(bus, path, introspect=False) |
| 426 | properties_iface = dbus.Interface( |
| 427 | obj, dbus_interface=dbus.PROPERTIES_IFACE) |
| 428 | for i in interfaces: |
| 429 | properties.update(self.get_properties_on_iface( |
| 430 | properties_iface, i)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 431 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 432 | return properties |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 433 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 434 | def do_put(self, path): |
| 435 | # make sure all properties exist in the request |
| 436 | obj = set(self.do_get(path).keys()) |
| 437 | req = set(request.parameter_list.keys()) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 438 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 439 | diff = list(obj.difference(req)) |
| 440 | if diff: |
| 441 | abort(403, _4034_msg % ( |
| 442 | 'resource', 'removed', '%s/attr/%s' % (path, diff[0]))) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 443 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 444 | diff = list(req.difference(obj)) |
| 445 | if diff: |
| 446 | abort(403, _4034_msg % ( |
| 447 | 'resource', 'created', '%s/attr/%s' % (path, diff[0]))) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 448 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 449 | for p, v in request.parameter_list.iteritems(): |
| 450 | self.app.property_handler.do_put( |
| 451 | path, p, v) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 452 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 453 | def do_delete(self, path): |
| 454 | for bus_info in request.route_data['map'][path].iteritems(): |
| 455 | if self.bus_missing_delete(path, *bus_info): |
| 456 | abort(403, _4034_msg % ('resource', 'removed', path)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 457 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 458 | for bus in request.route_data['map'][path].iterkeys(): |
| 459 | self.delete_on_bus(path, bus) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 460 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 461 | def bus_missing_delete(self, path, bus, interfaces): |
| 462 | return DELETE_IFACE not in interfaces |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 463 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 464 | def delete_on_bus(self, path, bus): |
| 465 | obj = self.bus.get_object(bus, path, introspect=False) |
| 466 | delete_iface = dbus.Interface( |
| 467 | obj, dbus_interface=DELETE_IFACE) |
| 468 | delete_iface.Delete() |
| 469 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 470 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 471 | class SessionHandler(MethodHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 472 | ''' Handles the /login and /logout routes, manages |
| 473 | server side session store and session cookies. ''' |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 474 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 475 | rules = ['/login', '/logout'] |
| 476 | login_str = "User '%s' logged %s" |
| 477 | bad_passwd_str = "Invalid username or password" |
| 478 | no_user_str = "No user logged in" |
| 479 | bad_json_str = "Expecting request format { 'data': " \ |
| 480 | "[<username>, <password>] }, got '%s'" |
| 481 | _require_auth = None |
| 482 | MAX_SESSIONS = 16 |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 483 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 484 | def __init__(self, app, bus): |
| 485 | super(SessionHandler, self).__init__( |
| 486 | app, bus) |
| 487 | self.hmac_key = os.urandom(128) |
| 488 | self.session_store = [] |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 489 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 490 | @staticmethod |
| 491 | def authenticate(username, clear): |
| 492 | try: |
| 493 | encoded = spwd.getspnam(username)[1] |
| 494 | return encoded == crypt.crypt(clear, encoded) |
| 495 | except KeyError: |
| 496 | return False |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 497 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 498 | def invalidate_session(self, session): |
| 499 | try: |
| 500 | self.session_store.remove(session) |
| 501 | except ValueError: |
| 502 | pass |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 503 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 504 | def new_session(self): |
| 505 | sid = os.urandom(32) |
| 506 | if self.MAX_SESSIONS <= len(self.session_store): |
| 507 | self.session_store.pop() |
| 508 | self.session_store.insert(0, {'sid': sid}) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 509 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 510 | return self.session_store[0] |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 511 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 512 | def get_session(self, sid): |
| 513 | sids = [x['sid'] for x in self.session_store] |
| 514 | try: |
| 515 | return self.session_store[sids.index(sid)] |
| 516 | except ValueError: |
| 517 | return None |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 518 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 519 | def get_session_from_cookie(self): |
| 520 | return self.get_session( |
| 521 | request.get_cookie( |
| 522 | 'sid', secret=self.hmac_key)) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 523 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 524 | def do_post(self, **kw): |
| 525 | if request.path == '/login': |
| 526 | return self.do_login(**kw) |
| 527 | else: |
| 528 | return self.do_logout(**kw) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 529 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 530 | def do_logout(self, **kw): |
| 531 | session = self.get_session_from_cookie() |
| 532 | if session is not None: |
| 533 | user = session['user'] |
| 534 | self.invalidate_session(session) |
| 535 | response.delete_cookie('sid') |
| 536 | return self.login_str % (user, 'out') |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 537 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 538 | return self.no_user_str |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 539 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 540 | def do_login(self, **kw): |
| 541 | session = self.get_session_from_cookie() |
| 542 | if session is not None: |
| 543 | return self.login_str % (session['user'], 'in') |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 544 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 545 | if len(request.parameter_list) != 2: |
| 546 | abort(400, self.bad_json_str % (request.json)) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 547 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 548 | if not self.authenticate(*request.parameter_list): |
| 549 | return self.bad_passwd_str |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 550 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 551 | user = request.parameter_list[0] |
| 552 | session = self.new_session() |
| 553 | session['user'] = user |
| 554 | response.set_cookie( |
| 555 | 'sid', session['sid'], secret=self.hmac_key, |
| 556 | secure=True, |
| 557 | httponly=True) |
| 558 | return self.login_str % (user, 'in') |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 559 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 560 | def find(self, **kw): |
| 561 | pass |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 562 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 563 | def setup(self, **kw): |
| 564 | pass |
| 565 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 566 | |
| 567 | class AuthorizationPlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 568 | ''' Invokes an optional list of authorization callbacks. ''' |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 569 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 570 | name = 'authorization' |
| 571 | api = 2 |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 572 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 573 | class Compose: |
| 574 | def __init__(self, validators, callback, session_mgr): |
| 575 | self.validators = validators |
| 576 | self.callback = callback |
| 577 | self.session_mgr = session_mgr |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 578 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 579 | def __call__(self, *a, **kw): |
| 580 | sid = request.get_cookie('sid', secret=self.session_mgr.hmac_key) |
| 581 | session = self.session_mgr.get_session(sid) |
| 582 | for x in self.validators: |
| 583 | x(session, *a, **kw) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 584 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 585 | return self.callback(*a, **kw) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 586 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 587 | def apply(self, callback, route): |
| 588 | undecorated = route.get_undecorated_callback() |
| 589 | if not isinstance(undecorated, RouteHandler): |
| 590 | return callback |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 591 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 592 | auth_types = getattr( |
| 593 | undecorated, '_require_auth', None) |
| 594 | if not auth_types: |
| 595 | return callback |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 596 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 597 | return self.Compose( |
| 598 | auth_types, callback, undecorated.app.session_handler) |
| 599 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 600 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 601 | class JsonApiRequestPlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 602 | ''' Ensures request content satisfies the OpenBMC json api format. ''' |
| 603 | name = 'json_api_request' |
| 604 | api = 2 |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 605 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 606 | error_str = "Expecting request format { 'data': <value> }, got '%s'" |
| 607 | type_error_str = "Unsupported Content-Type: '%s'" |
| 608 | json_type = "application/json" |
| 609 | request_methods = ['PUT', 'POST', 'PATCH'] |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 610 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 611 | @staticmethod |
| 612 | def content_expected(): |
| 613 | return request.method in JsonApiRequestPlugin.request_methods |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 614 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 615 | def validate_request(self): |
| 616 | if request.content_length > 0 and \ |
| 617 | request.content_type != self.json_type: |
| 618 | abort(415, self.type_error_str % request.content_type) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 619 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 620 | try: |
| 621 | request.parameter_list = request.json.get('data') |
| 622 | except ValueError, e: |
| 623 | abort(400, str(e)) |
| 624 | except (AttributeError, KeyError, TypeError): |
| 625 | abort(400, self.error_str % request.json) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 626 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 627 | def apply(self, callback, route): |
| 628 | verbs = getattr( |
| 629 | route.get_undecorated_callback(), '_verbs', None) |
| 630 | if verbs is None: |
| 631 | return callback |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 632 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 633 | if not set(self.request_methods).intersection(verbs): |
| 634 | return callback |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 635 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 636 | def wrap(*a, **kw): |
| 637 | if self.content_expected(): |
| 638 | self.validate_request() |
| 639 | return callback(*a, **kw) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 640 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 641 | return wrap |
| 642 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 643 | |
| 644 | class JsonApiRequestTypePlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 645 | ''' Ensures request content type satisfies the OpenBMC json api format. ''' |
| 646 | name = 'json_api_method_request' |
| 647 | api = 2 |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 648 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 649 | error_str = "Expecting request format { 'data': %s }, got '%s'" |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 650 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 651 | def apply(self, callback, route): |
| 652 | request_type = getattr( |
| 653 | route.get_undecorated_callback(), 'request_type', None) |
| 654 | if request_type is None: |
| 655 | return callback |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 656 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 657 | def validate_request(): |
| 658 | if not isinstance(request.parameter_list, request_type): |
| 659 | abort(400, self.error_str % (str(request_type), request.json)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 660 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 661 | def wrap(*a, **kw): |
| 662 | if JsonApiRequestPlugin.content_expected(): |
| 663 | validate_request() |
| 664 | return callback(*a, **kw) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 665 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 666 | return wrap |
| 667 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 668 | |
| 669 | class JsonApiResponsePlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 670 | ''' Emits normal responses in the OpenBMC json api format. ''' |
| 671 | name = 'json_api_response' |
| 672 | api = 2 |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 673 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 674 | def apply(self, callback, route): |
| 675 | def wrap(*a, **kw): |
| 676 | resp = {'data': callback(*a, **kw)} |
| 677 | resp['status'] = 'ok' |
| 678 | resp['message'] = response.status_line |
| 679 | return resp |
| 680 | return wrap |
| 681 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 682 | |
| 683 | class JsonApiErrorsPlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 684 | ''' Emits error responses in the OpenBMC json api format. ''' |
| 685 | name = 'json_api_errors' |
| 686 | api = 2 |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 687 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 688 | def __init__(self, **kw): |
| 689 | self.app = None |
| 690 | self.function_type = None |
| 691 | self.original = None |
| 692 | self.json_opts = { |
| 693 | x: y for x, y in kw.iteritems() |
| 694 | if x in ['indent', 'sort_keys']} |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 695 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 696 | def setup(self, app): |
| 697 | self.app = app |
| 698 | self.function_type = type(app.default_error_handler) |
| 699 | self.original = app.default_error_handler |
| 700 | self.app.default_error_handler = self.function_type( |
| 701 | self.json_errors, app, Bottle) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 702 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 703 | def apply(self, callback, route): |
| 704 | return callback |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 705 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 706 | def close(self): |
| 707 | self.app.default_error_handler = self.function_type( |
| 708 | self.original, self.app, Bottle) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 709 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 710 | def json_errors(self, res, error): |
| 711 | response_object = {'status': 'error', 'data': {}} |
| 712 | response_object['message'] = error.status_line |
| 713 | response_object['data']['description'] = str(error.body) |
| 714 | if error.status_code == 500: |
| 715 | response_object['data']['exception'] = repr(error.exception) |
| 716 | response_object['data']['traceback'] = error.traceback.splitlines() |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 717 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 718 | json_response = json.dumps(response_object, **self.json_opts) |
| 719 | response.content_type = 'application/json' |
| 720 | return json_response |
| 721 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 722 | |
| 723 | class RestApp(Bottle): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 724 | def __init__(self, bus): |
| 725 | super(RestApp, self).__init__(autojson=False) |
| 726 | self.bus = bus |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame^] | 727 | self.mapper = obmc.mapper.Mapper(bus) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 728 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 729 | self.install_hooks() |
| 730 | self.install_plugins() |
| 731 | self.create_handlers() |
| 732 | self.install_handlers() |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 733 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 734 | def install_plugins(self): |
| 735 | # install json api plugins |
| 736 | json_kw = {'indent': 2, 'sort_keys': True} |
| 737 | self.install(JSONPlugin(**json_kw)) |
| 738 | self.install(JsonApiErrorsPlugin(**json_kw)) |
| 739 | self.install(AuthorizationPlugin()) |
| 740 | self.install(JsonApiResponsePlugin()) |
| 741 | self.install(JsonApiRequestPlugin()) |
| 742 | self.install(JsonApiRequestTypePlugin()) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 743 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 744 | def install_hooks(self): |
| 745 | self.real_router_match = self.router.match |
| 746 | self.router.match = self.custom_router_match |
| 747 | self.add_hook('before_request', self.strip_extra_slashes) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 748 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 749 | def create_handlers(self): |
| 750 | # create route handlers |
| 751 | self.session_handler = SessionHandler(self, self.bus) |
| 752 | self.directory_handler = DirectoryHandler(self, self.bus) |
| 753 | self.list_names_handler = ListNamesHandler(self, self.bus) |
| 754 | self.list_handler = ListHandler(self, self.bus) |
| 755 | self.method_handler = MethodHandler(self, self.bus) |
| 756 | self.property_handler = PropertyHandler(self, self.bus) |
| 757 | self.schema_handler = SchemaHandler(self, self.bus) |
| 758 | self.instance_handler = InstanceHandler(self, self.bus) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 759 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 760 | def install_handlers(self): |
| 761 | self.session_handler.install() |
| 762 | self.directory_handler.install() |
| 763 | self.list_names_handler.install() |
| 764 | self.list_handler.install() |
| 765 | self.method_handler.install() |
| 766 | self.property_handler.install() |
| 767 | self.schema_handler.install() |
| 768 | # this has to come last, since it matches everything |
| 769 | self.instance_handler.install() |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 770 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 771 | def custom_router_match(self, environ): |
| 772 | ''' The built-in Bottle algorithm for figuring out if a 404 or 405 is |
| 773 | needed doesn't work for us since the instance rules match |
| 774 | everything. This monkey-patch lets the route handler figure |
| 775 | out which response is needed. This could be accomplished |
| 776 | with a hook but that would require calling the router match |
| 777 | function twice. |
| 778 | ''' |
| 779 | route, args = self.real_router_match(environ) |
| 780 | if isinstance(route.callback, RouteHandler): |
| 781 | route.callback._setup(**args) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 782 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 783 | return route, args |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 784 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 785 | @staticmethod |
| 786 | def strip_extra_slashes(): |
| 787 | path = request.environ['PATH_INFO'] |
| 788 | trailing = ("", "/")[path[-1] == '/'] |
| 789 | parts = filter(bool, path.split('/')) |
| 790 | request.environ['PATH_INFO'] = '/' + '/'.join(parts) + trailing |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 791 | |
| 792 | if __name__ == '__main__': |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 793 | log = logging.getLogger('Rocket.Errors') |
| 794 | log.setLevel(logging.INFO) |
| 795 | log.addHandler(logging.StreamHandler(sys.stdout)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 796 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 797 | bus = dbus.SystemBus() |
| 798 | app = RestApp(bus) |
| 799 | default_cert = os.path.join( |
| 800 | sys.prefix, 'share', os.path.basename(__file__), 'cert.pem') |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 801 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 802 | server = Rocket( |
| 803 | ('0.0.0.0', 443, default_cert, default_cert), |
| 804 | 'wsgi', {'wsgi_app': app}, |
| 805 | min_threads=1, |
| 806 | max_threads=1) |
| 807 | server.start() |