Class: Inspec::Plugin::V2::Loader

Inherits:
Object
  • Object
show all
Extended by:
FilterPredicates
Includes:
FilterPredicates, GemSpecHelper
Defined in:
lib/inspec/plugin/v2/loader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FilterPredicates

inspec_plugin_name?, train_plugin_name?, valid_plugin_name?

Methods included from GemSpecHelper

#loaded_recent_most_version_of?

Constructor Details

#initialize(options = {}) ⇒ Loader

Returns a new instance of Loader.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/inspec/plugin/v2/loader.rb', line 16

def initialize(options = {})
  @options = options
  @registry = Inspec::Plugin::V2::Registry.instance

  # User plugins are those installed by the user via `inspec plugin install`
  # and are installed under ~/.inspec/gems
  unless options[:omit_user_plugins]
    @conf_file = Inspec::Plugin::V2::ConfigFile.new
    read_conf_file_into_registry
  end

  # Old-style (v0, v1) co-distributed plugins were called 'bundles'
  # and were located in lib/bundles
  detect_bundled_plugins unless options[:omit_bundles]

  # New-style (v2) co-distributed plugins are in lib/plugins,
  # and may be safely loaded
  detect_core_plugins unless options[:omit_core_plugins]

  # Identify plugins that inspec is co-installed with
  detect_system_plugins unless options[:omit_sys_plugins]

  # Train plugins are not true InSpec plugins; we need to decorate them a
  # bit more to integrate them. Wait to do this until after we know if
  # they are system or user.
  registry.each do |plugin_name, status|
    fixup_train_plugin_status(status) if train_plugin_name?(plugin_name)
  end
end

Instance Attribute Details

#conf_fileObject (readonly)

Returns the value of attribute conf_file.



9
10
11
# File 'lib/inspec/plugin/v2/loader.rb', line 9

def conf_file
  @conf_file
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/inspec/plugin/v2/loader.rb', line 9

def options
  @options
end

#registryObject (readonly)

Returns the value of attribute registry.



9
10
11
# File 'lib/inspec/plugin/v2/loader.rb', line 9

def registry
  @registry
end

Class Method Details

.find_gem_directory(gem_name, version = nil) ⇒ Object



164
165
166
167
# File 'lib/inspec/plugin/v2/loader.rb', line 164

def self.find_gem_directory(gem_name, version = nil)
  selected_gemspec = find_gemspec_of(gem_name, version)
  selected_gemspec&.full_gem_path
end

.find_gemspec_directory(gem_name, version = nil) ⇒ Object



169
170
171
172
# File 'lib/inspec/plugin/v2/loader.rb', line 169

def self.find_gemspec_directory(gem_name, version = nil)
  selected_gemspec = find_gemspec_of(gem_name, version)
  selected_gemspec&.loaded_from
end

.find_gemspec_of(gem_name, version = nil) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/inspec/plugin/v2/loader.rb', line 174

def self.find_gemspec_of(gem_name, version = nil)
  version = Gem::Version.new(version) if version && !version.is_a?(Gem::Version)

  list_managed_gems
    .select { |g| g.name == gem_name }
    .sort_by(&:version)
    .yield_self { |gems| version ? gems.find { |g| g.version == version } : gems.last }
end

.list_installed_plugin_gemsArray[Gem::Specification]

Lists all plugin gems found in the plugin_gem_path. This is simply all gems that begin with train- or inspec- and are not on the exclusion list.

Returns:

  • (Array[Gem::Specification])

    Specs of all gems found.



195
196
197
# File 'lib/inspec/plugin/v2/loader.rb', line 195

def self.list_installed_plugin_gems
  list_managed_gems.select { |spec| valid_plugin_name?(spec.name) }
end

.list_managed_gemsArray[Gem::Specification]

Lists all gems found in the plugin_gem_path.

Returns:

  • (Array[Gem::Specification])

    Specs of all gems found.



156
157
158
# File 'lib/inspec/plugin/v2/loader.rb', line 156

