2019-06-11 15:24:35 -07:00
|
|
|
require "inspec/fetcher"
|
|
|
|
require "inspec/metadata"
|
2016-02-21 13:26:31 +01:00
|
|
|
|
|
|
|
module SourceReaders
|
|
|
|
class InspecReader < Inspec.source_reader(1)
|
2019-06-11 15:24:35 -07:00
|
|
|
name "inspec"
|
2016-02-21 13:26:31 +01:00
|
|
|
priority 10
|
|
|
|
|
|
|
|
def self.resolve(target)
|
2019-06-11 15:24:35 -07:00
|
|
|
return new(target, "inspec.yml") if target.files.include?("inspec.yml")
|
2019-07-08 17:20:30 -07:00
|
|
|
|
2016-02-21 13:26:31 +01:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2017-04-26 03:07:39 +02:00
|
|
|
attr_reader :metadata, :tests, :libraries, :data_files
|
2016-02-21 13:26:31 +01:00
|
|
|
|
2016-11-08 02:01:15 -05:00
|
|
|
# This create a new instance of an InSpec profile source reader
|
|
|
|
#
|
2017-02-13 13:17:39 -05:00
|
|
|
# @param [FileProvider] target An instance of a FileProvider object that can list files and read them
|
2016-11-08 02:01:15 -05:00
|
|
|
# @param [String] metadata_source eg. inspec.yml or metadata.rb
|
2016-02-21 13:26:31 +01:00
|
|
|
def initialize(target, metadata_source)
|
2017-04-26 03:07:39 +02:00
|
|
|
@target = target
|
|
|
|
@metadata = load_metadata(metadata_source)
|
|
|
|
@tests = load_tests
|
|
|
|
@libraries = load_libs
|
|
|
|
@data_files = load_data_files
|
2016-02-21 13:26:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-03-08 13:22:24 -07:00
|
|
|
def load_metadata(metadata_source)
|
|
|
|
Inspec::Metadata.from_ref(
|
|
|
|
metadata_source,
|
|
|
|
@target.read(metadata_source),
|
2019-06-11 15:24:35 -07:00
|
|
|
nil
|
2017-11-21 02:49:41 -05:00
|
|
|
)
|
2017-03-08 13:22:24 -07: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
|
|
|
|
|
2019-08-09 15:21:47 -07:00
|
|
|
def find_all(regexp)
|
|
|
|
@target.files.grep(regexp)
|
2019-08-08 13:43:47 -07:00
|
|
|
end
|
|
|
|
|
2019-08-09 15:21:47 -07:00
|
|
|
def load_all(regexp)
|
|
|
|
find_all(regexp)
|
2019-08-09 14:57:34 -07:00
|
|
|
.map { |path| file = @target.read(path); [path, file] if file }
|
2019-08-08 14:21:38 -07:00
|
|
|
.compact
|
|
|
|
.to_h
|
2019-08-08 13:58:28 -07:00
|
|
|
end
|
|
|
|
|
2016-02-21 13:26:31 +01:00
|
|
|
def load_tests
|
2019-08-09 15:21:47 -07:00
|
|
|
load_all(/^controls\/.*\.rb$/)
|
2016-02-21 13:26:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_libs
|
2019-08-09 15:21:47 -07:00
|
|
|
load_all(/^libraries\/.*\.rb$/)
|
2016-02-21 13:26:31 +01:00
|
|
|
end
|
2017-04-26 03:07:39 +02:00
|
|
|
|
|
|
|
def load_data_files
|
2019-08-09 15:21:47 -07:00
|
|
|
load_all(/^files\//)
|
2017-04-26 03:07:39 +02:00
|
|
|
end
|
2016-02-21 13:26:31 +01:00
|
|
|
end
|
|
|
|
end
|