Skip to content

Commit

Permalink
Detect Azure when DHCP domain is set to reddog.microsoft.com
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Barnett <jason.w.barnett@gmail.com>
  • Loading branch information
jasonwbarnett committed Sep 30, 2020
1 parent e0d81b6 commit 384ba0b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/ohai/plugins/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions spec/unit/plugins/azure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 384ba0b

Please sign in to comment.