Skip to content

Commit c02389c

Browse files
committed
(maint) Read system custom facts when running as a user
When Facter is run as a non-privileged user, the system custom facts are not available. Always take system custom facts into account, and if Facter is running as a user who is not root, prepend this user custom facts directories to the list of search directories.
1 parent a3d61a3 commit c02389c

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

Diff for: lib/facter/custom_facts/util/config.rb

+17-18
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,23 @@ def self.facts_cache_dir
3737
end
3838

3939
def self.setup_default_ext_facts_dirs
40-
if LegacyFacter::Util::Root.root?
41-
windows_dir = windows_data_dir
42-
Facter::Options[:default_external_dir] = if windows_dir
43-
[File.join(windows_dir, 'PuppetLabs', 'facter', 'facts.d')]
44-
else
45-
[
46-
'/etc/puppetlabs/facter/facts.d',
47-
'/etc/facter/facts.d/',
48-
'/opt/puppetlabs/facter/facts.d'
49-
]
50-
end
51-
elsif ENV['HOME']
52-
Facter::Options[:default_external_dir] =
53-
[File.join(ENV['HOME'], '.facter', 'facts.d'),
54-
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d')]
55-
else
56-
Facter::Options[:default_external_dir] = []
57-
end
40+
windows_dir = windows_data_dir
41+
Facter::Options[:default_external_dir] = if windows_dir
42+
[File.join(windows_dir, 'PuppetLabs', 'facter', 'facts.d')]
43+
else
44+
[
45+
'/etc/puppetlabs/facter/facts.d',
46+
'/etc/facter/facts.d/',
47+
'/opt/puppetlabs/facter/facts.d'
48+
]
49+
end
50+
51+
return unless !LegacyFacter::Util::Root.root? && ENV['HOME']
52+
53+
Facter::Options[:default_external_dir] = [
54+
File.join(ENV['HOME'], '.facter', 'facts.d'),
55+
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d')
56+
] + Facter::Options[:default_external_dir]
5857
end
5958

6059
if LegacyFacter::Util::Config.windows?

Diff for: spec/custom_facts/util/config_spec.rb

+29-15
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@
44
describe LegacyFacter::Util::Config do
55
include PuppetlabsSpec::Files
66

7-
describe "ENV['HOME'] is unset", unless: LegacyFacter::Util::Root.root? do
8-
around do |example|
9-
Facter::Core::Execution.with_env('HOME' => nil) do
10-
example.run
11-
end
12-
end
13-
14-
it 'does not set @external_facts_dirs' do
15-
LegacyFacter::Util::Config.setup_default_ext_facts_dirs
16-
expect(LegacyFacter::Util::Config.external_facts_dirs).to be_empty
17-
end
18-
end
19-
207
describe 'is_windows? function' do
218
it "detects windows if Ruby RbConfig::CONFIG['host_os'] returns a windows OS" do
229
host_os = %w[mswin win32 dos mingw cygwin]
@@ -78,12 +65,39 @@
7865
.to eq [File.join('C:\\Documents', 'PuppetLabs', 'facter', 'facts.d')]
7966
end
8067

81-
it "returns the old and new (AIO) paths under user's home directory when not root" do
68+
it "returns the old and new (AIO) paths under user's home directory when not root on windows 2008" do
8269
allow(LegacyFacter::Util::Root).to receive(:root?).and_return(false)
70+
allow(LegacyFacter::Util::Config).to receive(:windows?).and_return(true)
71+
allow(LegacyFacter::Util::Config).to receive(:windows_data_dir).and_return('C:\\ProgramData')
72+
LegacyFacter::Util::Config.setup_default_ext_facts_dirs
73+
expect(LegacyFacter::Util::Config.external_facts_dirs)
74+
.to eq [File.join(ENV['HOME'], '.facter', 'facts.d'),
75+
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d'),
76+
File.join('C:\\ProgramData', 'PuppetLabs', 'facter', 'facts.d')]
77+
end
78+
79+
it "returns the old and new (AIO) paths under user's home directory when not root on windows 2003R2" do
80+
allow(LegacyFacter::Util::Root).to receive(:root?).and_return(false)
81+
allow(LegacyFacter::Util::Config).to receive(:windows?).and_return(true)
82+
allow(LegacyFacter::Util::Config).to receive(:windows_data_dir).and_return('C:\\Documents')
83+
LegacyFacter::Util::Config.setup_default_ext_facts_dirs
84+
expect(LegacyFacter::Util::Config.external_facts_dirs)
85+
.to eq [File.join(ENV['HOME'], '.facter', 'facts.d'),
86+
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d'),
87+
File.join('C:\\Documents', 'PuppetLabs', 'facter', 'facts.d')]
88+
end
89+
90+
it "returns the old and new (AIO) paths under user's home directory when not root on linux" do
91+
allow(LegacyFacter::Util::Root).to receive(:root?).and_return(false)
92+
allow(LegacyFacter::Util::Config).to receive(:windows?).and_return(false)
93+
allow(LegacyFacter::Util::Config).to receive(:windows_data_dir).and_return(nil)
8394
LegacyFacter::Util::Config.setup_default_ext_facts_dirs
8495
expect(LegacyFacter::Util::Config.external_facts_dirs)
8596
.to eq [File.join(ENV['HOME'], '.facter', 'facts.d'),
86-
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d')]
97+
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d'),
98+
'/etc/puppetlabs/facter/facts.d',
99+
'/etc/facter/facts.d/',
100+
'/opt/puppetlabs/facter/facts.d']
87101
end
88102

89103
it 'includes additional values when user appends to the list' do

0 commit comments

Comments
 (0)