Codebase list facter / 5aa2a6f
(#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. Jeff Weiss authored 12 years ago Ken Barber committed 12 years ago
4 changed file(s) with 122 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
5151 Facter.add(:domain) do
5252 confine :kernel => :windows
5353 setcode do
54 require 'facter/util/wmi'
54 require 'facter/util/registry'
5555 domain = ""
56 Facter::Util::WMI.execquery("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").each { |nic|
57 domain = nic.DNSDomain
58 break
59 }
56 regvalue = Facter::Util::Registry.hklm_read('SYSTEM\CurrentControlSet\Services\Tcpip\Parameters', 'Domain')
57 domain = regvalue if regvalue
58 if domain == ""
59 require 'facter/util/wmi'
60 Facter::Util::WMI.execquery("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").each { |nic|
61 domain = nic.DNSDomain
62 break
63 }
64 end
6065 domain
6166 end
6267 end
0 module Facter::Util::Registry
1 class << self
2 def hklm_read(key, value)
3 require 'win32/registry'
4 reg = Win32::Registry::HKEY_LOCAL_MACHINE.open(key)
5 reg[value]
6 end
7 end
8 end
9797 end
9898
9999 describe "on Windows" do
100 it "should use the DNSDomain for the first nic where ip is enabled" do
100 before(:each) do
101101 Facter.fact(:kernel).stubs(:value).returns("windows")
102 require 'facter/util/registry'
103 end
104 describe "with primary dns suffix" do
105 before(:each) do
106 Facter::Util::Registry.stubs(:hklm_read).returns('baz.com')
107 end
108 it "should get the primary dns suffix" do
109 Facter.fact(:domain).value.should == 'baz.com'
110 end
111 it "should not execute the wmi query" do
112 require 'facter/util/wmi'
113 Facter::Util::WMI.expects(:execquery).never
114 Facter.fact(:domain).value
115 end
116 end
117 describe "without primary dns suffix" do
118 before(:each) do
119 Facter::Util::Registry.stubs(:hklm_read).returns('')
120 end
121 it "should use the DNSDomain for the first nic where ip is enabled" do
122 nic = stubs 'nic'
123 nic.stubs(:DNSDomain).returns("foo.com")
102124
103 nic = stubs 'nic'
104 nic.stubs(:DNSDomain).returns("foo.com")
125 nic2 = stubs 'nic'
126 nic2.stubs(:DNSDomain).returns("bar.com")
105127
106 nic2 = stubs 'nic'
107 nic2.stubs(:DNSDomain).returns("bar.com")
128 require 'facter/util/wmi'
129 Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
108130
109 require 'facter/util/wmi'
110 Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
111
112 Facter.fact(:domain).value.should == 'foo.com'
131 Facter.fact(:domain).value.should == 'foo.com'
132 end
113133 end
114134 end
115135 end
0 #!/usr/bin/env rspec
1 require 'spec_helper'
2 require 'facter/operatingsystem'
3 require 'facter/util/registry'
4
5 describe Facter::Util::Registry do
6 describe "hklm_read", :if => Facter::Util::Config.is_windows? do
7 before(:all) do
8 require 'win32/registry'
9 end
10 describe "valid params" do
11 [ {:key => "valid_key", :value => "valid_value", :expected => "valid"},
12 {:key => "valid_key", :value => "", :expected => "valid"},
13 {:key => "valid_key", :value => nil, :expected => "invalid"},
14 {:key => "", :value => "valid_value", :expected => "valid"},
15 {:key => "", :value => "", :expected => "valid"},
16 {:key => "", :value => nil, :expected => "invalid"},
17 {:key => nil, :value => "valid_value", :expected => "invalid"},
18 {:key => nil, :value => "", :expected => "invalid"},
19 {:key => nil, :value => nil, :expected => "invalid"}
20 ].each do |scenario|
21 describe "with key #{scenario[:key] || "nil"} and value #{scenario[:value] || "nil"}" do
22 let :fake_registry_key do
23 fake = {}
24 fake[scenario[:value]] = scenario[:expected]
25 fake
26 end
27 it "should return #{scenario[:expected]} value" do
28 Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).returns(fake_registry_key)
29
30 Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value]).should == scenario[:expected]
31 end
32 end
33 end
34 end
35
36 describe "invalid params" do
37 [ {:key => "valid_key", :value => "invalid_value"},
38 {:key => "valid_key", :value => ""},
39 {:key => "valid_key", :value => nil},
40 ].each do |scenario|
41 describe "with valid key and value #{scenario[:value] || "nil"}" do
42 let :fake_registry_key do
43 {}
44 end
45 it "should raise an error" do
46 Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).returns(fake_registry_key)
47
48 Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value]).should raise_error
49 end
50 end
51 end
52 [ {:key => "invalid_key", :value => "valid_value"},
53 {:key => "invalid_key", :value => ""},
54 {:key => "invalid_key", :value => nil},
55 {:key => "", :value => "valid_value"},
56 {:key => "", :value => ""},
57 {:key => "", :value => nil},
58 {:key => nil, :value => "valid_value"},
59 {:key => nil, :value => ""},
60 {:key => nil, :value => nil}
61 ].each do |scenario|
62 describe "with invalid key #{scenario[:key] || "nil"} and value #{scenario[:value] || "nil"}" do
63 it "should raise an error" do
64 Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(scenario[:key]).raises(Win32::Registry::Error, 2)
65 expect do
66 Facter::Util::Registry.hklm_read(scenario[:key], scenario[:value])
67 end.to raise_error Win32::Registry::Error
68 end
69 end
70 end
71 end
72 end
73 end