Allow no_log=False to silence the no_log warnings for module parameters (#64733)

As AnsibleModule._log_invocation is currently implemented, any parameter
with a name that matches PASSWORD_MATCH triggers the no_log warning as a
precaution against parameters that may contain sensitive data, but have not
been marked as sensitive by the module author.

This patch would allow module authors to explicitly mark the aforementioned
parameters as not sensitive thereby bypassing an erroneous warning message,
while still catching parameters which have not been marked at all by the
author.

Adds tests for various no_log states including True, False, and None (as
extracted by AnsibleModule._log_invocation) when applied to an argument with
a name that matches PASSWORD_MATCH.

Fixes: #49465 #64656
This commit is contained in:
kaorihinata
2020-01-09 16:47:57 -05:00
committed by Sam Doran
parent 93bfa4b07a
commit 3ca4580cb4
5 changed files with 50 additions and 8 deletions

View File

@ -593,3 +593,40 @@ class TestLoadFileCommonArguments:
res = am.load_file_common_arguments(params=extended_params)
assert res == final_params
@pytest.mark.parametrize("stdin", [{"arg_pass": "testing"}], indirect=["stdin"])
def test_no_log_true(stdin, capfd):
"""Explicitly mask an argument (no_log=True)."""
arg_spec = {
"arg_pass": {"no_log": True}
}
am = basic.AnsibleModule(arg_spec)
# no_log=True is picked up by both am._log_invocation and list_no_log_values
# (called by am._handle_no_log_values). As a result, we can check for the
# value in am.no_log_values.
assert "testing" in am.no_log_values
@pytest.mark.parametrize("stdin", [{"arg_pass": "testing"}], indirect=["stdin"])
def test_no_log_false(stdin, capfd):
"""Explicitly log and display an argument (no_log=False)."""
arg_spec = {
"arg_pass": {"no_log": False}
}
am = basic.AnsibleModule(arg_spec)
assert "testing" not in am.no_log_values and not am._warnings
@pytest.mark.parametrize("stdin", [{"arg_pass": "testing"}], indirect=["stdin"])
def test_no_log_none(stdin, capfd):
"""Allow Ansible to make the decision by matching the argument name
against PASSWORD_MATCH."""
arg_spec = {
"arg_pass": {}
}
am = basic.AnsibleModule(arg_spec)
# Omitting no_log is only picked up by _log_invocation, so the value never
# makes it into am.no_log_values. Instead we can check for the warning
# emitted by am._log_invocation.
assert len(am._warnings) > 0