Skip to content

Commit eefca80

Browse files
committed
Warn in user-data processing on non-multipart, non-handled data
If user-data is supplied that is not multipart, and is unhandled, then log a warning. A warning by default will get to the console, so the user can see it even if they cannot get into the instance. If they don't see it there, it would still be available in the cloud-init log.
2 parents 430f0fc + c1a006b commit eefca80

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- DataSourceCloudStack: add support for CloudStack datasource [Cosmin Luta]
3737
- add option 'apt_pipelining' to address issue with S3 mirrors
3838
(LP: #948461) [Ben Howard]
39+
- warn on non-multipart, non-handled user-data [Martin Packman]
3940
0.6.2:
4041
- fix bug where update was not done unless update was explicitly set.
4142
It would not be run if 'upgrade' or packages were set to be installed

cloudinit/UserDataHandler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def process_includes(msg, appendmsg=None):
180180

181181
payload = part.get_payload(decode=True)
182182

183-
if ctype_orig == "text/plain":
183+
if ctype_orig in ("text/plain", "text/x-not-multipart"):
184184
ctype = type_from_startswith(payload)
185185

186186
if ctype is None:
@@ -213,7 +213,7 @@ def message_from_string(data, headers=None):
213213
else:
214214
msg[key] = val
215215
else:
216-
mtype = headers.get("Content-Type", "text/plain")
216+
mtype = headers.get("Content-Type", "text/x-not-multipart")
217217
maintype, subtype = mtype.split("/", 1)
218218
msg = MIMEBase(maintype, subtype, *headers)
219219
msg.set_payload(data)

cloudinit/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,14 @@ def partwalker_callback(pdata, ctype, filename, payload):
606606
partwalker_handle_handler(pdata, ctype, filename, payload)
607607
return
608608
if ctype not in pdata['handlers']:
609+
if ctype == "text/x-not-multipart":
610+
# Extract the first line or 24 bytes for displaying in the log
611+
start = payload.split("\n", 1)[0][:24]
612+
if start < payload:
613+
details = "starting '%s...'" % start.encode("string-escape")
614+
else:
615+
details = repr(payload)
616+
log.warning("Unhandled non-multipart userdata %s", details)
609617
return
610618
handler_handle_part(pdata['handlers'][ctype], pdata['data'],
611619
ctype, filename, payload, pdata['frequency'])

tests/unittests/test_userdata.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""Tests for handling of userdata within cloud init"""
2+
3+
import logging
4+
import StringIO
5+
6+
from email.mime.base import MIMEBase
7+
8+
from mocker import MockerTestCase
9+
10+
import cloudinit
11+
from cloudinit.DataSource import DataSource
12+
13+
14+
instance_id = "i-testing"
15+
16+
17+
class FakeDataSource(DataSource):
18+
19+
def __init__(self, userdata):
20+
self.metadata = {'instance-id': instance_id}
21+
self.userdata_raw = userdata
22+
23+
24+
class TestConsumeUserData(MockerTestCase):
25+
26+
def setUp(self):
27+
self.mock_write = self.mocker.replace("cloudinit.util.write_file",
28+
passthrough=False)
29+
self.mock_write(self.get_ipath("cloud_config"), "", 0600)
30+
self.capture_log()
31+
32+
def tearDown(self):
33+
self._log.removeHandler(self._log_handler)
34+
35+
@staticmethod
36+
def get_ipath(name):
37+
return "%s/instances/%s%s" % (cloudinit.varlibdir, instance_id,
38+
cloudinit.pathmap[name])
39+
40+
def capture_log(self):
41+
self.log_file = StringIO.StringIO()
42+
self._log_handler = logging.StreamHandler(self.log_file)
43+
self._log_handler.setLevel(logging.DEBUG)
44+
self._log = logging.getLogger(cloudinit.logger_name)
45+
self._log.addHandler(self._log_handler)
46+
47+
def test_unhandled_type_warning(self):
48+
"""Raw text without magic is ignored but shows warning"""
49+
self.mocker.replay()
50+
ci = cloudinit.CloudInit()
51+
ci.datasource = FakeDataSource("arbitrary text\n")
52+
ci.consume_userdata()
53+
self.assertEqual(
54+
"Unhandled non-multipart userdata starting 'arbitrary text...'\n",
55+
self.log_file.getvalue())
56+
57+
def test_mime_text_plain(self):
58+
"""Mime message of type text/plain is ignored without warning"""
59+
self.mocker.replay()
60+
ci = cloudinit.CloudInit()
61+
message = MIMEBase("text", "plain")
62+
message.set_payload("Just text")
63+
ci.datasource = FakeDataSource(message.as_string())
64+
ci.consume_userdata()
65+
self.assertEqual("", self.log_file.getvalue())
66+
67+
def test_shellscript(self):
68+
"""Raw text starting #!/bin/sh is treated as script"""
69+
script = "#!/bin/sh\necho hello\n"
70+
outpath = cloudinit.get_ipath_cur("scripts") + "/part-001"
71+
self.mock_write(outpath, script, 0700)
72+
self.mocker.replay()
73+
ci = cloudinit.CloudInit()
74+
ci.datasource = FakeDataSource(script)
75+
ci.consume_userdata()
76+
self.assertEqual("", self.log_file.getvalue())
77+
78+
def test_mime_text_x_shellscript(self):
79+
"""Mime message of type text/x-shellscript is treated as script"""
80+
script = "#!/bin/sh\necho hello\n"
81+
outpath = cloudinit.get_ipath_cur("scripts") + "/part-001"
82+
self.mock_write(outpath, script, 0700)
83+
self.mocker.replay()
84+
ci = cloudinit.CloudInit()
85+
message = MIMEBase("text", "x-shellscript")
86+
message.set_payload(script)
87+
ci.datasource = FakeDataSource(message.as_string())
88+
ci.consume_userdata()
89+
self.assertEqual("", self.log_file.getvalue())
90+
91+
def test_mime_text_plain_shell(self):
92+
"""Mime type text/plain starting #!/bin/sh is treated as script"""
93+
script = "#!/bin/sh\necho hello\n"
94+
outpath = cloudinit.get_ipath_cur("scripts") + "/part-001"
95+
self.mock_write(outpath, script, 0700)
96+
self.mocker.replay()
97+
ci = cloudinit.CloudInit()
98+
message = MIMEBase("text", "plain")
99+
message.set_payload(script)
100+
ci.datasource = FakeDataSource(message.as_string())
101+
ci.consume_userdata()
102+
self.assertEqual("", self.log_file.getvalue())

0 commit comments

Comments
 (0)