From 865b30f3083305c5f2af46d0814bc44ee90b2b9f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alfonso=20Mart=C3=ADnez?= Date: Thu, 25 Feb 2021 15:55:06 +0100 Subject: [PATCH] nautilus: mgr/dashboard: python 2: error when setting user's non-ASCII password MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Fixes: https://tracker.ceph.com/issues/49278 Signed-off-by: Alfonso Martínez --- .../mgr/dashboard/services/access_control.py | 3 +++ .../mgr/dashboard/tests/test_access_control.py | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/pybind/mgr/dashboard/services/access_control.py b/src/pybind/mgr/dashboard/services/access_control.py index 16a31285f49b5..9ea7a01cd44f8 100644 --- a/src/pybind/mgr/dashboard/services/access_control.py +++ b/src/pybind/mgr/dashboard/services/access_control.py @@ -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: diff --git a/src/pybind/mgr/dashboard/tests/test_access_control.py b/src/pybind/mgr/dashboard/tests/test_access_control.py index edc36fb5aa66b..34452267558c7 100644 --- a/src/pybind/mgr/dashboard/tests/test_access_control.py +++ b/src/pybind/mgr/dashboard/tests/test_access_control.py @@ -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', -- 2.39.5