diff --git a/lib/ohai/plugins/azure.rb b/lib/ohai/plugins/azure.rb index c19d3c807..31cbaeed4 100644 --- a/lib/ohai/plugins/azure.rb +++ b/lib/ohai/plugins/azure.rb @@ -34,7 +34,7 @@ azure Mash.new azure_metadata_from_hints.each { |k, v| azure[k] = v } azure["metadata"] = parse_metadata - elsif has_waagent? || has_dhcp_option_245? + elsif has_waagent? || has_dhcp_option_245? || has_reddog_dhcp_domain? logger.trace("Plugin Azure: No hints present, but system appears to be on Azure.") azure Mash.new azure["metadata"] = parse_metadata @@ -67,6 +67,26 @@ def has_dhcp_option_245? has_245 end + def has_reddog_dhcp_domain? + tcp_ip_dhcp_domain == "reddog.microsoft.com" + end + + def tcp_ip_dhcp_domain + return unless RUBY_PLATFORM.match?(/mswin|mingw32|windows/) + + require "win32/registry" unless defined?(Win32::Registry) + + begin + key = Win32::Registry::HKEY_LOCAL_MACHINE.open("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters") + dhcp_domain = key["DhcpDomain"] + Ohai::Log.trace("Plugin Azure: DhcpDomain registry value is #{dhcp_domain}") + rescue Win32::Registry::Error + Ohai::Log.trace("Plugin Azure: DhcpDomain registry value cannot be found") + end + + dhcp_domain + end + # create the basic structure we'll store our data in def initialize_metadata_mash_compute metadata = Mash.new diff --git a/spec/unit/plugins/azure_spec.rb b/spec/unit/plugins/azure_spec.rb index 8ffd6222c..8eca57f09 100644 --- a/spec/unit/plugins/azure_spec.rb +++ b/spec/unit/plugins/azure_spec.rb @@ -72,6 +72,49 @@ end end + context "when on windows", :windows_only do + let(:plugin) do + get_plugin("azure").tap do |plugin| + plugin[:platform_family] = "windows" + end + end + + let(:tcpip_reg_key) { "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters" } + let(:win_reg_double) { instance_double("Win32::Registry") } + + before do + allow(Win32::Registery::HKEY_LOCAL_MACHINE) + .to receive(:open) + .with(tcpip_reg_key) + .and_return(win_reg_double) + allow(win_reg_double).to receive(:[]).with("DhcpDomain").and_return("domain.com") + end + + context "without azure hint file or agent" do + before do + allow(plugin).to receive(:hint?).with("azure").and_return(false) + allow(plugin).to receive(:has_waagent?).and_return(false) + allow(plugin).to receive(:has_dhcp_option_245?).and_return(false) + end + + context "DHCP option 15 is set to reddog.microsoft.com" do + before do + allow(win_reg_double).to receive(:[]).with("DhcpDomain").and_return("reddog.microsoft.com") + end + + it_behaves_like "azure" + end + + context "DHCP option 15 is not set to reddog.microsoft.com" do + before do + allow(win_reg_double).to receive(:[]).with("DhcpDomain").and_return("domain.com") + end + + it_behaves_like "!azure" + end + end + end + describe "with azure hint file" do before do allow(plugin).to receive(:hint?).with("azure").and_return(hint)