class SourceReaders::InspecReader

Attributes

data_files[R]
libraries[R]
metadata[R]
target[R]
tests[R]

Public Class Methods

new(target, metadata_source) click to toggle source

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

# File lib/source_readers/inspec.rb, line 21
def initialize(target, metadata_source)
  @target     = target
  @metadata   = load_metadata(metadata_source)
  @tests      = load_tests
  @libraries  = load_libs
  @data_files = load_data_files
end
resolve(target) click to toggle source
# File lib/source_readers/inspec.rb, line 9
def self.resolve(target)
  return new(target, "inspec.yml") if target.files.include?("inspec.yml")

  nil
end

Private Instance Methods

find_all(regexp) click to toggle source
# File lib/source_readers/inspec.rb, line 43
def find_all(regexp)
  @target.files.grep(regexp)
end
load_all(regexp) click to toggle source
# File lib/source_readers/inspec.rb, line 47
def load_all(regexp)
  find_all(regexp)
    .map { |path| file = @target.read(path); [path, file] if file }
    .compact
    .to_h
end
load_data_files() click to toggle source
# File lib/source_readers/inspec.rb, line 62
def load_data_files
  load_all(%r{^files/})
end
load_libs() click to toggle source
# File lib/source_readers/inspec.rb, line 58
def load_libs
  load_all(%r{^libraries/.*\.rb$})
end
load_metadata(metadata_source) click to toggle source
# File lib/source_readers/inspec.rb, line 31
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
load_tests() click to toggle source
# File lib/source_readers/inspec.rb, line 54
def load_tests
  load_all(%r{^controls/.*\.rb$})
end