Add support for alternate Gem sources for plugin installation/search

Signed-off-by: Thomas Heinen <theinen@tecracer.de>
This commit is contained in:
Thomas Heinen 2020-03-19 11:39:35 +01:00
parent dc4bc8701b
commit 61646ad3af
4 changed files with 57 additions and 12 deletions

View file

@ -47,6 +47,15 @@ $ inspec plugin install train-some-plugin
For more details on what the `plugin` command can do, see the [online help](https://www.inspec.io/docs/reference/cli/#plugin), or run `inspec plugin help`.
## How do I use a different Gem server?
You can specify an alternate source by passing the base of your Gem repository to the `--source` parameter:
```bash
$ inspec plugin search --source https://my.private.server inspec-private
$ inspec plugin install --source https://my.private.server inspec-private-plugin
```
## How do I write a plugin?
### Chef InSpec Plugins

View file

@ -3,6 +3,7 @@
require "singleton"
require "forwardable"
require "fileutils"
require "uri"
# Gem extensions for doing unusual things - not loaded by Gem default
require "rubygems/package"
@ -56,6 +57,7 @@ module Inspec::Plugin::V2
# @option opts [String] :gem_file Path to a local gem file to install from
# @option opts [String] :path Path to a file to be used as the entry point for a path-based plugin
# @option opts [String] :version Version constraint for remote gem installs
# @option opts [String] :source Alternate URL to use instead of rubygems.org
def install(plugin_name, opts = {})
# TODO: - check plugins.json for validity before trying anything that needs to modify it.
validate_installation_opts(plugin_name, opts)
@ -127,6 +129,11 @@ module Inspec::Plugin::V2
validate_search_opts(plugin_query, opts)
fetcher = Gem::SpecFetcher.fetcher
if opts[:source]
source_list = Gem::SourceList.from([opts[:source]])
fetcher = Gem::SpecFetcher.new(source_list)
end
matched_tuples = []
if opts[:exact]
matched_tuples = fetcher.detect(opts[:scope]) { |tuple| tuple.name == plugin_query }
@ -284,8 +291,22 @@ module Inspec::Plugin::V2
def install_from_remote_gems(requested_plugin_name, opts)
plugin_dependency = Gem::Dependency.new(requested_plugin_name, opts[:version] || "> 0")
# BestSet is rubygems.org API + indexing
install_gem_to_plugins_dir(plugin_dependency, [Gem::Resolver::BestSet.new], opts[:update_mode])
# BestSet is rubygems.org API + indexing, APISet is for custom sources
sources = if opts[:source]
Gem::Resolver::APISet.new(URI.join(opts[:source] + "/api/v1/dependencies"))
else
Gem::Resolver::BestSet.new
end
begin
install_gem_to_plugins_dir(plugin_dependency, [sources], opts[:update_mode])
rescue Gem::RemoteFetcher::FetchError => gem_ex
# TODO: Give a hint if the host was not resolvable or a 404 occured
ex = Inspec::Plugin::V2::InstallError.new(gem_ex.message)
ex.plugin_name = requested_plugin_name
raise ex
end
end
def install_gem_to_plugins_dir(new_plugin_dependency, # rubocop: disable Metrics/AbcSize

View file

@ -60,18 +60,19 @@ module InspecPlugins
# inspec plugin search
#==================================================================#
desc "search [options] PATTERN", "Searches rubygems.org for plugins."
desc "search [options] PATTERN", "Searches for plugins."
long_desc <<~EOLD
Searches rubygems.org for #{PRODUCT_NAME} plugins. Exits 0 on a search hit, 1 on user error,
Searches rubygems.org or alternate source for #{PRODUCT_NAME} plugins. Exits 0 on a search hit, 1 on user error,
2 on a search miss. PATTERN is a simple string; a wildcard will be added as
a suffix, unless -e is used.
EOLD
option :all, desc: "List all available versions, not just the latest one.", type: :boolean, aliases: [:a]
option :exact, desc: "Assume PATTERN is exact; do not add a wildcard to the end", type: :boolean, aliases: [:e]
option :'include-test-fixture', type: :boolean, desc: "Internal use", hide: true
option :source, type: :string, desc: "URL of repository, defaults to https://rubygems.org", aliases: [:s]
# Justification for disabling ABC: currently at 33.51/33
def search(search_term) # rubocop: disable Metrics/AbcSize
search_results = installer.search(search_term, exact: options[:exact])
search_results = installer.search(search_term, exact: options[:exact], source: options[:source])
# The search results have already been filtered by the reject list. But the
# RejectList doesn't filter {inspec, train}-test-fixture because we need those
# for testing. We want to hide those from users, so unless we know we're in
@ -102,9 +103,9 @@ module InspecPlugins
#==================================================================#
# inspec plugin install
#==================================================================#
desc "install [-v VERSION] PLUGIN", "Installs a plugin from rubygems.org, a gemfile, or a path to local source."
desc "install [options] PLUGIN", "Installs a plugin from rubygems.org, a gemfile, or a path to local source."
long_desc <<~EOLD
PLUGIN may be the name of a gem on rubygems.org that begins with inspec- or train-.
PLUGIN may be the name of a gem on rubygems.org (or an alternate source) that begins with `inspec-` or `train-`.
PLUGIN may also be the path to a local gemfile, which will then be installed like
any other gem. Finally, if PLUGIN is a path ending in .rb, it is taken to be a
local file that will act as athe entry point for a plugin (this mode is provided
@ -112,6 +113,7 @@ module InspecPlugins
already installed, and 1 if any other error occurs.
EOLD
option :version, desc: "When installing from rubygems.org, specifies a specific version to install.", aliases: [:v]
option :source, type: :string, desc: "URL of repository, defaults to https://rubygems.org", aliases: [:s]
def install(plugin_id_arg)
if plugin_id_arg =~ /\.gem$/ # Does it end in .gem?
install_from_gemfile(plugin_id_arg)
@ -407,7 +409,7 @@ module InspecPlugins
# Rationale for RuboCop variance: This is a one-line method with heavy UX-focused error handling.
def install_attempt_install(plugin_name) # rubocop: disable Metrics/AbcSize
installer.install(plugin_name, version: options[:version])
installer.install(plugin_name, version: options[:version], source: options[:source])
rescue Inspec::Plugin::V2::PluginExcludedError => ex
ui.red("Plugin on Exclusion List - #{plugin_name} is listed as an " \
"incompatible gem - refusing to install.\n")
@ -422,12 +424,13 @@ module InspecPlugins
raise if Inspec::Log.level == :debug
results = installer.search(plugin_name, exact: true)
source_host = URI(options[:source] || "https://rubygems.org/").host
if results.empty?
ui.red("No such plugin gem #{plugin_name} could be found on " \
"rubygems.org - installation failed.\n")
"#{source_host} - installation failed.\n")
elsif options[:version] && !results[plugin_name].include?(options[:version])
ui.red("No such version - #{plugin_name} exists, but no such " \
"version #{options[:version]} found on rubygems.org - " \
"version #{options[:version]} found on #{source_host} - " \
"installation failed.\n")
else
ui.red("Unknown error occured - installation failed.\n")

View file

@ -28,7 +28,7 @@ class PluginManagerCliOptions < Minitest::Test
def test_search_args
arg_config = cli_class.all_commands["search"].options
assert_equal 3, arg_config.count, "The search command should have 3 options"
assert_equal 4, arg_config.count, "The search command should have 4 options"
assert_includes arg_config.keys, :all, "The search command should have an --all option"
assert_equal :boolean, arg_config[:all].type, "The --all option should be boolean"
@ -42,6 +42,12 @@ class PluginManagerCliOptions < Minitest::Test
refute_nil arg_config[:exact].description, "The --exact option should have a description"
refute arg_config[:exact].required, "The --exact option should not be required"
assert_includes arg_config.keys, :source, "The search command should have a --source option"
assert_equal :string, arg_config[:source].type, "The --source option should be a string"
assert_equal :s, arg_config[:source].aliases.first, "The --source option should be aliased as -s"
refute_nil arg_config[:source].description, "The --source option should have a description"
refute arg_config[:source].required, "The --source option should not be required"
assert_includes arg_config.keys, :'include-test-fixture', "The search command should have an --include-test-fixture option"
assert_equal :boolean, arg_config[:'include-test-fixture'].type, "The --include-test-fixture option should be boolean"
refute arg_config[:'include-test-fixture'].required, "The --include-test-fixture option should not be required"
@ -51,7 +57,7 @@ class PluginManagerCliOptions < Minitest::Test
def test_install_args
arg_config = cli_class.all_commands["install"].options
assert_equal 1, arg_config.count, "The install command should have 1 option"
assert_equal 2, arg_config.count, "The install command should have 2 options"
assert_includes arg_config.keys, :version, "The install command should have a --version option"
assert_equal :string, arg_config[:version].type, "The --version option should be a string"
@ -59,6 +65,12 @@ class PluginManagerCliOptions < Minitest::Test
refute_nil arg_config[:version].description, "The --version option should have a description"
refute arg_config[:version].required, "The --version option should not be required"
assert_includes arg_config.keys, :source, "The install command should have a --source option"
assert_equal :string, arg_config[:source].type, "The --source option should be a string"
assert_equal :s, arg_config[:source].aliases.first, "The --source option should be aliased as -s"
refute_nil arg_config[:source].description, "The --source option should have a description"
refute arg_config[:source].required, "The --source option should not be required"
assert_equal 1, cli_class.instance_method(:install).arity, "The install command should take one argument"
end