Skip to content

Commit 82bda0e

Browse files
authored
Merge pull request #8892 from cthorn42/bug_fix/6.x/PUP-14437_when_env_specified_lookup_fix
(PUP-11437) Fix bug on lookup when environment is supplied
2 parents d2323a5 + 074dd44 commit 82bda0e

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

Diff for: acceptance/tests/parser_functions/puppet_lookup_cmd.rb

-9
Original file line numberDiff line numberDiff line change
@@ -2605,15 +2605,6 @@ def data()
26052605
"lookup in ENC specified environment failed"
26062606
)
26072607

2608-
step "--compile uses environment specified in --envrionment flag"
2609-
r = on(master, puppet('lookup', '--compile', "--node #{@node1}", "--confdir #{@confdir}", "--facts #{@coderoot}/facts.yaml", "--environment env1", 'environment_key'))
2610-
result = r.stdout
2611-
assert_match(
2612-
/env-env1 hiera provided value/,
2613-
result,
2614-
"env1 environment_key lookup failed, expected 'env-env1 hiera"
2615-
)
2616-
26172608
step "without --compile does not use environment specified in ENC"
26182609
r = on(master, puppet('lookup', "--node #{@node1}", "--confdir #{@confdir}", "--facts #{@coderoot}/facts.yaml", 'environment_key'))
26192610
result = r.stdout

Diff for: lib/puppet/application/lookup.rb

+24-28
Original file line numberDiff line numberDiff line change
@@ -373,38 +373,34 @@ def generate_scope
373373
end
374374

375375
unless node.is_a?(Puppet::Node) # to allow unit tests to pass a node instance
376-
facts = retrieve_node_facts(node, given_facts)
377-
if Puppet.settings.set_by_cli?('environment')
378-
node = Puppet::Node.new(node, :classes => nil, :parameters => nil, :facts => facts, :environment => Puppet.settings.value('environment'))
379-
else
380-
ni = Puppet::Node.indirection
381-
tc = ni.terminus_class
382-
if options[:compile]
383-
if tc == :plain
384-
node = ni.find(node, facts: facts)
385-
else
386-
begin
387-
service = Puppet.runtime[:http]
388-
session = service.create_session
389-
cert = session.route_to(:ca)
390-
391-
_, x509 = cert.get_certificate(node)
392-
cert = OpenSSL::X509::Certificate.new(x509)
393-
Puppet::SSL::Oids.register_puppet_oids
394-
trusted = Puppet::Context::TrustedInformation.remote(true, facts.values['certname'] || node, Puppet::SSL::Certificate.from_instance(cert))
395-
Puppet.override(trusted_information: trusted) do
396-
node = ni.find(node, facts: facts)
397-
end
398-
rescue
399-
Puppet.warning _("CA is not available, the operation will continue without using trusted facts.")
376+
facts = retrieve_node_facts(node, given_facts)
377+
ni = Puppet::Node.indirection
378+
tc = ni.terminus_class
379+
if options[:compile] && !Puppet.settings.set_by_cli?('environment')
380+
if tc == :plain
381+
node = ni.find(node, facts: facts)
382+
else
383+
begin
384+
service = Puppet.runtime[:http]
385+
session = service.create_session
386+
cert = session.route_to(:ca)
387+
388+
_, x509 = cert.get_certificate(node)
389+
cert = OpenSSL::X509::Certificate.new(x509)
390+
Puppet::SSL::Oids.register_puppet_oids
391+
trusted = Puppet::Context::TrustedInformation.remote(true, facts.values['certname'] || node, Puppet::SSL::Certificate.from_instance(cert))
392+
Puppet.override(trusted_information: trusted) do
400393
node = ni.find(node, facts: facts)
401394
end
395+
rescue
396+
Puppet.warning _("CA is not available, the operation will continue without using trusted facts.")
397+
node = ni.find(node, facts: facts)
402398
end
403-
else
404-
ni.terminus_class = :plain
405-
node = ni.find(node, facts: facts)
406-
ni.terminus_class = tc
407399
end
400+
else
401+
ni.terminus_class = :plain
402+
node = ni.find(node, facts: facts, environment: Puppet[:environment])
403+
ni.terminus_class = tc
408404
end
409405
else
410406
node.add_extra_facts(given_facts) if given_facts

Diff for: spec/integration/application/lookup_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ def expect_lookup_with_output(exitcode, out)
7272
expect_lookup_with_output(0, /value a/)
7373
end
7474

75+
it "resolves hiera data using a top-level node parameter" do
76+
File.write(File.join(env_dir, env_name, 'hiera.yaml'), <<~YAML)
77+
---
78+
version: 5
79+
hierarchy:
80+
- name: "Per Node"
81+
data_hash: yaml_data
82+
path: "%{my_fact}.yaml"
83+
YAML
84+
85+
File.write(File.join(env_dir, env_name, 'data', "my_fact_value.yaml"), <<~YAML)
86+
---
87+
a: value from per node data
88+
YAML
89+
90+
app.command_line.args << 'a'
91+
expect_lookup_with_output(0, /--- value from per node data/)
92+
end
93+
7594
it 'loads trusted information from the node certificate' do
7695
Puppet.settings[:node_terminus] = 'exec'
7796
expect_any_instance_of(Puppet::Node::Exec).to receive(:find) do |args|

0 commit comments

Comments
 (0)