From d5d527dc0ffd07be00d16455a1f297b512edf3f3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alfonso=20Mart=C3=ADnez?= Date: Tue, 6 Apr 2021 09:42:27 +0200 Subject: [PATCH] nautilus: mgr/dashboard: python 2: fix error when setting non-ASCII password MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Fixes: https://tracker.ceph.com/issues/50155 Signed-off-by: Alfonso Martínez --- .../mgr/dashboard/services/access_control.py | 17 +++++++---------- .../mgr/dashboard/tests/test_access_control.py | 7 ++++--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/pybind/mgr/dashboard/services/access_control.py b/src/pybind/mgr/dashboard/services/access_control.py index fafc3789ab0a..4a09991a9e67 100644 --- a/src/pybind/mgr/dashboard/services/access_control.py +++ b/src/pybind/mgr/dashboard/services/access_control.py @@ -21,24 +21,21 @@ from ..exceptions import RoleAlreadyExists, RoleDoesNotExist, ScopeNotValid, \ RoleNotInUser -# replicates tools.ensure_str() to avoid recursive import: -# ..tools -> .services.auth -> .access_control -> ..tools -def ensure_str(s, encoding='utf-8', errors='strict'): +def ensure_text(s, encoding='utf-8', errors='strict'): """Ported from six.""" - if not isinstance(s, (six.text_type, six.binary_type)): + if isinstance(s, six.binary_type): + return s.decode(encoding, errors) + elif isinstance(s, six.text_type): + return s + else: raise TypeError("not expecting type '%s'" % type(s)) - if six.PY2 and isinstance(s, six.text_type): - s = s.encode(encoding, errors) - elif six.PY3 and isinstance(s, six.binary_type): - s = s.decode(encoding, errors) - return s # password hashing algorithm def password_hash(password, salt_password=None): if not password: return None - password = ensure_str(password) + password = ensure_text(password) if not salt_password: salt_password = bcrypt.gensalt() else: diff --git a/src/pybind/mgr/dashboard/tests/test_access_control.py b/src/pybind/mgr/dashboard/tests/test_access_control.py index 2ff48517ab9b..1f0f804a5fea 100644 --- a/src/pybind/mgr/dashboard/tests/test_access_control.py +++ b/src/pybind/mgr/dashboard/tests/test_access_control.py @@ -13,7 +13,8 @@ from mgr_module import ERROR_MSG_EMPTY_INPUT_FILE from . import CmdException, CLICommandTestMixin from .. import mgr from ..security import Scope, Permission -from ..services.access_control import load_access_control_db, \ +from ..services.access_control import ensure_text, \ + load_access_control_db, \ password_hash, AccessControlDB, \ SYSTEM_ROLES @@ -571,9 +572,9 @@ class AccessControlTest(unittest.TestCase, CLICommandTestMixin): def test_unicode_password(self): self.test_create_user() - password = '章鱼不是密码' + password = ensure_text('章鱼不是密码') user = self.exec_cmd('ac-user-set-password', username='admin', - inbuf=password.encode(), force_password=True) + inbuf=password.encode('utf-8'), force_password=True) pass_hash = password_hash(password, user['password']) self.assertEqual(user['password'], pass_hash) -- 2.47.3