Skip to content

Commit af00fe2

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 f0af74f commit af00fe2

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

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?

spec/custom_facts/util/config_spec.rb

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

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

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

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

0 commit comments

Comments
 (0)