From 01caf0502016abac7375d85849d478e0959303ce Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Tue, 26 Apr 2016 14:00:00 -0400 Subject: [PATCH] add cmd for executing calls against the inspec api --- lib/inspec/cli.rb | 26 +++++++++++++------------- lib/resources/os.rb | 9 +++++++++ lib/utils/detect.rb | 15 --------------- test/functional/inspec_test.rb | 23 +++++++++++++++++++++++ 4 files changed, 45 insertions(+), 28 deletions(-) delete mode 100644 lib/utils/detect.rb diff --git a/lib/inspec/cli.rb b/lib/inspec/cli.rb index 6a422d232..5b55e678c 100644 --- a/lib/inspec/cli.rb +++ b/lib/inspec/cli.rb @@ -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 diff --git a/lib/resources/os.rb b/lib/resources/os.rb index 6256a979c..c30cead09 100644 --- a/lib/resources/os.rb +++ b/lib/resources/os.rb @@ -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 diff --git a/lib/utils/detect.rb b/lib/utils/detect.rb deleted file mode 100644 index c928f32cc..000000000 --- a/lib/utils/detect.rb +++ /dev/null @@ -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 diff --git a/test/functional/inspec_test.rb b/test/functional/inspec_test.rb index 7f32645db..001df8822 100644 --- a/test/functional/inspec_test.rb +++ b/test/functional/inspec_test.rb @@ -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')