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 |
Alexander Filippov | d08a456 | 2018-03-20 12:02:23 +0300 | [diff] [blame] | 18 | import sys |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 19 | import dbus |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 20 | import dbus.exceptions |
| 21 | import json |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 22 | from xml.etree import ElementTree |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 23 | from bottle import Bottle, abort, request, response, JSONPlugin, HTTPError |
Jayanth Othayoth | 9bc9499 | 2017-06-29 06:30:40 -0500 | [diff] [blame] | 24 | from bottle import static_file |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame] | 25 | import obmc.utils.misc |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame] | 26 | from obmc.dbuslib.introspection import IntrospectionNodeParser |
| 27 | import obmc.mapper |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 28 | import spwd |
| 29 | import grp |
| 30 | import crypt |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 31 | import tempfile |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 32 | import re |
Matt Spinler | d41643e | 2018-02-02 13:51:38 -0600 | [diff] [blame] | 33 | import mimetypes |
Deepak Kodihalli | 639b502 | 2017-10-13 06:40:26 -0500 | [diff] [blame] | 34 | have_wsock = True |
| 35 | try: |
| 36 | from geventwebsocket import WebSocketError |
| 37 | except ImportError: |
| 38 | have_wsock = False |
| 39 | if have_wsock: |
| 40 | from dbus.mainloop.glib import DBusGMainLoop |
| 41 | DBusGMainLoop(set_as_default=True) |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 42 | # TODO: openbmc/openbmc#2994 remove python 2 support |
| 43 | try: # python 2 |
| 44 | import gobject |
| 45 | except ImportError: # python 3 |
| 46 | from gi.repository import GObject as gobject |
Deepak Kodihalli | 639b502 | 2017-10-13 06:40:26 -0500 | [diff] [blame] | 47 | import gevent |
Deepak Kodihalli | 5c518f6 | 2018-04-23 03:26:38 -0500 | [diff] [blame] | 48 | from gevent import socket |
| 49 | from gevent import Greenlet |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 50 | |
Adriana Kobylak | f92cf4d | 2017-12-13 11:46:50 -0600 | [diff] [blame] | 51 | DBUS_UNKNOWN_INTERFACE = 'org.freedesktop.DBus.Error.UnknownInterface' |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 52 | DBUS_UNKNOWN_METHOD = 'org.freedesktop.DBus.Error.UnknownMethod' |
Adriana Kobylak | a8b05d1 | 2018-08-23 10:44:07 -0500 | [diff] [blame] | 53 | DBUS_PROPERTY_READONLY = 'org.freedesktop.DBus.Error.PropertyReadOnly' |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 54 | DBUS_INVALID_ARGS = 'org.freedesktop.DBus.Error.InvalidArgs' |
Brad Bishop | d457892 | 2015-12-02 11:10:36 -0500 | [diff] [blame] | 55 | DBUS_TYPE_ERROR = 'org.freedesktop.DBus.Python.TypeError' |
Deepak Kodihalli | 6075bb4 | 2017-04-04 05:49:17 -0500 | [diff] [blame] | 56 | DELETE_IFACE = 'xyz.openbmc_project.Object.Delete' |
Adriana Kobylak | 5369389 | 2018-03-12 13:05:50 -0500 | [diff] [blame] | 57 | SOFTWARE_PATH = '/xyz/openbmc_project/software' |
Jayashankar Padath | bec10c2 | 2018-05-29 18:22:59 +0530 | [diff] [blame] | 58 | WEBSOCKET_TIMEOUT = 45 |
Brad Bishop | 9ee57c4 | 2015-11-03 14:59:29 -0500 | [diff] [blame] | 59 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 60 | _4034_msg = "The specified %s cannot be %s: '%s'" |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 61 | |
Matt Spinler | d41643e | 2018-02-02 13:51:38 -0600 | [diff] [blame] | 62 | www_base_path = '/usr/share/www/' |
| 63 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 64 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 65 | def valid_user(session, *a, **kw): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 66 | ''' Authorization plugin callback that checks |
| 67 | that the user is logged in. ''' |
| 68 | if session is None: |
Brad Bishop | dc3fbfa | 2016-09-08 09:51:38 -0400 | [diff] [blame] | 69 | abort(401, 'Login required') |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 70 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 71 | |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 72 | def get_type_signature_by_introspection(bus, service, object_path, |
| 73 | property_name): |
| 74 | obj = bus.get_object(service, object_path) |
| 75 | iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable') |
| 76 | xml_string = iface.Introspect() |
| 77 | for child in ElementTree.fromstring(xml_string): |
| 78 | # Iterate over each interfaces's properties to find |
| 79 | # matching property_name, and return its signature string |
| 80 | if child.tag == 'interface': |
| 81 | for i in child.iter(): |
| 82 | if ('name' in i.attrib) and \ |
| 83 | (i.attrib['name'] == property_name): |
| 84 | type_signature = i.attrib['type'] |
| 85 | return type_signature |
| 86 | |
| 87 | |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 88 | def get_method_signature(bus, service, object_path, interface, method): |
| 89 | obj = bus.get_object(service, object_path) |
| 90 | iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable') |
| 91 | xml_string = iface.Introspect() |
| 92 | arglist = [] |
| 93 | |
| 94 | root = ElementTree.fromstring(xml_string) |
| 95 | for dbus_intf in root.findall('interface'): |
| 96 | if (dbus_intf.get('name') == interface): |
| 97 | for dbus_method in dbus_intf.findall('method'): |
| 98 | if(dbus_method.get('name') == method): |
| 99 | for arg in dbus_method.findall('arg'): |
| 100 | arglist.append(arg.get('type')) |
| 101 | return arglist |
| 102 | |
| 103 | |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 104 | def split_struct_signature(signature): |
| 105 | struct_regex = r'(b|y|n|i|x|q|u|t|d|s|a\(.+?\)|\(.+?\))|a\{.+?\}+?' |
| 106 | struct_matches = re.findall(struct_regex, signature) |
| 107 | return struct_matches |
| 108 | |
| 109 | |
| 110 | def convert_type(signature, value): |
| 111 | # Basic Types |
| 112 | converted_value = None |
| 113 | converted_container = None |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 114 | # TODO: openbmc/openbmc#2994 remove python 2 support |
| 115 | try: # python 2 |
| 116 | basic_types = {'b': bool, 'y': dbus.Byte, 'n': dbus.Int16, 'i': int, |
| 117 | 'x': long, 'q': dbus.UInt16, 'u': dbus.UInt32, |
| 118 | 't': dbus.UInt64, 'd': float, 's': str} |
| 119 | except NameError: # python 3 |
| 120 | basic_types = {'b': bool, 'y': dbus.Byte, 'n': dbus.Int16, 'i': int, |
| 121 | 'x': int, 'q': dbus.UInt16, 'u': dbus.UInt32, |
| 122 | 't': dbus.UInt64, 'd': float, 's': str} |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 123 | array_matches = re.match(r'a\((\S+)\)', signature) |
| 124 | struct_matches = re.match(r'\((\S+)\)', signature) |
| 125 | dictionary_matches = re.match(r'a{(\S+)}', signature) |
| 126 | if signature in basic_types: |
| 127 | converted_value = basic_types[signature](value) |
| 128 | return converted_value |
| 129 | # Array |
| 130 | if array_matches: |
| 131 | element_type = array_matches.group(1) |
| 132 | converted_container = list() |
| 133 | # Test if value is a list |
| 134 | # to avoid iterating over each character in a string. |
| 135 | # Iterate over each item and convert type |
| 136 | if isinstance(value, list): |
| 137 | for i in value: |
| 138 | converted_element = convert_type(element_type, i) |
| 139 | converted_container.append(converted_element) |
| 140 | # Convert non-sequence to expected type, and append to list |
| 141 | else: |
| 142 | converted_element = convert_type(element_type, value) |
| 143 | converted_container.append(converted_element) |
| 144 | return converted_container |
| 145 | # Struct |
| 146 | if struct_matches: |
| 147 | element_types = struct_matches.group(1) |
| 148 | split_element_types = split_struct_signature(element_types) |
| 149 | converted_container = list() |
| 150 | # Test if value is a list |
| 151 | if isinstance(value, list): |
| 152 | for index, val in enumerate(value): |
| 153 | converted_element = convert_type(split_element_types[index], |
| 154 | value[index]) |
| 155 | converted_container.append(converted_element) |
| 156 | else: |
| 157 | converted_element = convert_type(element_types, value) |
| 158 | converted_container.append(converted_element) |
| 159 | return tuple(converted_container) |
| 160 | # Dictionary |
| 161 | if dictionary_matches: |
| 162 | element_types = dictionary_matches.group(1) |
| 163 | split_element_types = split_struct_signature(element_types) |
| 164 | converted_container = dict() |
| 165 | # Convert each element of dict |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 166 | for key, val in value.items(): |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 167 | converted_key = convert_type(split_element_types[0], key) |
| 168 | converted_val = convert_type(split_element_types[1], val) |
| 169 | converted_container[converted_key] = converted_val |
| 170 | return converted_container |
| 171 | |
| 172 | |
Jayashankar Padath | bec10c2 | 2018-05-29 18:22:59 +0530 | [diff] [blame] | 173 | def send_ws_ping(wsock, timeout) : |
| 174 | # Most webservers close websockets after 60 seconds of |
| 175 | # inactivity. Make sure to send a ping before that. |
| 176 | payload = "ping" |
| 177 | # the ping payload can be anything, the receiver has to just |
| 178 | # return the same back. |
| 179 | while True: |
| 180 | gevent.sleep(timeout) |
| 181 | try: |
| 182 | if wsock: |
| 183 | wsock.send_frame(payload, wsock.OPCODE_PING) |
| 184 | except Exception as e: |
| 185 | wsock.close() |
| 186 | return |
| 187 | |
| 188 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 189 | class UserInGroup: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 190 | ''' Authorization plugin callback that checks that the user is logged in |
| 191 | and a member of a group. ''' |
| 192 | def __init__(self, group): |
| 193 | self.group = group |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 194 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 195 | def __call__(self, session, *a, **kw): |
| 196 | valid_user(session, *a, **kw) |
| 197 | res = False |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 198 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 199 | try: |
| 200 | res = session['user'] in grp.getgrnam(self.group)[3] |
| 201 | except KeyError: |
| 202 | pass |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 203 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 204 | if not res: |
| 205 | abort(403, 'Insufficient access') |
| 206 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 207 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 208 | class RouteHandler(object): |
Brad Bishop | 6d19060 | 2016-04-15 13:09:39 -0400 | [diff] [blame] | 209 | _require_auth = obmc.utils.misc.makelist(valid_user) |
Brad Bishop | d0c404a | 2017-02-21 09:23:25 -0500 | [diff] [blame] | 210 | _enable_cors = True |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 211 | |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 212 | def __init__(self, app, bus, verbs, rules, content_type=''): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 213 | self.app = app |
| 214 | self.bus = bus |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame] | 215 | self.mapper = obmc.mapper.Mapper(bus) |
Brad Bishop | 6d19060 | 2016-04-15 13:09:39 -0400 | [diff] [blame] | 216 | self._verbs = obmc.utils.misc.makelist(verbs) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 217 | self._rules = rules |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 218 | self._content_type = content_type |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 219 | |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 220 | if 'GET' in self._verbs: |
| 221 | self._verbs = list(set(self._verbs + ['HEAD'])) |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 222 | if 'OPTIONS' not in self._verbs: |
| 223 | self._verbs.append('OPTIONS') |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 224 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 225 | def _setup(self, **kw): |
| 226 | request.route_data = {} |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 227 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 228 | if request.method in self._verbs: |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 229 | if request.method != 'OPTIONS': |
| 230 | return self.setup(**kw) |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 231 | |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 232 | # Javascript implementations will not send credentials |
| 233 | # with an OPTIONS request. Don't help malicious clients |
| 234 | # by checking the path here and returning a 404 if the |
| 235 | # path doesn't exist. |
| 236 | return None |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 237 | |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 238 | # Return 405 |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 239 | raise HTTPError( |
| 240 | 405, "Method not allowed.", Allow=','.join(self._verbs)) |
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 __call__(self, **kw): |
| 243 | return getattr(self, 'do_' + request.method.lower())(**kw) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 244 | |
Brad Bishop | 88c76a4 | 2017-02-21 00:02:02 -0500 | [diff] [blame] | 245 | def do_head(self, **kw): |
| 246 | return self.do_get(**kw) |
| 247 | |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 248 | def do_options(self, **kw): |
| 249 | for v in self._verbs: |
| 250 | response.set_header( |
| 251 | 'Allow', |
| 252 | ','.join(self._verbs)) |
| 253 | return None |
| 254 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 255 | def install(self): |
| 256 | self.app.route( |
| 257 | self._rules, callback=self, |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 258 | method=['OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE']) |
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 | @staticmethod |
| 261 | def try_mapper_call(f, callback=None, **kw): |
| 262 | try: |
| 263 | return f(**kw) |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 264 | except dbus.exceptions.DBusException as e: |
Brad Bishop | fce7756 | 2016-11-28 15:44:18 -0500 | [diff] [blame] | 265 | if e.get_dbus_name() == \ |
| 266 | 'org.freedesktop.DBus.Error.ObjectPathInUse': |
| 267 | abort(503, str(e)) |
Brad Bishop | b103d2d | 2016-03-04 16:19:14 -0500 | [diff] [blame] | 268 | if e.get_dbus_name() != obmc.mapper.MAPPER_NOT_FOUND: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 269 | raise |
| 270 | if callback is None: |
| 271 | def callback(e, **kw): |
| 272 | abort(404, str(e)) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 273 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 274 | callback(e, **kw) |
| 275 | |
| 276 | @staticmethod |
| 277 | def try_properties_interface(f, *a): |
| 278 | try: |
| 279 | return f(*a) |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 280 | except dbus.exceptions.DBusException as e: |
Adriana Kobylak | f92cf4d | 2017-12-13 11:46:50 -0600 | [diff] [blame] | 281 | if DBUS_UNKNOWN_INTERFACE in e.get_dbus_name(): |
Brad Bishop | f4e7498 | 2016-04-01 14:53:05 -0400 | [diff] [blame] | 282 | # interface doesn't have any properties |
| 283 | return None |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 284 | if DBUS_UNKNOWN_METHOD == e.get_dbus_name(): |
| 285 | # properties interface not implemented at all |
| 286 | return None |
| 287 | raise |
| 288 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 289 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 290 | class DirectoryHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 291 | verbs = 'GET' |
| 292 | rules = '<path:path>/' |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 293 | suppress_logging = True |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 294 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 295 | def __init__(self, app, bus): |
| 296 | super(DirectoryHandler, self).__init__( |
Brad Bishop | c431e1a | 2017-07-10 16:44:51 -0400 | [diff] [blame] | 297 | app, bus, self.verbs, self.rules) |
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 find(self, path='/'): |
| 300 | return self.try_mapper_call( |
| 301 | self.mapper.get_subtree_paths, path=path, depth=1) |
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 setup(self, path='/'): |
| 304 | request.route_data['map'] = self.find(path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 305 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 306 | def do_get(self, path='/'): |
| 307 | return request.route_data['map'] |
| 308 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 309 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 310 | class ListNamesHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 311 | verbs = 'GET' |
| 312 | rules = ['/list', '<path:path>/list'] |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 313 | suppress_logging = True |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 314 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 315 | def __init__(self, app, bus): |
| 316 | super(ListNamesHandler, self).__init__( |
Brad Bishop | c431e1a | 2017-07-10 16:44:51 -0400 | [diff] [blame] | 317 | app, bus, self.verbs, self.rules) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 318 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 319 | def find(self, path='/'): |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 320 | return list(self.try_mapper_call( |
| 321 | self.mapper.get_subtree, path=path).keys()) |
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 | def setup(self, path='/'): |
| 324 | request.route_data['map'] = self.find(path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 325 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 326 | def do_get(self, path='/'): |
| 327 | return request.route_data['map'] |
| 328 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 329 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 330 | class ListHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 331 | verbs = 'GET' |
| 332 | rules = ['/enumerate', '<path:path>/enumerate'] |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 333 | suppress_logging = True |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 334 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 335 | def __init__(self, app, bus): |
| 336 | super(ListHandler, self).__init__( |
Brad Bishop | c431e1a | 2017-07-10 16:44:51 -0400 | [diff] [blame] | 337 | app, bus, self.verbs, self.rules) |
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(self, path='/'): |
| 340 | return self.try_mapper_call( |
| 341 | self.mapper.get_subtree, path=path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 342 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 343 | def setup(self, path='/'): |
| 344 | request.route_data['map'] = self.find(path) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 345 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 346 | def do_get(self, path='/'): |
Brad Bishop | 71527b4 | 2016-04-01 14:51:14 -0400 | [diff] [blame] | 347 | return {x: y for x, y in self.mapper.enumerate_subtree( |
| 348 | path, |
| 349 | mapper_data=request.route_data['map']).dataitems()} |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 350 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 351 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 352 | class MethodHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 353 | verbs = 'POST' |
| 354 | rules = '<path:path>/action/<method>' |
| 355 | request_type = list |
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(MethodHandler, self).__init__( |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 360 | app, bus, self.verbs, self.rules, self.content_type) |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 361 | self.service = '' |
| 362 | self.interface = '' |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 363 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 364 | def find(self, path, method): |
Saqib Khan | 3a00b1f | 2017-11-04 15:56:21 -0500 | [diff] [blame] | 365 | method_list = [] |
Gunnar Mills | 313aadb | 2018-04-08 14:50:09 -0500 | [diff] [blame] | 366 | buses = self.try_mapper_call( |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 367 | self.mapper.get_object, path=path) |
Gunnar Mills | 313aadb | 2018-04-08 14:50:09 -0500 | [diff] [blame] | 368 | for items in buses.items(): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 369 | m = self.find_method_on_bus(path, method, *items) |
| 370 | if m: |
Saqib Khan | 3a00b1f | 2017-11-04 15:56:21 -0500 | [diff] [blame] | 371 | method_list.append(m) |
Nagaraju Goruganti | 765c2c8 | 2017-11-13 06:17:13 -0600 | [diff] [blame] | 372 | if method_list: |
| 373 | return method_list |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 374 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 375 | abort(404, _4034_msg % ('method', 'found', method)) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 376 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 377 | def setup(self, path, method): |
Saqib Khan | 3a00b1f | 2017-11-04 15:56:21 -0500 | [diff] [blame] | 378 | request.route_data['map'] = self.find(path, method) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 379 | |
Marri Devender Rao | bc0c673 | 2017-11-20 00:15:47 -0600 | [diff] [blame] | 380 | def do_post(self, path, method, retry=True): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 381 | try: |
Nagaraju Goruganti | 765c2c8 | 2017-11-13 06:17:13 -0600 | [diff] [blame] | 382 | args = [] |
| 383 | if request.parameter_list: |
| 384 | args = request.parameter_list |
| 385 | # To see if the return type is capable of being merged |
| 386 | if len(request.route_data['map']) > 1: |
| 387 | results = None |
| 388 | for item in request.route_data['map']: |
| 389 | tmp = item(*args) |
| 390 | if not results: |
| 391 | if tmp is not None: |
| 392 | results = type(tmp)() |
| 393 | if isinstance(results, dict): |
| 394 | results = results.update(tmp) |
| 395 | elif isinstance(results, list): |
| 396 | results = results + tmp |
| 397 | elif isinstance(results, type(None)): |
| 398 | results = None |
| 399 | else: |
| 400 | abort(501, 'Don\'t know how to merge method call ' |
| 401 | 'results of {}'.format(type(tmp))) |
| 402 | return results |
| 403 | # There is only one method |
| 404 | return request.route_data['map'][0](*args) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 405 | |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 406 | except dbus.exceptions.DBusException as e: |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 407 | paramlist = [] |
Brad Bishop | b7fca9b | 2018-01-23 12:16:50 -0500 | [diff] [blame] | 408 | if e.get_dbus_name() == DBUS_INVALID_ARGS and retry: |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 409 | |
| 410 | signature_list = get_method_signature(self.bus, self.service, |
| 411 | path, self.interface, |
| 412 | method) |
| 413 | if not signature_list: |
| 414 | abort(400, "Failed to get method signature: %s" % str(e)) |
| 415 | if len(signature_list) != len(request.parameter_list): |
| 416 | abort(400, "Invalid number of args") |
| 417 | converted_value = None |
| 418 | try: |
| 419 | for index, expected_type in enumerate(signature_list): |
| 420 | value = request.parameter_list[index] |
| 421 | converted_value = convert_type(expected_type, value) |
| 422 | paramlist.append(converted_value) |
| 423 | request.parameter_list = paramlist |
Marri Devender Rao | bc0c673 | 2017-11-20 00:15:47 -0600 | [diff] [blame] | 424 | self.do_post(path, method, False) |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 425 | return |
| 426 | except Exception as ex: |
Nagaraju Goruganti | ab404fa | 2017-12-14 10:24:40 -0600 | [diff] [blame] | 427 | abort(400, "Bad Request/Invalid Args given") |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 428 | abort(400, str(e)) |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 429 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 430 | if e.get_dbus_name() == DBUS_TYPE_ERROR: |
| 431 | abort(400, str(e)) |
| 432 | raise |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 433 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 434 | @staticmethod |
| 435 | def find_method_in_interface(method, obj, interface, methods): |
| 436 | if methods is None: |
| 437 | return None |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 438 | |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 439 | method = obmc.utils.misc.find_case_insensitive(method, list(methods.keys())) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 440 | if method is not None: |
| 441 | iface = dbus.Interface(obj, interface) |
| 442 | return iface.get_dbus_method(method) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 443 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 444 | def find_method_on_bus(self, path, method, bus, interfaces): |
| 445 | obj = self.bus.get_object(bus, path, introspect=False) |
| 446 | iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE) |
| 447 | data = iface.Introspect() |
| 448 | parser = IntrospectionNodeParser( |
| 449 | ElementTree.fromstring(data), |
Brad Bishop | aeb995d | 2018-04-04 22:28:42 -0400 | [diff] [blame] | 450 | intf_match=lambda x: x in interfaces) |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 451 | for x, y in parser.get_interfaces().items(): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 452 | m = self.find_method_in_interface( |
| 453 | method, obj, x, y.get('method')) |
| 454 | if m: |
Ratan Gupta | a6a8a4c | 2017-08-07 08:18:44 +0530 | [diff] [blame] | 455 | self.service = bus |
| 456 | self.interface = x |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 457 | return m |
| 458 | |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 459 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 460 | class PropertyHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 461 | verbs = ['PUT', 'GET'] |
| 462 | rules = '<path:path>/attr/<prop>' |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 463 | content_type = 'application/json' |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 464 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 465 | def __init__(self, app, bus): |
| 466 | super(PropertyHandler, self).__init__( |
Deepak Kodihalli | 83afbaf | 2017-04-10 06:37:19 -0500 | [diff] [blame] | 467 | app, bus, self.verbs, self.rules, self.content_type) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 468 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 469 | def find(self, path, prop): |
| 470 | self.app.instance_handler.setup(path) |
| 471 | obj = self.app.instance_handler.do_get(path) |
Brad Bishop | 56ad87f | 2017-02-21 23:33:29 -0500 | [diff] [blame] | 472 | real_name = obmc.utils.misc.find_case_insensitive( |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 473 | prop, list(obj.keys())) |
Brad Bishop | aa65f6e | 2015-10-27 16:28:51 -0400 | [diff] [blame] | 474 | |
Brad Bishop | 56ad87f | 2017-02-21 23:33:29 -0500 | [diff] [blame] | 475 | if not real_name: |
| 476 | if request.method == 'PUT': |
| 477 | abort(403, _4034_msg % ('property', 'created', prop)) |
| 478 | else: |
| 479 | abort(404, _4034_msg % ('property', 'found', prop)) |
| 480 | return real_name, {path: obj} |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 481 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 482 | def setup(self, path, prop): |
Brad Bishop | 56ad87f | 2017-02-21 23:33:29 -0500 | [diff] [blame] | 483 | name, obj = self.find(path, prop) |
| 484 | request.route_data['obj'] = obj |
| 485 | request.route_data['name'] = name |
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 do_get(self, path, prop): |
Brad Bishop | 56ad87f | 2017-02-21 23:33:29 -0500 | [diff] [blame] | 488 | name = request.route_data['name'] |
| 489 | return request.route_data['obj'][path][name] |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 490 | |
Marri Devender Rao | bc0c673 | 2017-11-20 00:15:47 -0600 | [diff] [blame] | 491 | def do_put(self, path, prop, value=None, retry=True): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 492 | if value is None: |
| 493 | value = request.parameter_list |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 494 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 495 | prop, iface, properties_iface = self.get_host_interface( |
| 496 | path, prop, request.route_data['map'][path]) |
| 497 | try: |
| 498 | properties_iface.Set(iface, prop, value) |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 499 | except ValueError as e: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 500 | abort(400, str(e)) |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 501 | except dbus.exceptions.DBusException as e: |
Adriana Kobylak | a8b05d1 | 2018-08-23 10:44:07 -0500 | [diff] [blame] | 502 | if e.get_dbus_name() == DBUS_PROPERTY_READONLY: |
| 503 | abort(403, str(e)) |
Brad Bishop | b7fca9b | 2018-01-23 12:16:50 -0500 | [diff] [blame] | 504 | if e.get_dbus_name() == DBUS_INVALID_ARGS and retry: |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 505 | bus_name = properties_iface.bus_name |
| 506 | expected_type = get_type_signature_by_introspection(self.bus, |
| 507 | bus_name, |
| 508 | path, |
| 509 | prop) |
| 510 | if not expected_type: |
| 511 | abort(403, "Failed to get expected type: %s" % str(e)) |
| 512 | converted_value = None |
| 513 | try: |
| 514 | converted_value = convert_type(expected_type, value) |
Leonel Gonzalez | 0bdef95 | 2017-04-18 08:17:49 -0500 | [diff] [blame] | 515 | except Exception as ex: |
| 516 | abort(403, "Failed to convert %s to type %s" % |
| 517 | (value, expected_type)) |
Lei YU | 1eea5c3 | 2018-07-12 15:32:37 +0800 | [diff] [blame] | 518 | try: |
| 519 | self.do_put(path, prop, converted_value, False) |
| 520 | return |
| 521 | except Exception as ex: |
| 522 | abort(403, str(ex)) |
| 523 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 524 | abort(403, str(e)) |
| 525 | raise |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 526 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 527 | def get_host_interface(self, path, prop, bus_info): |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 528 | for bus, interfaces in bus_info.items(): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 529 | obj = self.bus.get_object(bus, path, introspect=True) |
| 530 | properties_iface = dbus.Interface( |
| 531 | obj, dbus_interface=dbus.PROPERTIES_IFACE) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 532 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 533 | info = self.get_host_interface_on_bus( |
| 534 | path, prop, properties_iface, bus, interfaces) |
| 535 | if info is not None: |
| 536 | prop, iface = info |
| 537 | return prop, iface, properties_iface |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 538 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 539 | def get_host_interface_on_bus(self, path, prop, iface, bus, interfaces): |
| 540 | for i in interfaces: |
| 541 | properties = self.try_properties_interface(iface.GetAll, i) |
Brad Bishop | 69cb6d1 | 2017-02-21 12:01:52 -0500 | [diff] [blame] | 542 | if not properties: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 543 | continue |
Leonel Gonzalez | 409f671 | 2017-05-24 09:51:55 -0500 | [diff] [blame] | 544 | match = obmc.utils.misc.find_case_insensitive( |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 545 | prop, list(properties.keys())) |
Leonel Gonzalez | 409f671 | 2017-05-24 09:51:55 -0500 | [diff] [blame] | 546 | if match is None: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 547 | continue |
Leonel Gonzalez | 409f671 | 2017-05-24 09:51:55 -0500 | [diff] [blame] | 548 | prop = match |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 549 | return prop, i |
| 550 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 551 | |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 552 | class SchemaHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 553 | verbs = ['GET'] |
| 554 | rules = '<path:path>/schema' |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 555 | suppress_logging = True |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 556 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 557 | def __init__(self, app, bus): |
| 558 | super(SchemaHandler, self).__init__( |
Brad Bishop | 529029b | 2017-07-10 16:46:01 -0400 | [diff] [blame] | 559 | app, bus, self.verbs, self.rules) |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 560 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 561 | def find(self, path): |
| 562 | return self.try_mapper_call( |
| 563 | self.mapper.get_object, |
| 564 | path=path) |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 565 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 566 | def setup(self, path): |
| 567 | request.route_data['map'] = self.find(path) |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 568 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 569 | def do_get(self, path): |
| 570 | schema = {} |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 571 | for x in request.route_data['map'].keys(): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 572 | obj = self.bus.get_object(x, path, introspect=False) |
| 573 | iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE) |
| 574 | data = iface.Introspect() |
| 575 | parser = IntrospectionNodeParser( |
| 576 | ElementTree.fromstring(data)) |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 577 | for x, y in parser.get_interfaces().items(): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 578 | schema[x] = y |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 579 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 580 | return schema |
| 581 | |
Brad Bishop | 2503bd6 | 2015-12-16 17:56:12 -0500 | [diff] [blame] | 582 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 583 | class InstanceHandler(RouteHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 584 | verbs = ['GET', 'PUT', 'DELETE'] |
| 585 | rules = '<path:path>' |
| 586 | request_type = dict |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 587 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 588 | def __init__(self, app, bus): |
| 589 | super(InstanceHandler, self).__init__( |
Brad Bishop | 529029b | 2017-07-10 16:46:01 -0400 | [diff] [blame] | 590 | app, bus, self.verbs, self.rules) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 591 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 592 | def find(self, path, callback=None): |
| 593 | return {path: self.try_mapper_call( |
| 594 | self.mapper.get_object, |
| 595 | callback, |
| 596 | path=path)} |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 597 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 598 | def setup(self, path): |
| 599 | callback = None |
| 600 | if request.method == 'PUT': |
| 601 | def callback(e, **kw): |
| 602 | abort(403, _4034_msg % ('resource', 'created', path)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 603 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 604 | if request.route_data.get('map') is None: |
| 605 | request.route_data['map'] = self.find(path, callback) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 606 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 607 | def do_get(self, path): |
Brad Bishop | 71527b4 | 2016-04-01 14:51:14 -0400 | [diff] [blame] | 608 | return self.mapper.enumerate_object( |
| 609 | path, |
| 610 | mapper_data=request.route_data['map']) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 611 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 612 | def do_put(self, path): |
| 613 | # make sure all properties exist in the request |
| 614 | obj = set(self.do_get(path).keys()) |
| 615 | req = set(request.parameter_list.keys()) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 616 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 617 | diff = list(obj.difference(req)) |
| 618 | if diff: |
| 619 | abort(403, _4034_msg % ( |
| 620 | 'resource', 'removed', '%s/attr/%s' % (path, diff[0]))) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 621 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 622 | diff = list(req.difference(obj)) |
| 623 | if diff: |
| 624 | abort(403, _4034_msg % ( |
| 625 | 'resource', 'created', '%s/attr/%s' % (path, diff[0]))) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 626 | |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 627 | for p, v in request.parameter_list.items(): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 628 | self.app.property_handler.do_put( |
| 629 | path, p, v) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 630 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 631 | def do_delete(self, path): |
Matt Spinler | b1f6a2c | 2018-05-14 12:25:21 -0500 | [diff] [blame] | 632 | deleted = False |
| 633 | for bus, interfaces in request.route_data['map'][path].items(): |
| 634 | if self.bus_has_delete(interfaces): |
| 635 | self.delete_on_bus(path, bus) |
| 636 | deleted = True |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 637 | |
Matt Spinler | b1f6a2c | 2018-05-14 12:25:21 -0500 | [diff] [blame] | 638 | #It's OK if some objects didn't have a Delete, but not all |
| 639 | if not deleted: |
| 640 | abort(403, _4034_msg % ('resource', 'removed', path)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 641 | |
Matt Spinler | b1f6a2c | 2018-05-14 12:25:21 -0500 | [diff] [blame] | 642 | def bus_has_delete(self, interfaces): |
| 643 | return DELETE_IFACE in interfaces |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 644 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 645 | def delete_on_bus(self, path, bus): |
| 646 | obj = self.bus.get_object(bus, path, introspect=False) |
| 647 | delete_iface = dbus.Interface( |
| 648 | obj, dbus_interface=DELETE_IFACE) |
| 649 | delete_iface.Delete() |
| 650 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 651 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 652 | class SessionHandler(MethodHandler): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 653 | ''' Handles the /login and /logout routes, manages |
| 654 | server side session store and session cookies. ''' |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 655 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 656 | rules = ['/login', '/logout'] |
| 657 | login_str = "User '%s' logged %s" |
| 658 | bad_passwd_str = "Invalid username or password" |
| 659 | no_user_str = "No user logged in" |
| 660 | bad_json_str = "Expecting request format { 'data': " \ |
| 661 | "[<username>, <password>] }, got '%s'" |
Alexander Filippov | d08a456 | 2018-03-20 12:02:23 +0300 | [diff] [blame] | 662 | bmc_not_ready_str = "BMC is not ready (booting)" |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 663 | _require_auth = None |
| 664 | MAX_SESSIONS = 16 |
Alexander Filippov | d08a456 | 2018-03-20 12:02:23 +0300 | [diff] [blame] | 665 | BMCSTATE_IFACE = 'xyz.openbmc_project.State.BMC' |
| 666 | BMCSTATE_PATH = '/xyz/openbmc_project/state/bmc0' |
| 667 | BMCSTATE_PROPERTY = 'CurrentBMCState' |
| 668 | BMCSTATE_READY = 'xyz.openbmc_project.State.BMC.BMCState.Ready' |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 669 | suppress_json_logging = True |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 670 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 671 | def __init__(self, app, bus): |
| 672 | super(SessionHandler, self).__init__( |
| 673 | app, bus) |
| 674 | self.hmac_key = os.urandom(128) |
| 675 | self.session_store = [] |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 676 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 677 | @staticmethod |
| 678 | def authenticate(username, clear): |
| 679 | try: |
| 680 | encoded = spwd.getspnam(username)[1] |
| 681 | return encoded == crypt.crypt(clear, encoded) |
| 682 | except KeyError: |
| 683 | return False |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 684 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 685 | def invalidate_session(self, session): |
| 686 | try: |
| 687 | self.session_store.remove(session) |
| 688 | except ValueError: |
| 689 | pass |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 690 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 691 | def new_session(self): |
| 692 | sid = os.urandom(32) |
| 693 | if self.MAX_SESSIONS <= len(self.session_store): |
| 694 | self.session_store.pop() |
| 695 | self.session_store.insert(0, {'sid': sid}) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 696 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 697 | return self.session_store[0] |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 698 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 699 | def get_session(self, sid): |
| 700 | sids = [x['sid'] for x in self.session_store] |
| 701 | try: |
| 702 | return self.session_store[sids.index(sid)] |
| 703 | except ValueError: |
| 704 | return None |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 705 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 706 | def get_session_from_cookie(self): |
| 707 | return self.get_session( |
| 708 | request.get_cookie( |
| 709 | 'sid', secret=self.hmac_key)) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 710 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 711 | def do_post(self, **kw): |
| 712 | if request.path == '/login': |
| 713 | return self.do_login(**kw) |
| 714 | else: |
| 715 | return self.do_logout(**kw) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 716 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 717 | def do_logout(self, **kw): |
| 718 | session = self.get_session_from_cookie() |
| 719 | if session is not None: |
| 720 | user = session['user'] |
| 721 | self.invalidate_session(session) |
| 722 | response.delete_cookie('sid') |
| 723 | return self.login_str % (user, 'out') |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 724 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 725 | return self.no_user_str |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 726 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 727 | def do_login(self, **kw): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 728 | if len(request.parameter_list) != 2: |
| 729 | abort(400, self.bad_json_str % (request.json)) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 730 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 731 | if not self.authenticate(*request.parameter_list): |
Brad Bishop | dc3fbfa | 2016-09-08 09:51:38 -0400 | [diff] [blame] | 732 | abort(401, self.bad_passwd_str) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 733 | |
Alexander Filippov | d08a456 | 2018-03-20 12:02:23 +0300 | [diff] [blame] | 734 | force = False |
| 735 | try: |
| 736 | force = request.json.get('force') |
| 737 | except (ValueError, AttributeError, KeyError, TypeError): |
| 738 | force = False |
| 739 | |
| 740 | if not force and not self.is_bmc_ready(): |
| 741 | abort(503, self.bmc_not_ready_str) |
| 742 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 743 | user = request.parameter_list[0] |
| 744 | session = self.new_session() |
| 745 | session['user'] = user |
| 746 | response.set_cookie( |
| 747 | 'sid', session['sid'], secret=self.hmac_key, |
| 748 | secure=True, |
| 749 | httponly=True) |
| 750 | return self.login_str % (user, 'in') |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 751 | |
Alexander Filippov | d08a456 | 2018-03-20 12:02:23 +0300 | [diff] [blame] | 752 | def is_bmc_ready(self): |
| 753 | if not self.app.with_bmc_check: |
| 754 | return True |
| 755 | |
| 756 | try: |
| 757 | obj = self.bus.get_object(self.BMCSTATE_IFACE, self.BMCSTATE_PATH) |
| 758 | iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE) |
| 759 | state = iface.Get(self.BMCSTATE_IFACE, self.BMCSTATE_PROPERTY) |
| 760 | if state == self.BMCSTATE_READY: |
| 761 | return True |
| 762 | |
| 763 | except dbus.exceptions.DBusException: |
| 764 | pass |
| 765 | |
| 766 | return False |
| 767 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 768 | def find(self, **kw): |
| 769 | pass |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 770 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 771 | def setup(self, **kw): |
| 772 | pass |
| 773 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 774 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 775 | class ImageUploadUtils: |
| 776 | ''' Provides common utils for image upload. ''' |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 777 | |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 778 | file_loc = '/tmp/images' |
| 779 | file_prefix = 'img' |
| 780 | file_suffix = '' |
Adriana Kobylak | 5369389 | 2018-03-12 13:05:50 -0500 | [diff] [blame] | 781 | signal = None |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 782 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 783 | @classmethod |
| 784 | def do_upload(cls, filename=''): |
Adriana Kobylak | 5369389 | 2018-03-12 13:05:50 -0500 | [diff] [blame] | 785 | def cleanup(): |
| 786 | os.close(handle) |
| 787 | if cls.signal: |
| 788 | cls.signal.remove() |
| 789 | cls.signal = None |
| 790 | |
| 791 | def signal_callback(path, a, **kw): |
| 792 | # Just interested on the first Version interface created which is |
| 793 | # triggered when the file is uploaded. This helps avoid getting the |
| 794 | # wrong information for multiple upload requests in a row. |
| 795 | if "xyz.openbmc_project.Software.Version" in a and \ |
| 796 | "xyz.openbmc_project.Software.Activation" not in a: |
| 797 | paths.append(path) |
| 798 | |
| 799 | while cls.signal: |
| 800 | # Serialize uploads by waiting for the signal to be cleared. |
| 801 | # This makes it easier to ensure that the version information |
| 802 | # is the right one instead of the data from another upload request. |
| 803 | gevent.sleep(1) |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 804 | if not os.path.exists(cls.file_loc): |
Gunnar Mills | fb51579 | 2017-11-09 15:52:17 -0600 | [diff] [blame] | 805 | abort(500, "Error Directory not found") |
Adriana Kobylak | 5369389 | 2018-03-12 13:05:50 -0500 | [diff] [blame] | 806 | paths = [] |
| 807 | bus = dbus.SystemBus() |
| 808 | cls.signal = bus.add_signal_receiver( |
| 809 | signal_callback, |
| 810 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 811 | signal_name='InterfacesAdded', |
| 812 | path=SOFTWARE_PATH) |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 813 | if not filename: |
| 814 | handle, filename = tempfile.mkstemp(cls.file_suffix, |
| 815 | cls.file_prefix, cls.file_loc) |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 816 | else: |
| 817 | filename = os.path.join(cls.file_loc, filename) |
Gunnar Mills | b66b18c | 2017-08-21 16:17:21 -0500 | [diff] [blame] | 818 | handle = os.open(filename, os.O_WRONLY | os.O_CREAT) |
Leonel Gonzalez | 0b62edf | 2017-06-08 15:10:03 -0500 | [diff] [blame] | 819 | try: |
| 820 | file_contents = request.body.read() |
| 821 | request.body.close() |
Gunnar Mills | b66b18c | 2017-08-21 16:17:21 -0500 | [diff] [blame] | 822 | os.write(handle, file_contents) |
Adriana Kobylak | 5369389 | 2018-03-12 13:05:50 -0500 | [diff] [blame] | 823 | # Close file after writing, the image manager process watches for |
| 824 | # the close event to know the upload is complete. |
Gunnar Mills | b66b18c | 2017-08-21 16:17:21 -0500 | [diff] [blame] | 825 | os.close(handle) |
Adriana Kobylak | 5369389 | 2018-03-12 13:05:50 -0500 | [diff] [blame] | 826 | except (IOError, ValueError) as e: |
| 827 | cleanup() |
| 828 | abort(400, str(e)) |
| 829 | except Exception: |
| 830 | cleanup() |
| 831 | abort(400, "Unexpected Error") |
| 832 | loop = gobject.MainLoop() |
| 833 | gcontext = loop.get_context() |
| 834 | count = 0 |
| 835 | version_id = '' |
| 836 | while loop is not None: |
| 837 | try: |
| 838 | if gcontext.pending(): |
| 839 | gcontext.iteration() |
| 840 | if not paths: |
| 841 | gevent.sleep(1) |
| 842 | else: |
| 843 | version_id = os.path.basename(paths.pop()) |
| 844 | break |
| 845 | count += 1 |
| 846 | if count == 10: |
| 847 | break |
| 848 | except Exception: |
| 849 | break |
| 850 | cls.signal.remove() |
| 851 | cls.signal = None |
Adriana Kobylak | 97fe435 | 2018-04-10 10:44:11 -0500 | [diff] [blame] | 852 | if version_id: |
| 853 | return version_id |
| 854 | else: |
| 855 | abort(400, "Version already exists or failed to be extracted") |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 856 | |
| 857 | |
| 858 | class ImagePostHandler(RouteHandler): |
| 859 | ''' Handles the /upload/image route. ''' |
| 860 | |
| 861 | verbs = ['POST'] |
| 862 | rules = ['/upload/image'] |
| 863 | content_type = 'application/octet-stream' |
| 864 | |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 865 | def __init__(self, app, bus): |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 866 | super(ImagePostHandler, self).__init__( |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 867 | app, bus, self.verbs, self.rules, self.content_type) |
| 868 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 869 | def do_post(self, filename=''): |
Adriana Kobylak | 5369389 | 2018-03-12 13:05:50 -0500 | [diff] [blame] | 870 | return ImageUploadUtils.do_upload() |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 871 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 872 | def find(self, **kw): |
| 873 | pass |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 874 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 875 | def setup(self, **kw): |
| 876 | pass |
| 877 | |
| 878 | |
Dhruvaraj Subhashchandran | dee2ef5 | 2018-09-05 05:36:31 -0500 | [diff] [blame^] | 879 | class CertificateHandler: |
| 880 | file_loc = '/tmp' |
| 881 | file_suffix = '.pem' |
| 882 | file_prefix = 'cert_' |
| 883 | CERT_BUSNAME = 'xyz.openbmc_project.Certs.Manager' |
| 884 | CERT_PATH = '/xyz/openbmc_project/certs' |
| 885 | CERT_IFACE = 'xyz.openbmc_project.Certs.Install' |
| 886 | |
| 887 | def do_upload(cls, cert_type, service): |
| 888 | def cleanup(): |
| 889 | if os.path.exists(temp.name): |
| 890 | os.remove(temp.name) |
| 891 | |
| 892 | if not service: |
| 893 | abort(500, "Missing service") |
| 894 | if not cert_type: |
| 895 | abort(500, "Missing certificate type") |
| 896 | |
| 897 | with tempfile.NamedTemporaryFile( |
| 898 | suffix=cls.file_suffix, |
| 899 | prefix=cls.file_prefix, |
| 900 | delete=False) as temp: |
| 901 | try: |
| 902 | file_contents = request.body.read() |
| 903 | request.body.close() |
| 904 | temp.write(file_contents) |
| 905 | except (IOError, ValueError) as e: |
| 906 | cleanup() |
| 907 | abort(500, str(e)) |
| 908 | except Exception: |
| 909 | cleanup() |
| 910 | abort(500, "Unexpected Error") |
| 911 | |
| 912 | try: |
| 913 | bus = dbus.SystemBus() |
| 914 | busName = cls.CERT_BUSNAME + "." + cert_type.capitalize() + "." \ |
| 915 | + service.capitalize() |
| 916 | certPath = cls.CERT_PATH + "/" + cert_type + "/" + service |
| 917 | obj = bus.get_object(busName, certPath) |
| 918 | iface = dbus.Interface(obj, cls.CERT_IFACE) |
| 919 | iface.Install(temp.name) |
| 920 | except dbus.exceptions.DBusException as e: |
| 921 | cleanup() |
| 922 | abort(503, str(e)) |
| 923 | cleanup() |
| 924 | |
| 925 | |
| 926 | class CertificatePutHandler(RouteHandler): |
| 927 | ''' Handles the /xyz/openbmc_project/certs/<cert_type>/<service> route. ''' |
| 928 | |
| 929 | verbs = ['PUT'] |
| 930 | rules = ['/xyz/openbmc_project/certs/<cert_type>/<service>'] |
| 931 | content_type = 'application/octet-stream' |
| 932 | |
| 933 | def __init__(self, app, bus): |
| 934 | super(CertificatePutHandler, self).__init__( |
| 935 | app, bus, self.verbs, self.rules, self.content_type) |
| 936 | |
| 937 | def do_put(self, cert_type, service): |
| 938 | return CertificateHandler().do_upload(cert_type, service) |
| 939 | |
| 940 | def find(self, **kw): |
| 941 | pass |
| 942 | |
| 943 | def setup(self, **kw): |
| 944 | pass |
| 945 | |
| 946 | |
Deepak Kodihalli | 639b502 | 2017-10-13 06:40:26 -0500 | [diff] [blame] | 947 | class EventNotifier: |
| 948 | keyNames = {} |
| 949 | keyNames['event'] = 'event' |
| 950 | keyNames['path'] = 'path' |
| 951 | keyNames['intfMap'] = 'interfaces' |
| 952 | keyNames['propMap'] = 'properties' |
| 953 | keyNames['intf'] = 'interface' |
| 954 | |
| 955 | def __init__(self, wsock, filters): |
| 956 | self.wsock = wsock |
| 957 | self.paths = filters.get("paths", []) |
| 958 | self.interfaces = filters.get("interfaces", []) |
| 959 | if not self.paths: |
| 960 | self.paths.append(None) |
| 961 | bus = dbus.SystemBus() |
| 962 | # Add a signal receiver for every path the client is interested in |
| 963 | for path in self.paths: |
| 964 | bus.add_signal_receiver( |
| 965 | self.interfaces_added_handler, |
| 966 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 967 | signal_name='InterfacesAdded', |
| 968 | path=path) |
| 969 | bus.add_signal_receiver( |
| 970 | self.properties_changed_handler, |
| 971 | dbus_interface=dbus.PROPERTIES_IFACE, |
| 972 | signal_name='PropertiesChanged', |
| 973 | path=path, |
| 974 | path_keyword='path') |
| 975 | loop = gobject.MainLoop() |
| 976 | # gobject's mainloop.run() will block the entire process, so the gevent |
| 977 | # scheduler and hence greenlets won't execute. The while-loop below |
| 978 | # works around this limitation by using gevent's sleep, instead of |
| 979 | # calling loop.run() |
| 980 | gcontext = loop.get_context() |
| 981 | while loop is not None: |
| 982 | try: |
| 983 | if gcontext.pending(): |
| 984 | gcontext.iteration() |
| 985 | else: |
| 986 | # gevent.sleep puts only the current greenlet to sleep, |
| 987 | # not the entire process. |
| 988 | gevent.sleep(5) |
| 989 | except WebSocketError: |
| 990 | break |
| 991 | |
| 992 | def interfaces_added_handler(self, path, iprops, **kw): |
| 993 | ''' If the client is interested in these changes, respond to the |
| 994 | client. This handles d-bus interface additions.''' |
| 995 | if (not self.interfaces) or \ |
| 996 | (not set(iprops).isdisjoint(self.interfaces)): |
| 997 | response = {} |
| 998 | response[self.keyNames['event']] = "InterfacesAdded" |
| 999 | response[self.keyNames['path']] = path |
| 1000 | response[self.keyNames['intfMap']] = iprops |
| 1001 | try: |
| 1002 | self.wsock.send(json.dumps(response)) |
| 1003 | except WebSocketError: |
| 1004 | return |
| 1005 | |
| 1006 | def properties_changed_handler(self, interface, new, old, **kw): |
| 1007 | ''' If the client is interested in these changes, respond to the |
| 1008 | client. This handles d-bus property changes. ''' |
| 1009 | if (not self.interfaces) or (interface in self.interfaces): |
| 1010 | path = str(kw['path']) |
| 1011 | response = {} |
| 1012 | response[self.keyNames['event']] = "PropertiesChanged" |
| 1013 | response[self.keyNames['path']] = path |
| 1014 | response[self.keyNames['intf']] = interface |
| 1015 | response[self.keyNames['propMap']] = new |
| 1016 | try: |
| 1017 | self.wsock.send(json.dumps(response)) |
| 1018 | except WebSocketError: |
| 1019 | return |
| 1020 | |
| 1021 | |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 1022 | class EventHandler(RouteHandler): |
| 1023 | ''' Handles the /subscribe route, for clients to be able |
| 1024 | to subscribe to BMC events. ''' |
| 1025 | |
| 1026 | verbs = ['GET'] |
| 1027 | rules = ['/subscribe'] |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1028 | suppress_logging = True |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 1029 | |
| 1030 | def __init__(self, app, bus): |
| 1031 | super(EventHandler, self).__init__( |
| 1032 | app, bus, self.verbs, self.rules) |
| 1033 | |
| 1034 | def find(self, **kw): |
| 1035 | pass |
| 1036 | |
| 1037 | def setup(self, **kw): |
| 1038 | pass |
| 1039 | |
| 1040 | def do_get(self): |
| 1041 | wsock = request.environ.get('wsgi.websocket') |
| 1042 | if not wsock: |
| 1043 | abort(400, 'Expected WebSocket request.') |
Jayashankar Padath | bec10c2 | 2018-05-29 18:22:59 +0530 | [diff] [blame] | 1044 | ping_sender = Greenlet.spawn(send_ws_ping, wsock, WEBSOCKET_TIMEOUT) |
Deepak Kodihalli | 639b502 | 2017-10-13 06:40:26 -0500 | [diff] [blame] | 1045 | filters = wsock.receive() |
| 1046 | filters = json.loads(filters) |
| 1047 | notifier = EventNotifier(wsock, filters) |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 1048 | |
Deepak Kodihalli | 5c518f6 | 2018-04-23 03:26:38 -0500 | [diff] [blame] | 1049 | class HostConsoleHandler(RouteHandler): |
| 1050 | ''' Handles the /console route, for clients to be able |
| 1051 | read/write the host serial console. The way this is |
| 1052 | done is by exposing a websocket that's mirrored to an |
| 1053 | abstract UNIX domain socket, which is the source for |
| 1054 | the console data. ''' |
| 1055 | |
| 1056 | verbs = ['GET'] |
| 1057 | # Naming the route console0, because the numbering will help |
| 1058 | # on multi-bmc/multi-host systems. |
| 1059 | rules = ['/console0'] |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1060 | suppress_logging = True |
Deepak Kodihalli | 5c518f6 | 2018-04-23 03:26:38 -0500 | [diff] [blame] | 1061 | |
| 1062 | def __init__(self, app, bus): |
| 1063 | super(HostConsoleHandler, self).__init__( |
| 1064 | app, bus, self.verbs, self.rules) |
| 1065 | |
| 1066 | def find(self, **kw): |
| 1067 | pass |
| 1068 | |
| 1069 | def setup(self, **kw): |
| 1070 | pass |
| 1071 | |
| 1072 | def read_wsock(self, wsock, sock): |
| 1073 | while True: |
| 1074 | try: |
| 1075 | incoming = wsock.receive() |
| 1076 | if incoming: |
| 1077 | # Read websocket, write to UNIX socket |
| 1078 | sock.send(incoming) |
| 1079 | except Exception as e: |
| 1080 | sock.close() |
| 1081 | return |
| 1082 | |
| 1083 | def read_sock(self, sock, wsock): |
| 1084 | max_sock_read_len = 4096 |
| 1085 | while True: |
| 1086 | try: |
| 1087 | outgoing = sock.recv(max_sock_read_len) |
| 1088 | if outgoing: |
| 1089 | # Read UNIX socket, write to websocket |
| 1090 | wsock.send(outgoing) |
| 1091 | except Exception as e: |
| 1092 | wsock.close() |
| 1093 | return |
| 1094 | |
Deepak Kodihalli | 5c518f6 | 2018-04-23 03:26:38 -0500 | [diff] [blame] | 1095 | def do_get(self): |
| 1096 | wsock = request.environ.get('wsgi.websocket') |
| 1097 | if not wsock: |
| 1098 | abort(400, 'Expected WebSocket based request.') |
| 1099 | |
| 1100 | # A UNIX domain socket structure defines a 108-byte pathname. The |
| 1101 | # server in this case, obmc-console-server, expects a 108-byte path. |
| 1102 | socket_name = "\0obmc-console" |
| 1103 | trailing_bytes = "\0" * (108 - len(socket_name)) |
| 1104 | socket_path = socket_name + trailing_bytes |
| 1105 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1106 | |
| 1107 | try: |
| 1108 | sock.connect(socket_path) |
| 1109 | except Exception as e: |
| 1110 | abort(500, str(e)) |
| 1111 | |
| 1112 | wsock_reader = Greenlet.spawn(self.read_wsock, wsock, sock) |
| 1113 | sock_reader = Greenlet.spawn(self.read_sock, sock, wsock) |
Jayashankar Padath | bec10c2 | 2018-05-29 18:22:59 +0530 | [diff] [blame] | 1114 | ping_sender = Greenlet.spawn(send_ws_ping, wsock, WEBSOCKET_TIMEOUT) |
Deepak Kodihalli | 5c518f6 | 2018-04-23 03:26:38 -0500 | [diff] [blame] | 1115 | gevent.joinall([wsock_reader, sock_reader, ping_sender]) |
| 1116 | |
| 1117 | |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 1118 | class ImagePutHandler(RouteHandler): |
| 1119 | ''' Handles the /upload/image/<filename> route. ''' |
| 1120 | |
| 1121 | verbs = ['PUT'] |
| 1122 | rules = ['/upload/image/<filename>'] |
| 1123 | content_type = 'application/octet-stream' |
| 1124 | |
| 1125 | def __init__(self, app, bus): |
| 1126 | super(ImagePutHandler, self).__init__( |
| 1127 | app, bus, self.verbs, self.rules, self.content_type) |
| 1128 | |
| 1129 | def do_put(self, filename=''): |
Adriana Kobylak | 5369389 | 2018-03-12 13:05:50 -0500 | [diff] [blame] | 1130 | return ImageUploadUtils.do_upload(filename) |
Deepak Kodihalli | 1af301a | 2017-04-11 07:29:01 -0500 | [diff] [blame] | 1131 | |
| 1132 | def find(self, **kw): |
| 1133 | pass |
| 1134 | |
| 1135 | def setup(self, **kw): |
| 1136 | pass |
| 1137 | |
| 1138 | |
Jayanth Othayoth | 9bc9499 | 2017-06-29 06:30:40 -0500 | [diff] [blame] | 1139 | class DownloadDumpHandler(RouteHandler): |
| 1140 | ''' Handles the /download/dump route. ''' |
| 1141 | |
| 1142 | verbs = 'GET' |
| 1143 | rules = ['/download/dump/<dumpid>'] |
| 1144 | content_type = 'application/octet-stream' |
Jayanth Othayoth | 18c3a24 | 2017-08-02 08:16:11 -0500 | [diff] [blame] | 1145 | dump_loc = '/var/lib/phosphor-debug-collector/dumps' |
Brad Bishop | 944cd04 | 2017-07-10 16:42:41 -0400 | [diff] [blame] | 1146 | suppress_json_resp = True |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1147 | suppress_logging = True |
Jayanth Othayoth | 9bc9499 | 2017-06-29 06:30:40 -0500 | [diff] [blame] | 1148 | |
| 1149 | def __init__(self, app, bus): |
| 1150 | super(DownloadDumpHandler, self).__init__( |
| 1151 | app, bus, self.verbs, self.rules, self.content_type) |
| 1152 | |
| 1153 | def do_get(self, dumpid): |
| 1154 | return self.do_download(dumpid) |
| 1155 | |
| 1156 | def find(self, **kw): |
| 1157 | pass |
| 1158 | |
| 1159 | def setup(self, **kw): |
| 1160 | pass |
| 1161 | |
| 1162 | def do_download(self, dumpid): |
| 1163 | dump_loc = os.path.join(self.dump_loc, dumpid) |
| 1164 | if not os.path.exists(dump_loc): |
| 1165 | abort(404, "Path not found") |
| 1166 | |
| 1167 | files = os.listdir(dump_loc) |
| 1168 | num_files = len(files) |
| 1169 | if num_files == 0: |
| 1170 | abort(404, "Dump not found") |
| 1171 | |
| 1172 | return static_file(os.path.basename(files[0]), root=dump_loc, |
| 1173 | download=True, mimetype=self.content_type) |
| 1174 | |
| 1175 | |
Matt Spinler | d41643e | 2018-02-02 13:51:38 -0600 | [diff] [blame] | 1176 | class WebHandler(RouteHandler): |
| 1177 | ''' Handles the routes for the web UI files. ''' |
| 1178 | |
| 1179 | verbs = 'GET' |
| 1180 | |
| 1181 | # Match only what we know are web files, so everything else |
| 1182 | # can get routed to the REST handlers. |
| 1183 | rules = ['//', '/<filename:re:.+\.js>', '/<filename:re:.+\.svg>', |
| 1184 | '/<filename:re:.+\.css>', '/<filename:re:.+\.ttf>', |
| 1185 | '/<filename:re:.+\.eot>', '/<filename:re:.+\.woff>', |
| 1186 | '/<filename:re:.+\.woff2>', '/<filename:re:.+\.map>', |
| 1187 | '/<filename:re:.+\.png>', '/<filename:re:.+\.html>', |
| 1188 | '/<filename:re:.+\.ico>'] |
| 1189 | |
| 1190 | # The mimetypes module knows about most types, but not these |
| 1191 | content_types = { |
| 1192 | '.eot': 'application/vnd.ms-fontobject', |
| 1193 | '.woff': 'application/x-font-woff', |
| 1194 | '.woff2': 'application/x-font-woff2', |
| 1195 | '.ttf': 'application/x-font-ttf', |
| 1196 | '.map': 'application/json' |
| 1197 | } |
| 1198 | |
| 1199 | _require_auth = None |
| 1200 | suppress_json_resp = True |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1201 | suppress_logging = True |
Matt Spinler | d41643e | 2018-02-02 13:51:38 -0600 | [diff] [blame] | 1202 | |
| 1203 | def __init__(self, app, bus): |
| 1204 | super(WebHandler, self).__init__( |
| 1205 | app, bus, self.verbs, self.rules) |
| 1206 | |
| 1207 | def get_type(self, filename): |
| 1208 | ''' Returns the content type and encoding for a file ''' |
| 1209 | |
| 1210 | content_type, encoding = mimetypes.guess_type(filename) |
| 1211 | |
| 1212 | # Try our own list if mimetypes didn't recognize it |
| 1213 | if content_type is None: |
| 1214 | if filename[-3:] == '.gz': |
| 1215 | filename = filename[:-3] |
| 1216 | extension = filename[filename.rfind('.'):] |
| 1217 | content_type = self.content_types.get(extension, None) |
| 1218 | |
| 1219 | return content_type, encoding |
| 1220 | |
| 1221 | def do_get(self, filename='index.html'): |
| 1222 | |
| 1223 | # If a gzipped version exists, use that instead. |
| 1224 | # Possible future enhancement: if the client doesn't |
| 1225 | # accept compressed files, unzip it ourselves before sending. |
| 1226 | if not os.path.exists(os.path.join(www_base_path, filename)): |
| 1227 | filename = filename + '.gz' |
| 1228 | |
| 1229 | # Though bottle should protect us, ensure path is valid |
| 1230 | realpath = os.path.realpath(filename) |
| 1231 | if realpath[0] == '/': |
| 1232 | realpath = realpath[1:] |
| 1233 | if not os.path.exists(os.path.join(www_base_path, realpath)): |
| 1234 | abort(404, "Path not found") |
| 1235 | |
| 1236 | mimetype, encoding = self.get_type(filename) |
| 1237 | |
| 1238 | # Couldn't find the type - let static_file() deal with it, |
| 1239 | # though this should never happen. |
| 1240 | if mimetype is None: |
| 1241 | print("Can't figure out content-type for %s" % filename) |
| 1242 | mimetype = 'auto' |
| 1243 | |
| 1244 | # This call will set several header fields for us, |
| 1245 | # including the charset if the type is text. |
| 1246 | response = static_file(filename, www_base_path, mimetype) |
| 1247 | |
| 1248 | # static_file() will only set the encoding if the |
| 1249 | # mimetype was auto, so set it here. |
| 1250 | if encoding is not None: |
| 1251 | response.set_header('Content-Encoding', encoding) |
| 1252 | |
| 1253 | return response |
| 1254 | |
| 1255 | def find(self, **kw): |
| 1256 | pass |
| 1257 | |
| 1258 | def setup(self, **kw): |
| 1259 | pass |
| 1260 | |
| 1261 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 1262 | class AuthorizationPlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1263 | ''' Invokes an optional list of authorization callbacks. ''' |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 1264 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1265 | name = 'authorization' |
| 1266 | api = 2 |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 1267 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1268 | class Compose: |
| 1269 | def __init__(self, validators, callback, session_mgr): |
| 1270 | self.validators = validators |
| 1271 | self.callback = callback |
| 1272 | self.session_mgr = session_mgr |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 1273 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1274 | def __call__(self, *a, **kw): |
| 1275 | sid = request.get_cookie('sid', secret=self.session_mgr.hmac_key) |
| 1276 | session = self.session_mgr.get_session(sid) |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 1277 | if request.method != 'OPTIONS': |
| 1278 | for x in self.validators: |
| 1279 | x(session, *a, **kw) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 1280 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1281 | return self.callback(*a, **kw) |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 1282 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1283 | def apply(self, callback, route): |
| 1284 | undecorated = route.get_undecorated_callback() |
| 1285 | if not isinstance(undecorated, RouteHandler): |
| 1286 | return callback |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 1287 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1288 | auth_types = getattr( |
| 1289 | undecorated, '_require_auth', None) |
| 1290 | if not auth_types: |
| 1291 | return callback |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 1292 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1293 | return self.Compose( |
| 1294 | auth_types, callback, undecorated.app.session_handler) |
| 1295 | |
Brad Bishop | 2f42858 | 2015-12-02 10:56:11 -0500 | [diff] [blame] | 1296 | |
Brad Bishop | d0c404a | 2017-02-21 09:23:25 -0500 | [diff] [blame] | 1297 | class CorsPlugin(object): |
| 1298 | ''' Add CORS headers. ''' |
| 1299 | |
| 1300 | name = 'cors' |
| 1301 | api = 2 |
| 1302 | |
| 1303 | @staticmethod |
| 1304 | def process_origin(): |
| 1305 | origin = request.headers.get('Origin') |
| 1306 | if origin: |
| 1307 | response.add_header('Access-Control-Allow-Origin', origin) |
| 1308 | response.add_header( |
| 1309 | 'Access-Control-Allow-Credentials', 'true') |
| 1310 | |
| 1311 | @staticmethod |
| 1312 | def process_method_and_headers(verbs): |
| 1313 | method = request.headers.get('Access-Control-Request-Method') |
| 1314 | headers = request.headers.get('Access-Control-Request-Headers') |
| 1315 | if headers: |
| 1316 | headers = [x.lower() for x in headers.split(',')] |
| 1317 | |
| 1318 | if method in verbs \ |
| 1319 | and headers == ['content-type']: |
| 1320 | response.add_header('Access-Control-Allow-Methods', method) |
| 1321 | response.add_header( |
| 1322 | 'Access-Control-Allow-Headers', 'Content-Type') |
Ratan Gupta | 91b46f8 | 2018-01-14 12:52:23 +0530 | [diff] [blame] | 1323 | response.add_header('X-Frame-Options', 'deny') |
| 1324 | response.add_header('X-Content-Type-Options', 'nosniff') |
| 1325 | response.add_header('X-XSS-Protection', '1; mode=block') |
| 1326 | response.add_header( |
| 1327 | 'Content-Security-Policy', "default-src 'self'") |
| 1328 | response.add_header( |
| 1329 | 'Strict-Transport-Security', |
| 1330 | 'max-age=31536000; includeSubDomains; preload') |
Brad Bishop | d0c404a | 2017-02-21 09:23:25 -0500 | [diff] [blame] | 1331 | |
| 1332 | def __init__(self, app): |
| 1333 | app.install_error_callback(self.error_callback) |
| 1334 | |
| 1335 | def apply(self, callback, route): |
| 1336 | undecorated = route.get_undecorated_callback() |
| 1337 | if not isinstance(undecorated, RouteHandler): |
| 1338 | return callback |
| 1339 | |
| 1340 | if not getattr(undecorated, '_enable_cors', None): |
| 1341 | return callback |
| 1342 | |
| 1343 | def wrap(*a, **kw): |
| 1344 | self.process_origin() |
| 1345 | self.process_method_and_headers(undecorated._verbs) |
| 1346 | return callback(*a, **kw) |
| 1347 | |
| 1348 | return wrap |
| 1349 | |
| 1350 | def error_callback(self, **kw): |
| 1351 | self.process_origin() |
| 1352 | |
| 1353 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1354 | class JsonApiRequestPlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1355 | ''' Ensures request content satisfies the OpenBMC json api format. ''' |
| 1356 | name = 'json_api_request' |
| 1357 | api = 2 |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1358 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1359 | error_str = "Expecting request format { 'data': <value> }, got '%s'" |
| 1360 | type_error_str = "Unsupported Content-Type: '%s'" |
| 1361 | json_type = "application/json" |
| 1362 | request_methods = ['PUT', 'POST', 'PATCH'] |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1363 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1364 | @staticmethod |
| 1365 | def content_expected(): |
| 1366 | return request.method in JsonApiRequestPlugin.request_methods |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1367 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1368 | def validate_request(self): |
| 1369 | if request.content_length > 0 and \ |
| 1370 | request.content_type != self.json_type: |
| 1371 | abort(415, self.type_error_str % request.content_type) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1372 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1373 | try: |
| 1374 | request.parameter_list = request.json.get('data') |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 1375 | except ValueError as e: |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1376 | abort(400, str(e)) |
| 1377 | except (AttributeError, KeyError, TypeError): |
| 1378 | abort(400, self.error_str % request.json) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1379 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1380 | def apply(self, callback, route): |
Deepak Kodihalli | fb6cd48 | 2017-04-10 07:27:09 -0500 | [diff] [blame] | 1381 | content_type = getattr( |
| 1382 | route.get_undecorated_callback(), '_content_type', None) |
| 1383 | if self.json_type != content_type: |
| 1384 | return callback |
| 1385 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1386 | verbs = getattr( |
| 1387 | route.get_undecorated_callback(), '_verbs', None) |
| 1388 | if verbs is None: |
| 1389 | return callback |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1390 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1391 | if not set(self.request_methods).intersection(verbs): |
| 1392 | return callback |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1393 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1394 | def wrap(*a, **kw): |
| 1395 | if self.content_expected(): |
| 1396 | self.validate_request() |
| 1397 | return callback(*a, **kw) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1398 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1399 | return wrap |
| 1400 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1401 | |
| 1402 | class JsonApiRequestTypePlugin(object): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1403 | ''' Ensures request content type satisfies the OpenBMC json api format. ''' |
| 1404 | name = 'json_api_method_request' |
| 1405 | api = 2 |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1406 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1407 | error_str = "Expecting request format { 'data': %s }, got '%s'" |
Deepak Kodihalli | fb6cd48 | 2017-04-10 07:27:09 -0500 | [diff] [blame] | 1408 | json_type = "application/json" |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1409 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1410 | def apply(self, callback, route): |
Deepak Kodihalli | fb6cd48 | 2017-04-10 07:27:09 -0500 | [diff] [blame] | 1411 | content_type = getattr( |
| 1412 | route.get_undecorated_callback(), '_content_type', None) |
| 1413 | if self.json_type != content_type: |
| 1414 | return callback |
| 1415 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1416 | request_type = getattr( |
| 1417 | route.get_undecorated_callback(), 'request_type', None) |
| 1418 | if request_type is None: |
| 1419 | return callback |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1420 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1421 | def validate_request(): |
| 1422 | if not isinstance(request.parameter_list, request_type): |
| 1423 | abort(400, self.error_str % (str(request_type), request.json)) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1424 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1425 | def wrap(*a, **kw): |
| 1426 | if JsonApiRequestPlugin.content_expected(): |
| 1427 | validate_request() |
| 1428 | return callback(*a, **kw) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1429 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1430 | return wrap |
| 1431 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1432 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1433 | class JsonErrorsPlugin(JSONPlugin): |
| 1434 | ''' Extend the Bottle JSONPlugin such that it also encodes error |
| 1435 | responses. ''' |
| 1436 | |
| 1437 | def __init__(self, app, **kw): |
| 1438 | super(JsonErrorsPlugin, self).__init__(**kw) |
| 1439 | self.json_opts = { |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 1440 | x: y for x, y in kw.items() |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1441 | if x in ['indent', 'sort_keys']} |
| 1442 | app.install_error_callback(self.error_callback) |
| 1443 | |
| 1444 | def error_callback(self, response_object, response_body, **kw): |
| 1445 | response_body['body'] = json.dumps(response_object, **self.json_opts) |
| 1446 | response.content_type = 'application/json' |
| 1447 | |
| 1448 | |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1449 | class JsonApiResponsePlugin(object): |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1450 | ''' Emits responses in the OpenBMC json api format. ''' |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1451 | name = 'json_api_response' |
| 1452 | api = 2 |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1453 | |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 1454 | @staticmethod |
| 1455 | def has_body(): |
| 1456 | return request.method not in ['OPTIONS'] |
| 1457 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1458 | def __init__(self, app): |
| 1459 | app.install_error_callback(self.error_callback) |
| 1460 | |
Matt Spinler | 6691e7c | 2018-06-25 14:11:58 -0500 | [diff] [blame] | 1461 | @staticmethod |
| 1462 | def dbus_boolean_to_bool(data): |
| 1463 | ''' Convert all dbus.Booleans to true/false instead of 1/0 as |
| 1464 | the JSON encoder thinks they're ints. Note that unlike |
| 1465 | dicts and lists, tuples (from a dbus.Struct) are immutable |
| 1466 | so they need special handling. ''' |
| 1467 | |
| 1468 | def walkdict(data): |
| 1469 | for key, value in data.items(): |
| 1470 | if isinstance(value, dbus.Boolean): |
| 1471 | data[key] = bool(value) |
| 1472 | elif isinstance(value, tuple): |
| 1473 | data[key] = walktuple(value) |
| 1474 | else: |
| 1475 | JsonApiResponsePlugin.dbus_boolean_to_bool(value) |
| 1476 | |
| 1477 | def walklist(data): |
| 1478 | for i in range(len(data)): |
| 1479 | if isinstance(data[i], dbus.Boolean): |
| 1480 | data[i] = bool(data[i]) |
| 1481 | elif isinstance(data[i], tuple): |
| 1482 | data[i] = walktuple(data[i]) |
| 1483 | else: |
| 1484 | JsonApiResponsePlugin.dbus_boolean_to_bool(data[i]) |
| 1485 | |
| 1486 | def walktuple(data): |
| 1487 | new = [] |
| 1488 | for item in data: |
| 1489 | if isinstance(item, dbus.Boolean): |
| 1490 | item = bool(item) |
| 1491 | else: |
| 1492 | JsonApiResponsePlugin.dbus_boolean_to_bool(item) |
| 1493 | new.append(item) |
| 1494 | return tuple(new) |
| 1495 | |
| 1496 | if isinstance(data, dict): |
| 1497 | walkdict(data) |
| 1498 | elif isinstance(data, list): |
| 1499 | walklist(data) |
| 1500 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1501 | def apply(self, callback, route): |
Brad Bishop | 944cd04 | 2017-07-10 16:42:41 -0400 | [diff] [blame] | 1502 | skip = getattr( |
| 1503 | route.get_undecorated_callback(), 'suppress_json_resp', None) |
| 1504 | if skip: |
Jayanth Othayoth | 1444fd8 | 2017-06-29 05:45:07 -0500 | [diff] [blame] | 1505 | return callback |
| 1506 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1507 | def wrap(*a, **kw): |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 1508 | data = callback(*a, **kw) |
Matt Spinler | 6691e7c | 2018-06-25 14:11:58 -0500 | [diff] [blame] | 1509 | JsonApiResponsePlugin.dbus_boolean_to_bool(data) |
Brad Bishop | d4c1c55 | 2017-02-21 00:07:28 -0500 | [diff] [blame] | 1510 | if self.has_body(): |
| 1511 | resp = {'data': data} |
| 1512 | resp['status'] = 'ok' |
| 1513 | resp['message'] = response.status_line |
| 1514 | return resp |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1515 | return wrap |
| 1516 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1517 | def error_callback(self, error, response_object, **kw): |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1518 | response_object['message'] = error.status_line |
Brad Bishop | 9c2531e | 2017-03-07 10:22:40 -0500 | [diff] [blame] | 1519 | response_object['status'] = 'error' |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1520 | response_object.setdefault('data', {})['description'] = str(error.body) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1521 | if error.status_code == 500: |
| 1522 | response_object['data']['exception'] = repr(error.exception) |
| 1523 | response_object['data']['traceback'] = error.traceback.splitlines() |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1524 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1525 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1526 | class JsonpPlugin(object): |
Brad Bishop | 80fe37a | 2016-03-29 10:54:54 -0400 | [diff] [blame] | 1527 | ''' Json javascript wrapper. ''' |
| 1528 | name = 'jsonp' |
| 1529 | api = 2 |
| 1530 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1531 | def __init__(self, app, **kw): |
| 1532 | app.install_error_callback(self.error_callback) |
Brad Bishop | 80fe37a | 2016-03-29 10:54:54 -0400 | [diff] [blame] | 1533 | |
| 1534 | @staticmethod |
| 1535 | def to_jsonp(json): |
| 1536 | jwrapper = request.query.callback or None |
| 1537 | if(jwrapper): |
| 1538 | response.set_header('Content-Type', 'application/javascript') |
| 1539 | json = jwrapper + '(' + json + ');' |
| 1540 | return json |
| 1541 | |
| 1542 | def apply(self, callback, route): |
| 1543 | def wrap(*a, **kw): |
| 1544 | return self.to_jsonp(callback(*a, **kw)) |
| 1545 | return wrap |
| 1546 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1547 | def error_callback(self, response_body, **kw): |
| 1548 | response_body['body'] = self.to_jsonp(response_body['body']) |
Brad Bishop | 80fe37a | 2016-03-29 10:54:54 -0400 | [diff] [blame] | 1549 | |
| 1550 | |
Deepak Kodihalli | 461367a | 2017-04-10 07:11:38 -0500 | [diff] [blame] | 1551 | class ContentCheckerPlugin(object): |
| 1552 | ''' Ensures that a route is associated with the expected content-type |
| 1553 | header. ''' |
| 1554 | name = 'content_checker' |
| 1555 | api = 2 |
| 1556 | |
| 1557 | class Checker: |
| 1558 | def __init__(self, type, callback): |
| 1559 | self.expected_type = type |
| 1560 | self.callback = callback |
| 1561 | self.error_str = "Expecting content type '%s', got '%s'" |
| 1562 | |
| 1563 | def __call__(self, *a, **kw): |
Deepak Kodihalli | db1a21e | 2017-04-27 06:30:11 -0500 | [diff] [blame] | 1564 | if request.method in ['PUT', 'POST', 'PATCH'] and \ |
| 1565 | self.expected_type and \ |
Deepak Kodihalli | 461367a | 2017-04-10 07:11:38 -0500 | [diff] [blame] | 1566 | self.expected_type != request.content_type: |
| 1567 | abort(415, self.error_str % (self.expected_type, |
| 1568 | request.content_type)) |
| 1569 | |
| 1570 | return self.callback(*a, **kw) |
| 1571 | |
| 1572 | def apply(self, callback, route): |
| 1573 | content_type = getattr( |
| 1574 | route.get_undecorated_callback(), '_content_type', None) |
| 1575 | |
| 1576 | return self.Checker(content_type, callback) |
| 1577 | |
| 1578 | |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1579 | class LoggingPlugin(object): |
| 1580 | ''' Wraps a request in order to emit a log after the request is handled. ''' |
| 1581 | name = 'loggingp' |
| 1582 | api = 2 |
| 1583 | |
| 1584 | class Logger: |
| 1585 | def __init__(self, suppress_json_logging, callback, app): |
| 1586 | self.suppress_json_logging = suppress_json_logging |
| 1587 | self.callback = callback |
| 1588 | self.app = app |
Deepak Kodihalli | 9580368 | 2018-09-07 03:13:59 -0500 | [diff] [blame] | 1589 | self.logging_enabled = None |
| 1590 | self.bus = dbus.SystemBus() |
| 1591 | self.dbus_path = '/xyz/openbmc_project/logging/rest_api_logs' |
| 1592 | self.bus.add_signal_receiver( |
| 1593 | self.properties_changed_handler, |
| 1594 | dbus_interface=dbus.PROPERTIES_IFACE, |
| 1595 | signal_name='PropertiesChanged', |
| 1596 | path=self.dbus_path) |
| 1597 | Greenlet.spawn(self.dbus_loop) |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1598 | |
| 1599 | def __call__(self, *a, **kw): |
| 1600 | resp = self.callback(*a, **kw) |
Deepak Kodihalli | 9580368 | 2018-09-07 03:13:59 -0500 | [diff] [blame] | 1601 | if not self.enabled(): |
Deepak Kodihalli | 4aa1000 | 2018-09-13 11:48:45 -0500 | [diff] [blame] | 1602 | return resp |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1603 | if request.method == 'GET': |
Deepak Kodihalli | 4aa1000 | 2018-09-13 11:48:45 -0500 | [diff] [blame] | 1604 | return resp |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1605 | json = request.json |
| 1606 | if self.suppress_json_logging: |
| 1607 | json = None |
| 1608 | session = self.app.session_handler.get_session_from_cookie() |
| 1609 | user = None |
| 1610 | if "/login" in request.url: |
| 1611 | user = request.parameter_list[0] |
| 1612 | elif session is not None: |
| 1613 | user = session['user'] |
| 1614 | print("{remote} user:{user} {method} {url} json:{json} {status}" \ |
| 1615 | .format( |
| 1616 | user=user, |
| 1617 | remote=request.remote_addr, |
| 1618 | method=request.method, |
| 1619 | url=request.url, |
| 1620 | json=json, |
| 1621 | status=response.status)) |
Deepak Kodihalli | 4aa1000 | 2018-09-13 11:48:45 -0500 | [diff] [blame] | 1622 | return resp |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1623 | |
Deepak Kodihalli | 9580368 | 2018-09-07 03:13:59 -0500 | [diff] [blame] | 1624 | def enabled(self): |
| 1625 | if self.logging_enabled is None: |
| 1626 | try: |
| 1627 | obj = self.bus.get_object( |
| 1628 | 'xyz.openbmc_project.Settings', |
| 1629 | self.dbus_path) |
| 1630 | iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE) |
| 1631 | logging_enabled = iface.Get( |
| 1632 | 'xyz.openbmc_project.Object.Enable', |
| 1633 | 'Enabled') |
| 1634 | self.logging_enabled = logging_enabled |
| 1635 | except dbus.exceptions.DBusException: |
| 1636 | self.logging_enabled = False |
| 1637 | return self.logging_enabled |
| 1638 | |
| 1639 | def dbus_loop(self): |
| 1640 | loop = gobject.MainLoop() |
| 1641 | gcontext = loop.get_context() |
| 1642 | while loop is not None: |
| 1643 | try: |
| 1644 | if gcontext.pending(): |
| 1645 | gcontext.iteration() |
| 1646 | else: |
| 1647 | gevent.sleep(5) |
| 1648 | except Exception as e: |
| 1649 | break |
| 1650 | |
| 1651 | def properties_changed_handler(self, interface, new, old, **kw): |
| 1652 | self.logging_enabled = new.values()[0] |
| 1653 | |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1654 | def apply(self, callback, route): |
| 1655 | cb = route.get_undecorated_callback() |
| 1656 | skip = getattr( |
| 1657 | cb, 'suppress_logging', None) |
| 1658 | if skip: |
| 1659 | return callback |
| 1660 | |
| 1661 | suppress_json_logging = getattr( |
| 1662 | cb, 'suppress_json_logging', None) |
| 1663 | return self.Logger(suppress_json_logging, callback, cb.app) |
| 1664 | |
| 1665 | |
Brad Bishop | 2c6fc76 | 2016-08-29 15:53:25 -0400 | [diff] [blame] | 1666 | class App(Bottle): |
Deepak Kodihalli | 0fe213f | 2017-10-11 00:08:48 -0500 | [diff] [blame] | 1667 | def __init__(self, **kw): |
Brad Bishop | 2c6fc76 | 2016-08-29 15:53:25 -0400 | [diff] [blame] | 1668 | super(App, self).__init__(autojson=False) |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 1669 | |
| 1670 | self.have_wsock = kw.get('have_wsock', False) |
Alexander Filippov | d08a456 | 2018-03-20 12:02:23 +0300 | [diff] [blame] | 1671 | self.with_bmc_check = '--with-bmc-check' in sys.argv |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 1672 | |
Brad Bishop | 2ddfa00 | 2016-08-29 15:11:55 -0400 | [diff] [blame] | 1673 | self.bus = dbus.SystemBus() |
| 1674 | self.mapper = obmc.mapper.Mapper(self.bus) |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1675 | self.error_callbacks = [] |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1676 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1677 | self.install_hooks() |
| 1678 | self.install_plugins() |
| 1679 | self.create_handlers() |
| 1680 | self.install_handlers() |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1681 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1682 | def install_plugins(self): |
| 1683 | # install json api plugins |
| 1684 | json_kw = {'indent': 2, 'sort_keys': True} |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1685 | self.install(AuthorizationPlugin()) |
Brad Bishop | d0c404a | 2017-02-21 09:23:25 -0500 | [diff] [blame] | 1686 | self.install(CorsPlugin(self)) |
Deepak Kodihalli | 461367a | 2017-04-10 07:11:38 -0500 | [diff] [blame] | 1687 | self.install(ContentCheckerPlugin()) |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1688 | self.install(JsonpPlugin(self, **json_kw)) |
| 1689 | self.install(JsonErrorsPlugin(self, **json_kw)) |
| 1690 | self.install(JsonApiResponsePlugin(self)) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1691 | self.install(JsonApiRequestPlugin()) |
| 1692 | self.install(JsonApiRequestTypePlugin()) |
Deepak Kodihalli | 6e1ca53 | 2018-09-04 03:51:04 -0500 | [diff] [blame] | 1693 | self.install(LoggingPlugin()) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1694 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1695 | def install_hooks(self): |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1696 | self.error_handler_type = type(self.default_error_handler) |
| 1697 | self.original_error_handler = self.default_error_handler |
| 1698 | self.default_error_handler = self.error_handler_type( |
| 1699 | self.custom_error_handler, self, Bottle) |
| 1700 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1701 | self.real_router_match = self.router.match |
| 1702 | self.router.match = self.custom_router_match |
| 1703 | self.add_hook('before_request', self.strip_extra_slashes) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1704 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1705 | def create_handlers(self): |
| 1706 | # create route handlers |
| 1707 | self.session_handler = SessionHandler(self, self.bus) |
Matt Spinler | d41643e | 2018-02-02 13:51:38 -0600 | [diff] [blame] | 1708 | self.web_handler = WebHandler(self, self.bus) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1709 | self.directory_handler = DirectoryHandler(self, self.bus) |
| 1710 | self.list_names_handler = ListNamesHandler(self, self.bus) |
| 1711 | self.list_handler = ListHandler(self, self.bus) |
| 1712 | self.method_handler = MethodHandler(self, self.bus) |
| 1713 | self.property_handler = PropertyHandler(self, self.bus) |
| 1714 | self.schema_handler = SchemaHandler(self, self.bus) |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 1715 | self.image_upload_post_handler = ImagePostHandler(self, self.bus) |
| 1716 | self.image_upload_put_handler = ImagePutHandler(self, self.bus) |
Jayanth Othayoth | 9bc9499 | 2017-06-29 06:30:40 -0500 | [diff] [blame] | 1717 | self.download_dump_get_handler = DownloadDumpHandler(self, self.bus) |
Dhruvaraj Subhashchandran | dee2ef5 | 2018-09-05 05:36:31 -0500 | [diff] [blame^] | 1718 | self.certificate_put_handler = CertificatePutHandler(self, self.bus) |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 1719 | if self.have_wsock: |
| 1720 | self.event_handler = EventHandler(self, self.bus) |
Deepak Kodihalli | 5c518f6 | 2018-04-23 03:26:38 -0500 | [diff] [blame] | 1721 | self.host_console_handler = HostConsoleHandler(self, self.bus) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1722 | self.instance_handler = InstanceHandler(self, self.bus) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1723 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1724 | def install_handlers(self): |
| 1725 | self.session_handler.install() |
Matt Spinler | d41643e | 2018-02-02 13:51:38 -0600 | [diff] [blame] | 1726 | self.web_handler.install() |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1727 | self.directory_handler.install() |
| 1728 | self.list_names_handler.install() |
| 1729 | self.list_handler.install() |
| 1730 | self.method_handler.install() |
| 1731 | self.property_handler.install() |
| 1732 | self.schema_handler.install() |
Deepak Kodihalli | 7ec0a4f | 2017-04-11 07:50:27 -0500 | [diff] [blame] | 1733 | self.image_upload_post_handler.install() |
| 1734 | self.image_upload_put_handler.install() |
Jayanth Othayoth | 9bc9499 | 2017-06-29 06:30:40 -0500 | [diff] [blame] | 1735 | self.download_dump_get_handler.install() |
Dhruvaraj Subhashchandran | dee2ef5 | 2018-09-05 05:36:31 -0500 | [diff] [blame^] | 1736 | self.certificate_put_handler.install() |
Deepak Kodihalli | b209dd1 | 2017-10-11 01:19:17 -0500 | [diff] [blame] | 1737 | if self.have_wsock: |
| 1738 | self.event_handler.install() |
Deepak Kodihalli | 5c518f6 | 2018-04-23 03:26:38 -0500 | [diff] [blame] | 1739 | self.host_console_handler.install() |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1740 | # this has to come last, since it matches everything |
| 1741 | self.instance_handler.install() |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1742 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1743 | def install_error_callback(self, callback): |
| 1744 | self.error_callbacks.insert(0, callback) |
| 1745 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1746 | def custom_router_match(self, environ): |
| 1747 | ''' The built-in Bottle algorithm for figuring out if a 404 or 405 is |
| 1748 | needed doesn't work for us since the instance rules match |
| 1749 | everything. This monkey-patch lets the route handler figure |
| 1750 | out which response is needed. This could be accomplished |
| 1751 | with a hook but that would require calling the router match |
| 1752 | function twice. |
| 1753 | ''' |
| 1754 | route, args = self.real_router_match(environ) |
| 1755 | if isinstance(route.callback, RouteHandler): |
| 1756 | route.callback._setup(**args) |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1757 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1758 | return route, args |
Brad Bishop | b1cbdaf | 2015-11-13 21:28:16 -0500 | [diff] [blame] | 1759 | |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1760 | def custom_error_handler(self, res, error): |
Gunnar Mills | f01d0ba | 2017-10-25 20:37:24 -0500 | [diff] [blame] | 1761 | ''' Allow plugins to modify error responses too via this custom |
Brad Bishop | 080a48e | 2017-02-21 22:34:43 -0500 | [diff] [blame] | 1762 | error handler. ''' |
| 1763 | |
| 1764 | response_object = {} |
| 1765 | response_body = {} |
| 1766 | for x in self.error_callbacks: |
| 1767 | x(error=error, |
| 1768 | response_object=response_object, |
| 1769 | response_body=response_body) |
| 1770 | |
| 1771 | return response_body.get('body', "") |
| 1772 | |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1773 | @staticmethod |
| 1774 | def strip_extra_slashes(): |
| 1775 | path = request.environ['PATH_INFO'] |
| 1776 | trailing = ("", "/")[path[-1] == '/'] |
CamVan Nguyen | 249d132 | 2018-03-05 10:08:33 -0600 | [diff] [blame] | 1777 | parts = list(filter(bool, path.split('/'))) |
Brad Bishop | 87b63c1 | 2016-03-18 14:47:51 -0400 | [diff] [blame] | 1778 | request.environ['PATH_INFO'] = '/' + '/'.join(parts) + trailing |