Merge pull request #4627 from inspec/zenspider/json-and-resource-cleanup

JSON processing and resource cleanup
This commit is contained in:
Ryan Davis 2019-10-21 14:26:37 -07:00 committed by GitHub
commit 8d4e8c1d96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 41 additions and 16 deletions

View file

@ -16,7 +16,7 @@ module Inspec
module ResourceDSL module ResourceDSL
def name(name = nil) def name(name = nil)
return if name.nil? return @name if name.nil?
@name = name @name = name
__register(name, self) __register(name, self)

View file

@ -3,12 +3,6 @@ require "inspec/version"
require "inspec/plugin/v2/config_file" require "inspec/plugin/v2/config_file"
require "inspec/plugin/v2/filter" 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 module Inspec::Plugin::V2
class Loader class Loader
attr_reader :conf_file, :registry, :options attr_reader :conf_file, :registry, :options

View file

@ -332,6 +332,7 @@ module Inspec
# convert legacy os-* supports to their platform counterpart # convert legacy os-* supports to their platform counterpart
if res[:supports] && !res[:supports].empty? if res[:supports] && !res[:supports].empty?
res[:supports].each do |support| res[:supports].each do |support|
# TODO: deprecate
support[:"platform-family"] = support.delete(:"os-family") if support.key?(:"os-family") support[:"platform-family"] = support.delete(:"os-family") if support.key?(:"os-family")
support[:"platform-name"] = support.delete(:"os-name") if support.key?(:"os-name") support[:"platform-name"] = support.delete(:"os-name") if support.key?(:"os-name")
end end

View file

@ -10,10 +10,12 @@ module Inspec
@default_registry ||= {} @default_registry ||= {}
end end
# TODO: these are keyed off of strings
def self.registry def self.registry
@registry ||= default_registry @registry ||= default_registry
end end
# TODO: these are keyed off of symbols
def self.supports def self.supports
@supports ||= {} @supports ||= {}
end end
@ -22,6 +24,29 @@ module Inspec
default_registry.dup default_registry.dup
end 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 # Creates the inner DSL which includes all resources for
# creating tests. It is always connected to one target, # creating tests. It is always connected to one target,
# which is specified via the backend argument. # which is specified via the backend argument.

View file

@ -5,6 +5,7 @@ require "inspec/utils/file_reader"
module Inspec::Resources module Inspec::Resources
class JsonConfig < Inspec.resource(1) class JsonConfig < Inspec.resource(1)
name "json" name "json"
supports platform: "os"
desc "Use the json InSpec audit resource to test data in a JSON file." desc "Use the json InSpec audit resource to test data in a JSON file."
example <<~EXAMPLE example <<~EXAMPLE
describe json('policyfile.lock.json') do describe json('policyfile.lock.json') do

View file

@ -11,6 +11,7 @@ module Inspec::Resources
# @see https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-connect-and-query-sqlcmd # @see https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-connect-and-query-sqlcmd
class MssqlSession < Inspec.resource(1) class MssqlSession < Inspec.resource(1)
name "mssql_session" 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." desc "Use the mssql_session InSpec audit resource to test SQL commands run against a MS Sql Server database."
example <<~EXAMPLE example <<~EXAMPLE
# Using SQL authentication # Using SQL authentication

View file

@ -2,7 +2,7 @@
require "method_source" require "method_source"
require "date" require "date"
require "inspec/describe" require "inspec/describe_base"
require "inspec/expect" require "inspec/expect"
require "inspec/resource" require "inspec/resource"
require "inspec/resources/os" require "inspec/resources/os"

View file

@ -202,7 +202,7 @@ module FunctionalHelper
run_result.stdout.sub!("\n1 deprecation warning total\n", "") run_result.stdout.sub!("\n1 deprecation warning total\n", "")
end end
if opts[:json] if opts[:json] && !run_result.stdout.empty?
begin begin
payload = JSON.parse(run_result.stdout) payload = JSON.parse(run_result.stdout)
@ -261,14 +261,14 @@ module PluginFunctionalHelper
File.write(File.join(tmp_dir, "plugins.json"), content) File.write(File.join(tmp_dir, "plugins.json"), content)
end end
opts.merge!({ opts = {
pre_run: pre, pre_run: pre,
tmpdir: true, tmpdir: true,
json: true, json: true,
env: { env: {
"INSPEC_CONFIG_DIR" => ".", # We're in tmpdir "INSPEC_CONFIG_DIR" => ".", # We're in tmpdir
}, },
}) }.merge(opts)
run_inspec_process(command, opts) run_inspec_process(command, opts)
end end

View file

@ -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(:json_path) { File.join(config_dir_path, "json-config", "good.json") }
let(:cli_args) { "--config -" } let(:cli_args) { "--config -" }
let(:opts) { { prefix: "cat " + json_path + " | ", json: true, env: env } } 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 # 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 # 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 end
it "detect should exit 0" do 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 _(result.stderr).must_be_empty
@ -780,7 +781,7 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n"
end end
it "shell should exit 0" do 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 _(result.stderr).must_be_empty

View file

@ -9,8 +9,10 @@ require "functional/helper"
module VisibleSpaces module VisibleSpaces
def show_spaces(str) def show_spaces(str)
str.tr!(" ", "S") str
str.tr!("\n", "N") .tr(" ", "S")
.tr("\n", "N")
.b
end end
end end
@ -21,7 +23,7 @@ describe "InSpec UI behavior" do
parallelize_me! parallelize_me!
let(:plugin_path) { File.join(mock_path, "plugins", "inspec-test-ui", "lib", "inspec-test-ui") } 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(:pre_opts) { "" }
let(:post_opts) { "" } let(:post_opts) { "" }