Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(maint) Read system custom facts when running as a user #2231

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions lib/facter/custom_facts/util/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,23 @@ def self.facts_cache_dir
end

def self.setup_default_ext_facts_dirs
if LegacyFacter::Util::Root.root?
windows_dir = windows_data_dir
Facter::Options[:default_external_dir] = if windows_dir
[File.join(windows_dir, 'PuppetLabs', 'facter', 'facts.d')]
else
[
'/etc/puppetlabs/facter/facts.d',
'/etc/facter/facts.d/',
'/opt/puppetlabs/facter/facts.d'
]
end
elsif ENV['HOME']
Facter::Options[:default_external_dir] =
[File.join(ENV['HOME'], '.facter', 'facts.d'),
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d')]
else
Facter::Options[:default_external_dir] = []
end
windows_dir = windows_data_dir
Facter::Options[:default_external_dir] = if windows_dir
[File.join(windows_dir, 'PuppetLabs', 'facter', 'facts.d')]
else
[
'/etc/puppetlabs/facter/facts.d',
'/etc/facter/facts.d/',
'/opt/puppetlabs/facter/facts.d'
]
end

return unless !LegacyFacter::Util::Root.root? && ENV['HOME']

Facter::Options[:default_external_dir] = [
File.join(ENV['HOME'], '.facter', 'facts.d'),
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d')
] + Facter::Options[:default_external_dir]
end

if LegacyFacter::Util::Config.windows?
Expand Down
44 changes: 29 additions & 15 deletions spec/custom_facts/util/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@
describe LegacyFacter::Util::Config do
include PuppetlabsSpec::Files

describe "ENV['HOME'] is unset", unless: LegacyFacter::Util::Root.root? do
around do |example|
Facter::Core::Execution.with_env('HOME' => nil) do
example.run
end
end

it 'does not set @external_facts_dirs' do
LegacyFacter::Util::Config.setup_default_ext_facts_dirs
expect(LegacyFacter::Util::Config.external_facts_dirs).to be_empty
end
end

describe 'is_windows? function' do
it "detects windows if Ruby RbConfig::CONFIG['host_os'] returns a windows OS" do
host_os = %w[mswin win32 dos mingw cygwin]
Expand Down Expand Up @@ -78,12 +65,39 @@
.to eq [File.join('C:\\Documents', 'PuppetLabs', 'facter', 'facts.d')]
end

it "returns the old and new (AIO) paths under user's home directory when not root" do
it "returns the old and new (AIO) paths under user's home directory when not root on windows 2008" do
allow(LegacyFacter::Util::Root).to receive(:root?).and_return(false)
allow(LegacyFacter::Util::Config).to receive(:windows?).and_return(true)
allow(LegacyFacter::Util::Config).to receive(:windows_data_dir).and_return('C:\\ProgramData')
LegacyFacter::Util::Config.setup_default_ext_facts_dirs
expect(LegacyFacter::Util::Config.external_facts_dirs)
.to eq [File.join(ENV['HOME'], '.facter', 'facts.d'),
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d'),
File.join('C:\\ProgramData', 'PuppetLabs', 'facter', 'facts.d')]
end

it "returns the old and new (AIO) paths under user's home directory when not root on windows 2003R2" do
allow(LegacyFacter::Util::Root).to receive(:root?).and_return(false)
allow(LegacyFacter::Util::Config).to receive(:windows?).and_return(true)
allow(LegacyFacter::Util::Config).to receive(:windows_data_dir).and_return('C:\\Documents')
LegacyFacter::Util::Config.setup_default_ext_facts_dirs
expect(LegacyFacter::Util::Config.external_facts_dirs)
.to eq [File.join(ENV['HOME'], '.facter', 'facts.d'),
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d'),
File.join('C:\\Documents', 'PuppetLabs', 'facter', 'facts.d')]
end

it "returns the old and new (AIO) paths under user's home directory when not root on linux" do
allow(LegacyFacter::Util::Root).to receive(:root?).and_return(false)
allow(LegacyFacter::Util::Config).to receive(:windows?).and_return(false)
allow(LegacyFacter::Util::Config).to receive(:windows_data_dir).and_return(nil)
LegacyFacter::Util::Config.setup_default_ext_facts_dirs
expect(LegacyFacter::Util::Config.external_facts_dirs)
.to eq [File.join(ENV['HOME'], '.facter', 'facts.d'),
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d')]
File.join(ENV['HOME'], '.puppetlabs', 'opt', 'facter', 'facts.d'),
'/etc/puppetlabs/facter/facts.d',
'/etc/facter/facts.d/',
'/opt/puppetlabs/facter/facts.d']
end

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