Class: Inspec::Fetcher::Gem

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/fetcher/gem.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, opts = {}) ⇒ Gem

Returns a new instance of Gem.



20
21
22
23
24
25
26
27
28
# File 'lib/inspec/fetcher/gem.rb', line 20

def initialize(target, opts = {})
  @target = target
  @gem_name = target[:gem]
  @version = target[:version] # optional
  @source = target[:source] # optional
  @gem_path = target[:path] # optional, sets local path installation mode
  @backend = opts[:backend]
  @archive_shasum = nil
end

Instance Attribute Details

#archive_pathObject (readonly)

Returns the value of attribute archive_path.



76
77
78
# File 'lib/inspec/fetcher/gem.rb', line 76

def archive_path
  @archive_path
end

Class Method Details

.resolve(target) ⇒ Object

Priority is used for setting precedence of fetchers. And registry plugin(v1) decides which fetcher to use for loading profiles by using this priority Gem fetcher’s priority should be lowest because gem profiles are only executables via inspec metadata



10
11
12
# File 'lib/inspec/fetcher/gem.rb', line 10

def self.resolve(target)
  resolve_from_hash(target) if target.is_a?(Hash) && target.key?(:gem)
end

.resolve_from_hash(target) ⇒ Object



14
15
16
17
18
# File 'lib/inspec/fetcher/gem.rb', line 14

def self.resolve_from_hash(target)
  return unless target.key?(:gem)

  new(target)
end

Instance Method Details

#cache_keyObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/inspec/fetcher/gem.rb', line 83

def cache_key
  # This special value is interpreted by Inspec::Cache.exists?
  # SHA256 on gem:gemname:version
  # SHA256 on gem_path:/filesystem/path/entrypoint.rb
  @cache_key ||= begin
    key = if @gem_path
            "gem_path:#{@gem_path}"
          else
            "gem:#{@gem_name}:#{gem_version}"
          end
    OpenSSL::Digest.hexdigest("SHA256", key)
  end
end

#fetch(path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/inspec/fetcher/gem.rb', line 30

def fetch(path)
  plugin_installer = Inspec::Plugin::V2::Installer.instance

  Inspec::Log.debug("GemFetcher fetching #{@gem_name} v" + (@version || "ANY"))

  have_plugin = if @version
                  plugin_installer.plugin_version_installed?(@gem_name, @version)
                else
                  plugin_installer.plugin_installed?(@gem_name)
                end

  unless have_plugin
    # Install
    # TODO - error handling?
    Inspec::Log.debug("GemFetcher - install request for #{@gem_name}")
    if @gem_path
      # No version permitted
      plugin_installer.install(@gem_name, gem: @gem_name, path: @gem_path)
    else
      # Passing an extra gem argument to enable detecting gem based plugins
      plugin_installer.install(@gem_name, version: @version, source: @source, gem: @gem_name)
    end
  end

  # Usually this `path` is cache path
  # We want to copy installed gem to cache path so it can be vendored
  # and read again as a cache
  if path
    loader = Inspec::Plugin::V2::Loader.new
    gem_dir_path = loader.find_gem_directory(@gem_name, @version)
    if gem_dir_path
      # Cache the gem file
      FileUtils.mkdir_p(path)
      FileUtils.cp_r(gem_dir_path, path)
      @archive_path = path
    else
      @archive_path = @target
    end
  end

  # Should the plugin activate? No, it should only be "fetched" (installed)
  # Activation would load resource libararies and would effectively execute the profile

  @target
end

#resolved_sourceObject



105
106
107
108
109
# File 'lib/inspec/fetcher/gem.rb', line 105

def resolved_source
  h = { gem: @gem_name, version: gem_version, gem_path: @gem_path }
  h[:sha256] = sha256
  h
end

#sha256Object

The intent here is to provide a signature that would change with the content of the profile. In the case of gems, for released gems, “name-version” should suffice. For development gems specified by path, the’re ever-changing anyway, so just give the path. In eith case, that string is in fact just the cache key.



101
102
103
# File 'lib/inspec/fetcher/gem.rb', line 101

def sha256
  cache_key
end

#writable?Boolean

Returns:

  • (Boolean)


78
79
80
81
# File 'lib/inspec/fetcher/gem.rb', line 78

def writable?
  # Gem based profile is not writable because it is not cached in lockfile
  false
end