From: Alfonso Martínez Date: Tue, 6 Apr 2021 07:42:27 +0000 (+0200) Subject: nautilus: mgr/dashboard: python 2: fix error when setting non-ASCII password X-Git-Tag: v14.2.22~27^2~4^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F40610%2Fhead;p=ceph.git nautilus: mgr/dashboard: python 2: fix error when setting non-ASCII password Fixes: https://tracker.ceph.com/issues/50155 Signed-off-by: Alfonso Martínez --- diff --git a/src/pybind/mgr/dashboard/services/access_control.py b/src/pybind/mgr/dashboard/services/access_control.py index fafc3789ab0..4a09991a9e6 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 2ff48517ab9..1f0f804a5fe 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)