From 151d83efc3e32339d3397d9ec8e05e34b93e1a62 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 3 Mar 2020 11:17:58 +1000 Subject: [PATCH] win_credential - fix wildcard name (#67549) (#67552) (cherry picked from commit d7059881a264dcadc16ddbc264f96aa9cbf8020c) --- .../fragments/win_credential-wildcard.yaml | 2 + .../modules/windows/win_credential.ps1 | 8 ++-- .../targets/win_credential/tasks/tests.yml | 46 +++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/win_credential-wildcard.yaml diff --git a/changelogs/fragments/win_credential-wildcard.yaml b/changelogs/fragments/win_credential-wildcard.yaml new file mode 100644 index 00000000000..a27b5842365 --- /dev/null +++ b/changelogs/fragments/win_credential-wildcard.yaml @@ -0,0 +1,2 @@ +bugfixes: +- win_credential - Fix issue that errors when trying to add a ``name`` with wildcards. diff --git a/lib/ansible/modules/windows/win_credential.ps1 b/lib/ansible/modules/windows/win_credential.ps1 index a16334024af..fdc83584f0f 100644 --- a/lib/ansible/modules/windows/win_credential.ps1 +++ b/lib/ansible/modules/windows/win_credential.ps1 @@ -378,8 +378,10 @@ namespace Ansible.CredentialManager using (SafeMemoryBuffer attributes = new SafeMemoryBuffer(attributeBytes.Length)) { if (attributeBytes.Length != 0) + { Marshal.Copy(attributeBytes, 0, attributes.DangerousGetHandle(), attributeBytes.Length); - credential.Attributes = attributes.DangerousGetHandle(); + credential.Attributes = attributes.DangerousGetHandle(); + } NativeHelpers.CredentialCreateFlags createFlags = 0; if (preserveExisting) @@ -588,10 +590,10 @@ if ($state -eq "absent") { $new_credential = New-Object -TypeName Ansible.CredentialManager.Credential $new_credential.Type = $type $new_credential.TargetName = $name - $new_credential.Comment = $comment + $new_credential.Comment = if ($comment) { $comment } else { [NullString]::Value } $new_credential.Secret = $secret $new_credential.Persist = $persistence - $new_credential.TargetAlias = $alias + $new_credential.TargetAlias = if ($alias) { $alias } else { [NullString]::Value } $new_credential.UserName = $username if ($null -ne $attributes) { diff --git a/test/integration/targets/win_credential/tasks/tests.yml b/test/integration/targets/win_credential/tasks/tests.yml index f671c3c494e..cec2cf0236e 100644 --- a/test/integration/targets/win_credential/tasks/tests.yml +++ b/test/integration/targets/win_credential/tasks/tests.yml @@ -366,6 +366,52 @@ that: - not remove_cred_again is changed +# https://github.com/ansible/ansible/issues/67278 +- name: create credential with wildcard + win_credential: + name: '*.{{ test_hostname }}' + type: domain_password + username: DOMAIN\username + secret: password + state: present + persistence: enterprise + register: wildcard_cred + vars: *become_vars + +- name: get result of create credential with wildcard + test_cred_facts: + name: '*.{{ test_hostname }}' + type: domain_password + register: wildcard_cred_actual + vars: *become_vars + +- name: assert create credential with wildcard + assert: + that: + - wildcard_cred is changed + - wildcard_cred_actual.name == '*.' ~ test_hostname + +- name: remove credential with wildcard + win_credential: + name: '*.{{ test_hostname }}' + type: domain_password + state: absent + register: wildcard_remove + vars: *become_vars + +- name: get result of remove credential with wildcard + test_cred_facts: + name: '*.{{ test_hostname }}' + type: domain_password + register: wildcard_remove_actual + vars: *become_vars + +- name: assert remove credential with wildcard + assert: + that: + - wildcard_remove is changed + - not wildcard_remove_actual.exists + - name: create generic password (check mode) win_credential: name: '{{ test_hostname }}'