From ba59e7c45018c6e3c76e7a1166861e078f247fac Mon Sep 17 00:00:00 2001 From: Yoon Hong Date: Wed, 4 Jan 2017 17:52:07 -0800 Subject: [PATCH 1/2] Fix password encrypting failure in python 2.* (#516) --- azurelinuxagent/common/utils/textutil.py | 3 +++ tests/utils/test_passwords.txt | 4 ++++ tests/utils/test_text_util.py | 12 ++++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 tests/utils/test_passwords.txt diff --git a/azurelinuxagent/common/utils/textutil.py b/azurelinuxagent/common/utils/textutil.py index 59b8fe7..c683a13 100644 --- a/azurelinuxagent/common/utils/textutil.py +++ b/azurelinuxagent/common/utils/textutil.py @@ -267,6 +267,9 @@ def gen_password_hash(password, crypt_id, salt_len): collection = string.ascii_letters + string.digits salt = ''.join(random.choice(collection) for _ in range(salt_len)) salt = "${0}${1}".format(crypt_id, salt) + if sys.version_info[0] == 2: + # if python 2.*, encode to type 'str' to prevent Unicode Encode Error from crypt.crypt + password = password.encode('utf-8') return crypt.crypt(password, salt) diff --git a/tests/utils/test_passwords.txt b/tests/utils/test_passwords.txt new file mode 100644 index 0000000..0d995ef --- /dev/null +++ b/tests/utils/test_passwords.txt @@ -0,0 +1,4 @@ +김치 +करी +hamburger +café \ No newline at end of file diff --git a/tests/utils/test_text_util.py b/tests/utils/test_text_util.py index dc3de85..77e89de 100644 --- a/tests/utils/test_text_util.py +++ b/tests/utils/test_text_util.py @@ -23,12 +23,16 @@ from azurelinuxagent.common.future import ustr import azurelinuxagent.common.utils.textutil as textutil from azurelinuxagent.common.utils.textutil import Version + class TestTextUtil(AgentTestCase): def test_get_password_hash(self): - password_hash = textutil.gen_password_hash("asdf", 6, 10) - self.assertNotEquals(None, password_hash) - password_hash = textutil.gen_password_hash("asdf", 6, 0) - self.assertNotEquals(None, password_hash) + with open('./test_passwords.txt', 'rb') as in_file: + for data in in_file: + # Remove bom on bytes data before it is converted into string. + data = textutil.remove_bom(data) + data = ustr(data, encoding='utf-8') + password_hash = textutil.gen_password_hash(data, 6, 10) + self.assertNotEquals(None, password_hash) def test_remove_bom(self): #Test bom could be removed From e0eb4c9d4ff72879c05cf042c7938a82b22c9be8 Mon Sep 17 00:00:00 2001 From: Yoon Hong Date: Wed, 4 Jan 2017 21:14:18 -0800 Subject: [PATCH 2/2] Fix file not found issue --- tests/utils/test_text_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utils/test_text_util.py b/tests/utils/test_text_util.py index 77e89de..6f204c7 100644 --- a/tests/utils/test_text_util.py +++ b/tests/utils/test_text_util.py @@ -26,7 +26,7 @@ from azurelinuxagent.common.utils.textutil import Version class TestTextUtil(AgentTestCase): def test_get_password_hash(self): - with open('./test_passwords.txt', 'rb') as in_file: + with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_passwords.txt'), 'rb') as in_file: for data in in_file: # Remove bom on bytes data before it is converted into string. data = textutil.remove_bom(data)