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