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