Skip to content

Commit

Permalink
Merge pull request #2616 from mhashizume/FACT-3433/main/external-fact…
Browse files Browse the repository at this point in the history
…-validation

(FACT-3433) Log error if external fact is invalid
  • Loading branch information
joshcooper authored Oct 4, 2023
2 parents 421f973 + 47a729a commit a866afe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/facter/custom_facts/util/directory_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ def load_searched_facts(collection, searched_facts, weight)
if data == false
log.warn "Could not interpret fact file #{fact.file}"
elsif (data == {}) || data.nil?
log.debug("Fact file #{fact.file} was parsed but no key=>value data was returned")
log.debug(
"Structured data fact file #{fact.file} was parsed but was either empty or an invalid filetype "\
'(valid filetypes are .yaml, .json, and .txt).'
)
elsif !data.is_a?(Hash)
log.error("Structured data fact file #{fact.file} was parsed but no key=>value data was returned.")
else
add_data(data, collection, fact, weight)
end
Expand Down
25 changes: 25 additions & 0 deletions spec/custom_facts/util/directory_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@
expect(collection_double).to have_received(:add).with('f1', value: 'one', fact_type: :external, file: file)
end

it 'ignores when a structured data fact contains empty hash' do
data = {}
write_to_file('data.yaml', YAML.dump(data))
file = File.join(dir_loader.directories[0], 'data.yaml')

expect(log_spy).to receive(:debug).with(
"Structured data fact file #{file} was parsed but was either empty or an invalid filetype (valid filetypes "\
'are .yaml, .json, and .txt).'
)

dir_loader.load(collection)
end

it 'ignores when fact with external type contains invalid data type' do
data = 'foo'
write_to_file('data.yaml', YAML.dump(data))
file = File.join(dir_loader.directories[0], 'data.yaml')

expect(log_spy).to receive(:error).with(
"Structured data fact file #{file} was parsed but no key=>value data was returned."
)

dir_loader.load(collection)
end

it "ignores files that begin with '.'" do
not_to_be_used_collection = double('collection should not be used')
expect(not_to_be_used_collection).not_to receive(:add)
Expand Down

0 comments on commit a866afe

Please sign in to comment.