inspec-core 4.20.6 → 4.20.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bad597d54eeb47c2f634b9c2bba2fcec8afa0d507e0b321244caee264c3b59cf
4
- data.tar.gz: 966803761b3601c83b1d2474b3405b863b245396f8e4962fc950484ed87ee9ea
3
+ metadata.gz: ef611d3b2bb1d1c8ddca64adb1ed3eab50fcbec55a45cb49334ed2084c45e7da
4
+ data.tar.gz: 96664cb6183b137db4f84cd26aed2b19496475045bbb458ea9d9004e92aad3f7
5
5
  SHA512:
6
- metadata.gz: 2ea62a2de16846b6d3bd1075868e070e41a14d9a8dce9a711155e7d75519eb56fa23238fa4cc3b53a97480396c4bd7a1b20a98251918268a66278c970ab0cda1
7
- data.tar.gz: a420f3368a7ae4b6f264f15901848e7615bd6e7fefac559dda3c955d83b7059dd59464e685cef5e4310da2d503e61f72c254739c4a054e7b6f76c7bd5f61ef37
6
+ metadata.gz: f1d44fc61e4663862a0628a0fcc4c4e3538bdb35d50cee94f1130c47fed5a982297da9c559128b0dddc2a7375658252924959020f24f518202b25939357340de
7
+ data.tar.gz: e33e28b5863ce15a54c8f2f76dd3a458762c24fb7d365ba145ae06b6b0be14134b90da5f2610d834ae71a170e5f6b180c78a8ac8763f484b3bbcfa808eec8565
@@ -375,6 +375,12 @@ class Inspec::InspecCLI < Inspec::BaseCLI
375
375
  puts "Valid schemas are #{Inspec::Schema::OutputSchema.names.join(", ")}"
376
376
  end
377
377
 
378
+ desc "run_context", "used to test run-context detection", hide: true
379
+ def run_context
380
+ require "inspec/utils/telemetry/run_context_probe"
381
+ puts Inspec::Telemetry::RunContextProbe.guess_run_context
382
+ end
383
+
378
384
  desc "version", "prints the version of this tool"
379
385
  option :format, type: :string
380
386
  def version
@@ -387,11 +393,6 @@ class Inspec::InspecCLI < Inspec::BaseCLI
387
393
  end
388
394
  map %w{-v --version} => :version
389
395
 
390
- desc "nothing", "does nothing"
391
- def nothing
392
- puts "you did nothing"
393
- end
394
-
395
396
  private
396
397
 
397
398
  def run_command(opts)
@@ -341,6 +341,7 @@ module Inspec
341
341
  # Tracked on https://github.com/inspec/inspec/issues/3667
