Class: Inspec::CachedFetcher

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/inspec/cached_fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, cache) ⇒ CachedFetcher

Returns a new instance of CachedFetcher.



10
11
12
13
14
15
16
17
18
19
# File 'lib/inspec/cached_fetcher.rb', line 10

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



9
10
11
# File 'lib/inspec/cached_fetcher.rb', line 9

def cache
  @cache
end

#fetcherObject (readonly)

Returns the value of attribute fetcher.



9
10
11
# File 'lib/inspec/cached_fetcher.rb', line 9

def fetcher
  @fetcher
end

#targetObject (readonly)

Returns the value of attribute target.



9
10
11
# File 'lib/inspec/cached_fetcher.rb', line 9

def target
  @target
end

Instance Method Details

#assert_cache_sanity!Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/inspec/cached_fetcher.rb', line 50

def assert_cache_sanity!
  if target.respond_to?(:key?) && target.key?(:sha256)
    if fetcher.resolved_source[:sha256] != target[:sha256]
      raise <<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
    end
  end
end

#cache_keyObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/inspec/cached_fetcher.rb', line 26

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

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

#fetchObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/inspec/cached_fetcher.rb', line 38

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

#resolved_sourceObject



21
22
23
24
# File 'lib/inspec/cached_fetcher.rb', line 21

def resolved_source
  fetch
  @fetcher.resolved_source
end