2016-02-21 12:26:31 +00:00
|
|
|
# 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')
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2017-04-26 01:07:39 +00:00
|
|
|
attr_reader :metadata, :tests, :libraries, :data_files
|
2016-02-21 12:26:31 +00:00
|
|
|
|
2016-11-08 07:01:15 +00:00
|
|
|
# This create a new instance of an InSpec profile source reader
|
|
|
|
#
|
2017-02-13 18:17:39 +00:00
|
|
|
# @param [FileProvider] target An instance of a FileProvider object that can list files and read them
|
2016-11-08 07:01:15 +00:00
|
|
|
# @param [String] metadata_source eg. inspec.yml or metadata.rb
|
2016-02-21 12:26:31 +00:00
|
|
|
def initialize(target, metadata_source)
|
2017-04-26 01:07:39 +00:00
|
|
|
@target = target
|
|
|
|
@metadata = load_metadata(metadata_source)
|
|
|
|
@tests = load_tests
|
|
|
|
@libraries = load_libs
|
|
|
|
@data_files = load_data_files
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-03-08 20:22:24 +00:00
|
|
|
def load_metadata(metadata_source)
|
|
|
|
Inspec::Metadata.from_ref(
|
|
|
|
metadata_source,
|
|
|
|
@target.read(metadata_source),
|
2017-11-21 07:49:41 +00:00
|
|
|
nil,
|
|
|
|
)
|
2017-03-08 20:22:24 +00:00
|
|
|
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
|
|
|
|
|
2016-02-21 12:26:31 +00:00
|
|
|
def load_tests
|
|
|
|
tests = @target.files.find_all do |path|
|
2016-11-25 22:48:22 +00:00
|
|
|
path.start_with?('controls') && path.end_with?('.rb')
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|
2017-07-11 19:33:55 +00:00
|
|
|
Hash[tests.map { |x| [x, @target.read(x)] }.delete_if { |_file, contents| contents.nil? }]
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_libs
|
|
|
|
tests = @target.files.find_all do |path|
|
|
|
|
path.start_with?('libraries') && path.end_with?('.rb')
|
|
|
|
end
|
2017-07-11 19:33:55 +00:00
|
|
|
Hash[tests.map { |x| [x, @target.read(x)] }.delete_if { |_file, contents| contents.nil? }]
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|
2017-04-26 01:07:39 +00:00
|
|
|
|
|
|
|
def load_data_files
|
|
|
|
files = @target.files.find_all do |path|
|
|
|
|
path.start_with?('files' + File::SEPARATOR)
|
|
|
|
end
|
2017-07-11 19:33:55 +00:00
|
|
|
Hash[files.map { |x| [x, @target.read(x)] }.delete_if { |_file, contents| contents.nil? }]
|
2017-04-26 01:07:39 +00:00
|
|
|
end
|
2016-02-21 12:26:31 +00:00
|
|
|
end
|
|
|
|
end
|