Skip to content

Commit 278cbfb

Browse files
committed
Detect Hiera files that are no valid hash
This was reported on slack via @ikonia. with such malformed keys, Hiera just reports a string: ``` irb(main):002> require 'yaml' => true irb(main):003> yamlargs = (Psych::VERSION >= '4.0') ? { aliases: true } : {} => {aliases: true} irb(main):004> hiera_file='spec/fixtures/hiera/hiera_key_no_value.yaml' => "spec/fixtures/hiera/hiera_key_no_value.yaml" irb(main):005> YAML.load_file(hiera_file, **yamlargs) => "prometheus::initstyle:sysv prometheus::node_exporter::init_style:sysv" irb(main):006> ``` And puppetserver will fail to compile a catalog: ``` Notice: Catalog compiled by *** Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Resource Statement, /etc/puppetlabs/code/environments/***.yaml: file does not contain a valid yaml hash on node *** Warning: Not using cache on failed catalog ```
1 parent 26e931d commit 278cbfb

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/puppet-syntax/hiera.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ def check(filelist)
9292
end
9393
next unless yamldata
9494

95+
unless yamldata.is_a?(Hash)
96+
errors << "ERROR: #{hiera_file} doesn't contain a valid Hash, datatype is #{yamldata.class}"
97+
next
98+
end
99+
95100
yamldata.each do |k, v|
96101
if PuppetSyntax.check_hiera_keys
97102
key_msg = check_hiera_key(k)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
prometheus::initstyle:sysv
2+
prometheus::node_exporter::init_style:sysv

spec/puppet-syntax/hiera_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
expect(res.first).to match(expected)
2222
end
2323

24+
it 'returns warnings on malformed keys' do
25+
files = fixture_hiera('hiera_key_no_value.yaml')
26+
expected = /ERROR: #{files[0]} doesn't contain a valid Hash, datatype is/
27+
res = subject.check(files)
28+
expect(res.size).to eq 1
29+
expect(res.first).to match(expected)
30+
end
31+
2432
context 'check_hiera_keys = true' do
2533
before do
2634
PuppetSyntax.check_hiera_keys = true

0 commit comments

Comments
 (0)