From 87ff98c95e8cac278b1e0ba6fa8ccb4f006e5b95 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Fri, 27 Apr 2018 13:39:07 +0200 Subject: [PATCH] mgr/dashboard: Be more selective when catching exceptions. - Do some minor improvements in the RGW client - Do not catch ALL exceptions to do not block the current exception handler that prints the stack trace in the log file - Throw RequestException istead of Exception when user does not have the required keys Signed-off-by: Volker Theile --- src/pybind/mgr/dashboard/controllers/rgw.py | 12 +++++++++--- src/pybind/mgr/dashboard/services/rgw_client.py | 10 +++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/pybind/mgr/dashboard/controllers/rgw.py b/src/pybind/mgr/dashboard/controllers/rgw.py index 05420473980ae..249df76addf71 100644 --- a/src/pybind/mgr/dashboard/controllers/rgw.py +++ b/src/pybind/mgr/dashboard/controllers/rgw.py @@ -8,6 +8,7 @@ from . import ApiController, BaseController, RESTController, AuthRequired from .. import logger from ..services.ceph_service import CephService from ..services.rgw_client import RgwClient +from ..rest_client import RequestException @ApiController('rgw') @@ -79,7 +80,7 @@ class RgwProxy(BaseController): data = cherrypy.request.body.read() return rgw_client.proxy(method, path, params, data) - except Exception as e: # pylint: disable=broad-except + except RequestException as e: # Always use status code 500 and NOT the status that may delivered # by the exception. That's because we do not want to forward e.g. # 401 or 404 that may trigger unwanted actions in the UI. @@ -93,5 +94,10 @@ class RgwProxy(BaseController): class RgwBucket(RESTController): def create(self, bucket, uid): - rgw_client = RgwClient.instance(uid) - return rgw_client.create_bucket(bucket) + try: + rgw_client = RgwClient.instance(uid) + return rgw_client.create_bucket(bucket) + except RequestException as e: + cherrypy.response.headers['Content-Type'] = 'application/json' + cherrypy.response.status = 500 + return {'detail': str(e)} diff --git a/src/pybind/mgr/dashboard/services/rgw_client.py b/src/pybind/mgr/dashboard/services/rgw_client.py index 0089e9f006f67..e34fb75edce80 100644 --- a/src/pybind/mgr/dashboard/services/rgw_client.py +++ b/src/pybind/mgr/dashboard/services/rgw_client.py @@ -118,7 +118,7 @@ class RgwClient(RestClient): logger.info("Creating new connection for user: %s", userid) keys = RgwClient.admin_instance().get_user_keys(userid) if not keys: - raise Exception( + raise RequestException( "User '{}' does not have any keys configured.".format( userid)) @@ -188,11 +188,11 @@ class RgwClient(RestClient): colon_idx = userid.find(':') user = userid if colon_idx == -1 else userid[:colon_idx] response = request({'uid': user}) - for keys in response['keys']: - if keys['user'] == userid: + for key in response['keys']: + if key['user'] == userid: return { - 'access_key': keys['access_key'], - 'secret_key': keys['secret_key'] + 'access_key': key['access_key'], + 'secret_key': key['secret_key'] } return None -- 2.39.5