Sha256: 1038f846902b90b7e64efec84e249f568dfc5f42013b8281f4216a7c91d8b78e

Contents?: true

Size: 1.61 KB

Versions: 60

Compression:

Stored size: 1.61 KB

Contents

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

    attr_reader :metadata, :tests, :libraries, :data_files, :target

    # 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 find_all(regexp)
      @target.files.grep(regexp)
    end

    def load_all(regexp)
      find_all(regexp)
        .map { |path| file = @target.read(path); [path, file] if file }
        .compact
        .to_h
    end

    def load_tests
      load_all(%r{^controls/.*\.rb$})
    end

    def load_libs
      load_all(%r{^libraries/.*\.rb$})
    end

    def load_data_files
      load_all(%r{^files/})
    end
  end
end

Version data entries

60 entries across 60 versions & 1 rubygems

Version Path
inspec-core-4.56.58 lib/source_readers/inspec.rb
inspec-core-5.17.4 lib/source_readers/inspec.rb
inspec-core-5.14.0 lib/source_readers/inspec.rb
inspec-core-4.56.20 lib/source_readers/inspec.rb
inspec-core-5.12.2 lib/source_readers/inspec.rb
inspec-core-5.10.5 lib/source_readers/inspec.rb
inspec-core-4.56.19 lib/source_readers/inspec.rb
inspec-core-5.7.9 lib/source_readers/inspec.rb
inspec-core-4.56.17 lib/source_readers/inspec.rb
inspec-core-4.52.9 lib/source_readers/inspec.rb
inspec-core-4.50.3 lib/source_readers/inspec.rb
inspec-core-4.49.0 lib/source_readers/inspec.rb
inspec-core-4.46.13 lib/source_readers/inspec.rb
inspec-core-4.41.20 lib/source_readers/inspec.rb
inspec-core-4.41.2 lib/source_readers/inspec.rb
inspec-core-4.38.9 lib/source_readers/inspec.rb
inspec-core-4.38.3 lib/source_readers/inspec.rb
inspec-core-4.37.30 lib/source_readers/inspec.rb
inspec-core-4.37.25 lib/source_readers/inspec.rb
inspec-core-4.37.23 lib/source_readers/inspec.rb