diff options
author | Tim Smith <tsmith84@gmail.com> | 2016-02-01 19:32:32 -0800 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2016-02-01 19:32:32 -0800 |
commit | 5d619d3f941e551d4cd7dd1aa1bd5d137ada8a44 (patch) | |
tree | 0b9c36a3f12c904c7ad7667e99ad3756b2f1bc66 | |
parent | 50e002ef9227318b89242803f647b6c51a1e1a74 (diff) | |
download | ohai-5d619d3f941e551d4cd7dd1aa1bd5d137ada8a44.tar.gz |
Detect Azure using the Azure agent and DHCP options
-rw-r--r-- | lib/ohai/plugins/azure.rb | 23 | ||||
-rw-r--r-- | spec/unit/plugins/azure_spec.rb | 83 |
2 files changed, 101 insertions, 5 deletions
diff --git a/lib/ohai/plugins/azure.rb b/lib/ohai/plugins/azure.rb index 97ba1cc4..852a556a 100644 --- a/lib/ohai/plugins/azure.rb +++ b/lib/ohai/plugins/azure.rb @@ -19,7 +19,7 @@ Ohai.plugin(:Azure) do collect_data do # The azure hints are populated by the knife plugin for Azure. - # The project is located at https://github.com/opscode/knife-azure + # The project is located at https://github.com/chef/knife-azure # Please see the lib/chef/knife/azure_server_create.rb file in that # project for details azure_metadata_from_hints = hint?('azure') @@ -27,9 +27,28 @@ Ohai.plugin(:Azure) do Ohai::Log.debug("azure_metadata_from_hints is present.") azure Mash.new azure_metadata_from_hints.each {|k, v| azure[k] = v } + elsif looks_like_azure? + Ohai::Log.debug("No hints present, but system appears to be on azure.") + azure Mash.new else - Ohai::Log.debug("No hints present for azure.") + Ohai::Log.debug("No hints present for azure and doesn't appear to be azure.") false end end + + # check for either the waagent or the unknown-245 DHCP option that Azure uses + # http://blog.mszcool.com/index.php/2015/04/detecting-if-a-virtual-machine-runs-in-microsoft-azure-linux-windows-to-protect-your-software-when-distributed-via-the-azure-marketplace/ + def looks_like_azure? + if ::File.exist?('/usr/sbin/waagent') || ::Dir.exist?('C:\WindowsAzure') + Ohai::Log.debug("Found waagent used by MS Azure.") + return true + elsif File.exist?('/var/lib/dhcp/dhclient.eth0.leases') + File.open("/var/lib/dhcp/dhclient.eth0.leases").each do |line| + if line =~ /unknown-245/ + Ohai::Log.debug("Found unknown-245 DHCP option used by MS Azure.") + return true + end + end + end + end end diff --git a/spec/unit/plugins/azure_spec.rb b/spec/unit/plugins/azure_spec.rb index 0f29cdc7..e845df8c 100644 --- a/spec/unit/plugins/azure_spec.rb +++ b/spec/unit/plugins/azure_spec.rb @@ -25,7 +25,7 @@ describe Ohai::System, "plugin azure" do @plugin = get_plugin("azure") end - describe "with azure cloud file" do + describe "with azure hint file" do before(:each) do allow(File).to receive(:exist?).with('/etc/chef/ohai/hints/azure.json').and_return(true) allow(File).to receive(:read).with('/etc/chef/ohai/hints/azure.json').and_return('{"public_ip":"137.135.46.202","vm_name":"test-vm","public_fqdn":"service.cloudapp.net","public_ssh_port":"22", "public_winrm_port":"5985"}') @@ -45,10 +45,14 @@ describe Ohai::System, "plugin azure" do end - describe "without azure cloud file" do + describe "without azure hint file or agent or dhcp options" do before(:each) do allow(File).to receive(:exist?).with('/etc/chef/ohai/hints/azure.json').and_return(false) allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false) + allow(File).to receive(:exist?).with('/usr/sbin/waagent').and_return(false) + allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(false) + allow(File).to receive(:exist?).with('/var/lib/dhcp/dhclient.eth0.leases').and_return(false) + @plugin.run end it 'should not behave like azure' do @@ -56,12 +60,18 @@ describe Ohai::System, "plugin azure" do end end - describe "with rackspace cloud file" do + describe "with rackspace hint file no agent and no dhcp options" do before(:each) do allow(File).to receive(:exist?).with('/etc/chef/ohai/hints/rackspace.json').and_return(true) allow(File).to receive(:read).with('/etc/chef/ohai/hints/rackspace.json').and_return('') allow(File).to receive(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(true) allow(File).to receive(:read).with('C:\chef\ohai\hints/rackspace.json').and_return('') + allow(File).to receive(:exist?).with('/usr/sbin/waagent').and_return(false) + allow(File).to receive(:exist?).with('/etc/chef/ohai/hints/azure.json').and_return(false) + allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false) + allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(false) + allow(File).to receive(:exist?).with('/var/lib/dhcp/dhclient.eth0.leases').and_return(false) + @plugin.run end it 'should not behave like azure' do @@ -69,4 +79,71 @@ describe Ohai::System, "plugin azure" do end end + describe "without azure hint file but with agent on linux" do + before(:each) do + allow(File).to receive(:exist?).with('/etc/chef/ohai/hints/azure.json').and_return(false) + allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false) + allow(File).to receive(:exist?).with('/usr/sbin/waagent').and_return(true) + allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(false) + @plugin.run + end + + it 'should create empty azure mash' do + expect(@plugin[:azure]).to be_empty + end + end + + describe "without azure hint file but with agent on windows" do + before(:each) do + allow(File).to receive(:exist?).with('/etc/chef/ohai/hints/azure.json').and_return(false) + allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false) + allow(File).to receive(:exist?).with('/usr/sbin/waagent').and_return(false) + allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(true) + @plugin.run + end + + it 'should create empty azure mash' do + puts "The dir exists?:" + "#{Dir.exist?('C:\WindowsAzure')}" + expect(@plugin[:azure]).to be_empty + end + end + + describe "without azure hint or agent but with dhcp option" do + before(:each) do + allow(File).to receive(:exist?).with('/etc/chef/ohai/hints/azure.json').and_return(false) + allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false) + allow(File).to receive(:exist?).with('/usr/sbin/waagent').and_return(false) + allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(false) + allow(File).to receive(:exist?).with('/var/lib/dhcp/dhclient.eth0.leases').and_return(true) + @double_file = double("/var/lib/dhcp/dhclient.eth0.leases") + allow(@double_file).to receive(:each). + and_yield('lease {'). + and_yield(' interface "eth0";'). + and_yield(' fixed-address 10.1.0.5;'). + and_yield(' server-name "RD24BE05C6F140";'). + and_yield(' option subnet-mask 255.255.255.0;'). + and_yield(' option dhcp-lease-time 4294967295;'). + and_yield(' option routers 10.1.0.1;'). + and_yield(' option dhcp-message-type 5;'). + and_yield(' option dhcp-server-identifier 168.63.129.16;'). + and_yield(' option domain-name-servers 168.63.129.16;'). + and_yield(' option dhcp-renewal-time 4294967295;'). + and_yield(' option rfc3442-classless-static-routes 0,10,1,0,1,32,168,63,129,16,10,1,0,1;'). + and_yield(' option unknown-245 a8:3f:81:10;'). + and_yield(' option dhcp-rebinding-time 4294967295;'). + and_yield(' option domain-name "v4wvfurds4relghweduc4zqjmd.dx.internal.cloudapp.net";'). + and_yield(' renew 5 2152/03/10 09:03:39;'). + and_yield(' rebind 5 2152/03/10 09:03:39;'). + and_yield(' expire 5 2152/03/10 09:03:39;'). + and_yield('}') + allow(File).to receive(:open).with("/var/lib/dhcp/dhclient.eth0.leases").and_return(@double_file) + @plugin.run + end + + it 'should create empty azure mash' do + puts "The dir exists?:" + "#{Dir.exist?('C:\WindowsAzure')}" + expect(@plugin[:azure]).to be_empty + end + end + end |