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