Allow target-id passthrough (#3320)

* Allow uuid passthrough
* Update flag to be target-id.
* Updated to use proper formatting for header.
* Fix empty line after cli banner.

Signed-off-by: Jared Quick <jquick@chef.io>
This commit is contained in:
Jared Quick 2018-09-05 15:07:34 -04:00 committed by GitHub
parent d69791e2bb
commit 76b453eee9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 53 additions and 12 deletions

View file

@ -62,6 +62,8 @@ module Inspec
desc: 'Specifies the bastion port if applicable' desc: 'Specifies the bastion port if applicable'
option :insecure, type: :boolean, default: false, option :insecure, type: :boolean, default: false,
desc: 'Disable SSL verification on select targets' desc: 'Disable SSL verification on select targets'
option :target_id, type: :string,
desc: 'Provide a ID which will be included on reports'
end end
def self.profile_options def self.profile_options
@ -142,6 +144,7 @@ module Inspec
'file' => target, 'file' => target,
'stdout' => false, 'stdout' => false,
} }
reports[reporter_name]['target_id'] = opts['target_id'] if opts['target_id']
end end
end end
opts['reporter'] = reports opts['reporter'] = reports
@ -152,6 +155,7 @@ module Inspec
opts['reporter'].each do |reporter_name, config| opts['reporter'].each do |reporter_name, config|
opts['reporter'][reporter_name] = {} if config.nil? opts['reporter'][reporter_name] = {} if config.nil?
opts['reporter'][reporter_name]['stdout'] = true if opts['reporter'][reporter_name].empty? opts['reporter'][reporter_name]['stdout'] = true if opts['reporter'][reporter_name].empty?
opts['reporter'][reporter_name]['target_id'] = opts['target_id'] if opts['target_id']
end end
end end

View file

@ -69,7 +69,6 @@ module Inspec::Formatters
name: platform(:name), name: platform(:name),
release: platform(:release), release: platform(:release),
target: backend_target, target: backend_target,
uuid: platform(:uuid),
} }
end end

View file

@ -23,7 +23,7 @@ module Inspec::Reporters
final_report[:type] = 'inspec_report' final_report[:type] = 'inspec_report'
final_report[:end_time] = Time.now.utc.strftime('%FT%TZ') final_report[:end_time] = Time.now.utc.strftime('%FT%TZ')
final_report[:node_uuid] = @config['node_uuid'] || @run_data[:platform][:uuid] final_report[:node_uuid] = @config['node_uuid'] || @config['target_id']
raise Inspec::ReporterError, 'Cannot find a UUID for your node. Please specify one via json-config.' if final_report[:node_uuid].nil? raise Inspec::ReporterError, 'Cannot find a UUID for your node. Please specify one via json-config.' if final_report[:node_uuid].nil?
final_report[:report_uuid] = @config['report_uuid'] || uuid_from_string(final_report[:end_time] + final_report[:node_uuid]) final_report[:report_uuid] = @config['report_uuid'] || uuid_from_string(final_report[:end_time] + final_report[:node_uuid])

View file

@ -63,9 +63,17 @@ module Inspec::Reporters
private private
def print_profile_header(profile) def print_profile_header(profile)
output("Profile: #{format_profile_name(profile)}") header = {
output("Version: #{profile[:version] || '(not specified)'}") 'Profile' => format_profile_name(profile),
output("Target: #{run_data[:platform][:target]}") unless run_data[:platform][:target].nil? 'Version' => profile[:version] || '(not specified)',
}
header['Target'] = run_data[:platform][:target] unless run_data[:platform][:target].nil?
header['Target ID'] = @config['target_id'] unless @config['target_id'].nil?
pad = header.keys.max_by(&:length).length + 1
header.each do |title, value|
output(format("%-#{pad}s %s", title + ':', value))
end
output('') output('')
end end

View file

@ -22,10 +22,12 @@ module Inspec::Reporters
private private
def platform def platform
{ platform = {
name: run_data[:platform][:name], name: run_data[:platform][:name],
release: run_data[:platform][:release], release: run_data[:platform][:release],
} }
platform[:target_id] = @config['target_id'] if @config['target_id']
platform
end end
def profile_results(control) def profile_results(control)

View file

@ -42,6 +42,7 @@ module Inspec
'properties' => { 'properties' => {
'name' => { 'type' => 'string' }, 'name' => { 'type' => 'string' },
'release' => { 'type' => 'string' }, 'release' => { 'type' => 'string' },
'target_id' => { 'type' => 'string', 'optional' => true },
}, },
}.freeze }.freeze

View file

