From 322e22583018a8f775c79baaa15021d799eb564e Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 24 Sep 2019 21:45:53 -0400 Subject: [PATCH] Fix issue when setting an empty pass to no_log param (#62804) * Fix issue when setting an empty pass to no_log param * Fix typo --- .../ansible_basic_no_log_empty_string.yaml | 2 + .../module_utils/csharp/Ansible.Basic.cs | 5 ++- .../library/ansible_basic_tests.ps1 | 42 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/ansible_basic_no_log_empty_string.yaml diff --git a/changelogs/fragments/ansible_basic_no_log_empty_string.yaml b/changelogs/fragments/ansible_basic_no_log_empty_string.yaml new file mode 100644 index 00000000000..7da15a2bbd9 --- /dev/null +++ b/changelogs/fragments/ansible_basic_no_log_empty_string.yaml @@ -0,0 +1,2 @@ +bugfixes: +- Ansible.Basic - Fix issue when setting a ``no_log`` parameter to an empty string - https://github.com/ansible/ansible/issues/62613 diff --git a/lib/ansible/module_utils/csharp/Ansible.Basic.cs b/lib/ansible/module_utils/csharp/Ansible.Basic.cs index d1011c860ff..629e4fdcf26 100644 --- a/lib/ansible/module_utils/csharp/Ansible.Basic.cs +++ b/lib/ansible/module_utils/csharp/Ansible.Basic.cs @@ -700,8 +700,9 @@ namespace Ansible.Basic if ((bool)v["no_log"]) { object noLogObject = parameters.Contains(k) ? parameters[k] : null; - if (noLogObject != null) - noLogValues.Add(noLogObject.ToString()); + string noLogString = noLogObject == null ? "" : noLogObject.ToString(); + if (!String.IsNullOrEmpty(noLogString)) + noLogValues.Add(noLogString); } object removedInVersion = v["removed_in_version"]; diff --git a/test/integration/targets/win_csharp_utils/library/ansible_basic_tests.ps1 b/test/integration/targets/win_csharp_utils/library/ansible_basic_tests.ps1 index bdd45f71ae3..bdbe6e4b976 100644 --- a/test/integration/targets/win_csharp_utils/library/ansible_basic_tests.ps1 +++ b/test/integration/targets/win_csharp_utils/library/ansible_basic_tests.ps1 @@ -710,6 +710,48 @@ test_no_log - Invoked with: $actual_event | Assert-DictionaryEquals -Expected $expected_event } + "No log value with an empty string" = { + $spec = @{ + options = @{ + password1 = @{type = "str"; no_log = $true} + password2 = @{type = "str"; no_log = $true} + } + } + $complex_args = @{ + _ansible_module_name = "test_no_log" + password1 = "" + } + + $m = [Ansible.Basic.AnsibleModule]::Create(@(), $spec) + $m.Result.data = $complex_args.dict + + # verify params internally aren't masked + $m.Params.password1 | Assert-Equals -Expected "" + $m.Params.password2 | Assert-Equals -Expected $null + + $failed = $false + try { + $m.ExitJson() + } catch [System.Management.Automation.RuntimeException] { + $failed = $true + $_.Exception.Message | Assert-Equals -Expected "exit: 0" + $actual = [Ansible.Basic.AnsibleModule]::FromJson($_.Exception.InnerException.Output) + } + $failed | Assert-Equals -Expected $true + + $expected = @{ + invocation = @{ + module_args = @{ + password1 = "" + password2 = $null + } + } + changed = $false + data = $complex_args.dict + } + $actual | Assert-DictionaryEquals -Expected $expected + } + "Removed in version" = { $spec = @{ options = @{