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