Skip to content

Commit d2c1ae8

Browse files
committed
explicitly do AAAA lookups even if IPv6 is unavailable
Resolv will only return IPv6 records if there is a local IPv6 address, and this is problematic since the agent may be in a completely different situation than the master - where this code runs.
1 parent f1a8166 commit d2c1ae8

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

lib/puppet_x/puppetlabs/firewall/utility.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,29 @@ def self.host_to_ip(value, proto = nil)
9393
begin
9494
value = PuppetX::Firewall::IPCidr.new(value)
9595
rescue StandardError
96-
family = case proto
97-
when 'IPv4', 'iptables'
98-
Socket::AF_INET
99-
when 'IPv6', 'ip6tables'
100-
Socket::AF_INET6
101-
when nil
102-
raise ArgumentError, 'Proto must be specified for a hostname'
103-
else
104-
raise ArgumentError, "Unsupported address family: #{proto}"
105-
end
96+
case proto
97+
when 'IPv4', 'iptables'
98+
family = Socket::AF_INET
99+
rr = Resolv::DNS::Resource::IN::A
100+
when 'IPv6', 'ip6tables'
101+
family = Socket::AF_INET6
102+
rr = Resolv::DNS::Resource::IN::AAAA
103+
when nil
104+
raise ArgumentError, 'Proto must be specified for a hostname'
105+
else
106+
raise ArgumentError, "Unsupported address family: #{proto}"
107+
end
106108

107109
new_value = nil
108-
Resolv.each_address(value) do |addr|
110+
Resolv::DNS.new.each_resource(value, rr) do |addr|
109111
begin # rubocop:disable Style/RedundantBegin
110-
new_value = PuppetX::Firewall::IPCidr.new(addr, family)
112+
new_value = PuppetX::Firewall::IPCidr.new(addr.address.to_s, family)
111113
break
112114
rescue StandardError # looking for the one that works # rubocop:disable Lint/SuppressedException
113115
end
114116
end
115117

116-
raise "Failed to resolve hostname #{value}" if new_value.nil?
118+
raise "Failed to resolve hostname #{proto} #{value}" if new_value.nil?
117119

118120
value = new_value
119121
end

0 commit comments

Comments
 (0)