From d71522cafb5bc1e1025d7c78f9ca8aa5f9a9ce0e Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Wed, 20 Oct 2021 15:13:26 -0700 Subject: Fix hostname deprecation error Signed-off-by: Lamont Granquist --- lib/ohai/mixin/network_helper.rb | 18 ++++++ lib/ohai/plugins/hostname.rb | 96 +++--------------------------- spec/unit/plugins/freebsd/hostname_spec.rb | 2 +- spec/unit/plugins/hostname_spec.rb | 40 ++++++------- spec/unit/plugins/linux/hostname_spec.rb | 6 +- 5 files changed, 51 insertions(+), 111 deletions(-) diff --git a/lib/ohai/mixin/network_helper.rb b/lib/ohai/mixin/network_helper.rb index bb6d1367..134d274b 100644 --- a/lib/ohai/mixin/network_helper.rb +++ b/lib/ohai/mixin/network_helper.rb @@ -32,6 +32,24 @@ module Ohai [2, 4, 6].each { |n| dec = dec + "." + netmask[n..n + 1].to_i(16).to_s(10) } dec end + + # This does a forward and reverse lookup on the hostname to return what should be + # the FQDN for the host determined by name lookup (generally DNS) + # + def canonicalize_hostname(hostname) + Addrinfo.getaddrinfo(hostname, nil).first.getnameinfo.first + end + + def canonicalize_hostname_with_retries(hostname) + retries = 3 + begin + canonicalize_hostname(hostname) + rescue + retries -= 1 + retry if retries > 0 + hostname + end + end end end end diff --git a/lib/ohai/plugins/hostname.rb b/lib/ohai/plugins/hostname.rb index abc37996..1c95d7c3 100644 --- a/lib/ohai/plugins/hostname.rb +++ b/lib/ohai/plugins/hostname.rb @@ -26,7 +26,11 @@ # limitations under the License. # +require_relative "../mixin/network_helper" + Ohai.plugin(:Hostname) do + include Ohai::Mixin::NetworkHelper + provides "domain", "hostname", "fqdn", "machinename" # hostname : short hostname @@ -42,38 +46,8 @@ Ohai.plugin(:Hostname) do end # forward and reverse lookup to canonicalize FQDN (hostname -f equivalent) - # this is ipv6-safe, works on ruby 1.8.7+ def resolve_fqdn - require "socket" unless defined?(Socket) - require "ipaddr" unless defined?(IPAddr) - - hostname = from_cmd("hostname") - begin - addrinfo = Socket.getaddrinfo(hostname, nil).first - rescue SocketError - # In the event that we got an exception from Socket, it's possible - # that it will work if we restrict it to IPv4 only either because of - # IPv6 misconfiguration or other bugs. - # - # Specifically it's worth noting that on macOS, getaddrinfo() will choke - # if it gets back a link-local address (say if you have 'fe80::1 myhost' - # in /etc/hosts). This will raise: - # SocketError (getnameinfo: Non-recoverable failure in name resolution) - # - # But general misconfiguration could cause similar issues, so attempt to - # fall back to v4-only - begin - addrinfo = Socket.getaddrinfo(hostname, nil, :INET).first - rescue - # and if *that* fails, then try v6-only, in case we're in a v6-only - # environment with v4 config issues - addrinfo = Socket.getaddrinfo(hostname, nil, :INET6).first - end - end - iaddr = IPAddr.new(addrinfo[3]) - Socket.gethostbyaddr(iaddr.hton)[0] - rescue - nil + canonicalize_hostname_with_retries(from_cmd("hostname")) end def collect_domain @@ -119,58 +93,21 @@ Ohai.plugin(:Hostname) do collect_data(:darwin) do hostname from_cmd("hostname -s") machinename from_cmd("hostname") - begin - our_fqdn = resolve_fqdn - # Sometimes... very rarely, but sometimes, 'hostname --fqdn' falsely - # returns a blank string. WTF. - if our_fqdn.nil? || our_fqdn.empty? - logger.trace("Plugin Hostname: hostname returned an empty string, retrying once.") - our_fqdn = resolve_fqdn - end - - if our_fqdn.nil? || our_fqdn.empty? - logger.trace("Plugin Hostname: hostname returned an empty string twice and will" + - "not be set.") - else - fqdn our_fqdn - end - rescue - logger.trace( - "Plugin Hostname: hostname returned an error, probably no domain set" - ) - end + fqdn resolve_fqdn domain collect_domain end collect_data(:freebsd) do hostname from_cmd("hostname -s") machinename from_cmd("hostname") - fqdn from_cmd("hostname -f") + fqdn resolve_fqdn collect_domain end collect_data(:linux) do hostname from_cmd("hostname -s") machinename from_cmd("hostname") - begin - our_fqdn = from_cmd("hostname --fqdn") - # Sometimes... very rarely, but sometimes, 'hostname --fqdn' falsely - # returns a blank string. WTF. - if our_fqdn.nil? || our_fqdn.empty? - logger.trace("Plugin Hostname: hostname --fqdn returned an empty string, retrying " + - "once.") - our_fqdn = from_cmd("hostname --fqdn") - end - - if our_fqdn.nil? || our_fqdn.empty? - logger.trace("Plugin Hostname: hostname --fqdn returned an empty string twice and " + - "will not be set.") - else - fqdn our_fqdn - end - rescue - logger.trace("Plugin Hostname: hostname --fqdn returned an error, probably no domain set") - end + fqdn resolve_fqdn domain collect_domain end @@ -190,22 +127,7 @@ Ohai.plugin(:Hostname) do hostname host["dnshostname"].to_s machinename host["name"].to_s - - info = Socket.gethostbyname(Socket.gethostname) - if /.+?\.(.*)/.match?(info.first) - fqdn info.first - else - # host is not in dns. optionally use: - # C:\WINDOWS\system32\drivers\etc\hosts - info[3..info.length].reverse_each do |addr| - hostent = Socket.gethostbyaddr(addr) - if /.+?\.(.*)/.match?(hostent.first) - fqdn hostent.first - break - end - end - fqdn info.first unless fqdn - end + fqdn canonicalize_hostname_with_retries(Socket.gethostname) domain collect_domain end end diff --git a/spec/unit/plugins/freebsd/hostname_spec.rb b/spec/unit/plugins/freebsd/hostname_spec.rb index 025ccbf9..dc7fb578 100644 --- a/spec/unit/plugins/freebsd/hostname_spec.rb +++ b/spec/unit/plugins/freebsd/hostname_spec.rb @@ -22,8 +22,8 @@ describe Ohai::System, "FreeBSD hostname plugin" do before do @plugin = get_plugin("hostname") allow(@plugin).to receive(:collect_os).and_return(:freebsd) + allow(@plugin).to receive(:canonicalize_hostname).with("katie.local").and_return("katie.bethell") allow(@plugin).to receive(:shell_out).with("hostname -s").and_return(mock_shell_out(0, "katie", "")) - allow(@plugin).to receive(:shell_out).with("hostname -f").and_return(mock_shell_out(0, "katie.bethell", "")) allow(@plugin).to receive(:shell_out).with("hostname").and_return(mock_shell_out(0, "katie.local", "")) end diff --git a/spec/unit/plugins/hostname_spec.rb b/spec/unit/plugins/hostname_spec.rb index 9819c42f..0390ddac 100644 --- a/spec/unit/plugins/hostname_spec.rb +++ b/spec/unit/plugins/hostname_spec.rb @@ -25,35 +25,37 @@ describe Ohai::System, "hostname plugin" do allow(@plugin).to receive(:shell_out).with("hostname").and_return(mock_shell_out(0, "katie.local", "")) end - context "default behavior" - before do - allow(@plugin).to receive(:resolve_fqdn).and_return("katie.bethell") - end + context "default behavior" do + before do + allow(@plugin).to receive(:canonicalize_hostname).with("katie.local").and_return("katie.bethell") + end - it_should_check_from("linux::hostname", "machinename", "hostname", "katie.local") + it_should_check_from("linux::hostname", "machinename", "hostname", "katie.local") - it "uses #resolve_fqdn to find the fqdn" do - @plugin.run - expect(@plugin[:fqdn]).to eq("katie.bethell") - end + it "uses #resolve_fqdn to find the fqdn" do + @plugin.run + expect(@plugin[:fqdn]).to eq("katie.bethell") + end - it "sets the domain to everything after the first dot of the fqdn" do - @plugin.run - expect(@plugin[:domain]).to eq("bethell") - end + it "sets the domain to everything after the first dot of the fqdn" do + @plugin.run + expect(@plugin[:domain]).to eq("bethell") + end - it "sets the [short] hostname to everything before the first dot of the fqdn" do - @plugin.run - expect(@plugin[:hostname]).to eq("katie") + it "sets the [short] hostname to everything before the first dot of the fqdn" do + @plugin.run + expect(@plugin[:hostname]).to eq("katie") + end end context "when a system has a bare hostname without a FQDN" do before do allow(@plugin).to receive(:collect_os).and_return(:default) allow(@plugin).to receive(:shell_out).with("hostname").and_return(mock_shell_out(0, "katie", "")) + allow(@plugin).to receive(:canonicalize_hostname).with("katie").and_return("katie.bethell") end - it "correctlies set the [short] hostname" do + it "correctly sets the [short] hostname" do @plugin.run expect(@plugin[:hostname]).to eq("katie") end @@ -65,9 +67,7 @@ describe Ohai::System, "hostname plugin" do allow(@plugin).to receive(:shell_out).with("hostname -s").and_return( mock_shell_out(0, "katie", "") ) - allow(@plugin).to receive(:shell_out).with("hostname --fqdn").and_return( - mock_shell_out(0, "", ""), mock_shell_out(0, "katie.local", "") - ) + expect(@plugin).to receive(:canonicalize_hostname).with("katie.local").at_least(:once).and_raise(RuntimeError) end it "is called twice" do diff --git a/spec/unit/plugins/linux/hostname_spec.rb b/spec/unit/plugins/linux/hostname_spec.rb index 9bc6e86d..400a48d1 100644 --- a/spec/unit/plugins/linux/hostname_spec.rb +++ b/spec/unit/plugins/linux/hostname_spec.rb @@ -23,7 +23,7 @@ describe Ohai::System, "Linux hostname plugin" do @plugin = get_plugin("hostname") allow(@plugin).to receive(:collect_os).and_return(:linux) allow(@plugin).to receive(:shell_out).with("hostname -s").and_return(mock_shell_out(0, "katie", "")) - allow(@plugin).to receive(:shell_out).with("hostname --fqdn").and_return(mock_shell_out(0, "katie.bethell", "")) + allow(@plugin).to receive(:canonicalize_hostname).with("katie.local").and_return("katie.bethell") allow(@plugin).to receive(:shell_out).with("hostname").and_return(mock_shell_out(0, "katie.local", "")) end @@ -35,7 +35,7 @@ describe Ohai::System, "Linux hostname plugin" do describe "when domain name is unset" do before do - expect(@plugin).to receive(:shell_out).with("hostname --fqdn").and_raise("Ohai::Exception::Exec") + allow(@plugin).to receive(:canonicalize_hostname).with("katie.local").and_raise(RuntimeError) end it "does not raise an error" do @@ -44,7 +44,7 @@ describe Ohai::System, "Linux hostname plugin" do it "does not set fqdn" do @plugin.run - expect(@plugin.fqdn).to eq(nil) + expect(@plugin.fqdn).to eq("katie.local") end end -- cgit v1.2.1 From 69d7ebac17965b58cfc9d51bf5237ed33a45895c Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Wed, 20 Oct 2021 15:29:23 -0700 Subject: revert back to prior behavior if DNS fails after 3 tries we should probably leave this unset so we do not silently fall back to the short hostname. Signed-off-by: Lamont Granquist --- lib/ohai/mixin/network_helper.rb | 2 +- spec/unit/plugins/hostname_spec.rb | 6 ++---- spec/unit/plugins/linux/hostname_spec.rb | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/ohai/mixin/network_helper.rb b/lib/ohai/mixin/network_helper.rb index 134d274b..71ea0bdd 100644 --- a/lib/ohai/mixin/network_helper.rb +++ b/lib/ohai/mixin/network_helper.rb @@ -47,7 +47,7 @@ module Ohai rescue retries -= 1 retry if retries > 0 - hostname + nil end end end diff --git a/spec/unit/plugins/hostname_spec.rb b/spec/unit/plugins/hostname_spec.rb index 0390ddac..bf37ef1a 100644 --- a/spec/unit/plugins/hostname_spec.rb +++ b/spec/unit/plugins/hostname_spec.rb @@ -72,7 +72,7 @@ describe Ohai::System, "hostname plugin" do it "is called twice" do @plugin.run - expect(@plugin[:fqdn]).to eq("katie.local") + expect(@plugin[:fqdn]).to eq(nil) end end @@ -82,9 +82,7 @@ describe Ohai::System, "hostname plugin" do allow(@plugin).to receive(:shell_out).with("hostname -s").and_return( mock_shell_out(0, "katie", "") ) - allow(@plugin).to receive(:shell_out).with("hostname --fqdn").and_return( - mock_shell_out(0, "katie.local", "") - ) + expect(@plugin).to receive(:canonicalize_hostname).with("katie.local").and_return("katie.local") end it "is not be called twice" do diff --git a/spec/unit/plugins/linux/hostname_spec.rb b/spec/unit/plugins/linux/hostname_spec.rb index 400a48d1..72ec6139 100644 --- a/spec/unit/plugins/linux/hostname_spec.rb +++ b/spec/unit/plugins/linux/hostname_spec.rb @@ -44,7 +44,7 @@ describe Ohai::System, "Linux hostname plugin" do it "does not set fqdn" do @plugin.run - expect(@plugin.fqdn).to eq("katie.local") + expect(@plugin.fqdn).to eq(nil) end end -- cgit v1.2.1 From efa3ab980f7bc12f1fbaaf830b6ebc8f2998e3f1 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 21 Oct 2021 13:27:11 -0700 Subject: Cannot using require gating here It causes a constant redefinition error when SortedSet gets removed by the ConstantHelper Mixin. Signed-off-by: Lamont Granquist --- lib/ohai/plugins/filesystem.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ohai/plugins/filesystem.rb b/lib/ohai/plugins/filesystem.rb index e86d0908..7de1ddee 100644 --- a/lib/ohai/plugins/filesystem.rb +++ b/lib/ohai/plugins/filesystem.rb @@ -697,7 +697,7 @@ Ohai.plugin(:Filesystem) do end collect_data(:windows) do - require "set" unless defined?(Set) + require "set" # rubocop: disable Chef/Ruby/UnlessDefinedRequire require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) require_relative "../mash" -- cgit v1.2.1 From 3a3c2f601a0416e819a803762640f7c6ee869ac9 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 21 Oct 2021 13:31:40 -0700 Subject: Fix windows hostname plugin specs Signed-off-by: Lamont Granquist --- spec/unit/plugins/hostname_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/unit/plugins/hostname_spec.rb b/spec/unit/plugins/hostname_spec.rb index bf37ef1a..9c975fe7 100644 --- a/spec/unit/plugins/hostname_spec.rb +++ b/spec/unit/plugins/hostname_spec.rb @@ -142,7 +142,7 @@ describe Ohai::System, "hostname plugin for windows", :windows_only do context "when hostname is not set for the machine" do it "returns short machine name" do - allow(Socket).to receive(:gethostbyaddr).with(anything).and_return(local_hostent) + expect(@plugin).to receive(:canonicalize_hostname).with(anything).and_return("local") @plugin.run expect(@plugin[:fqdn]).to eq("local") end @@ -150,7 +150,7 @@ describe Ohai::System, "hostname plugin for windows", :windows_only do context "when hostname is set for the machine" do it "returns the fqdn of the machine" do - allow(Socket).to receive(:gethostbyaddr).with(anything).and_return(fqdn_hostent) + expect(@plugin).to receive(:canonicalize_hostname).with(anything).and_return("local.dx.internal.cloudapp.net") @plugin.run expect(@plugin[:fqdn]).to eq("local.dx.internal.cloudapp.net") end -- cgit v1.2.1 From a098e163490ad6604338d3d04fed601d18255693 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 21 Oct 2021 13:48:34 -0700 Subject: move require to top of definition Signed-off-by: Lamont Granquist --- lib/ohai/plugins/filesystem.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/ohai/plugins/filesystem.rb b/lib/ohai/plugins/filesystem.rb index 7de1ddee..51e53c73 100644 --- a/lib/ohai/plugins/filesystem.rb +++ b/lib/ohai/plugins/filesystem.rb @@ -23,6 +23,8 @@ # limitations under the License. # +require "set" unless defined?(Set) + Ohai.plugin(:Filesystem) do provides "filesystem".freeze @@ -697,7 +699,6 @@ Ohai.plugin(:Filesystem) do end collect_data(:windows) do - require "set" # rubocop: disable Chef/Ruby/UnlessDefinedRequire require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) require_relative "../mash" -- cgit v1.2.1 From 3892e40891342ab21e14561d322e08e4bf86073e Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 21 Oct 2021 14:24:37 -0700 Subject: omit SortedSet directly from ConstantHelper Signed-off-by: Lamont Granquist --- lib/ohai/mixin/constant_helper.rb | 2 +- lib/ohai/plugins/filesystem.rb | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/ohai/mixin/constant_helper.rb b/lib/ohai/mixin/constant_helper.rb index a02aa6fa..933d4ed0 100644 --- a/lib/ohai/mixin/constant_helper.rb +++ b/lib/ohai/mixin/constant_helper.rb @@ -23,7 +23,7 @@ module Ohai module ConstantHelper def remove_constants - new_object_constants = Object.constants - @object_pristine.constants + new_object_constants = Object.constants - @object_pristine.constants - [ :SortedSet ] new_object_constants.each do |constant| Object.send(:remove_const, constant) unless Object.const_get(constant).is_a?(Module) end diff --git a/lib/ohai/plugins/filesystem.rb b/lib/ohai/plugins/filesystem.rb index 51e53c73..e86d0908 100644 --- a/lib/ohai/plugins/filesystem.rb +++ b/lib/ohai/plugins/filesystem.rb @@ -23,8 +23,6 @@ # limitations under the License. # -require "set" unless defined?(Set) - Ohai.plugin(:Filesystem) do provides "filesystem".freeze @@ -699,6 +697,7 @@ Ohai.plugin(:Filesystem) do end collect_data(:windows) do + require "set" unless defined?(Set) require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) require_relative "../mash" -- cgit v1.2.1 From f9e4ca0cc69abf8a6bbc3c7c6ed97765019d3d15 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 21 Oct 2021 14:50:57 -0700 Subject: turn off fail-fast Signed-off-by: Lamont Granquist --- .github/workflows/exec.yml | 1 + .github/workflows/unit.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/exec.yml b/.github/workflows/exec.yml index 2d35a0e4..5bc19646 100644 --- a/.github/workflows/exec.yml +++ b/.github/workflows/exec.yml @@ -10,6 +10,7 @@ name: exec jobs: test: strategy: + fail-fast: false matrix: os: ['macos-latest', 'ubuntu-latest', 'windows-latest'] ruby: ['2.7', '3.0'] diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index d98f705b..14fae5bc 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -10,6 +10,7 @@ name: unit jobs: test: strategy: + fail-fast: false matrix: os: ['ubuntu-latest', 'macos-latest', 'windows-latest'] ruby: ['2.7', '3.0'] -- cgit v1.2.1 From 199add424c4406e059930cf92fc6cd07428d118d Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 21 Oct 2021 15:07:21 -0700 Subject: This might be a dirty thing to do Signed-off-by: Lamont Granquist --- lib/ohai/plugins/packages.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ohai/plugins/packages.rb b/lib/ohai/plugins/packages.rb index e9e74909..4228fb09 100644 --- a/lib/ohai/plugins/packages.rb +++ b/lib/ohai/plugins/packages.rb @@ -136,7 +136,7 @@ Ohai.plugin(:Packages) do collect_programs_from_registry_key(Win32::Registry::HKEY_LOCAL_MACHINE, 'Software\Microsoft\Windows\CurrentVersion\Uninstall') # on 64 bit systems, 32 bit programs are stored here moved before HKEY_CURRENT_USER otherwise it is not collected (impacts both ohai 16 & 17) collect_programs_from_registry_key(Win32::Registry::HKEY_LOCAL_MACHINE, 'Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall') - collect_programs_from_registry_key(Win32::Registry::HKEY_CURRENT_USER, 'Software\Microsoft\Windows\CurrentVersion\Uninstall') + collect_programs_from_registry_key(Win32::Registry::HKEY_CURRENT_USER, 'Software\Microsoft\Windows\CurrentVersion\Uninstall') rescue nil end collect_data(:aix) do -- cgit v1.2.1