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