diff options
author | Scott Moser <smoser@ubuntu.com> | 2016-06-21 16:27:02 -0400 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2016-06-21 16:27:02 -0400 |
commit | 145256f964388423965bc77c2a780a89ed62dc2e (patch) | |
tree | e2383b7269204172ca57f835f92bb3820654451a /cloudinit/user_data.py | |
parent | a919f6e6ee3bfaf489411a4452fc708061b1239f (diff) | |
download | vyos-cloud-init-145256f964388423965bc77c2a780a89ed62dc2e.tar.gz vyos-cloud-init-145256f964388423965bc77c2a780a89ed62dc2e.zip |
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
Diffstat (limited to 'cloudinit/user_data.py')
-rw-r--r-- | cloudinit/user_data.py | 28 |
1 files changed, 16 insertions, 12 deletions
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 |