]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
nautilus: mgr/dashboard: python 2: fix error when setting non-ASCII password 40610/head
authorAlfonso Martínez <almartin@redhat.com>
Tue, 6 Apr 2021 07:42:27 +0000 (09:42 +0200)
committerAlfonso Martínez <almartin@redhat.com>
Tue, 6 Apr 2021 07:42:27 +0000 (09:42 +0200)
Fixes: https://tracker.ceph.com/issues/50155
Signed-off-by: Alfonso Martínez <almartin@redhat.com>
src/pybind/mgr/dashboard/services/access_control.py
src/pybind/mgr/dashboard/tests/test_access_control.py

index fafc3789ab0af8fa672262c98d2c92144ee095c6..4a09991a9e67cba48c7f992d6efc1450d3b794a7 100644 (file)
@@ -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:
index 2ff48517ab9bbc7d8ff3d667fc94fb8b86316d26..1f0f804a5fea30e170c1966eb1307ffc10b278a1 100644 (file)
@@ -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)