]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
nautilus: mgr/dashboard: python 2: error when setting user's non-ASCII password 39441/head
authorAlfonso Martínez <almartin@redhat.com>
Thu, 25 Feb 2021 14:55:06 +0000 (15:55 +0100)
committerAlfonso Martínez <almartin@redhat.com>
Thu, 25 Feb 2021 14:55:06 +0000 (15:55 +0100)
Fixes: https://tracker.ceph.com/issues/49278
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 16a31285f49b5a90f2d019d859246d2595fabebc..9ea7a01cd44f888b7376a76181196b044fbebbf3 100644 (file)
@@ -7,6 +7,7 @@ import errno
 import json
 import threading
 import time
+import six
 
 import bcrypt
 
@@ -24,6 +25,8 @@ from ..exceptions import RoleAlreadyExists, RoleDoesNotExist, ScopeNotValid, \
 def password_hash(password, salt_password=None):
     if not password:
         return None
+    if six.PY2:
+        password = unicode(password, 'utf-8') if isinstance(password, str) else password
     if not salt_password:
         salt_password = bcrypt.gensalt()
     else:
index edc36fb5aa66bdb8da8858d49f5d259289b64543..34452267558c73076c49da5b14340565470ea3d8 100644 (file)
@@ -4,6 +4,7 @@ from __future__ import absolute_import
 
 import errno
 import json
+import tempfile
 import time
 import unittest
 
@@ -559,7 +560,7 @@ class AccessControlTest(unittest.TestCase, CLICommandTestMixin):
     def test_sanitize_password(self):
         self.test_create_user()
         password = 'myPass\\n\\r\\n'
-        with open('/tmp/test_sanitize_password.txt', 'w+') as pwd_file:
+        with tempfile.TemporaryFile(mode='w+') as pwd_file:
             # Add new line separators (like some text editors when a file is saved).
             pwd_file.write('{}{}'.format(password, '\n\r\n\n'))
             pwd_file.seek(0)
@@ -568,6 +569,17 @@ class AccessControlTest(unittest.TestCase, CLICommandTestMixin):
             pass_hash = password_hash(password, user['password'])
             self.assertEqual(user['password'], pass_hash)
 
+    def test_unicode_password(self):
+        self.test_create_user()
+        password = '章鱼不是密码'
+        with tempfile.TemporaryFile(mode='w+') as pwd_file:
+            pwd_file.write(password)
+            pwd_file.seek(0)
+            user = self.exec_cmd('ac-user-set-password', username='admin',
+                                 inbuf=pwd_file.read(), force_password=True)
+            pass_hash = password_hash(password, user['password'])
+            self.assertEqual(user['password'], pass_hash)
+
     def test_set_user_password_nonexistent_user(self):
         with self.assertRaises(CmdException) as ctx:
             self.exec_cmd('ac-user-set-password', username='admin',