inspec/lib/source_readers/inspec.rb
Adam Leff 9580732814 Source reader should not hand back files with nil contents (#2003)
If a profile has a data files directory that looks like this:

```
files/platforms/one/data.json
files/platforms/two/data.json
files/platforms/three/data.json
```

... the source reader will return the directories in the list of files but with
nil contents. This causes an issue when Inspec::Profile tries to create a sha256
checksum of the profile contents only to try to cast nil to a string when
building the null-delimited profile contents string.

Files that are empty will have an empty string as its contents, so it's safe to
assume that file entries with nil contents are actually a directory and have no
affect on the profile's checksum. Therefore, this change will eliminate any file
entries in responses from the source readers where the contents are nil.

Signed-off-by: Adam Leff <adam@leff.co>
2017-07-11 21:33:55 +02:00

74 lines
2.2 KiB
Ruby

# encoding: utf-8
# author: Dominik Richter
# author: Christoph Hartmann
require 'inspec/fetcher'
require 'inspec/metadata'
module SourceReaders
class InspecReader < Inspec.source_reader(1)
name 'inspec'
priority 10
def self.resolve(target)
return new(target, 'inspec.yml') if target.files.include?('inspec.yml')
# TODO: deprecated for 1.0.0 release
if target.files.include?('metadata.rb') &&
(
target.files.include?('controls') ||
target.files.include?('test')
)
return new(target, 'metadata.rb')
end
nil
end
attr_reader :metadata, :tests, :libraries, :data_files
# This create a new instance of an InSpec profile source reader
#
# @param [FileProvider] target An instance of a FileProvider object that can list files and read them
# @param [String] metadata_source eg. inspec.yml or metadata.rb
def initialize(target, metadata_source)
@target = target
@metadata = load_metadata(metadata_source)
@tests = load_tests
@libraries = load_libs
@data_files = load_data_files
end
private
def load_metadata(metadata_source)
Inspec::Metadata.from_ref(
metadata_source,
@target.read(metadata_source),
nil)
rescue Psych::SyntaxError => e
raise "Unable to parse inspec.yml: line #{e.line}, #{e.problem} #{e.context}"
rescue => e
raise "Unable to parse #{metadata_source}: #{e.class} -- #{e.message}"
end
def load_tests
tests = @target.files.find_all do |path|
path.start_with?('controls') && path.end_with?('.rb')
end
Hash[tests.map { |x| [x, @target.read(x)] }.delete_if { |_file, contents| contents.nil? }]
end
def load_libs
tests = @target.files.find_all do |path|
path.start_with?('libraries') && path.end_with?('.rb')
end
Hash[tests.map { |x| [x, @target.read(x)] }.delete_if { |_file, contents| contents.nil? }]
end
def load_data_files
files = @target.files.find_all do |path|
path.start_with?('files' + File::SEPARATOR)
end
Hash[files.map { |x| [x, @target.read(x)] }.delete_if { |_file, contents| contents.nil? }]
end
end
end