Skip to content

Commit

Permalink
Merge pull request #3751 from inspec/cw/plugins-add-no-user-plugins-o…
Browse files Browse the repository at this point in the history
…ption

Add CLI options to disable loading plugins
  • Loading branch information
clintoncwolfe committed Feb 1, 2019
2 parents 167ada9 + 5d2934c commit 3612a21
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/inspec/cli.rb
Expand Up @@ -32,6 +32,12 @@ class Inspec::InspecCLI < Inspec::BaseCLI
class_option :interactive, type: :boolean,
desc: 'Allow or disable user interaction'

class_option :disable_core_plugins, type: :string, banner: '', # Actually a boolean, but this suppresses the creation of a --no-disable...
desc: 'Disable loading all plugins that are shipped in the lib/plugins directory of InSpec. Useful in development.'

class_option :disable_user_plugins, type: :string, banner: '',
desc: 'Disable loading all plugins that the user installed.'

desc 'json PATH', 'read all tests in PATH and generate a JSON summary'
option :output, aliases: :o, type: :string,
desc: 'Save the created profile to a path'
Expand Down Expand Up @@ -378,8 +384,10 @@ def run_command(opts)
end
end

# Load v2 plugins
v2_loader = Inspec::Plugin::V2::Loader.new
# Load v2 plugins. Manually check for plugin disablement.
omit_core = ARGV.delete('--disable-core-plugins')
omit_user = ARGV.delete('--disable-user-plugins')
v2_loader = Inspec::Plugin::V2::Loader.new(omit_core_plugins: omit_core, omit_user_plugins: omit_user)
v2_loader.load_all
v2_loader.exit_on_load_error
v2_loader.activate_mentioned_cli_plugins
Expand Down
6 changes: 4 additions & 2 deletions lib/inspec/plugin/v2/loader.rb
Expand Up @@ -14,8 +14,10 @@ class Loader
def initialize(options = {})
@options = options
@registry = Inspec::Plugin::V2::Registry.instance
@conf_file = Inspec::Plugin::V2::ConfigFile.new
read_conf_file_into_registry
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
Expand Down
40 changes: 40 additions & 0 deletions test/functional/plugins_test.rb
Expand Up @@ -24,6 +24,32 @@
end
end

#=========================================================================================#
# Disabling Plugins
#=========================================================================================#
describe 'when disabling plugins' do
include FunctionalHelper

describe 'when disabling the core plugins' do
it 'should not be able to use core-provided commands' do
run_result = run_inspec_process('--disable-core-plugins habitat')
run_result.stderr.must_include 'Could not find command "habitat".'
# One might think that this should be code 2 (plugin error)
# But because the core plugins are not loaded, 'habitat' is not
# a known command, which makes it a usage error, code 1.
run_result.exit_status.must_equal 1
end
end

describe 'when disabling the user plugins' do
it 'should not be able to use user commands' do
run_result = run_inspec_process('--disable-user-plugins meaningoflife answer', env: { INSPEC_CONFIG_DIR: File.join(config_dir_path, 'meaning_by_path') })
run_result.stderr.must_include 'Could not find command "meaningoflife"'
run_result.exit_status.must_equal 1
end
end
end

#=========================================================================================#
# CliCommand plugin type
#=========================================================================================#
Expand Down Expand Up @@ -62,6 +88,20 @@
#=========================================================================================#
# See lib/plugins/inspec-plugin-manager-cli/test

#=========================================================================================#
# Plugin Disable Messaging
#=========================================================================================#
describe 'disable plugin usage message integration' do
include FunctionalHelper

it "mentions the --disable-{user,core}-plugins options" do
outcome = inspec('help')
['--disable-user-plugins', '--disable-core-plugins'].each do |option|
outcome.stdout.must_include(option)
end
end
end

#=========================================================================================#
# DSL Plugin Support
#=========================================================================================#
Expand Down

0 comments on commit 3612a21

Please sign in to comment.