From fa9e93be3c36b33f72de1a445bd0cd5609002de6 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Mon, 29 Jan 2018 09:38:03 +0000 Subject: [PATCH] mgr/dashboard_v2: Refactoring of the access to mgr module instance Signed-off-by: Ricardo Dias --- .../mgr/dashboard_v2/controllers/auth.py | 26 +++++++------------ src/pybind/mgr/dashboard_v2/tools.py | 3 ++- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/pybind/mgr/dashboard_v2/controllers/auth.py b/src/pybind/mgr/dashboard_v2/controllers/auth.py index 8829c9c3f55..246314f6d3d 100644 --- a/src/pybind/mgr/dashboard_v2/controllers/auth.py +++ b/src/pybind/mgr/dashboard_v2/controllers/auth.py @@ -30,31 +30,26 @@ class Auth(RESTController): DEFAULT_SESSION_EXPIRE = 1200.0 - def __init__(self): - # pylint: disable=E1101 - self._mod = Auth._mgr_module_ - self._log = self._mod.log - @RESTController.args_from_json def create(self, username, password): now = time.time() - config_username = self._mod.get_localized_config('username', None) - config_password = self._mod.get_localized_config('password', None) + config_username = self.mgr.get_localized_config('username', None) + config_password = self.mgr.get_localized_config('password', None) hash_password = Auth.password_hash(password, config_password) if username == config_username and hash_password == config_password: cherrypy.session.regenerate() cherrypy.session[Auth.SESSION_KEY] = username cherrypy.session[Auth.SESSION_KEY_TS] = now - self._log.debug('Login successful') + self.logger.debug('Login successful') return {'username': username} cherrypy.response.status = 403 - self._log.debug('Login failed') + self.logger.debug('Login failed') return {'detail': 'Invalid credentials'} def bulk_delete(self): - self._log.debug('Logout successful') + self.logger.debug('Logout successful') cherrypy.session[Auth.SESSION_KEY] = None cherrypy.session[Auth.SESSION_KEY_TS] = None @@ -68,22 +63,21 @@ class Auth(RESTController): @staticmethod def check_auth(): - module = Auth._mgr_module_ username = cherrypy.session.get(Auth.SESSION_KEY) if not username: - module.log.debug('Unauthorized access to {}'.format(cherrypy.url( + Auth.logger.debug('Unauthorized access to {}'.format(cherrypy.url( relative='server'))) raise cherrypy.HTTPError(401, 'You are not authorized to access ' 'that resource') now = time.time() - expires = float(module.get_localized_config( + expires = float(Auth.mgr.get_localized_config( 'session-expire', Auth.DEFAULT_SESSION_EXPIRE)) if expires > 0: username_ts = cherrypy.session.get(Auth.SESSION_KEY_TS, None) if username_ts and float(username_ts) < (now - expires): cherrypy.session[Auth.SESSION_KEY] = None cherrypy.session[Auth.SESSION_KEY_TS] = None - module.log.debug('Session expired') + Auth.logger.debug('Session expired') raise cherrypy.HTTPError(401, 'Session expired. You are not ' 'authorized to access that resource') @@ -91,6 +85,6 @@ class Auth(RESTController): @staticmethod def set_login_credentials(username, password): - Auth._mgr_module_.set_localized_config('username', username) + Auth.mgr.set_localized_config('username', username) hashed_passwd = Auth.password_hash(password) - Auth._mgr_module_.set_localized_config('password', hashed_passwd) + Auth.mgr.set_localized_config('password', hashed_passwd) diff --git a/src/pybind/mgr/dashboard_v2/tools.py b/src/pybind/mgr/dashboard_v2/tools.py index 7394e658774..6cc8688db6d 100644 --- a/src/pybind/mgr/dashboard_v2/tools.py +++ b/src/pybind/mgr/dashboard_v2/tools.py @@ -57,7 +57,8 @@ def load_controllers(mgrmodule): for _, cls in mod.__dict__.items(): if isinstance(cls, type) and hasattr(cls, '_cp_controller_'): # found controller - cls._mgr_module_ = mgrmodule + cls.mgr = mgrmodule + cls.logger = mgrmodule.log controllers.append(cls) return controllers -- 2.39.5