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