From 145256f964388423965bc77c2a780a89ed62dc2e Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 21 Jun 2016 16:27:02 -0400 Subject: user_data: fix error when user-data is not utf-8 decodable when user-data was not decodable, cloud-init would raise exception. LP: #1532072 --- ChangeLog | 1 + cloudinit/user_data.py | 28 ++++++++++++++++------------ tests/unittests/test_data.py | 12 ++++++++++++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8edf3249..fa5e7df4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -125,6 +125,7 @@ - Remove trailing dot from GCE metadata URL (LP: #1581200) [Phil Roche] - support network rendering to sysconfig (for centos and RHEL) - write_files: if no permissions are given, just use default without warn. + - user_data: fix error when user-data is not utf-8 decodable (LP: #1532072) 0.7.6: - open 0.7.6 diff --git a/cloudinit/user_data.py b/cloudinit/user_data.py index f0631906..393bf0bb 100644 --- a/cloudinit/user_data.py +++ b/cloudinit/user_data.py @@ -334,19 +334,23 @@ def is_skippable(part): # Coverts a raw string into a mime message -def convert_string(raw_data, headers=None): +def convert_string(raw_data, content_type=NOT_MULTIPART_TYPE): if not raw_data: raw_data = '' - if not headers: - headers = {} - data = util.decode_binary(util.decomp_gzip(raw_data)) - if "mime-version:" in data[0:4096].lower(): - msg = util.message_from_string(data) - for (key, val) in headers.items(): - _replace_header(msg, key, val) - else: - mtype = headers.get(CONTENT_TYPE, NOT_MULTIPART_TYPE) - maintype, subtype = mtype.split("/", 1) - msg = MIMEBase(maintype, subtype, *headers) + + def create_binmsg(data, content_type): + maintype, subtype = content_type.split("/", 1) + msg = MIMEBase(maintype, subtype) msg.set_payload(data) + return msg + + try: + data = util.decode_binary(util.decomp_gzip(raw_data)) + if "mime-version:" in data[0:4096].lower(): + msg = util.message_from_string(data) + else: + msg = create_binmsg(data, content_type) + except UnicodeDecodeError: + msg = create_binmsg(raw_data, content_type) + return msg diff --git a/tests/unittests/test_data.py b/tests/unittests/test_data.py index 1923e2af..17951117 100644 --- a/tests/unittests/test_data.py +++ b/tests/unittests/test_data.py @@ -557,3 +557,15 @@ class TestUDProcess(helpers.ResourceUsingTestCase): ud_proc = ud.UserDataProcessor(self.getCloudPaths()) message = ud_proc.process(msg) self.assertTrue(count_messages(message) == 1) + + +class TestConvert(helpers.TestCase): + def test_handles_binary(self): + blob = b'\x32\x99' + msg = ud.convert_string(blob) + self.assertEqual(blob, msg.get_payload(decode=True)) + + def test_handle_headers(self): + text = "hi mom" + msg = ud.convert_string(text) + self.assertEqual(text, msg.get_payload(decode=False)) -- cgit v1.2.3 From 7a39cb35f54be3d91ab0985a96fcb3663f20124c Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Wed, 22 Jun 2016 10:07:12 -0400 Subject: rename test class, add a test, improve test names --- tests/unittests/test_data.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/unittests/test_data.py b/tests/unittests/test_data.py index 17951117..13db8a4c 100644 --- a/tests/unittests/test_data.py +++ b/tests/unittests/test_data.py @@ -559,12 +559,17 @@ class TestUDProcess(helpers.ResourceUsingTestCase): self.assertTrue(count_messages(message) == 1) -class TestConvert(helpers.TestCase): - def test_handles_binary(self): +class TestConvertString(helpers.TestCase): + def test_handles_binary_non_utf8_decodable(self): blob = b'\x32\x99' msg = ud.convert_string(blob) self.assertEqual(blob, msg.get_payload(decode=True)) + def test_handles_binary_utf8_decodable(self): + blob = b'\x32\x32' + msg = ud.convert_string(blob) + self.assertEqual(blob, msg.get_payload(decode=True)) + def test_handle_headers(self): text = "hi mom" msg = ud.convert_string(text) -- cgit v1.2.3