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