@ -28,6 +28,17 @@ describe 'inspec exec with json formatter' do
JSON::Schema.validate(data, schema) JSON::Schema.validate(data, schema)
end end
it 'can execute a profile and validate the json schema with target_id' do
out = inspec('exec ' + example_profile + ' --reporter json --no-create-lockfile --target-id 1d3e399f-4d71-4863-ac54-84d437fbc444')
out.stderr.must_equal ''
out.exit_status.must_equal 101
data = JSON.parse(out.stdout)
data['platform']['target_id'].must_equal '1d3e399f-4d71-4863-ac54-84d437fbc444'
sout = inspec('schema exec-json')
schema = JSON.parse(sout.stdout)
JSON::Schema.validate(data, schema)
end
describe 'execute a profile with json formatting' do describe 'execute a profile with json formatting' do
let(:json) { JSON.load(inspec('exec ' + example_profile + ' --reporter json --no-create-lockfile').stdout) } let(:json) { JSON.load(inspec('exec ' + example_profile + ' --reporter json --no-create-lockfile').stdout) }
let(:profile) { json['profiles'][0] } let(:profile) { json['profiles'][0] }

View file

@ -46,6 +46,15 @@ Test Summary: 0 successful, 0 failures, 0 skipped
File.stat("#{outpath}/foo/bar/test.json").size.must_be :>, 0 File.stat("#{outpath}/foo/bar/test.json").size.must_be :>, 0
end end
it 'can execute the profile with a target_id passthrough' do
outpath = Dir.tmpdir
out = inspec("exec #{example_profile} --no-create-lockfile --target-id 1d3e399f-4d71-4863-ac54-84d437fbc444")
out.stderr.must_equal ''
out.exit_status.must_equal 101
stdout = out.stdout.force_encoding(Encoding::UTF_8)
stdout.must_include "Target ID: 1d3e399f-4d71-4863-ac54-84d437fbc444"
end
it 'executes a metadata-only profile' do it 'executes a metadata-only profile' do
out = inspec('exec ' + File.join(profile_path, 'complete-metadata') + ' --no-create-lockfile') out = inspec('exec ' + File.join(profile_path, 'complete-metadata') + ' --no-create-lockfile')
out.stderr.must_equal '' out.stderr.must_equal ''

View file

@ -149,15 +149,22 @@ EOF
it 'parse cli reporters' do it 'parse cli reporters' do
opts = { 'reporter' => ['cli'] } opts = { 'reporter' => ['cli'] }
parsed = Inspec::BaseCLI.parse_reporters(opts) parsed = Inspec::BaseCLI.parse_reporters(opts)
assert = { 'reporter' => { 'cli' => { 'stdout' => true }}} expected_value = { 'reporter' => { 'cli' => { 'stdout' => true }}}
parsed.must_equal assert parsed.must_equal expected_value
end
it 'parses cli report and attaches target_id' do
opts = { 'reporter' => ['cli'], 'target_id' => '1d3e399f-4d71-4863-ac54-84d437fbc444' }
parsed = Inspec::BaseCLI.parse_reporters(opts)
expected_value = {"reporter"=>{"cli"=>{"stdout"=>true, "target_id"=>"1d3e399f-4d71-4863-ac54-84d437fbc444"}}, "target_id"=>"1d3e399f-4d71-4863-ac54-84d437fbc444"}
parsed.must_equal expected_value
end end
it 'parse cli reporters with format' do it 'parse cli reporters with format' do
opts = { 'format' => 'json' } opts = { 'format' => 'json' }
parsed = Inspec::BaseCLI.parse_reporters(opts) parsed = Inspec::BaseCLI.parse_reporters(opts)
assert = { 'reporter' => { 'json' => { 'stdout' => true }}} expected_value = { 'reporter' => { 'json' => { 'stdout' => true }}}
parsed.must_equal assert parsed.must_equal expected_value
end end
it 'parse cli reporters with format and output' do it 'parse cli reporters with format and output' do
@ -166,8 +173,8 @@ EOF
proc { proc {
opts = { 'format' => 'json', 'output' => '/tmp/inspec_out.json' } opts = { 'format' => 'json', 'output' => '/tmp/inspec_out.json' }
parsed = Inspec::BaseCLI.parse_reporters(opts) parsed = Inspec::BaseCLI.parse_reporters(opts)
assert = { 'reporter' => { 'json' => { 'file' => '/tmp/inspec_out.json', 'stdout' => false }}} expected_value = { 'reporter' => { 'json' => { 'file' => '/tmp/inspec_out.json', 'stdout' => false }}}
parsed.must_equal assert parsed.must_equal expected_value
}.must_output nil, error end }.must_output nil, error end
end end