add cmd for executing calls against the inspec api

This commit is contained in:
Dominik Richter 2016-04-26 14:00:00 -04:00 committed by Christoph Hartmann
parent ab9f5f9c1a
commit 01caf05020
4 changed files with 45 additions and 28 deletions

View file

@ -116,21 +116,13 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
desc 'detect', 'detect the target OS'
target_options
def detect
diagnose
rel = File.join(File.dirname(__FILE__), *%w{.. utils detect.rb})
detect_util = File.expand_path(rel)
# exits on execution:
runner = Inspec::Runner.new(opts)
profile = Inspec::Profile.for_target(detect_util, opts)
runner.add_profile(profile)
exit runner.run
rescue RuntimeError => e
puts e.message
options_json[:command] = 'os.params'
shell_func
end
desc 'shell', 'open an interactive debugging shell'
target_options
option :command, aliases: :c
option :format, type: :string, default: Inspec::NoSummaryFormatter, hide: true
def shell_func
diagnose
@ -138,8 +130,16 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
o[:logger] = Logger.new(STDOUT)
o[:logger].level = get_log_level(o.log_level)
runner = Inspec::Runner.new(o)
Inspec::Shell.new(runner).start
if o[:command].nil?
runner = Inspec::Runner.new(o)
return Inspec::Shell.new(runner).start
else
opts[:test_collector] = 'mock'
runner = Inspec::Runner.new(opts)
res = runner.create_context.load(o[:command])
jres = res.respond_to?(:to_json) ? res.to_json : JSON.dump(res)
puts jres
end
rescue RuntimeError => e
puts e.message
end

View file

@ -25,6 +25,15 @@ module Inspec::Resources
inspec.backend.os[name]
end
def params
{
name: inspec.backend.os[:name],
family: inspec.backend.os[:family],
release: inspec.backend.os[:release],
arch: inspec.backend.os[:arch],
}
end
def to_s
'Operating System Detection'
end

View file

@ -1,15 +0,0 @@
# encoding: utf-8
# Copyright 2015, Vulcano Security GmbH
# author: Christoph Hartmann
# author: Dominik Richter
# Tiny test file to return OS info of the tested node
# print OS detection infos
conf = {
name: os[:name],
family: os[:family],
release: os[:release],
arch: os[:arch],
}
puts JSON.dump(conf)
exit 0

View file

@ -20,6 +20,29 @@ describe 'command tests' do
end
end
describe 'cmd' do
it 'can run arbitrary ruby' do
x = rand
y = rand
out = inspec("shell -c '#{x} + #{y}'")
out.stderr.must_equal ''
out.exit_status.must_equal 0
j = JSON.load(out.stdout)
j.must_equal x+y
end
it 'retrieves resources in JSON' do
out = inspec("shell -c 'os.params'")
out.stderr.must_equal ''
out.exit_status.must_equal 0
j = JSON.load(out.stdout)
j.keys.must_include 'name'
j.keys.must_include 'family'
j.keys.must_include 'arch'
j.keys.must_include 'release'
end
end
describe 'version' do
it 'provides the version number on stdout' do
out = inspec('version')