Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip junk after JSON return. #15822

Merged
merged 1 commit into from
May 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 40 additions & 12 deletions lib/ansible/plugins/action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def _fixup_perms(self, remote_path, remote_user, execute=False, recursive=True):
# contain a path to a tmp dir but doesn't know if it needs to
# exist or not. If there's no path, then there's no need for us
# to do work
self._display.debug('_fixup_perms called with remote_path==None. Sure this is correct?')
display.debug('_fixup_perms called with remote_path==None. Sure this is correct?')
return remote_path

if self._play_context.become and self._play_context.become_user not in ('root', remote_user):
Expand Down Expand Up @@ -360,7 +360,7 @@ def _fixup_perms(self, remote_path, remote_user, execute=False, recursive=True):
if C.ALLOW_WORLD_READABLE_TMPFILES:
# fs acls failed -- do things this insecure way only
# if the user opted in in the config file
self._display.warning('Using world-readable permissions for temporary files Ansible needs to create when becoming an unprivileged user which may be insecure. For information on securing this, see https://docs.ansible.com/ansible/become.html#becoming-an-unprivileged-user')
display.warning('Using world-readable permissions for temporary files Ansible needs to create when becoming an unprivileged user which may be insecure. For information on securing this, see https://docs.ansible.com/ansible/become.html#becoming-an-unprivileged-user')
res = self._remote_chmod('a+%s' % mode, remote_path, recursive=recursive)
if res['rc'] != 0:
raise AnsibleError('Failed to set file mode on remote files (rc: {0}, err: {1})'.format(res['rc'], res['stderr']))
Expand Down Expand Up @@ -480,21 +480,49 @@ def _remote_expand_user(self, path):
else:
return initial_fragment

def _filter_leading_non_json_lines(self, data):
@staticmethod
def _filter_non_json_lines(data):
'''
Used to avoid random output from SSH at the top of JSON output, like messages from
tcagetattr, or where dropbear spews MOTD on every single command (which is nuts).

need to filter anything which starts not with '{', '[', ', '=' or is an empty line.
filter only leading lines since multiline JSON is valid.
need to filter anything which does not start with '{', '[', or is an empty line.
Have to be careful how we filter trailing junk as multiline JSON is valid.
'''
idx = 0
for line in data.splitlines(True):
if line.startswith((u'{', u'[')):
# Filter initial junk
lines = data.splitlines()
for start, line in enumerate(lines):
line = line.strip()
if line.startswith(u'{'):
endchar = u'}'
break
idx = idx + len(line)
elif line.startswith(u'['):
endchar = u']'
break
else:
display.debug('No start of json char found')
raise ValueError('No start of json char found')

# Filter trailing junk
lines = lines[start:]
lines.reverse()
for end, line in enumerate(lines):
if line.strip().endswith(endchar):
break
else:
display.debug('No end of json char found')
raise ValueError('No end of json char found')

if end < len(lines) - 1:
# Trailing junk is uncommon and can point to things the user might
# want to change. So print a warning if we find any
trailing_junk = lines[:end]
trailing_junk.reverse()
display.warning('Module invocation had junk after the JSON data: %s' % '\n'.join(trailing_junk))

return data[idx:]
lines = lines[end:]
lines.reverse()
return '\n'.join(lines)

def _strip_success_message(self, data):
'''
Expand Down Expand Up @@ -539,7 +567,7 @@ def _execute_module(self, module_name=None, module_args=None, tmp=None, task_var
module_args['_ansible_diff'] = self._play_context.diff

# let module know our verbosity
module_args['_ansible_verbosity'] = self._display.verbosity
module_args['_ansible_verbosity'] = display.verbosity

(module_style, shebang, module_data) = self._configure_module(module_name=module_name, module_args=module_args, task_vars=task_vars)
if not shebang:
Expand Down Expand Up @@ -627,7 +655,7 @@ def _execute_module(self, module_name=None, module_args=None, tmp=None, task_var

def _parse_returned_data(self, res):
try:
data = json.loads(self._filter_leading_non_json_lines(res.get('stdout', u'')))
data = json.loads(self._filter_non_json_lines(res.get('stdout', u'')))
except ValueError:
# not valid json, lets try to capture error
data = dict(failed=True, parsed=False)
Expand Down
41 changes: 41 additions & 0 deletions test/units/plugins/action/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
except ImportError:
import __builtin__ as builtins

from nose.tools import eq_, raises

from ansible.release import __version__ as ansible_version
from ansible import constants as C
from ansible.compat.six import text_type
Expand Down Expand Up @@ -630,3 +632,42 @@ def test_action_base_sudo_only_if_user_differs(self):
play_context.make_become_cmd.assert_called_once_with("ECHO SAME", executable=None)
finally:
C.BECOME_ALLOW_SAME_USER = become_allow_same_user

# Note: Using nose's generator test cases here so we can't inherit from
# unittest.TestCase
class TestFilterNonJsonLines(object):
parsable_cases = (
(u'{"hello": "world"}', u'{"hello": "world"}'),
(u'{"hello": "world"}\n', u'{"hello": "world"}'),
(u'{"hello": "world"} ', u'{"hello": "world"} '),
(u'{"hello": "world"} \n', u'{"hello": "world"} '),
(u'Message of the Day\n{"hello": "world"}', u'{"hello": "world"}'),
(u'{"hello": "world"}\nEpilogue', u'{"hello": "world"}'),
(u'Several\nStrings\nbefore\n{"hello": "world"}\nAnd\nAfter\n', u'{"hello": "world"}'),
(u'{"hello": "world",\n"olá": "mundo"}', u'{"hello": "world",\n"olá": "mundo"}'),
(u'\nPrecedent\n{"hello": "world",\n"olá": "mundo"}\nAntecedent', u'{"hello": "world",\n"olá": "mundo"}'),
)

unparsable_cases = (
u'No json here',
u'"olá": "mundo"',
u'{"No json": "ending"',
u'{"wrong": "ending"]',
u'["wrong": "ending"}',
)

def check_filter_non_json_lines(self, stdout_line, parsed):
eq_(parsed, ActionBase._filter_non_json_lines(stdout_line))

def test_filter_non_json_lines(self):
for stdout_line, parsed in self.parsable_cases:
yield self.check_filter_non_json_lines, stdout_line, parsed

@raises(ValueError)
def check_unparsable_filter_non_json_lines(self, stdout_line):
ActionBase._filter_non_json_lines(stdout_line)

def test_unparsable_filter_non_json_lines(self):
for stdout_line in self.unparsable_cases:
yield self.check_unparsable_filter_non_json_lines, stdout_line