def self.list_managed_gems
  Dir.glob(File.join(plugin_gem_path, "specifications", "*.gemspec")).map { |p| Gem::Specification.load(p) }
end

.plugin_gem_pathObject



145
146
147
148
149
150
151
152
# File 'lib/inspec/plugin/v2/loader.rb', line 145

def self.plugin_gem_path
  require "rbconfig" unless defined?(RbConfig)
  ruby_abi_version = RbConfig::CONFIG["ruby_version"]
  # TODO: why are we installing under the api directory for plugins?
  base_dir = Inspec.config_dir
  base_dir = File.realpath base_dir if File.exist? base_dir
  File.join(base_dir, "gems", ruby_abi_version)
end

Instance Method Details

#activate_managed_gems_for_plugin(plugin_gem_name, version_constraint = "> 0") ⇒ Object

‘Activating’ a gem adds it to the load path, so ‘require “gemname”’ will work. Given a gem name, this activates the gem and all of its dependencies, respecting version pinning needs.



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/inspec/plugin/v2/loader.rb', line 206

def activate_managed_gems_for_plugin(plugin_gem_name, version_constraint = "> 0")
  # TODO: enforce first-level version pinning
  plugin_deps = [Gem::Dependency.new(plugin_gem_name.to_s, version_constraint)]
  managed_gem_set = Gem::Resolver::VendorSet.new

  list_managed_gems.each do |spec|
    if Gem::Specification.load spec.gem_dir
      managed_gem_set.add_vendor_gem(spec.name, spec.gem_dir)
    else
      # In case of invalid gemspec as mentioned in this PR https://github.com/brianmario/yajl-ruby/pull/223
      # the add_vendor_gem breaks. So this is patch to fix the loading issue.
      # Horribly, chdir to gemspec path to honor . in gemspec
      Dir.chdir(spec.gem_dir) do |dir|
        managed_gem_set.add_vendor_gem(spec.name, spec.gem_dir)
      end
    end
  end

  # TODO: Next two lines merge our managed gems with the other gems available
  # in our "local universe" - which may be the system, or it could be in a Bundler microcosm,
  # or rbenv, etc. Do we want to merge that, though?
  distrib_gem_set = Gem::Resolver::CurrentSet.new
  installed_gem_set = Gem::Resolver.compose_sets(managed_gem_set, distrib_gem_set)

  # So, given what we need, and what we have available, what activations are needed?
  resolver = Gem::Resolver.new(plugin_deps, installed_gem_set)
  begin
    solution = resolver.resolve
  rescue Gem::UnsatisfiableDependencyError => gem_ex
    # If you broke your install, or downgraded to a plugin with a bad gemspec, you could get here.
    ex = Inspec::Plugin::V2::LoadError.new(gem_ex.message)
    raise ex
  end
  solution.each do |activation_request|
    requested_gemspec = activation_request.full_spec
    next if requested_gemspec.activated?

    requested_gemspec.activate unless loaded_recent_most_version_of?(requested_gemspec)
  end
end

#activate_mentioned_cli_plugins(cli_args = ARGV) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/inspec/plugin/v2/loader.rb', line 105

def activate_mentioned_cli_plugins(cli_args = ARGV)
  # Get a list of CLI plugin activation hooks
  registry.find_activators(plugin_type: :cli_command).each do |act|
    next if act.activated?

    # Decide whether to activate.  Several conditions, so split them out for clarity.
    # Assume no, to start.  Each condition may flip it true, which will short-circuit
    # all following ||= ops.
    activate_me = false

    # If the user invoked `inspec help`, `inspec --help`, or only `inspec`
    # then activate all CLI plugins so they can display their usage message.
    activate_me ||= ["help", "--help", nil].include?(cli_args.first)

    # If there is anything in the CLI args with the same name, activate it.
    # This is the expected usual activation for individual plugins.
    # `inspec dosomething` => activate the :dosomething hook
    activate_me ||= cli_args.include?(act.activator_name.to_s)

    # Only one compliance command to be activated at one time.
    # Since both commands are defined in the same class,
    # activators were not getting fetched uniquely.
    if cli_args.include?("automate") && act.activator_name.to_s.eql?("compliance")
      activate_me = false
    elsif cli_args.include?("compliance") && act.activator_name.to_s.eql?("automate")
      activate_me = false
    end

    # OK, activate.
    if activate_me
      act.activate
      act.implementation_class.register_with_thor
    end
  end
