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