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