From ae4420a736946312ec1efbc4f8e7a46561286b7b Mon Sep 17 00:00:00 2001 From: Stephan Renatus Date: Fri, 4 Dec 2015 13:35:15 +0100 Subject: [PATCH] add `--json-config=` and `--diagnose` flags to inspect configurations Because of the way per-command arguments are handled, this is a little different from the way e.g. kitchen handles it: any inspec command can take the flag `--diagnose` to have it dump configuration first. This add support for a JSON configuration file, where both inspec detect --json-config=config.json and inspec detect --json-config=- e @@ -83,7 +95,9 @@ class InspecCLI < Thor desc 'detect', 'detect the target OS' target_options def detect - runner = Inspec::Runner.new(options) + diagnose + + runner = Inspec::Runner.new(opts) rel = File.join(File.dirname(__FILE__), *%w{.. lib utils detect.rb}) detect_util = File.expand_path(rel) runner.add_tests([detect_util]) @@ -96,7 +110,9 @@ class InspecCLI < Thor target_options option :format, type: :string, default: Inspec::NoSummaryFormatter, hide: true def shell_func - runner = Inspec::Runner.new(options) + diagnose + + runner = Inspec::Runner.new(opts) Inspec::Shell.new(runner).start rescue RuntimeError => e puts e.message @@ -106,5 +122,41 @@ class InspecCLI < Thor def version puts Inspec::VERSION end + + private + + def diagnose + return unless opts['diagnose'] + puts "InSpec version: #{Inspec::VERSION}" + puts "Train version: #{Train::VERSION}" + puts 'Command line configuration:' + pp options + puts 'JSON configuration file:' + pp options_json + puts 'Merged configuration:' + pp opts + puts + end + + def opts + # argv overrides json + Thor::CoreExt::HashWithIndifferentAccess.new(options_json.merge(options)) + end + + def options_json + conffile = options['json_config'] + @json ||= conffile ? read_config(conffile) : {} + end + + def read_config(file) + if file == '-' + puts 'WARN: reading JSON config from standard input' if STDIN.tty? + config = STDIN.read + else + config = File.read(file) + end + + JSON.load(config) + end end InspecCLI.start(ARGV) diff --git a/docs/ctl_inspec.rst b/docs/ctl_inspec.rst index 7ee72bea6..e347d1964 100644 --- a/docs/ctl_inspec.rst +++ b/docs/ctl_inspec.rst @@ -47,6 +47,12 @@ The following options may be used with any of the InSpec CLI subcommands: ``--user`` The login user for remote scanning. +``--json_config`` + A JSON file containing configuration options. Use `--json_config=-` to read from standard input. The file's format corresponds to the command line argument options. For example, `{"host": "example.com", "sudo": true}` is equivalent to `--host=example.com --sudo`. Command line switches override the configuration file. + +``--diagnose`` + Dump configuration values from a command line options, the configuration file, and the merged effective options. + check