From 6fd59d1bd2b909f66546389a94d1d08968536bde Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Fri, 26 Jan 2018 12:12:16 +0000 Subject: [PATCH] mgr/dashboard_v2: Moved set-login-cred command logic to Auth class Signed-off-by: Ricardo Dias --- src/pybind/mgr/dashboard_v2/controllers/auth.py | 6 ++++++ src/pybind/mgr/dashboard_v2/module.py | 4 +--- src/pybind/mgr/dashboard_v2/tests/test_auth.py | 13 +++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/pybind/mgr/dashboard_v2/controllers/auth.py b/src/pybind/mgr/dashboard_v2/controllers/auth.py index 36ce70be587..b36353053e8 100644 --- a/src/pybind/mgr/dashboard_v2/controllers/auth.py +++ b/src/pybind/mgr/dashboard_v2/controllers/auth.py @@ -88,3 +88,9 @@ class Auth(RESTController): 'Session expired. You are not ' 'authorized to access that resource') cherrypy.session[Auth.SESSION_KEY_TS] = now + + @staticmethod + def set_login_credentials(username, password): + Auth._mgr_module_.set_localized_config('username', username) + hashed_passwd = Auth.password_hash(password) + Auth._mgr_module_.set_localized_config('password', hashed_passwd) diff --git a/src/pybind/mgr/dashboard_v2/module.py b/src/pybind/mgr/dashboard_v2/module.py index a9cca611b38..14938534d9f 100644 --- a/src/pybind/mgr/dashboard_v2/module.py +++ b/src/pybind/mgr/dashboard_v2/module.py @@ -86,9 +86,7 @@ class Module(MgrModule): def handle_command(self, cmd): if cmd['prefix'] == 'dashboard set-login-credentials': - self.set_localized_config('username', cmd['username']) - hashed_passwd = Auth.password_hash(cmd['password']) - self.set_localized_config('password', hashed_passwd) + Auth.set_login_credentials(cmd['username'], cmd['password']) return 0, 'Username and password updated', '' return (-errno.EINVAL, '', 'Command not found \'{0}\'' diff --git a/src/pybind/mgr/dashboard_v2/tests/test_auth.py b/src/pybind/mgr/dashboard_v2/tests/test_auth.py index 1d57eba406d..8b32805fa74 100644 --- a/src/pybind/mgr/dashboard_v2/tests/test_auth.py +++ b/src/pybind/mgr/dashboard_v2/tests/test_auth.py @@ -32,6 +32,19 @@ class AuthTest(ControllerTestCase): cls._mgr_module.set_localized_config('password', Auth.password_hash('admin')) + def setUp(self): + self._mgr_module.set_localized_config('session-expire', '2') + self._mgr_module.set_localized_config('username', 'admin') + self._mgr_module.set_localized_config('password', + Auth.password_hash('admin')) + + def test_a_set_login_credentials(self): + Auth.set_login_credentials('admin2', 'admin2') + user = self._mgr_module.get_localized_config('username') + passwd = self._mgr_module.get_localized_config('password') + self.assertEqual(user, 'admin2') + self.assertEqual(passwd, Auth.password_hash('admin2', passwd)) + def test_login_valid(self): sess_mock = RamSession() with patch('cherrypy.session', sess_mock, create=True): -- 2.39.5