Class: Inspec::BaseCLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/inspec/base_cli.rb

Class Method Summary collapse

Class Method Details

.default_optionsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/inspec/base_cli.rb', line 88

def self.default_options
  {
    exec: {
      'reporter' => ['cli'],
      'show_progress' => false,
      'color' => true,
      'create_lockfile' => true,
      'backend_cache' => true,
    },
    shell: {
      'reporter' => ['cli'],
    },
  }
end

.detect(params: {}, indent: 0, color: 39) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/inspec/base_cli.rb', line 185

def self.detect(params: {}, indent: 0, color: 39)
  str = ''
  params.each { |item, info|
    data = info

    # Format Array for better output if applicable
    data = data.join(', ') if data.is_a?(Array)

    # Do not output fields of data is missing ('unknown' is fine)
    next if data.nil?

    data = "\e[1m\e[#{color}m#{data}\e[0m"
    str << format("#{' ' * indent}%-10s %s\n", item.to_s.capitalize + ':', data)
  }
  str
end

.exec_optionsObject



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/base_cli.rb', line 64

def self.exec_options
  target_options
  profile_options
  option :controls, type: :array,
    desc: 'A list of controls to run. Ignore all other tests.'
  option :format, type: :string,
    desc: '[DEPRECATED] Please use --reporter - this will be removed in InSpec 3.0'
  option :reporter, type: :array,
    banner: 'one two:/output/file/path',
    desc: 'Enable one or more output reporters: cli, documentation, html, progress, json, json-min, json-rspec, junit'
  option :color, type: :boolean,
    desc: 'Use colors in output.'
  option :attrs, type: :array,
    desc: 'Load attributes file (experimental)'
  option :vendor_cache, type: :string,
    desc: 'Use the given path for caching dependencies. (default: ~/.inspec/cache)'
  option :create_lockfile, type: :boolean,
    desc: 'Write out a lockfile based on this execution (unless one already exists)'
  option :backend_cache, type: :boolean,
    desc: 'Allow caching for backend command output. (default: true)'
  option :show_progress, type: :boolean,
    desc: 'Show progress while executing tests.'
end

.exit_on_failure?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/inspec/base_cli.rb', line 12

def self.exit_on_failure?
  true
end

.parse_reporters(opts) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



103
104
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
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/inspec/base_cli.rb', line 103

def self.parse_reporters(opts) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  # merge in any legacy formats as reporter
  # this method will only be used for ad-hoc runners
  if !opts['format'].nil? && opts['reporter'].nil?
    warn '[DEPRECATED] The option --format is being deprecated and will be removed in inspec 3.0. Please use --reporter'

    # see if we are using the legacy output to write to files
    if opts['output']
      warn '[DEPRECATED] The option \'output\' is being deprecated and will be removed in inspec 3.0. Please use --reporter name:path'
      opts['format'] = "#{opts['format']}:#{opts['output']}"
      opts.delete('output')
    end

    opts['reporter'] = Array(opts['format'])
    opts.delete('format')
  end

  # default to cli report for ad-hoc runners
  opts['reporter'] = ['cli'] if opts['reporter'].nil?

  # parse out cli to proper report format
  if opts['reporter'].is_a?(Array)
    reports = {}
    opts['reporter'].each do |report|
      reporter_name, target = report.split(':')
      if target.nil? || target.strip == '-'
        reports[reporter_name] = { 'stdout' => true }
      else
        reports[reporter_name] = {
          'file' => target,
          'stdout' => false,
        }
      end
    end
    opts['reporter'] = reports
  end

  # add in stdout if not specified
  if opts['reporter'].is_a?(Hash)
    opts['reporter'].each do |reporter_name, config|
      opts['reporter'][reporter_name] = {} if config.nil?
      opts['reporter'][reporter_name]['stdout'] = true if opts['reporter'][reporter_name].empty?
    end
  end

  validate_reporters(opts['reporter'])
  opts
end

.profile_optionsObject



59
60
61
62
# File 'lib/inspec/base_cli.rb', line 59

def self.profile_options
  option :profiles_path, type: :string,
    desc: 'Folder which contains referenced profiles.'
end

.target_optionsObject



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
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/inspec/base_cli.rb', line 16

def self.target_options
  option :target, aliases: :t, type: :string,
    desc: 'Simple targeting option using URIs, e.g. ssh://user:pass@host:port'
  option :backend, aliases: :b, type: :string,
    desc: 'Choose a backend: local, ssh, winrm, docker.'
  option :host, type: :string,
    desc: 'Specify a remote host which is tested.'
  option :port, aliases: :p, type: :numeric,
    desc: 'Specify the login port for a remote scan.'
  option :user, type: :string,
    desc: 'The login user for a remote scan.'
  option :password, type: :string, lazy_default: -1,
    desc: 'Login password for a remote scan, if required.'
  option :enable_password, type: :string, lazy_default: -1,
    desc: 'Password for enable mode on Cisco IOS devices.'
  option :key_files, aliases: :i, type: :array,
    desc: 'Login key or certificate file for a remote scan.'
  option :path, type: :string,
    desc: 'Login path to use when connecting to the target (WinRM).'
  option :sudo, type: :boolean,
    desc: 'Run scans with sudo. Only activates on Unix and non-root user.'
  option :sudo_password, type: :string, lazy_default: -1,
    desc: 'Specify a sudo password, if it is required.'
  option :sudo_options, type: :string,
    desc: 'Additional sudo options for a remote scan.'
  option :sudo_command, type: :string,
    desc: 'Alternate command for sudo.'
  option :shell, type: :boolean,
    desc: 'Run scans in a subshell. Only activates on Unix.'
  option :shell_options, type: :string,
    desc: 'Additional shell options.'
  option :shell_command, type: :string,
    desc: 'Specify a particular shell to use.'
  option :ssl, type: :boolean,
    desc: 'Use SSL for transport layer encryption (WinRM).'
  option :self_signed, type: :boolean,
    desc: 'Allow remote scans with self-signed certificates (WinRM).'
  option :json_config, type: :string,
    desc: 'Read configuration from JSON file (`-` reads from stdin).'
  option :proxy_command, type: :string,
    desc: 'Specifies the command to use to connect to the server'
end

.validate_reporters(reporters) ⇒ Object

Raises:

  • (ArgumentError)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/inspec/base_cli.rb', line 152

def self.validate_reporters(reporters)
  return if reporters.nil?

  valid_types = [
    'automate',
    'cli',
    'documentation',
    'html',
    'json',
    'json-min',
    'json-rspec',
    'junit',
    'progress',
  ]

  reporters.each do |k, v|
    raise NotImplementedError, "'#{k}' is not a valid reporter type." unless valid_types.include?(k)

    next unless k == 'automate'
    %w{token url}.each do |option|
      raise Inspec::ReporterError, "You must specify a automate #{option} via the json-config." if v[option].nil?
    end
  end

  # check to make sure we are only reporting one type to stdout
  stdout = 0
  reporters.each_value do |v|
    stdout += 1 if v['stdout'] == true
  end

  raise ArgumentError, 'The option --reporter can only have a single report outputting to stdout.' if stdout > 1
end