end

#exit_on_load_errorObject

This should possibly be in either lib/inspec/cli.rb or Registry



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/inspec/plugin/v2/loader.rb', line 89

def exit_on_load_error
  if registry.any_load_failures?
    Inspec::Log.error "Errors were encountered while loading plugins..."
    registry.plugin_statuses.select(&:load_exception).each do |plugin_status|
      Inspec::Log.error "Plugin name: " + plugin_status.name.to_s
      Inspec::Log.error "Error: " + plugin_status.load_exception.message
      if ARGV.include?("--debug")
        Inspec::Log.error "Exception: " + plugin_status.load_exception.class.name
        Inspec::Log.error "Trace: " + plugin_status.load_exception.backtrace.join("\n")
      end
    end
    Inspec::Log.error("Run again with --debug for a stacktrace.") unless ARGV.include?("--debug")
    exit 2
  end
end

#find_gem_directory(gem_name, version = nil) ⇒ Object



187
188
189
# File 'lib/inspec/plugin/v2/loader.rb', line 187

def find_gem_directory(gem_name, version = nil)
  self.class.find_gem_directory(gem_name, version)
end

#find_gemspec_directory(gem_name, version = nil) ⇒ Object



183
184
185
# File 'lib/inspec/plugin/v2/loader.rb', line 183

def find_gemspec_directory(gem_name, version = nil)
  self.class.find_gemspec_directory(gem_name, version)
end

#list_installed_plugin_gemsObject



199
200
201
# File 'lib/inspec/plugin/v2/loader.rb', line 199

def list_installed_plugin_gems
  self.class.list_managed_gems
end

#list_managed_gemsObject



160
161
162
# File 'lib/inspec/plugin/v2/loader.rb', line 160

def list_managed_gems
  self.class.list_managed_gems
end

#load_allObject



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
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/inspec/plugin/v2/loader.rb', line 46

def load_all
  # This fixes the gem paths on some bundles
  Gem.path << plugin_gem_path
  Gem.refresh

  # Be careful not to actually iterate directly over the registry here;
  # we want to allow "sidecar loading", in which case a plugin may add an entry to the registry.
  registry.plugin_names.dup.each do |plugin_name|
    plugin_details = registry[plugin_name]

    # Under some conditions (kitchen-inspec with multiple test suites, for example), this may be
    # called multple times. Don't reload anything.
    next if plugin_details.loaded

    # We want to capture literally any possible exception here, since we are storing them.
    # rubocop: disable Lint/RescueException
    begin
      # We could use require, but under testing, we need to repeatedly reload the same
      # plugin.  However, gems only work with require (rubygems dooes not overload `load`)
      case plugin_details.installation_type
      when :user_gem
        activate_managed_gems_for_plugin(plugin_name)
        require plugin_details.entry_point
      when :system_gem
        require plugin_details.entry_point
      else
        load_path = plugin_details.entry_point
        next if load_path.end_with?(".gem") # local resource pack's path will have gem path

        load_path += ".rb" unless load_path.end_with?(".rb")
        load load_path
      end
      plugin_details.loaded = true
      annotate_status_after_loading(plugin_name)
    rescue ::Exception => ex
      plugin_details.load_exception = ex
      Inspec::Log.error "Could not load plugin #{plugin_name}: #{ex.message}"
    end
    # rubocop: enable Lint/RescueException
  end
end

#plugin_gem_pathObject



141
142
143
# File 'lib/inspec/plugin/v2/loader.rb', line 141

def plugin_gem_path
  self.class.plugin_gem_path
end