Permalink
Browse files

(#12864) Windows: get primary DNS from registry

Windows does not expose the host's primary DNS suffix via WMI, only the
registry. This commit adds a registry helper class and will retrieve the
primary DNS suffix from the registry. In the case the primary DNS suffix
does not exist or is empty, the domain fact will fall back to using WMI
for any connection-specific DNS suffix.
  • Loading branch information...
1 parent 51dcf4c commit 5aa2a6f71ce0ccabe6097798115871dc08d9dc5e Jeff Weiss committed with kbarber Apr 27, 2012
Showing with 122 additions and 14 deletions.
  1. +10 −5 lib/facter/domain.rb
  2. +9 −0 lib/facter/util/registry.rb
  3. +29 −9 spec/unit/domain_spec.rb
  4. +74 −0 spec/unit/util/registry_spec.rb
View
@@ -52,12 +52,17 @@
Facter.add(:domain) do
confine :kernel => :windows
setcode do
- require 'facter/util/wmi'
+ require 'facter/util/registry'
domain = ""
- Facter::Util::WMI.execquery("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").each { |nic|
- domain = nic.DNSDomain
- break
- }
+ regvalue = Facter::Util::Registry.hklm_read('SYSTEM\CurrentControlSet\Services\Tcpip\Parameters', 'Domain')
+ domain = regvalue if regvalue
+ if domain == ""
+ require 'facter/util/wmi'
+ Facter::Util::WMI.execquery("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").each { |nic|
+ domain = nic.DNSDomain
+ break
+ }
+ end
domain
end
end
@@ -0,0 +1,9 @@
+module Facter::Util::Registry
+ class << self
+ def hklm_read(key, value)
+ require 'win32/registry'
+ reg = Win32::Registry::HKEY_LOCAL_MACHINE.open(key)
+ reg[value]
+ end
+ end
+end
@@ -98,19 +98,39 @@
end
describe "on Windows" do
- it "should use the DNSDomain for the first nic where ip is enabled" do
+ before(:each) do
Facter.fact(:kernel).stubs(:value).returns("windows")
+ require 'facter/util/registry'
+ end
+ describe "with primary dns suffix" do
+ before(:each) do
+ Facter::Util::Registry.stubs(:hklm_read).returns('baz.com')
+ end
+ it "should get the primary dns suffix" do
+ Facter.fact(:domain).value.should == 'baz.com'
+ end
+ it "should not execute the wmi query" do
+ require 'facter/util/wmi'
+ Facter::Util::WMI.expects(:execquery).never
+ Facter.fact(:domain).value
+ end
+ end
+ describe "without primary dns suffix" do
+ before(:each) do
+ Facter::Util::Registry.stubs(:hklm_read).returns('')
+ end
+ it "should use the DNSDomain for the first nic where ip is enabled" do
+ nic = stubs 'nic'
+ nic.stubs(:DNSDomain).returns("foo.com")
- nic = stubs 'nic'
- nic.stubs(:DNSDomain).returns("foo.com")
-
- nic2 = stubs 'nic'
- nic2.stubs(:DNSDomain).returns("bar.com")
+ nic2 = stubs 'nic'
+ nic2.stubs(:DNSDomain).returns("bar.com")
- require 'facter/util/wmi'
- Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
+ require 'facter/util/wmi'
+ Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
- Facter.fact(:domain).value.should == 'foo.com'
+ Facter.fact(:domain).value.should == 'foo.com'
+ end
end
end
end
@@ -0,0 +1,74 @@
+#!/usr/bin/env rspec
+require 'spec_helper'
+require 'facter/operatingsystem'
+require 'facter/util/registry'
+
+describe Facter::Util::Registry do
+ describe "hklm_read", :if => Facter::Util::Config.is_windows? do
+ before(:all) do
+ require 'win32/registry'
+ end
+ describe "valid params" do
+ [ {:key => "valid_key", :value => "valid_value", :expected => "valid"},
+ {:key => "valid_key", :value => "", :expected => "valid"},
+ {:key => "valid_key", :value => nil, :expected => "invalid"},
+ {:key => "", :value => "valid_value", :expected => "valid"},
+ {:key => "", :value => "", :expected => "valid"},
+ {:key => "", :value => nil, :expected => "invalid"},
+ {:key => nil, :value => "valid_value", :expected => "invalid"},
+ {:key => nil, :value => "", :expected => "invalid"},
+ {:key => nil, :value => nil, :expected => "invalid"}
+ ].each do |scenario|
+ describe "with key #{scenario[:key] || "nil"} and value #{scenario[:value] || "nil"}" do
+ let :fake_registry_key do
+ fake = {}
+ fake[scenario[:value]] = scenario[:expected]
+ fake
+ end
+ it "should return #{scenario[:expected]} value" do
+ Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).returns(fake_registry_key)
+
+ Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value]).should == scenario[:expected]
+ end
+ end
+ end
+ end
+
+ describe "invalid params" do
+ [ {:key => "valid_key", :value => "invalid_value"},
+ {:key => "valid_key", :value => ""},
+ {:key => "valid_key", :value => nil},
+ ].each do |scenario|
+ describe "with valid key and value #{scenario[:value] || "nil"}" do
+ let :fake_registry_key do
+ {}
+ end
+ it "should raise an error" do
+ Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).returns(fake_registry_key)
+
+ Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value]).should raise_error
+ end
+ end
+ end
+ [ {:key => "invalid_key", :value => "valid_value"},
+ {:key => "invalid_key", :value => ""},
+ {:key => "invalid_key", :value => nil},
+ {:key => "", :value => "valid_value"},
+ {:key => "", :value => ""},
+ {:key => "", :value => nil},
+ {:key => nil, :value => "valid_value"},
+ {:key => nil, :value => ""},
+ {:key => nil, :value => nil}
+ ].each do |scenario|
+ describe "with invalid key #{scenario[:key] || "nil"} and value #{scenario[:value] || "nil"}" do
+ it "should raise an error" do
+ Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).raises(Win32::Registry::Error, 2)
+ expect do
+ Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value])
+ end.to raise_error Win32::Registry::Error
+ end
+ end
+ end
+ end
+ end
+end

0 comments on commit 5aa2a6f

Please sign in to comment.