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