Skip to content

Commit

Permalink
win_dns_client - Handle null chars in raw registry dns servers value (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jborean93 authored Nov 10, 2022
1 parent 9909fbc commit f049245
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/win_dns_client-null-chars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- win_dns_client - Fix failure to lookup registry DNS servers when it contains null characters
8 changes: 6 additions & 2 deletions plugins/modules/win_dns_client.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ Function Get-RegistryNameServerInfo {
}

if (($ns = Get-OptionalProperty -InputObject $iprop -Name $items.StaticNameServer)) {
$famInfo.EffectiveNameServers = $famInfo.StaticNameServers = $ns -split '[,;\ ]'
# The raw string value may have null bytes at the end of the string so needs to be trimmed out.
# This seems to only happen when Set-DnsClientServerAddress was used to set both IPv4 and IPv6
# DNS servers at the same time.
# $famInfo.EffectiveNameServers = $famInfo.StaticNameServers = $ns.Trim([char]0) -split '[,;\ ]'
$famInfo.EffectiveNameServers = $famInfo.StaticNameServers = $ns.Trim([char]0) -split '[,;\ ]'
$famInfo.UsingDhcp = $false
$famInfo.NameServerBadFormat = $ns -match '[;\ ]'
}
Expand Down Expand Up @@ -381,7 +385,7 @@ Try {
Catch {
$excep = $_

Write-DebugLog "Exception: $($excep | out-string)"
Write-DebugLog "Exception: $($excep | Out-String)"

Throw
}
21 changes: 19 additions & 2 deletions tests/integration/targets/win_dns_client/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,34 @@
win_dns_client:
adapter_names: '{{ network_adapter_name }}'
dns_servers:
- 192.168.34.7
- 192.168.34.8
- 2001:db8::1
- 2001:db8::2
register: set_ipv6

- name: get result of set IPv6 DNS address
win_shell: (Get-DnsClientServerAddress -InterfaceAlias '{{ network_adapter_name }}' -AddressFAmily IPv6).ServerAddresses
win_shell: (Get-DnsClientServerAddress -InterfaceAlias '{{ network_adapter_name }}').ServerAddresses
changed_when: no
register: set_ipv6_actual

- name: assert set IPv6 DNS address
assert:
that:
- set_ipv6 is changed
- set_ipv6_actual.stdout_lines == ['2001:db8::1', '2001:db8::2']
- set_ipv6_actual.stdout_lines == ['192.168.34.7', '192.168.34.8', '2001:db8::1', '2001:db8::2']

- name: set IPv6 DNS address (idempotent)
win_dns_client:
adapter_names: '{{ network_adapter_name }}'
dns_servers:
- 192.168.34.7
- 192.168.34.8
- 2001:db8::1
- 2001:db8::2
register: set_ipv6_again

- name: assert set IPv6 DNS address (idempotent)
assert:
that:
- not set_ipv6_again is changed

0 comments on commit f049245

Please sign in to comment.