342
342
  inspec_reporters_that_are_not_yet_plugins = %w{
343
343
  automate
344
+ cli
344
345
  json
345
346
  json-automate
346
347
  junit
@@ -354,12 +355,7 @@ module Inspec
354
355
  .find_activators(plugin_type: :reporter)\
355
356
  .map(&:activator_name).map(&:to_s)
356
357
 
357
- # Include CLI explicitly, because it is the default reporter and will usually not be
358
- # present on the command line, and thus its plugin will not appear on the list.
359
- valid_types = rspec_built_in_formatters + \
360
- inspec_reporters_that_are_not_yet_plugins + \
361
- plugin_reporters + \
362
- [ "cli" ]
358
+ valid_types = rspec_built_in_formatters + inspec_reporters_that_are_not_yet_plugins + plugin_reporters
363
359
 
364
360
  reporters.each do |reporter_name, reporter_config|
365
361
  raise NotImplementedError, "'#{reporter_name}' is not a valid reporter type." unless valid_types.include?(reporter_name)
@@ -1,4 +1,5 @@
1
1
  require "inspec/reporters/base"
2
+ require "inspec/reporters/cli"
2
3
  require "inspec/reporters/json"
3
4
  require "inspec/reporters/json_automate"
4
5
  require "inspec/reporters/junit"
@@ -11,6 +12,8 @@ module Inspec::Reporters
11
12
  name, config = reporter.dup
12
13
  config[:run_data] = run_data
13
14
  case name
15
+ when "cli"
16
+ reporter = Inspec::Reporters::CLI.new(config)
14
17
  when "json"
15
18
  reporter = Inspec::Reporters::Json.new(config)
16
19
  # This reporter is only used for Chef internal. We reserve the
@@ -1,8 +1,5 @@
1
- require "forwardable"
2
-
3
- module InspecPlugins::CliReporter
4
- class Reporter < Inspec.plugin(2, :reporter)
5
-
1
+ module Inspec::Reporters
2
+ class CLI < Base
6
3
  case RUBY_PLATFORM
7
4
  when /windows|mswin|msys|mingw|cygwin/
8
5
  # Most currently available Windows terminals have poor support
@@ -43,15 +40,11 @@ module InspecPlugins::CliReporter
43
40
 
44
41
  MULTI_TEST_CONTROL_SUMMARY_MAX_LEN = 60
45
42
 
46
- def self.run_data_schema_constraints
47
- "~> 0.0"
48
- end
49
-
50
43
  def render
51
- run_data.profiles.each do |profile|
52
- if profile.status == "skipped"
53
- platform = run_data.platform
54
- output("Skipping profile: '#{profile.name}' on unsupported platform: '#{platform.name}/#{platform.release}'.")
44
+ run_data[:profiles].each do |profile|
45
+ if profile[:status] == "skipped"
46
+ platform = run_data[:platform]
47
+ output("Skipping profile: '#{profile[:name]}' on unsupported platform: '#{platform[:name]}/#{platform[:release]}'.")
55
48
  next
56
49
  end
57
50
  @control_count = 0
@@ -77,9 +70,9 @@ module InspecPlugins::CliReporter
77
70
  def print_profile_header(profile)
78
71
  header = {
79
72
  "Profile" => format_profile_name(profile),
80
- "Version" => profile.version || "(not specified)",
73
+ "Version" => profile[:version] || "(not specified)",
81
74
  }
82
- header["Target"] = run_data.platform.target unless run_data.platform.target.nil?
75
+ header["Target"] = run_data[:platform][:target] unless run_data[:platform][:target].nil?
83
76
  header["Target ID"] = @config["target_id"] unless @config["target_id"].nil?
84
77
 
85
78
  pad = header.keys.max_by(&:length).length + 1
@@ -91,8 +84,8 @@ module InspecPlugins::CliReporter
91
84
 
92
85
  def print_standard_control_results(profile)
93
86
  standard_controls_from_profile(profile).each do |control_from_profile|
94
- control = ControlForCliDisplay.new(control_from_profile)
95
- next if control.results.empty?
87
+ control = Control.new(control_from_profile)
88
+ next if control.results.nil?
96
89
 
97
90
  output(format_control_header(control))
98
91
  control.results.each do |result|
@@ -105,8 +98,8 @@ module InspecPlugins::CliReporter
105
98
 
106
99
  def print_anonymous_control_results(profile)
107
100
  anonymous_controls_from_profile(profile).each do |control_from_profile|
108
- control = ControlForCliDisplay.new(control_from_profile)
109
- next if control.results.empty?
101
+ control = Control.new(control_from_profile)
102
+ next if control.results.nil?
110
103
 
111
104
  output(format_control_header(control))
112
105
  control.results.each do |result|
@@ -117,10 +110,10 @@ module InspecPlugins::CliReporter
117
110
  end
118
111
 
119
112
  def format_profile_name(profile)
120
- if profile.title.nil?
121
- (profile.name || "unknown").to_s
113
+ if profile[:title].nil?
114
+ (profile[:name] || "unknown").to_s
122
115
  else
123
- "#{profile.title} (#{profile.name || "unknown"})"
116
+ "#{profile[:title]} (#{profile[:name] || "unknown"})"
124
117
  end
125
118
  end
126
119
 
@@ -136,16 +129,16 @@ module InspecPlugins::CliReporter
136
129
  def format_result(control, result, type)
137
130
  impact = control.impact_string_for_result(result)
138
131
 
139
- message = if result.status == "skipped"
140
- result.skip_message
132
+ message = if result[:status] == "skipped"
133
+ result[:skip_message]
141
134
  elsif type == :anonymous
142
- result.expectation_message
135
+ result[:expectation_message]
143
136
  else
144
- result.code_desc
137
+ result[:code_desc]
145
138
  end
146
139
 
147
140
  # append any failure details to the message if they exist
148
- message += "\n#{result.message}" if result.message
141
+ message += "\n#{result[:message]}" if result[:message]
149
142
 
150
143
  format_message(
151
144
  color: impact,
@@ -177,7 +170,9 @@ module InspecPlugins::CliReporter
177
170
 
178
171
  def all_unique_controls
179
172
  @unique_controls ||= begin
180
- run_data.profiles.flat_map(&:controls).uniq
173
+ run_data[:profiles].flat_map do |profile|
174
+ profile[:controls]
175
+ end.uniq
181
176
  end
182
177
  end
183
178
 
@@ -187,12 +182,12 @@ module InspecPlugins::CliReporter
187
182
  passed = 0
188
183
 
189
184
  all_unique_controls.each do |control|
190
- next if control.id.start_with? "(generated from "
191
- next if control.results.empty?
185
+ next if control[:id].start_with? "(generated from "
186
+ next unless control[:results]
192
187
 
193
- if control.results.any? { |r| r.status == "failed" }
188
+ if control[:results].any? { |r| r[:status] == "failed" }
194
189
  failed += 1
195
- elsif control.results.any? { |r| r.status == "skipped" }
190
+ elsif control[:results].any? { |r| r[:status] == "skipped" }
196
191
  skipped += 1
197
192
  else
198
193
  passed += 1
@@ -216,12 +211,12 @@ module InspecPlugins::CliReporter
216
211
  passed = 0
217
212
 
218
213
  all_unique_controls.each do |control|
219
- next if control.results.empty?
214
+ next unless control[:results]
220
215
 
221
- control.results.each do |result|
222
- if result.status == "failed"
216
+ control[:results].each do |result|
217
+ if result[:status] == "failed"
223
218
  failed += 1
224
- elsif result.status == "skipped"
219
+ elsif result[:status] == "skipped"
225
220
  skipped += 1
226
221
  else
227
222
  passed += 1
@@ -278,30 +273,42 @@ module InspecPlugins::CliReporter
278
273
  end
279
274
 
280
275
  def standard_controls_from_profile(profile)
281
- profile.controls.reject { |c| is_anonymous_control?(c) }
276
+ profile[:controls].reject { |c| is_anonymous_control?(c) }
282
277
  end
283
278
 
284
279
  def anonymous_controls_from_profile(profile)
285
- profile.controls.select { |c| is_anonymous_control?(c) && !c[:results].nil? }
280
+ profile[:controls].select { |c| is_anonymous_control?(c) && !c[:results].nil? }
286
281
  end
287
282
 
288
283
  def is_anonymous_control?(control)
289
- control.id.start_with?("(generated from ")
284
+ control[:id].start_with?("(generated from ")
290
285
  end
291
286
 
292
287
  def indent_lines(message, indentation)
293
288
  message.lines.map { |line| " " * indentation + line }.join
294
289
  end
295
290
 
296
- class ControlForCliDisplay
291
+ class Control
292
+ attr_reader :data
297
293
 
298
- extend Forwardable
294
+ def initialize(control_hash)
295
+ @data = control_hash
296
+ end
299
297
 
300
- attr_reader :control_obj
301
- def_delegators :@control_obj, :id, :impact, :results, :title
298
+ def id
299
+ data[:id]
300
+ end
301
+
302
+ def title
303
+ data[:title]
304
+ end
305
+
306
+ def results
307
+ data[:results]
308
+ end
302
309
 
303
- def initialize(control_obj)
304
- @control_obj = control_obj
310
+ def impact
311
+ data[:impact]
305
312
  end
306
313
 
307
314
  def anonymous?
@@ -310,9 +317,9 @@ module InspecPlugins::CliReporter
310
317
 
311
318
  def title_for_report
312
319
  # if this is an anonymous control, just grab the resource title from any result entry
313
- return results.first.resource_title if anonymous?
320
+ return results.first[:resource_title] if anonymous?
314
321
 
315
- title_for_report = "#{id}: #{title || results.first.resource_title}"
322
+ title_for_report = "#{id}: #{title || results.first[:resource_title]}"
316
323
 
317
324
  # we will not add any additional data to the title if there's only
318
325
  # zero or one test for this control.
@@ -330,9 +337,9 @@ module InspecPlugins::CliReporter
330
337
  nil
331
338
  elsif impact.nil?
332
339
  "unknown"
333
- elsif results&.find { |r| r.status == "skipped" }
340
+ elsif results&.find { |r| r[:status] == "skipped" }
334
341
  "skipped"
335
- elsif results.empty? || results.all? { |r| r.status == "passed" }
342
+ elsif results.nil? || results.empty? || results.all? { |r| r[:status] == "passed" }
336
343
  "passed"
337
344
  else
338
345
  "failed"
@@ -340,9 +347,9 @@ module InspecPlugins::CliReporter
340
347
  end
341
348
 
342
349
  def impact_string_for_result(result)
343
- if result.status == "skipped"
350
+ if result[:status] == "skipped"
344
351
  "skipped"
345
- elsif result.status == "passed"
352
+ elsif result[:status] == "passed"
346
353
  "passed"
347
354
  elsif impact.nil?
348
355
  "unknown"
@@ -352,11 +359,11 @@ module InspecPlugins::CliReporter
352
359
  end
353
360
 
354
361
  def failure_count
355
- results.select { |r| r.status == "failed" }.size
362
+ results.select { |r| r[:status] == "failed" }.size
356
363
  end
357
364
 
358
365
  def skipped_count
359
- results.select { |r| r.status == "skipped" }.size
366
+ results.select { |r| r[:status] == "skipped" }.size
360
367
  end
361
368
  end
362
369
  end
@@ -0,0 +1,48 @@
1
+ module Inspec
2
+ module Telemetry
3
+ # Guesses the run context of InSpec - how were we invoked?
4
+ # All stack values here are determined experimentally
5
+
6
+ class RunContextProbe
7
+ def self.guess_run_context(stack = nil)
8
+ stack ||= caller_locations
9
+ return "test-kitchen" if kitchen?(stack)
10
+ return "cli" if run_by_thor?(stack)
11
+ return "audit-cookbook" if audit_cookbook?(stack)
12
+
13
+ "unknown"
14
+ end
15
+
16
+ def self.run_by_thor?(stack)
17
+ stack_match(stack: stack, path: "thor/command", label: "run") &&
18
+ stack_match(stack: stack, path: "thor/invocation", label: "invoke_command")
19
+ end
20
+
21
+ def self.kitchen?(stack)
22
+ stack_match(stack: stack, path: "kitchen/instance", label: "verify_action") &&
23
+ stack_match(stack: stack, path: "kitchen/instance", label: "verify")
24
+ end
25
+
26
+ def self.audit_cookbook?(stack)
27
+ stack_match(stack: stack, path: "chef/handler", label: "run_report_handlers") &&
28
+ stack_match(stack: stack, path: "handler/audit_report", label: "report")
29
+ end
30
+
31
+ def self.stack_match(stack: [], label: nil, path: nil)
32
+ return false if stack.nil?
33
+
34
+ stack.any? do |frame|
35
+ if label && path
36
+ frame.label == label && frame.absolute_path.include?(path)
37
+ elsif label
38
+ frame.label == label
39
+ elsif path
40
+ frame.absolute_path.include?(path)
41
+ else
42
+ false
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  module Inspec
2
- VERSION = "4.20.6".freeze
2
+ VERSION = "4.20.10".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inspec-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.20.6
4
+ version: 4.20.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chef InSpec Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-17 00:00:00.000000000 Z
11
+ date: 2020-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chef-telemetry
@@ -480,6 +480,7 @@ files:
480
480
  - lib/inspec/reporters.rb
481
481
  - lib/inspec/reporters/automate.rb
482
482
  - lib/inspec/reporters/base.rb
483
+ - lib/inspec/reporters/cli.rb
483
484
  - lib/inspec/reporters/json.rb
484
485
  - lib/inspec/reporters/json_automate.rb
485
486
  - lib/inspec/reporters/junit.rb
@@ -665,6 +666,7 @@ files:
665
666
  - lib/inspec/utils/telemetry/collector.rb
666
667
  - lib/inspec/utils/telemetry/data_series.rb
667
668
  - lib/inspec/utils/telemetry/global_methods.rb
669
+ - lib/inspec/utils/telemetry/run_context_probe.rb
668
670
  - lib/inspec/version.rb
669
671
  - lib/matchers/matchers.rb
670
672
  - lib/plugins/README.md
@@ -722,10 +724,6 @@ files:
722
724
  - lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli.rb
723
725
  - lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/cli_command.rb
724
726
  - lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/plugin.rb
725
- - lib/plugins/inspec-reporter-cli/README.md
726
- - lib/plugins/inspec-reporter-cli/lib/inspec-reporter-cli.rb
727
- - lib/plugins/inspec-reporter-cli/lib/inspec-reporter-cli/reporter.rb
728
- - lib/plugins/inspec-reporter-cli/lib/inspec-reporter-cli/version.rb
729
727
  - lib/plugins/inspec-reporter-json-min/README.md
730
728
  - lib/plugins/inspec-reporter-json-min/lib/inspec-reporter-json-min.rb
731
729
  - lib/plugins/inspec-reporter-json-min/lib/inspec-reporter-json-min/reporter.rb
@@ -1,12 +0,0 @@
1
- # cli reporter
2
-
3
- This is the implementation of the "cli" reporter. It is the default reporter used if you do not specify a reporter.
4
-
5
- ## To Install This Plugin
6
-
7
- This plugin is included with inspec. There is no need to install it separately.
8
-
9
- ## What This Plugin Does
10
-
11
- This reporter generates a summary of output at the end of the run to STDOUT by default. By default it uses color and terminal graphics if possible, depending on the capabilities of the host platform.
12
-
@@ -1,13 +0,0 @@
1
- require_relative "inspec-reporter-cli/version"
2
-
3
- module InspecPlugins
4
- module CliReporter
5
- class Plugin < ::Inspec.plugin(2)
6
- plugin_name :'inspec-reporter-cli'
7
- reporter :cli do
8
- require_relative "inspec-reporter-cli/reporter"
9
- InspecPlugins::CliReporter::Reporter
10
- end
11
- end
12
- end
13
- end
@@ -1,5 +0,0 @@
1
- module InspecPlugins
2
- module CliReporter
3
- VERSION = "0.1.0".freeze
4
- end
5
- end