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 |
Jayanth Othayoth | 9bc9499 | 2017-06-29 06:30:40 -0500 | [diff] [blame] | 23 | from bottle import static_file |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame] | 24 | import obmc.utils.misc |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame] | 25 | from obmc.dbuslib.introspection import IntrospectionNodeParser |
| 26 | import obmc.mapper |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 27 | import spwd |
| 28 | import grp |
| 29 | import crypt |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 30 | import tempfile |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 31 | import re |
Deepak Kodihalli | 639b502 | 2017-10-13 06:40:26 -0500 | [diff] [blame] | 32 | have_wsock = True |
| 33 | try: |
| 34 | from geventwebsocket import WebSocketError |
| 35 | except ImportError: |
| 36 | have_wsock = False |
| 37 | if have_wsock: |
| 38 | from dbus.mainloop.glib import DBusGMainLoop |
| 39 | DBusGMainLoop(set_as_default=True) |
| 40 | import gobject |
| 41 | import gevent |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 42 | |
Adriana Kobylak | f92cf4d | 2017-12-13 11:46:50 -0600 | [diff] [blame] | 43 | DBUS_UNKNOWN_INTERFACE = 'org.freedesktop.DBus.Error.UnknownInterface' |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 44 | DBUS_UNKNOWN_METHOD = 'org.freedesktop.DBus.Error.UnknownMethod' |
| 45 | DBUS_INVALID_ARGS = 'org.freedesktop.DBus.Error.InvalidArgs' |
Brad Bishop | d457892 | 2015-12-02 11:10:36 -0500 | [diff] [blame] | 46 | DBUS_TYPE_ERROR = 'org.freedesktop.DBus.Python.TypeError' |
Deepak Kodihalli | 6075bb4 | 2017-04-04 05:49:17 -0500 | [diff] [blame] | 47 | DELETE_IFACE = 'xyz.openbmc_project.Object.Delete' |
Brad Bishop | 9ee57c4 | 2015-11-03 14:59:29 -0500 | [diff] [blame] | 48 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 49 | _4034_msg = "The specified %s cannot be %s: '%s'" |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 50 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 51 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 52 | def valid_user(session, *a, **kw): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 53 | ''' Authorization plugin callback that checks |
| 54 | that the user is logged in. ''' |
| 55 | if session is None: |
Brad Bishop | dc3fbfa | 2016-09-08 09:51:38 -0400 | [diff] [blame] | 56 | abort(401, 'Login required') |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 57 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 58 | |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 59 | def get_type_signature_by_introspection(bus, service, object_path, |
| 60 | property_name): |
| 61 | obj = bus.get_object(service, object_path) |
| 62 | iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable') |
| 63 | xml_string = iface.Introspect() |
| 64 | for child in ElementTree.fromstring(xml_string): |
| 65 | # Iterate over each interfaces's properties to find |
| 66 | # matching property_name, and return its signature string |
| 67 | if child.tag == 'interface': |
| 68 | for i in child.iter(): |
| 69 | if ('name' in i.attrib) and \ |
| 70 | (i.attrib['name'] == property_name): |
| 71 | type_signature = i.attrib['type'] |
| 72 | return type_signature |
| 73 | |
| 74 | |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 75 | def get_method_signature(bus, service, object_path, interface, method): |
| 76 | obj = bus.get_object(service, object_path) |
| 77 | iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable') |
| 78 | xml_string = iface.Introspect() |
| 79 | arglist = [] |
| 80 | |
| 81 | root = ElementTree.fromstring(xml_string) |
| 82 | for dbus_intf in root.findall('interface'): |
| 83 | if (dbus_intf.get('name') == interface): |
| 84 | for dbus_method in dbus_intf.findall('method'): |
| 85 | if(dbus_method.get('name') == method): |
| 86 | for arg in dbus_method.findall('arg'): |
| 87 | arglist.append(arg.get('type')) |
| 88 | return arglist |
| 89 | |
| 90 | |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 91 | def split_struct_signature(signature): |
| 92 | struct_regex = r'(b|y|n|i|x|q|u|t|d|s|a\(.+?\)|\(.+?\))|a\{.+?\}+?' |
| 93 | struct_matches = re.findall(struct_regex, signature) |
| 94 | return struct_matches |
| 95 | |
| 96 | |
| 97 | def convert_type(signature, value): |
| 98 | # Basic Types |
| 99 | converted_value = None |
| 100 | converted_container = None |
| 101 | basic_types = {'b': bool, 'y': dbus.Byte, 'n': dbus.Int16, 'i': int, |
| 102 | 'x': long, 'q': dbus.UInt16, 'u': dbus.UInt32, |
| 103 | 't': dbus.UInt64, 'd': float, 's': str} |
| 104 | array_matches = re.match(r'a\((\S+)\)', signature) |
| 105 | struct_matches = re.match(r'\((\S+)\)', signature) |
| 106 | dictionary_matches = re.match(r'a{(\S+)}', signature) |
| 107 | if signature in basic_types: |
| 108 | converted_value = basic_types[signature](value) |
| 109 | return converted_value |
| 110 | # Array |
| 111 | if array_matches: |
| 112 | element_type = array_matches.group(1) |
| 113 | converted_container = list() |
| 114 | # Test if value is a list |
| 115 | # to avoid iterating over each character in a string. |
| 116 | # Iterate over each item and convert type |
| 117 | if isinstance(value, list): |
| 118 | for i in value: |
| 119 | converted_element = convert_type(element_type, i) |
| 120 | converted_container.append(converted_element) |
| 121 | # Convert non-sequence to expected type, and append to list |
| 122 | else: |
| 123 | converted_element = convert_type(element_type, value) |
| 124 | converted_container.append(converted_element) |
| 125 | return converted_container |
| 126 | # Struct |
| 127 | if struct_matches: |
| 128 | element_types = struct_matches.group(1) |
| 129 | split_element_types = split_struct_signature(element_types) |
| 130 | converted_container = list() |
| 131 | # Test if value is a list |
| 132 | if isinstance(value, list): |
| 133 | for index, val in enumerate(value): |
| 134 | converted_element = convert_type(split_element_types[index], |
| 135 | value[index]) |
| 136 | converted_container.append(converted_element) |
| 137 | else: |
| 138 | converted_element = convert_type(element_types, value) |
| 139 | converted_container.append(converted_element) |
| 140 | return tuple(converted_container) |
| 141 | # Dictionary |
| 142 | if dictionary_matches: |
| 143 | element_types = dictionary_matches.group(1) |
| 144 | split_element_types = split_struct_signature(element_types) |
| 145 | converted_container = dict() |
| 146 | # Convert each element of dict |
| 147 | for key, val in value.iteritems(): |
| 148 | converted_key = convert_type(split_element_types[0], key) |
| 149 | converted_val = convert_type(split_element_types[1], val) |
| 150 | converted_container[converted_key] = converted_val |
| 151 | return converted_container |
| 152 | |
| 153 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 154 | class UserInGroup: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 155 | ''' Authorization plugin callback that checks that the user is logged in |
| 156 | and a member of a group. ''' |
| 157 | def __init__(self, group): |
| 158 | self.group = group |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 159 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 160 | def __call__(self, session, *a, **kw): |
| 161 | valid_user(session, *a, **kw) |
| 162 | res = False |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 163 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 164 | try: |
| 165 | res = session['user'] in grp.getgrnam(self.group)[3] |
| 166 | except KeyError: |
| 167 | pass |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 168 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 169 | if not res: |
| 170 | abort(403, 'Insufficient access') |
| 171 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 172 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 173 | class RouteHandler(object): |
Brad Bishop | 6d19060 | 2016-04-15 13:09:39 -0400 | [diff] [blame] | 174 | _require_auth = obmc.utils.misc.makelist(valid_user) |
Brad Bishop | d0c404a | 2017-02-21 09:23:25 -0500 | [diff] [blame] | 175 | _enable_cors = True |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 176 | |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 177 | def __init__(self, app, bus, verbs, rules, content_type=''): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 178 | self.app = app |
| 179 | self.bus = bus |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame] | 180 | self.mapper = obmc.mapper.Mapper(bus) |
Brad Bishop | 6d19060 | 2016-04-15 13:09:39 -0400 | [diff] [blame] | 181 | self._verbs = obmc.utils.misc.makelist(verbs) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 182 | self._rules = rules |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 183 | self._content_type = content_type |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 184 | |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 185 | if 'GET' in self._verbs: |
| 186 | self._verbs = list(set(self._verbs + ['HEAD'])) |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 187 | if 'OPTIONS' not in self._verbs: |
| 188 | self._verbs.append('OPTIONS') |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 189 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 190 | def _setup(self, **kw): |
| 191 | request.route_data = {} |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 192 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 193 | if request.method in self._verbs: |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 194 | if request.method != 'OPTIONS': |
| 195 | return self.setup(**kw) |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 196 | |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 197 | # Javascript implementations will not send credentials |
| 198 | # with an OPTIONS request. Don't help malicious clients |
| 199 | # by checking the path here and returning a 404 if the |
| 200 | # path doesn't exist. |
| 201 | return None |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 202 | |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 203 | # Return 405 |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 204 | raise HTTPError( |
| 205 | 405, "Method not allowed.", Allow=','.join(self._verbs)) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 206 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 207 | def __call__(self, **kw): |
| 208 | return getattr(self, 'do_' + request.method.lower())(**kw) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 209 | |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 210 | def do_head(self, **kw): |
| 211 | return self.do_get(**kw) |
| 212 | |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 213 | def do_options(self, **kw): |
| 214 | for v in self._verbs: |
| 215 | response.set_header( |
| 216 | 'Allow', |
| 217 | ','.join(self._verbs)) |
| 218 | return None |
| 219 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 220 | def install(self): |
| 221 | self.app.route( |
| 222 | self._rules, callback=self, |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 223 | method=['OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE']) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 224 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 225 | @staticmethod |
| 226 | def try_mapper_call(f, callback=None, **kw): |
| 227 | try: |
| 228 | return f(**kw) |
| 229 | except dbus.exceptions.DBusException, e: |
Brad Bishop | fce7756 | 2016-11-28 15:44:18 -0500 | [diff] [blame] | 230 | if e.get_dbus_name() == \ |
| 231 | 'org.freedesktop.DBus.Error.ObjectPathInUse': |
| 232 | abort(503, str(e)) |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame] | 233 | if e.get_dbus_name() != obmc.mapper.MAPPER_NOT_FOUND: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 234 | raise |
| 235 | if callback is None: |
| 236 | def callback(e, **kw): |
| 237 | abort(404, str(e)) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 238 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 239 | callback(e, **kw) |
| 240 | |
| 241 | @staticmethod |
| 242 | def try_properties_interface(f, *a): |
| 243 | try: |
| 244 | return f(*a) |
| 245 | except dbus.exceptions.DBusException, e: |
Adriana Kobylak | f92cf4d | 2017-12-13 11:46:50 -0600 | [diff] [blame] | 246 | if DBUS_UNKNOWN_INTERFACE in e.get_dbus_name(): |
Brad Bishop | f4e7498 | 2016-04-01 14:53:05 -0400 | [diff] [blame] | 247 | # interface doesn't have any properties |
| 248 | return None |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 249 | if DBUS_UNKNOWN_METHOD == e.get_dbus_name(): |
| 250 | # properties interface not implemented at all |
| 251 | return None |
| 252 | raise |
| 253 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 254 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 255 | class DirectoryHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 256 | verbs = 'GET' |
| 257 | rules = '<path:path>/' |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 258 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 259 | def __init__(self, app, bus): |
| 260 | super(DirectoryHandler, self).__init__( |
Brad Bishop | c431e1a | 2017-07-10 16:44:51 -0400 | [diff] [blame] | 261 | app, bus, self.verbs, self.rules) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 262 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 263 | def find(self, path='/'): |
| 264 | return self.try_mapper_call( |
| 265 | self.mapper.get_subtree_paths, path=path, depth=1) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 266 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 267 | def setup(self, path='/'): |
| 268 | request.route_data['map'] = self.find(path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 269 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 270 | def do_get(self, path='/'): |
| 271 | return request.route_data['map'] |
| 272 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 273 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 274 | class ListNamesHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 275 | verbs = 'GET' |
| 276 | rules = ['/list', '<path:path>/list'] |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 277 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 278 | def __init__(self, app, bus): |
| 279 | super(ListNamesHandler, self).__init__( |
Brad Bishop | c431e1a | 2017-07-10 16:44:51 -0400 | [diff] [blame] | 280 | app, bus, self.verbs, self.rules) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 281 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 282 | def find(self, path='/'): |
| 283 | return self.try_mapper_call( |
| 284 | self.mapper.get_subtree, path=path).keys() |
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 | def setup(self, path='/'): |
| 287 | request.route_data['map'] = self.find(path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 288 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 289 | def do_get(self, path='/'): |
| 290 | return request.route_data['map'] |
| 291 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 292 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 293 | class ListHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 294 | verbs = 'GET' |
| 295 | rules = ['/enumerate', '<path:path>/enumerate'] |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 296 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 297 | def __init__(self, app, bus): |
| 298 | super(ListHandler, self).__init__( |
Brad Bishop | c431e1a | 2017-07-10 16:44:51 -0400 | [diff] [blame] | 299 | app, bus, self.verbs, self.rules) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 300 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 301 | def find(self, path='/'): |
| 302 | return self.try_mapper_call( |
| 303 | self.mapper.get_subtree, path=path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 304 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 305 | def setup(self, path='/'): |
| 306 | request.route_data['map'] = self.find(path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 307 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 308 | def do_get(self, path='/'): |
Brad Bishop | 71527b4 | 2016-04-01 14:51:14 -0400 | [diff] [blame] | 309 | return {x: y for x, y in self.mapper.enumerate_subtree( |
| 310 | path, |
| 311 | mapper_data=request.route_data['map']).dataitems()} |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 312 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 313 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 314 | class MethodHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 315 | verbs = 'POST' |
| 316 | rules = '<path:path>/action/<method>' |
| 317 | request_type = list |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 318 | content_type = 'application/json' |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 319 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 320 | def __init__(self, app, bus): |
| 321 | super(MethodHandler, self).__init__( |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 322 | app, bus, self.verbs, self.rules, self.content_type) |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 323 | self.service = '' |
| 324 | self.interface = '' |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 325 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 326 | def find(self, path, method): |
Saqib Khan | 3a00b1f | 2017-11-04 15:56:21 -0500 | [diff] [blame] | 327 | method_list = [] |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 328 | busses = self.try_mapper_call( |
| 329 | self.mapper.get_object, path=path) |
| 330 | for items in busses.iteritems(): |
| 331 | m = self.find_method_on_bus(path, method, *items) |
| 332 | if m: |
Saqib Khan | 3a00b1f | 2017-11-04 15:56:21 -0500 | [diff] [blame] | 333 | method_list.append(m) |
Nagaraju Goruganti | 765c2c8 | 2017-11-13 06:17:13 -0600 | [diff] [blame] | 334 | if method_list: |
| 335 | return method_list |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 336 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 337 | abort(404, _4034_msg % ('method', 'found', method)) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 338 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 339 | def setup(self, path, method): |
Saqib Khan | 3a00b1f | 2017-11-04 15:56:21 -0500 | [diff] [blame] | 340 | request.route_data['map'] = self.find(path, method) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 341 | |
Marri Devender Rao | bc0c673 | 2017-11-20 00:15:47 -0600 | [diff] [blame] | 342 | def do_post(self, path, method, retry=True): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 343 | try: |
Nagaraju Goruganti | 765c2c8 | 2017-11-13 06:17:13 -0600 | [diff] [blame] | 344 | args = [] |
| 345 | if request.parameter_list: |
| 346 | args = request.parameter_list |
| 347 | # To see if the return type is capable of being merged |
| 348 | if len(request.route_data['map']) > 1: |
| 349 | results = None |
| 350 | for item in request.route_data['map']: |
| 351 | tmp = item(*args) |
| 352 | if not results: |
| 353 | if tmp is not None: |
| 354 | results = type(tmp)() |
| 355 | if isinstance(results, dict): |
| 356 | results = results.update(tmp) |
| 357 | elif isinstance(results, list): |
| 358 | results = results + tmp |
| 359 | elif isinstance(results, type(None)): |
| 360 | results = None |
| 361 | else: |
| 362 | abort(501, 'Don\'t know how to merge method call ' |
| 363 | 'results of {}'.format(type(tmp))) |
| 364 | return results |
| 365 | # There is only one method |
| 366 | return request.route_data['map'][0](*args) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 367 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 368 | except dbus.exceptions.DBusException, e: |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 369 | paramlist = [] |
Marri Devender Rao | bc0c673 | 2017-11-20 00:15:47 -0600 | [diff] [blame] | 370 | if e.get_dbus_name() == DBUS_INVALID_ARGS and retry == True: |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 371 | |
| 372 | signature_list = get_method_signature(self.bus, self.service, |
| 373 | path, self.interface, |
| 374 | method) |
| 375 | if not signature_list: |
| 376 | abort(400, "Failed to get method signature: %s" % str(e)) |
| 377 | if len(signature_list) != len(request.parameter_list): |
| 378 | abort(400, "Invalid number of args") |
| 379 | converted_value = None |
| 380 | try: |
| 381 | for index, expected_type in enumerate(signature_list): |
| 382 | value = request.parameter_list[index] |
| 383 | converted_value = convert_type(expected_type, value) |
| 384 | paramlist.append(converted_value) |
| 385 | request.parameter_list = paramlist |
Marri Devender Rao | bc0c673 | 2017-11-20 00:15:47 -0600 | [diff] [blame] | 386 | self.do_post(path, method, False) |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 387 | return |
| 388 | except Exception as ex: |
Nagaraju Goruganti | ab404fa | 2017-12-14 10:24:40 -0600 | [diff] [blame^] | 389 | abort(400, "Bad Request/Invalid Args given") |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 390 | abort(400, str(e)) |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 391 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 392 | if e.get_dbus_name() == DBUS_TYPE_ERROR: |
| 393 | abort(400, str(e)) |
| 394 | raise |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 395 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 396 | @staticmethod |
| 397 | def find_method_in_interface(method, obj, interface, methods): |
| 398 | if methods is None: |
| 399 | return None |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 400 | |
Brad Bishop | 6d19060 | 2016-04-15 13:09:39 -0400 | [diff] [blame] | 401 | method = obmc.utils.misc.find_case_insensitive(method, methods.keys()) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 402 | if method is not None: |
| 403 | iface = dbus.Interface(obj, interface) |
| 404 | return iface.get_dbus_method(method) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 405 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 406 | def find_method_on_bus(self, path, method, bus, interfaces): |
| 407 | obj = self.bus.get_object(bus, path, introspect=False) |
| 408 | iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE) |
| 409 | data = iface.Introspect() |
| 410 | parser = IntrospectionNodeParser( |
| 411 | ElementTree.fromstring(data), |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame] | 412 | intf_match=obmc.utils.misc.ListMatch(interfaces)) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 413 | for x, y in parser.get_interfaces().iteritems(): |
| 414 | m = self.find_method_in_interface( |
| 415 | method, obj, x, y.get('method')) |
| 416 | if m: |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 417 | self.service = bus |
| 418 | self.interface = x |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 419 | return m |
| 420 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 421 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 422 | class PropertyHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 423 | verbs = ['PUT', 'GET'] |
| 424 | rules = '<path:path>/attr/<prop>' |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 425 | content_type = 'application/json' |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 426 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 427 | def __init__(self, app, bus): |
| 428 | super(PropertyHandler, self).__init__( |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 429 | app, bus, self.verbs, self.rules, self.content_type) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 430 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 431 | def find(self, path, prop): |
| 432 | self.app.instance_handler.setup(path) |
| 433 | obj = self.app.instance_handler.do_get(path) |
Brad Bishop | 56ad87f | 2017-02-21 23:33:29 -0500 | [diff] [blame] | 434 | real_name = obmc.utils.misc.find_case_insensitive( |
| 435 | prop, obj.keys()) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 436 | |
Brad Bishop | 56ad87f | 2017-02-21 23:33:29 -0500 | [diff] [blame] | 437 | if not real_name: |
| 438 | if request.method == 'PUT': |
| 439 | abort(403, _4034_msg % ('property', 'created', prop)) |
| 440 | else: |
| 441 | abort(404, _4034_msg % ('property', 'found', prop)) |
| 442 | return real_name, {path: obj} |
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 | def setup(self, path, prop): |
Brad Bishop | 56ad87f | 2017-02-21 23:33:29 -0500 | [diff] [blame] | 445 | name, obj = self.find(path, prop) |
| 446 | request.route_data['obj'] = obj |
| 447 | request.route_data['name'] = name |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 448 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 449 | def do_get(self, path, prop): |
Brad Bishop | 56ad87f | 2017-02-21 23:33:29 -0500 | [diff] [blame] | 450 | name = request.route_data['name'] |
| 451 | return request.route_data['obj'][path][name] |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 452 | |
Marri Devender Rao | bc0c673 | 2017-11-20 00:15:47 -0600 | [diff] [blame] | 453 | def do_put(self, path, prop, value=None, retry=True): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 454 | if value is None: |
| 455 | value = request.parameter_list |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 456 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 457 | prop, iface, properties_iface = self.get_host_interface( |
| 458 | path, prop, request.route_data['map'][path]) |
| 459 | try: |
| 460 | properties_iface.Set(iface, prop, value) |
| 461 | except ValueError, e: |
| 462 | abort(400, str(e)) |
| 463 | except dbus.exceptions.DBusException, e: |
Marri Devender Rao | bc0c673 | 2017-11-20 00:15:47 -0600 | [diff] [blame] | 464 | if e.get_dbus_name() == DBUS_INVALID_ARGS and retry == True: |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 465 | bus_name = properties_iface.bus_name |
| 466 | expected_type = get_type_signature_by_introspection(self.bus, |
| 467 | bus_name, |
| 468 | path, |
| 469 | prop) |
| 470 | if not expected_type: |
| 471 | abort(403, "Failed to get expected type: %s" % str(e)) |
| 472 | converted_value = None |
| 473 | try: |
| 474 | converted_value = convert_type(expected_type, value) |
Marri Devender Rao | bc0c673 | 2017-11-20 00:15:47 -0600 | [diff] [blame] | 475 | self.do_put(path, prop, converted_value, False) |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 476 | return |
| 477 | except Exception as ex: |
| 478 | abort(403, "Failed to convert %s to type %s" % |
| 479 | (value, expected_type)) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 480 | abort(403, str(e)) |
| 481 | raise |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 482 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 483 | def get_host_interface(self, path, prop, bus_info): |
| 484 | for bus, interfaces in bus_info.iteritems(): |
| 485 | obj = self.bus.get_object(bus, path, introspect=True) |
| 486 | properties_iface = dbus.Interface( |
| 487 | obj, dbus_interface=dbus.PROPERTIES_IFACE) |
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 | info = self.get_host_interface_on_bus( |
| 490 | path, prop, properties_iface, bus, interfaces) |
| 491 | if info is not None: |
| 492 | prop, iface = info |
| 493 | return prop, iface, properties_iface |
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 get_host_interface_on_bus(self, path, prop, iface, bus, interfaces): |
| 496 | for i in interfaces: |
| 497 | properties = self.try_properties_interface(iface.GetAll, i) |
Brad Bishop | 69cb6d1 | 2017-02-21 12:01:52 -0500 | [diff] [blame] | 498 | if not properties: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 499 | continue |
Leonel Gonzalez | 409f671 | 2017-05-24 09:51:55 -0500 | [diff] [blame] | 500 | match = obmc.utils.misc.find_case_insensitive( |
Brad Bishop | 8b0d3fa | 2016-11-28 15:41:47 -0500 | [diff] [blame] | 501 | prop, properties.keys()) |
Leonel Gonzalez | 409f671 | 2017-05-24 09:51:55 -0500 | [diff] [blame] | 502 | if match is None: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 503 | continue |
Leonel Gonzalez | 409f671 | 2017-05-24 09:51:55 -0500 | [diff] [blame] | 504 | prop = match |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 505 | return prop, i |
| 506 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 507 | |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 508 | class SchemaHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 509 | verbs = ['GET'] |
| 510 | rules = '<path:path>/schema' |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 511 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 512 | def __init__(self, app, bus): |
| 513 | super(SchemaHandler, self).__init__( |
Brad Bishop | 529029b | 2017-07-10 16:46:01 -0400 | [diff] [blame] | 514 | app, bus, self.verbs, self.rules) |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 515 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 516 | def find(self, path): |
| 517 | return self.try_mapper_call( |
| 518 | self.mapper.get_object, |
| 519 | path=path) |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 520 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 521 | def setup(self, path): |
| 522 | request.route_data['map'] = self.find(path) |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 523 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 524 | def do_get(self, path): |
| 525 | schema = {} |
| 526 | for x in request.route_data['map'].iterkeys(): |
| 527 | obj = self.bus.get_object(x, path, introspect=False) |
| 528 | iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE) |
| 529 | data = iface.Introspect() |
| 530 | parser = IntrospectionNodeParser( |
| 531 | ElementTree.fromstring(data)) |
| 532 | for x, y in parser.get_interfaces().iteritems(): |
| 533 | schema[x] = y |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 534 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 535 | return schema |
| 536 | |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 537 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 538 | class InstanceHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 539 | verbs = ['GET', 'PUT', 'DELETE'] |
| 540 | rules = '<path:path>' |
| 541 | request_type = dict |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 542 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 543 | def __init__(self, app, bus): |
| 544 | super(InstanceHandler, self).__init__( |
Brad Bishop | 529029b | 2017-07-10 16:46:01 -0400 | [diff] [blame] | 545 | app, bus, self.verbs, self.rules) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 546 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 547 | def find(self, path, callback=None): |
| 548 | return {path: self.try_mapper_call( |
| 549 | self.mapper.get_object, |
| 550 | callback, |
| 551 | path=path)} |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 552 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 553 | def setup(self, path): |
| 554 | callback = None |
| 555 | if request.method == 'PUT': |
| 556 | def callback(e, **kw): |
| 557 | abort(403, _4034_msg % ('resource', 'created', path)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 558 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 559 | if request.route_data.get('map') is None: |
| 560 | request.route_data['map'] = self.find(path, callback) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 561 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 562 | def do_get(self, path): |
Brad Bishop | 71527b4 | 2016-04-01 14:51:14 -0400 | [diff] [blame] | 563 | return self.mapper.enumerate_object( |
| 564 | path, |
| 565 | mapper_data=request.route_data['map']) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 566 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 567 | def do_put(self, path): |
| 568 | # make sure all properties exist in the request |
| 569 | obj = set(self.do_get(path).keys()) |
| 570 | req = set(request.parameter_list.keys()) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 571 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 572 | diff = list(obj.difference(req)) |
| 573 | if diff: |
| 574 | abort(403, _4034_msg % ( |
| 575 | 'resource', 'removed', '%s/attr/%s' % (path, diff[0]))) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 576 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 577 | diff = list(req.difference(obj)) |
| 578 | if diff: |
| 579 | abort(403, _4034_msg % ( |
| 580 | 'resource', 'created', '%s/attr/%s' % (path, diff[0]))) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 581 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 582 | for p, v in request.parameter_list.iteritems(): |
| 583 | self.app.property_handler.do_put( |
| 584 | path, p, v) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 585 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 586 | def do_delete(self, path): |
| 587 | for bus_info in request.route_data['map'][path].iteritems(): |
| 588 | if self.bus_missing_delete(path, *bus_info): |
| 589 | abort(403, _4034_msg % ('resource', 'removed', path)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 590 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 591 | for bus in request.route_data['map'][path].iterkeys(): |
| 592 | self.delete_on_bus(path, bus) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 593 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 594 | def bus_missing_delete(self, path, bus, interfaces): |
| 595 | return DELETE_IFACE not in interfaces |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 596 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 597 | def delete_on_bus(self, path, bus): |
| 598 | obj = self.bus.get_object(bus, path, introspect=False) |
| 599 | delete_iface = dbus.Interface( |
| 600 | obj, dbus_interface=DELETE_IFACE) |
| 601 | delete_iface.Delete() |
| 602 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 603 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 604 | class SessionHandler(MethodHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 605 | ''' Handles the /login and /logout routes, manages |
| 606 | server side session store and session cookies. ''' |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 607 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 608 | rules = ['/login', '/logout'] |
| 609 | login_str = "User '%s' logged %s" |
| 610 | bad_passwd_str = "Invalid username or password" |
| 611 | no_user_str = "No user logged in" |
| 612 | bad_json_str = "Expecting request format { 'data': " \ |
| 613 | "[<username>, <password>] }, got '%s'" |
| 614 | _require_auth = None |
| 615 | MAX_SESSIONS = 16 |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 616 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 617 | def __init__(self, app, bus): |
| 618 | super(SessionHandler, self).__init__( |
| 619 | app, bus) |
| 620 | self.hmac_key = os.urandom(128) |
| 621 | self.session_store = [] |
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 | @staticmethod |
| 624 | def authenticate(username, clear): |
| 625 | try: |
| 626 | encoded = spwd.getspnam(username)[1] |
| 627 | return encoded == crypt.crypt(clear, encoded) |
| 628 | except KeyError: |
| 629 | return False |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 630 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 631 | def invalidate_session(self, session): |
| 632 | try: |
| 633 | self.session_store.remove(session) |
| 634 | except ValueError: |
| 635 | pass |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 636 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 637 | def new_session(self): |
| 638 | sid = os.urandom(32) |
| 639 | if self.MAX_SESSIONS <= len(self.session_store): |
| 640 | self.session_store.pop() |
| 641 | self.session_store.insert(0, {'sid': sid}) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 642 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 643 | return self.session_store[0] |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 644 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 645 | def get_session(self, sid): |
| 646 | sids = [x['sid'] for x in self.session_store] |
| 647 | try: |
| 648 | return self.session_store[sids.index(sid)] |
| 649 | except ValueError: |
| 650 | return None |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 651 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 652 | def get_session_from_cookie(self): |
| 653 | return self.get_session( |
| 654 | request.get_cookie( |
| 655 | 'sid', secret=self.hmac_key)) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 656 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 657 | def do_post(self, **kw): |
| 658 | if request.path == '/login': |
| 659 | return self.do_login(**kw) |
| 660 | else: |
| 661 | return self.do_logout(**kw) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 662 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 663 | def do_logout(self, **kw): |
| 664 | session = self.get_session_from_cookie() |
| 665 | if session is not None: |
| 666 | user = session['user'] |
| 667 | self.invalidate_session(session) |
| 668 | response.delete_cookie('sid') |
| 669 | return self.login_str % (user, 'out') |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 670 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 671 | return self.no_user_str |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 672 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 673 | def do_login(self, **kw): |
| 674 | session = self.get_session_from_cookie() |
| 675 | if session is not None: |
| 676 | return self.login_str % (session['user'], 'in') |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 677 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 678 | if len(request.parameter_list) != 2: |
| 679 | abort(400, self.bad_json_str % (request.json)) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 680 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 681 | if not self.authenticate(*request.parameter_list): |
Brad Bishop | dc3fbfa | 2016-09-08 09:51:38 -0400 | [diff] [blame] | 682 | abort(401, self.bad_passwd_str) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 683 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 684 | user = request.parameter_list[0] |
| 685 | session = self.new_session() |
| 686 | session['user'] = user |
| 687 | response.set_cookie( |
| 688 | 'sid', session['sid'], secret=self.hmac_key, |
| 689 | secure=True, |
| 690 | httponly=True) |
| 691 | return self.login_str % (user, 'in') |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 692 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 693 | def find(self, **kw): |
| 694 | pass |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 695 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 696 | def setup(self, **kw): |
| 697 | pass |
| 698 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 699 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 700 | class ImageUploadUtils: |
| 701 | ''' Provides common utils for image upload. ''' |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 702 | |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 703 | file_loc = '/tmp/images' |
| 704 | file_prefix = 'img' |
| 705 | file_suffix = '' |
| 706 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 707 | @classmethod |
| 708 | def do_upload(cls, filename=''): |
| 709 | if not os.path.exists(cls.file_loc): |
Gunnar Mills | fb51579 | 2017-11-09 15:52:17 -0600 | [diff] [blame] | 710 | abort(500, "Error Directory not found") |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 711 | if not filename: |
| 712 | handle, filename = tempfile.mkstemp(cls.file_suffix, |
| 713 | cls.file_prefix, cls.file_loc) |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 714 | else: |
| 715 | filename = os.path.join(cls.file_loc, filename) |
Gunnar Mills | b66b18c | 2017-08-21 16:17:21 -0500 | [diff] [blame] | 716 | handle = os.open(filename, os.O_WRONLY | os.O_CREAT) |
Leonel Gonzalez | 0b62edf | 2017-06-08 15:10:03 -0500 | [diff] [blame] | 717 | try: |
| 718 | file_contents = request.body.read() |
| 719 | request.body.close() |
Gunnar Mills | b66b18c | 2017-08-21 16:17:21 -0500 | [diff] [blame] | 720 | os.write(handle, file_contents) |
Leonel Gonzalez | 0b62edf | 2017-06-08 15:10:03 -0500 | [diff] [blame] | 721 | except (IOError, ValueError), e: |
| 722 | abort(400, str(e)) |
| 723 | except: |
| 724 | abort(400, "Unexpected Error") |
Gunnar Mills | b66b18c | 2017-08-21 16:17:21 -0500 | [diff] [blame] | 725 | finally: |
| 726 | os.close(handle) |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 727 | |
| 728 | |
| 729 | class ImagePostHandler(RouteHandler): |
| 730 | ''' Handles the /upload/image route. ''' |
| 731 | |
| 732 | verbs = ['POST'] |
| 733 | rules = ['/upload/image'] |
| 734 | content_type = 'application/octet-stream' |
| 735 | |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 736 | def __init__(self, app, bus): |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 737 | super(ImagePostHandler, self).__init__( |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 738 | app, bus, self.verbs, self.rules, self.content_type) |
| 739 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 740 | def do_post(self, filename=''): |
| 741 | ImageUploadUtils.do_upload() |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 742 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 743 | def find(self, **kw): |
| 744 | pass |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 745 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 746 | def setup(self, **kw): |
| 747 | pass |
| 748 | |
| 749 | |
Deepak Kodihalli | 639b502 | 2017-10-13 06:40:26 -0500 | [diff] [blame] | 750 | class EventNotifier: |
| 751 | keyNames = {} |
| 752 | keyNames['event'] = 'event' |
| 753 | keyNames['path'] = 'path' |
| 754 | keyNames['intfMap'] = 'interfaces' |
| 755 | keyNames['propMap'] = 'properties' |
| 756 | keyNames['intf'] = 'interface' |
| 757 | |
| 758 | def __init__(self, wsock, filters): |
| 759 | self.wsock = wsock |
| 760 | self.paths = filters.get("paths", []) |
| 761 | self.interfaces = filters.get("interfaces", []) |
| 762 | if not self.paths: |
| 763 | self.paths.append(None) |
| 764 | bus = dbus.SystemBus() |
| 765 | # Add a signal receiver for every path the client is interested in |
| 766 | for path in self.paths: |
| 767 | bus.add_signal_receiver( |
| 768 | self.interfaces_added_handler, |
| 769 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 770 | signal_name='InterfacesAdded', |
| 771 | path=path) |
| 772 | bus.add_signal_receiver( |
| 773 | self.properties_changed_handler, |
| 774 | dbus_interface=dbus.PROPERTIES_IFACE, |
| 775 | signal_name='PropertiesChanged', |
| 776 | path=path, |
| 777 | path_keyword='path') |
| 778 | loop = gobject.MainLoop() |
| 779 | # gobject's mainloop.run() will block the entire process, so the gevent |
| 780 | # scheduler and hence greenlets won't execute. The while-loop below |
| 781 | # works around this limitation by using gevent's sleep, instead of |
| 782 | # calling loop.run() |
| 783 | gcontext = loop.get_context() |
| 784 | while loop is not None: |
| 785 | try: |
| 786 | if gcontext.pending(): |
| 787 | gcontext.iteration() |
| 788 | else: |
| 789 | # gevent.sleep puts only the current greenlet to sleep, |
| 790 | # not the entire process. |
| 791 | gevent.sleep(5) |
| 792 | except WebSocketError: |
| 793 | break |
| 794 | |
| 795 | def interfaces_added_handler(self, path, iprops, **kw): |
| 796 | ''' If the client is interested in these changes, respond to the |
| 797 | client. This handles d-bus interface additions.''' |
| 798 | if (not self.interfaces) or \ |
| 799 | (not set(iprops).isdisjoint(self.interfaces)): |
| 800 | response = {} |
| 801 | response[self.keyNames['event']] = "InterfacesAdded" |
| 802 | response[self.keyNames['path']] = path |
| 803 | response[self.keyNames['intfMap']] = iprops |
| 804 | try: |
| 805 | self.wsock.send(json.dumps(response)) |
| 806 | except WebSocketError: |
| 807 | return |
| 808 | |
| 809 | def properties_changed_handler(self, interface, new, old, **kw): |
| 810 | ''' If the client is interested in these changes, respond to the |
| 811 | client. This handles d-bus property changes. ''' |
| 812 | if (not self.interfaces) or (interface in self.interfaces): |
| 813 | path = str(kw['path']) |
| 814 | response = {} |
| 815 | response[self.keyNames['event']] = "PropertiesChanged" |
| 816 | response[self.keyNames['path']] = path |
| 817 | response[self.keyNames['intf']] = interface |
| 818 | response[self.keyNames['propMap']] = new |
| 819 | try: |
| 820 | self.wsock.send(json.dumps(response)) |
| 821 | except WebSocketError: |
| 822 | return |
| 823 | |
| 824 | |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 825 | class EventHandler(RouteHandler): |
| 826 | ''' Handles the /subscribe route, for clients to be able |
| 827 | to subscribe to BMC events. ''' |
| 828 | |
| 829 | verbs = ['GET'] |
| 830 | rules = ['/subscribe'] |
| 831 | |
| 832 | def __init__(self, app, bus): |
| 833 | super(EventHandler, self).__init__( |
| 834 | app, bus, self.verbs, self.rules) |
| 835 | |
| 836 | def find(self, **kw): |
| 837 | pass |
| 838 | |
| 839 | def setup(self, **kw): |
| 840 | pass |
| 841 | |
| 842 | def do_get(self): |
| 843 | wsock = request.environ.get('wsgi.websocket') |
| 844 | if not wsock: |
| 845 | abort(400, 'Expected WebSocket request.') |
Deepak Kodihalli | 639b502 | 2017-10-13 06:40:26 -0500 | [diff] [blame] | 846 | filters = wsock.receive() |
| 847 | filters = json.loads(filters) |
| 848 | notifier = EventNotifier(wsock, filters) |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 849 | |
| 850 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 851 | class ImagePutHandler(RouteHandler): |
| 852 | ''' Handles the /upload/image/<filename> route. ''' |
| 853 | |
| 854 | verbs = ['PUT'] |
| 855 | rules = ['/upload/image/<filename>'] |
| 856 | content_type = 'application/octet-stream' |
| 857 | |
| 858 | def __init__(self, app, bus): |
| 859 | super(ImagePutHandler, self).__init__( |
| 860 | app, bus, self.verbs, self.rules, self.content_type) |
| 861 | |
| 862 | def do_put(self, filename=''): |
| 863 | ImageUploadUtils.do_upload(filename) |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 864 | |
| 865 | def find(self, **kw): |
| 866 | pass |
| 867 | |
| 868 | def setup(self, **kw): |
| 869 | pass |
| 870 | |
| 871 | |
Jayanth Othayoth | 9bc9499 | 2017-06-29 06:30:40 -0500 | [diff] [blame] | 872 | class DownloadDumpHandler(RouteHandler): |
| 873 | ''' Handles the /download/dump route. ''' |
| 874 | |
| 875 | verbs = 'GET' |
| 876 | rules = ['/download/dump/<dumpid>'] |
| 877 | content_type = 'application/octet-stream' |
Jayanth Othayoth | 18c3a24 | 2017-08-02 08:16:11 -0500 | [diff] [blame] | 878 | dump_loc = '/var/lib/phosphor-debug-collector/dumps' |
Brad Bishop | 944cd04 | 2017-07-10 16:42:41 -0400 | [diff] [blame] | 879 | suppress_json_resp = True |
Jayanth Othayoth | 9bc9499 | 2017-06-29 06:30:40 -0500 | [diff] [blame] | 880 | |
| 881 | def __init__(self, app, bus): |
| 882 | super(DownloadDumpHandler, self).__init__( |
| 883 | app, bus, self.verbs, self.rules, self.content_type) |
| 884 | |
| 885 | def do_get(self, dumpid): |
| 886 | return self.do_download(dumpid) |
| 887 | |
| 888 | def find(self, **kw): |
| 889 | pass |
| 890 | |
| 891 | def setup(self, **kw): |
| 892 | pass |
| 893 | |
| 894 | def do_download(self, dumpid): |
| 895 | dump_loc = os.path.join(self.dump_loc, dumpid) |
| 896 | if not os.path.exists(dump_loc): |
| 897 | abort(404, "Path not found") |
| 898 | |
| 899 | files = os.listdir(dump_loc) |
| 900 | num_files = len(files) |
| 901 | if num_files == 0: |
| 902 | abort(404, "Dump not found") |
| 903 | |
| 904 | return static_file(os.path.basename(files[0]), root=dump_loc, |
| 905 | download=True, mimetype=self.content_type) |
| 906 | |
| 907 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 908 | class AuthorizationPlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 909 | ''' Invokes an optional list of authorization callbacks. ''' |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 910 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 911 | name = 'authorization' |
| 912 | api = 2 |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 913 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 914 | class Compose: |
| 915 | def __init__(self, validators, callback, session_mgr): |
| 916 | self.validators = validators |
| 917 | self.callback = callback |
| 918 | self.session_mgr = session_mgr |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 919 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 920 | def __call__(self, *a, **kw): |
| 921 | sid = request.get_cookie('sid', secret=self.session_mgr.hmac_key) |
| 922 | session = self.session_mgr.get_session(sid) |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 923 | if request.method != 'OPTIONS': |
| 924 | for x in self.validators: |
| 925 | x(session, *a, **kw) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 926 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 927 | return self.callback(*a, **kw) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 928 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 929 | def apply(self, callback, route): |
| 930 | undecorated = route.get_undecorated_callback() |
| 931 | if not isinstance(undecorated, RouteHandler): |
| 932 | return callback |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 933 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 934 | auth_types = getattr( |
| 935 | undecorated, '_require_auth', None) |
| 936 | if not auth_types: |
| 937 | return callback |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 938 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 939 | return self.Compose( |
| 940 | auth_types, callback, undecorated.app.session_handler) |
| 941 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 942 | |
Brad Bishop | d0c404a | 2017-02-21 09:23:25 -0500 | [diff] [blame] | 943 | class CorsPlugin(object): |
| 944 | ''' Add CORS headers. ''' |
| 945 | |
| 946 | name = 'cors' |
| 947 | api = 2 |
| 948 | |
| 949 | @staticmethod |
| 950 | def process_origin(): |
| 951 | origin = request.headers.get('Origin') |
| 952 | if origin: |
| 953 | response.add_header('Access-Control-Allow-Origin', origin) |
| 954 | response.add_header( |
| 955 | 'Access-Control-Allow-Credentials', 'true') |
| 956 | |
| 957 | @staticmethod |
| 958 | def process_method_and_headers(verbs): |
| 959 | method = request.headers.get('Access-Control-Request-Method') |
| 960 | headers = request.headers.get('Access-Control-Request-Headers') |
| 961 | if headers: |
| 962 | headers = [x.lower() for x in headers.split(',')] |
| 963 | |
| 964 | if method in verbs \ |
| 965 | and headers == ['content-type']: |
| 966 | response.add_header('Access-Control-Allow-Methods', method) |
| 967 | response.add_header( |
| 968 | 'Access-Control-Allow-Headers', 'Content-Type') |
| 969 | |
| 970 | def __init__(self, app): |
| 971 | app.install_error_callback(self.error_callback) |
| 972 | |
| 973 | def apply(self, callback, route): |
| 974 | undecorated = route.get_undecorated_callback() |
| 975 | if not isinstance(undecorated, RouteHandler): |
| 976 | return callback |
| 977 | |
| 978 | if not getattr(undecorated, '_enable_cors', None): |
| 979 | return callback |
| 980 | |
| 981 | def wrap(*a, **kw): |
| 982 | self.process_origin() |
| 983 | self.process_method_and_headers(undecorated._verbs) |
| 984 | return callback(*a, **kw) |
| 985 | |
| 986 | return wrap |
| 987 | |
| 988 | def error_callback(self, **kw): |
| 989 | self.process_origin() |
| 990 | |
| 991 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 992 | class JsonApiRequestPlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 993 | ''' Ensures request content satisfies the OpenBMC json api format. ''' |
| 994 | name = 'json_api_request' |
| 995 | api = 2 |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 996 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 997 | error_str = "Expecting request format { 'data': <value> }, got '%s'" |
| 998 | type_error_str = "Unsupported Content-Type: '%s'" |
| 999 | json_type = "application/json" |
| 1000 | request_methods = ['PUT', 'POST', 'PATCH'] |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1001 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1002 | @staticmethod |
| 1003 | def content_expected(): |
| 1004 | return request.method in JsonApiRequestPlugin.request_methods |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1005 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1006 | def validate_request(self): |
| 1007 | if request.content_length > 0 and \ |
| 1008 | request.content_type != self.json_type: |
| 1009 | abort(415, self.type_error_str % request.content_type) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1010 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1011 | try: |
| 1012 | request.parameter_list = request.json.get('data') |
| 1013 | except ValueError, e: |
| 1014 | abort(400, str(e)) |
| 1015 | except (AttributeError, KeyError, TypeError): |
| 1016 | abort(400, self.error_str % request.json) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1017 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1018 | def apply(self, callback, route): |
Deepak Kodihalli | fb6cd48 | 2017-04-10 07:27:09 -0500 | [diff] [blame] | 1019 | content_type = getattr( |
| 1020 | route.get_undecorated_callback(), '_content_type', None) |
| 1021 | if self.json_type != content_type: |
| 1022 | return callback |
| 1023 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1024 | verbs = getattr( |
| 1025 | route.get_undecorated_callback(), '_verbs', None) |
| 1026 | if verbs is None: |
| 1027 | return callback |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1028 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1029 | if not set(self.request_methods).intersection(verbs): |
| 1030 | return callback |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1031 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1032 | def wrap(*a, **kw): |
| 1033 | if self.content_expected(): |
| 1034 | self.validate_request() |
| 1035 | return callback(*a, **kw) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1036 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1037 | return wrap |
| 1038 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1039 | |
| 1040 | class JsonApiRequestTypePlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1041 | ''' Ensures request content type satisfies the OpenBMC json api format. ''' |
| 1042 | name = 'json_api_method_request' |
| 1043 | api = 2 |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1044 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1045 | error_str = "Expecting request format { 'data': %s }, got '%s'" |
Deepak Kodihalli | fb6cd48 | 2017-04-10 07:27:09 -0500 | [diff] [blame] | 1046 | json_type = "application/json" |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1047 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1048 | def apply(self, callback, route): |
Deepak Kodihalli | fb6cd48 | 2017-04-10 07:27:09 -0500 | [diff] [blame] | 1049 | content_type = getattr( |
| 1050 | route.get_undecorated_callback(), '_content_type', None) |
| 1051 | if self.json_type != content_type: |
| 1052 | return callback |
| 1053 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1054 | request_type = getattr( |
| 1055 | route.get_undecorated_callback(), 'request_type', None) |
| 1056 | if request_type is None: |
| 1057 | return callback |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1058 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1059 | def validate_request(): |
| 1060 | if not isinstance(request.parameter_list, request_type): |
| 1061 | abort(400, self.error_str % (str(request_type), request.json)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1062 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1063 | def wrap(*a, **kw): |
| 1064 | if JsonApiRequestPlugin.content_expected(): |
| 1065 | validate_request() |
| 1066 | return callback(*a, **kw) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1067 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1068 | return wrap |
| 1069 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1070 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1071 | class JsonErrorsPlugin(JSONPlugin): |
| 1072 | ''' Extend the Bottle JSONPlugin such that it also encodes error |
| 1073 | responses. ''' |
| 1074 | |
| 1075 | def __init__(self, app, **kw): |
| 1076 | super(JsonErrorsPlugin, self).__init__(**kw) |
| 1077 | self.json_opts = { |
| 1078 | x: y for x, y in kw.iteritems() |
| 1079 | if x in ['indent', 'sort_keys']} |
| 1080 | app.install_error_callback(self.error_callback) |
| 1081 | |
| 1082 | def error_callback(self, response_object, response_body, **kw): |
| 1083 | response_body['body'] = json.dumps(response_object, **self.json_opts) |
| 1084 | response.content_type = 'application/json' |
| 1085 | |
| 1086 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1087 | class JsonApiResponsePlugin(object): |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1088 | ''' Emits responses in the OpenBMC json api format. ''' |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1089 | name = 'json_api_response' |
| 1090 | api = 2 |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1091 | |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 1092 | @staticmethod |
| 1093 | def has_body(): |
| 1094 | return request.method not in ['OPTIONS'] |
| 1095 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1096 | def __init__(self, app): |
| 1097 | app.install_error_callback(self.error_callback) |
| 1098 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1099 | def apply(self, callback, route): |
Brad Bishop | 944cd04 | 2017-07-10 16:42:41 -0400 | [diff] [blame] | 1100 | skip = getattr( |
| 1101 | route.get_undecorated_callback(), 'suppress_json_resp', None) |
| 1102 | if skip: |
Jayanth Othayoth | 1444fd8 | 2017-06-29 05:45:07 -0500 | [diff] [blame] | 1103 | return callback |
| 1104 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1105 | def wrap(*a, **kw): |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 1106 | data = callback(*a, **kw) |
| 1107 | if self.has_body(): |
| 1108 | resp = {'data': data} |
| 1109 | resp['status'] = 'ok' |
| 1110 | resp['message'] = response.status_line |
| 1111 | return resp |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1112 | return wrap |
| 1113 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1114 | def error_callback(self, error, response_object, **kw): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1115 | response_object['message'] = error.status_line |
Brad Bishop | 9c2531e | 2017-03-07 10:22:40 -0500 | [diff] [blame] | 1116 | response_object['status'] = 'error' |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1117 | response_object.setdefault('data', {})['description'] = str(error.body) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1118 | if error.status_code == 500: |
| 1119 | response_object['data']['exception'] = repr(error.exception) |
| 1120 | response_object['data']['traceback'] = error.traceback.splitlines() |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1121 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1122 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1123 | class JsonpPlugin(object): |
Brad Bishop | 80fe37a | 2016-03-29 10:54:54 -0400 | [diff] [blame] | 1124 | ''' Json javascript wrapper. ''' |
| 1125 | name = 'jsonp' |
| 1126 | api = 2 |
| 1127 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1128 | def __init__(self, app, **kw): |
| 1129 | app.install_error_callback(self.error_callback) |
Brad Bishop | 80fe37a | 2016-03-29 10:54:54 -0400 | [diff] [blame] | 1130 | |
| 1131 | @staticmethod |
| 1132 | def to_jsonp(json): |
| 1133 | jwrapper = request.query.callback or None |
| 1134 | if(jwrapper): |
| 1135 | response.set_header('Content-Type', 'application/javascript') |
| 1136 | json = jwrapper + '(' + json + ');' |
| 1137 | return json |
| 1138 | |
| 1139 | def apply(self, callback, route): |
| 1140 | def wrap(*a, **kw): |
| 1141 | return self.to_jsonp(callback(*a, **kw)) |
| 1142 | return wrap |
| 1143 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1144 | def error_callback(self, response_body, **kw): |
| 1145 | response_body['body'] = self.to_jsonp(response_body['body']) |
Brad Bishop | 80fe37a | 2016-03-29 10:54:54 -0400 | [diff] [blame] | 1146 | |
| 1147 | |
Deepak Kodihalli | 461367a | 2017-04-10 07:11:38 -0500 | [diff] [blame] | 1148 | class ContentCheckerPlugin(object): |
| 1149 | ''' Ensures that a route is associated with the expected content-type |
| 1150 | header. ''' |
| 1151 | name = 'content_checker' |
| 1152 | api = 2 |
| 1153 | |
| 1154 | class Checker: |
| 1155 | def __init__(self, type, callback): |
| 1156 | self.expected_type = type |
| 1157 | self.callback = callback |
| 1158 | self.error_str = "Expecting content type '%s', got '%s'" |
| 1159 | |
| 1160 | def __call__(self, *a, **kw): |
Deepak Kodihalli | db1a21e | 2017-04-27 06:30:11 -0500 | [diff] [blame] | 1161 | if request.method in ['PUT', 'POST', 'PATCH'] and \ |
| 1162 | self.expected_type and \ |
Deepak Kodihalli | 461367a | 2017-04-10 07:11:38 -0500 | [diff] [blame] | 1163 | self.expected_type != request.content_type: |
| 1164 | abort(415, self.error_str % (self.expected_type, |
| 1165 | request.content_type)) |
| 1166 | |
| 1167 | return self.callback(*a, **kw) |
| 1168 | |
| 1169 | def apply(self, callback, route): |
| 1170 | content_type = getattr( |
| 1171 | route.get_undecorated_callback(), '_content_type', None) |
| 1172 | |
| 1173 | return self.Checker(content_type, callback) |
| 1174 | |
| 1175 | |
Brad Bishop | 2c6fc76 | 2016-08-29 15:53:25 -0400 | [diff] [blame] | 1176 | class App(Bottle): |
Deepak Kodihalli | 0fe213f | 2017-10-11 00:08:48 -0500 | [diff] [blame] | 1177 | def __init__(self, **kw): |
Brad Bishop | 2c6fc76 | 2016-08-29 15:53:25 -0400 | [diff] [blame] | 1178 | super(App, self).__init__(autojson=False) |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 1179 | |
| 1180 | self.have_wsock = kw.get('have_wsock', False) |
| 1181 | |
Brad Bishop | 2ddfa00 | 2016-08-29 15:11:55 -0400 | [diff] [blame] | 1182 | self.bus = dbus.SystemBus() |
| 1183 | self.mapper = obmc.mapper.Mapper(self.bus) |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1184 | self.error_callbacks = [] |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1185 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1186 | self.install_hooks() |
| 1187 | self.install_plugins() |
| 1188 | self.create_handlers() |
| 1189 | self.install_handlers() |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1190 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1191 | def install_plugins(self): |
| 1192 | # install json api plugins |
| 1193 | json_kw = {'indent': 2, 'sort_keys': True} |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1194 | self.install(AuthorizationPlugin()) |
Brad Bishop | d0c404a | 2017-02-21 09:23:25 -0500 | [diff] [blame] | 1195 | self.install(CorsPlugin(self)) |
Deepak Kodihalli | 461367a | 2017-04-10 07:11:38 -0500 | [diff] [blame] | 1196 | self.install(ContentCheckerPlugin()) |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1197 | self.install(JsonpPlugin(self, **json_kw)) |
| 1198 | self.install(JsonErrorsPlugin(self, **json_kw)) |
| 1199 | self.install(JsonApiResponsePlugin(self)) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1200 | self.install(JsonApiRequestPlugin()) |
| 1201 | self.install(JsonApiRequestTypePlugin()) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1202 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1203 | def install_hooks(self): |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1204 | self.error_handler_type = type(self.default_error_handler) |
| 1205 | self.original_error_handler = self.default_error_handler |
| 1206 | self.default_error_handler = self.error_handler_type( |
| 1207 | self.custom_error_handler, self, Bottle) |
| 1208 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1209 | self.real_router_match = self.router.match |
| 1210 | self.router.match = self.custom_router_match |
| 1211 | self.add_hook('before_request', self.strip_extra_slashes) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1212 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1213 | def create_handlers(self): |
| 1214 | # create route handlers |
| 1215 | self.session_handler = SessionHandler(self, self.bus) |
| 1216 | self.directory_handler = DirectoryHandler(self, self.bus) |
| 1217 | self.list_names_handler = ListNamesHandler(self, self.bus) |
| 1218 | self.list_handler = ListHandler(self, self.bus) |
| 1219 | self.method_handler = MethodHandler(self, self.bus) |
| 1220 | self.property_handler = PropertyHandler(self, self.bus) |
| 1221 | self.schema_handler = SchemaHandler(self, self.bus) |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 1222 | self.image_upload_post_handler = ImagePostHandler(self, self.bus) |
| 1223 | self.image_upload_put_handler = ImagePutHandler(self, self.bus) |
Jayanth Othayoth | 9bc9499 | 2017-06-29 06:30:40 -0500 | [diff] [blame] | 1224 | self.download_dump_get_handler = DownloadDumpHandler(self, self.bus) |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 1225 | if self.have_wsock: |
| 1226 | self.event_handler = EventHandler(self, self.bus) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1227 | self.instance_handler = InstanceHandler(self, self.bus) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1228 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1229 | def install_handlers(self): |
| 1230 | self.session_handler.install() |
| 1231 | self.directory_handler.install() |
| 1232 | self.list_names_handler.install() |
| 1233 | self.list_handler.install() |
| 1234 | self.method_handler.install() |
| 1235 | self.property_handler.install() |
| 1236 | self.schema_handler.install() |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 1237 | self.image_upload_post_handler.install() |
| 1238 | self.image_upload_put_handler.install() |
Jayanth Othayoth | 9bc9499 | 2017-06-29 06:30:40 -0500 | [diff] [blame] | 1239 | self.download_dump_get_handler.install() |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 1240 | if self.have_wsock: |
| 1241 | self.event_handler.install() |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1242 | # this has to come last, since it matches everything |
| 1243 | self.instance_handler.install() |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1244 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1245 | def install_error_callback(self, callback): |
| 1246 | self.error_callbacks.insert(0, callback) |
| 1247 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1248 | def custom_router_match(self, environ): |
| 1249 | ''' The built-in Bottle algorithm for figuring out if a 404 or 405 is |
| 1250 | needed doesn't work for us since the instance rules match |
| 1251 | everything. This monkey-patch lets the route handler figure |
| 1252 | out which response is needed. This could be accomplished |
| 1253 | with a hook but that would require calling the router match |
| 1254 | function twice. |
| 1255 | ''' |
| 1256 | route, args = self.real_router_match(environ) |
| 1257 | if isinstance(route.callback, RouteHandler): |
| 1258 | route.callback._setup(**args) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1259 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1260 | return route, args |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1261 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1262 | def custom_error_handler(self, res, error): |
Gunnar Mills | f01d0ba | 2017-10-25 20:37:24 -0500 | [diff] [blame] | 1263 | ''' Allow plugins to modify error responses too via this custom |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1264 | error handler. ''' |
| 1265 | |
| 1266 | response_object = {} |
| 1267 | response_body = {} |
| 1268 | for x in self.error_callbacks: |
| 1269 | x(error=error, |
| 1270 | response_object=response_object, |
| 1271 | response_body=response_body) |
| 1272 | |
| 1273 | return response_body.get('body', "") |
| 1274 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1275 | @staticmethod |
| 1276 | def strip_extra_slashes(): |
| 1277 | path = request.environ['PATH_INFO'] |
| 1278 | trailing = ("", "/")[path[-1] == '/'] |
| 1279 | parts = filter(bool, path.split('/')) |
| 1280 | request.environ['PATH_INFO'] = '/' + '/'.join(parts) + trailing |