Skip to content

Commit 6cfed40

Browse files
authored
Merge pull request #8800 from Dorin-Pleava/PUP-8220/lookup_facts_file_restrictions_for_trusted_facts
(PUP-8220) puppet lookup based on factfile restriction
2 parents d48d683 + 38c221f commit 6cfed40

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/puppet/application/lookup.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Puppet::Application::Lookup < Puppet::Application
77

88
RUN_HELP = _("Run 'puppet lookup --help' for more details").freeze
99
DEEP_MERGE_OPTIONS = '--knock-out-prefix, --sort-merged-arrays, and --merge-hash-arrays'.freeze
10+
TRUSTED_INFORMATION_FACTS = ["hostname", "domain", "fqdn", "clientcert"].freeze
1011

1112
run_mode :server
1213

@@ -352,6 +353,13 @@ def generate_scope
352353
unless given_facts.instance_of?(Hash)
353354
raise _("Incorrectly formatted data in %{fact_file} given via the --facts flag (only accepts yaml and json files)") % { fact_file: fact_file }
354355
end
356+
357+
if TRUSTED_INFORMATION_FACTS.any? { |key| given_facts.key? key }
358+
unless TRUSTED_INFORMATION_FACTS.all? { |key| given_facts.key? key }
359+
raise _("When overriding any of the %{trusted_facts_list} facts with %{fact_file} "\
360+
"given via the --facts flag, they must all be overridden.") % { fact_file: fact_file ,trusted_facts_list: TRUSTED_INFORMATION_FACTS.join(',')}
361+
end
362+
end
355363
end
356364

357365
unless node.is_a?(Puppet::Node) # to allow unit tests to pass a node instance

spec/unit/application/lookup_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,33 @@ def run_lookup(lookup)
639639
expected_error = "No facts available for target node: #{lookup.options[:node]}"
640640
expect { lookup.run_command }.to raise_error(RuntimeError, expected_error)
641641
end
642+
643+
it 'raises error due to missing trusted information facts in --facts file' do
644+
file_path = file_containing('facts.yaml', <<~CONTENT)
645+
---
646+
fqdn: some.fqdn.com
647+
CONTENT
648+
lookup.options[:fact_file] = file_path
649+
650+
expect {
651+
lookup.run_command
652+
}.to raise_error(/When overriding any of the hostname,domain,fqdn,clientcert facts with #{file_path} given via the --facts flag, they must all be overridden./)
653+
end
654+
655+
it 'does not fail when all trusted information facts are provided via --facts file' do
656+
file_path = file_containing('facts.yaml', <<~CONTENT)
657+
---
658+
fqdn: some.fqdn.com
659+
hostname: some.hostname
660+
domain: some.domain
661+
clientcert: some.clientcert
662+
CONTENT
663+
lookup.options[:fact_file] = file_path
664+
665+
expect {
666+
lookup.run_command
667+
}.to exit_with(0)
668+
end
642669
end
643670
end
644671

0 commit comments

Comments
 (0)