Sha256: 556716b8c8b5494e81d23e417523fa8dc6cf57513f69e6d4bc7dba9276df1ef3

Contents?: true

Size: 1.69 KB

Versions: 125

Compression:

Stored size: 1.69 KB

Contents

# encoding: utf-8
require 'inspec/fetcher'
require 'forwardable'

module Inspec
  class CachedFetcher
    extend Forwardable

    attr_reader :cache, :target, :fetcher
    def initialize(target, cache)
      @target = target
      @fetcher = Inspec::Fetcher.resolve(target)

      if @fetcher.nil?
        raise("Could not fetch inspec profile in #{target.inspect}.")
      end

      @cache = cache
    end

    def resolved_source
      fetch
      @fetcher.resolved_source
    end

    def cache_key
      k = if target.is_a?(Hash)
            target[:sha256] || target[:ref]
          end

      if k.nil?
        fetcher.cache_key
      else
        k
      end
    end

    def fetch
      if cache.exists?(cache_key)
        Inspec::Log.debug "Using cached dependency for #{target}"
        [cache.prefered_entry_for(cache_key), false]
      else
        Inspec::Log.debug "Dependency does not exist in the cache #{target}"
        fetcher.fetch(cache.base_path_for(fetcher.cache_key))
        assert_cache_sanity!
        [fetcher.archive_path, fetcher.writable?]
      end
    end

    def assert_cache_sanity!
      return unless target.respond_to?(:key?) && target.key?(:sha256)

      exception_message = <<~EOF
        The remote source #{fetcher} no longer has the requested content:

        Request Content Hash: #{target[:sha256]}
        Actual Content Hash: #{fetcher.resolved_source[:sha256]}

        For URL, supermarket, compliance, and other sources that do not
        provide versioned artifacts, this likely means that the remote source
        has changed since your lockfile was generated.
      EOF
      raise exception_message if fetcher.resolved_source[:sha256] != target[:sha256]
    end
  end
end

Version data entries

125 entries across 125 versions & 2 rubygems

Version Path
inspec-core-4.3.2 lib/inspec/cached_fetcher.rb
inspec-4.3.2 lib/inspec/cached_fetcher.rb
inspec-core-4.2.0.preview lib/inspec/cached_fetcher.rb
inspec-4.2.0.preview lib/inspec/cached_fetcher.rb
inspec-core-4.1.4.preview lib/inspec/cached_fetcher.rb
inspec-4.1.4.preview lib/inspec/cached_fetcher.rb
inspec-core-3.9.3 lib/inspec/cached_fetcher.rb
inspec-3.9.3 lib/inspec/cached_fetcher.rb
inspec-core-3.9.0 lib/inspec/cached_fetcher.rb
inspec-3.9.0 lib/inspec/cached_fetcher.rb
inspec-core-3.7.11 lib/inspec/cached_fetcher.rb
inspec-3.7.11 lib/inspec/cached_fetcher.rb
inspec-core-3.7.1 lib/inspec/cached_fetcher.rb
inspec-3.7.1 lib/inspec/cached_fetcher.rb
inspec-core-3.6.6 lib/inspec/cached_fetcher.rb
inspec-3.6.6 lib/inspec/cached_fetcher.rb
inspec-core-3.6.4 lib/inspec/cached_fetcher.rb
inspec-3.6.4 lib/inspec/cached_fetcher.rb
inspec-core-2.3.28 lib/inspec/cached_fetcher.rb
inspec-2.3.28 lib/inspec/cached_fetcher.rb