summaryrefslogtreecommitdiff
path: root/cloudinit/user_data.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2018-06-13 12:54:43 -0400
committerScott Moser <smoser@brickies.net>2018-06-13 12:54:43 -0400
commitfaa6f07e9de4058083a5f69ed508b6e24bd53b23 (patch)
treeadbe18f27c27fc01d1744633a09745810802b694 /cloudinit/user_data.py
parent171378613ef4de3c1bb4170a10ec1748ac62f39f (diff)
downloadcloud-init-git-faa6f07e9de4058083a5f69ed508b6e24bd53b23.tar.gz
Be more safe on string/bytes when writing multipart user-data to disk.
When creating the multipart mime message that is written as user-data.txt.i, cloud-init losing data on conversion to some things as a string. LP: #1768600 Author: Scott Moser <smoser@ubuntu.com> Co-Authored-By: Chad Smith <chad.smith@canonical.com>
Diffstat (limited to 'cloudinit/user_data.py')
-rw-r--r--cloudinit/user_data.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/cloudinit/user_data.py b/cloudinit/user_data.py
index 8f6aba1e..ed83d2d8 100644
--- a/cloudinit/user_data.py
+++ b/cloudinit/user_data.py
@@ -337,8 +337,10 @@ def is_skippable(part):
# Coverts a raw string into a mime message
def convert_string(raw_data, content_type=NOT_MULTIPART_TYPE):
+ """convert a string (more likely bytes) or a message into
+ a mime message."""
if not raw_data:
- raw_data = ''
+ raw_data = b''
def create_binmsg(data, content_type):
maintype, subtype = content_type.split("/", 1)
@@ -346,15 +348,17 @@ def convert_string(raw_data, content_type=NOT_MULTIPART_TYPE):
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)
+ if isinstance(raw_data, six.text_type):
+ bdata = raw_data.encode('utf-8')
+ else:
+ bdata = raw_data
+ bdata = util.decomp_gzip(bdata, decode=False)
+ if b"mime-version:" in bdata[0:4096].lower():
+ msg = util.message_from_string(bdata.decode('utf-8'))
+ else:
+ msg = create_binmsg(bdata, content_type)
return msg
+
# vi: ts=4 expandtab