mirror of
https://github.com/inspec/inspec
synced 2024-11-26 22:50:36 +00:00
Merge pull request #4627 from inspec/zenspider/json-and-resource-cleanup
JSON processing and resource cleanup
This commit is contained in:
commit
8d4e8c1d96
11 changed files with 41 additions and 16 deletions
|
@ -16,7 +16,7 @@ module Inspec
|
|||
|
||||
module ResourceDSL
|
||||
def name(name = nil)
|
||||
return if name.nil?
|
||||
return @name if name.nil?
|
||||
|
||||
@name = name
|
||||
__register(name, self)
|
||||
|
|
|
@ -3,12 +3,6 @@ require "inspec/version"
|
|||
require "inspec/plugin/v2/config_file"
|
||||
require "inspec/plugin/v2/filter"
|
||||
|
||||
# Add the current directory of the process to the load path
|
||||
$LOAD_PATH.unshift(".") unless $LOAD_PATH.include?(".")
|
||||
# Add the InSpec source root directory to the load path
|
||||
folder = File.expand_path(File.join("..", "..", "..", ".."), __dir__)
|
||||
$LOAD_PATH.unshift(folder) unless $LOAD_PATH.include?("folder")
|
||||
|
||||
module Inspec::Plugin::V2
|
||||
class Loader
|
||||
attr_reader :conf_file, :registry, :options
|
||||
|
|
|
@ -332,6 +332,7 @@ module Inspec
|
|||
# convert legacy os-* supports to their platform counterpart
|
||||
if res[:supports] && !res[:supports].empty?
|
||||
res[:supports].each do |support|
|
||||
# TODO: deprecate
|
||||
support[:"platform-family"] = support.delete(:"os-family") if support.key?(:"os-family")
|
||||
support[:"platform-name"] = support.delete(:"os-name") if support.key?(:"os-name")
|
||||
end
|
||||
|
|
|
@ -10,10 +10,12 @@ module Inspec
|
|||
@default_registry ||= {}
|
||||
end
|
||||
|
||||
# TODO: these are keyed off of strings
|
||||
def self.registry
|
||||
@registry ||= default_registry
|
||||
end
|
||||
|
||||
# TODO: these are keyed off of symbols
|
||||
def self.supports
|
||||
@supports ||= {}
|
||||
end
|
||||
|
@ -22,6 +24,29 @@ module Inspec
|
|||
default_registry.dup
|
||||
end
|
||||
|
||||
def self.backfill_supports!
|
||||
reg = registry.keys.map(&:to_sym).sort
|
||||
sup = supports.keys.map(&:to_sym).sort
|
||||
|
||||
missings = reg - sup
|
||||
|
||||
supports[:platform] = [{ platform: "os" }] # patch the circular dep
|
||||
|
||||
missings.each do |missing|
|
||||
klass = registry[missing.to_s].superclass
|
||||
sklas = klass.superclass.name&.to_sym # might be resource = no name
|
||||
|
||||
klass = klass.name.to_sym
|
||||
|
||||
case
|
||||
when klass != missing # an alias
|
||||
supports[missing] = supports[klass]
|
||||
when sklas
|
||||
supports[klass] = supports[sklas]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Creates the inner DSL which includes all resources for
|
||||
# creating tests. It is always connected to one target,
|
||||
# which is specified via the backend argument.
|
||||
|
|
|
@ -5,6 +5,7 @@ require "inspec/utils/file_reader"
|
|||
module Inspec::Resources
|
||||
class JsonConfig < Inspec.resource(1)
|
||||
name "json"
|
||||
supports platform: "os"
|
||||
desc "Use the json InSpec audit resource to test data in a JSON file."
|
||||
example <<~EXAMPLE
|
||||
describe json('policyfile.lock.json') do
|
||||
|
|
|
@ -11,6 +11,7 @@ module Inspec::Resources
|
|||
# @see https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-connect-and-query-sqlcmd
|
||||
class MssqlSession < Inspec.resource(1)
|
||||
name "mssql_session"
|
||||
supports platform: "windows"
|
||||
desc "Use the mssql_session InSpec audit resource to test SQL commands run against a MS Sql Server database."
|
||||
example <<~EXAMPLE
|
||||
# Using SQL authentication
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
require "method_source"
|
||||
require "date"
|
||||
require "inspec/describe"
|
||||
require "inspec/describe_base"
|
||||
require "inspec/expect"
|
||||
require "inspec/resource"
|
||||
require "inspec/resources/os"
|
||||
|
|
|
@ -202,7 +202,7 @@ module FunctionalHelper
|
|||
run_result.stdout.sub!("\n1 deprecation warning total\n", "")
|
||||
end
|
||||
|
||||
if opts[:json]
|
||||
if opts[:json] && !run_result.stdout.empty?
|
||||
begin
|
||||
payload = JSON.parse(run_result.stdout)
|
||||
|
||||
|
@ -261,14 +261,14 @@ module PluginFunctionalHelper
|
|||
File.write(File.join(tmp_dir, "plugins.json"), content)
|
||||
end
|
||||
|
||||
opts.merge!({
|
||||
opts = {
|
||||
pre_run: pre,
|
||||
tmpdir: true,
|
||||
json: true,
|
||||
env: {
|
||||
"INSPEC_CONFIG_DIR" => ".", # We're in tmpdir
|
||||
},
|
||||
})
|
||||
}.merge(opts)
|
||||
run_inspec_process(command, opts)
|
||||
end
|
||||
|
||||
|
|
|
@ -761,6 +761,7 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n"
|
|||
let(:json_path) { File.join(config_dir_path, "json-config", "good.json") }
|
||||
let(:cli_args) { "--config -" }
|
||||
let(:opts) { { prefix: "cat " + json_path + " | ", json: true, env: env } }
|
||||
let(:njopts) { opts.merge(json: false) }
|
||||
|
||||
# DO NOT use the `let`-defined run_result through here
|
||||
# If you do, it will execute twice, and cause STDIN to read empty on the second time
|
||||
|
@ -772,7 +773,7 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n"
|
|||
end
|
||||
|
||||
it "detect should exit 0" do
|
||||
result = run_inspec_process( "detect " + cli_args + " ", opts )
|
||||
result = run_inspec_process( "detect " + cli_args + " ", njopts )
|
||||
|
||||
_(result.stderr).must_be_empty
|
||||
|
||||
|
@ -780,7 +781,7 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n"
|
|||
end
|
||||
|
||||
it "shell should exit 0" do
|
||||
result = run_inspec_process( 'shell -c "platform.family" ' + cli_args + " ", opts )
|
||||
result = run_inspec_process( 'shell -c "platform.family" ' + cli_args + " ", njopts )
|
||||
|
||||
_(result.stderr).must_be_empty
|
||||
|
||||
|
|
|
@ -9,8 +9,10 @@ require "functional/helper"
|
|||
|
||||
module VisibleSpaces
|
||||
def show_spaces(str)
|
||||
str.tr!(" ", "S")
|
||||
str.tr!("\n", "N")
|
||||
str
|
||||
.tr(" ", "S")
|
||||
.tr("\n", "N")
|
||||
.b
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -21,7 +23,7 @@ describe "InSpec UI behavior" do
|
|||
parallelize_me!
|
||||
|
||||
let(:plugin_path) { File.join(mock_path, "plugins", "inspec-test-ui", "lib", "inspec-test-ui") }
|
||||
let(:run_result) { run_inspec_with_plugin("#{pre_opts} testui #{feature} #{post_opts}", plugin_path: plugin_path) }
|
||||
let(:run_result) { run_inspec_with_plugin("#{pre_opts} testui #{feature} #{post_opts}", plugin_path: plugin_path, json: false) }
|
||||
let(:pre_opts) { "" }
|
||||
let(:post_opts) { "" }
|
||||
|
||||
|
|
Loading…
Reference in a new issue