diff --git a/lib/inspec/resources/chocolatey_package.rb b/lib/inspec/resources/chocolatey_package.rb index 48abc4e38..4510446f9 100644 --- a/lib/inspec/resources/chocolatey_package.rb +++ b/lib/inspec/resources/chocolatey_package.rb @@ -45,6 +45,10 @@ module Inspec::Resources end end + def resource_id + @package_name || "chocolatey_package" + end + def to_s "Chocolatey package #{package_name}" end diff --git a/lib/inspec/resources/chrony_conf.rb b/lib/inspec/resources/chrony_conf.rb index ba710dc8d..574834618 100644 --- a/lib/inspec/resources/chrony_conf.rb +++ b/lib/inspec/resources/chrony_conf.rb @@ -34,6 +34,10 @@ module Inspec::Resources param end + def resource_id + @conf_path || "chrony_conf" + end + def to_s "chrony.conf" end diff --git a/lib/inspec/resources/command.rb b/lib/inspec/resources/command.rb index 2869595e8..700a98b29 100644 --- a/lib/inspec/resources/command.rb +++ b/lib/inspec/resources/command.rb @@ -93,6 +93,11 @@ module Inspec::Resources res.exit_status.to_i == 0 end + # to_s method outputs the command which we are using here as UUID to identify resource and also it take cares of Redact output + def resource_id + to_s || "command" + end + def to_s output = "Command: `#{@command}`" # Redact output if the `redact_regex` option is passed diff --git a/lib/inspec/resources/cpan.rb b/lib/inspec/resources/cpan.rb index 626fe4052..759b1d8f8 100644 --- a/lib/inspec/resources/cpan.rb +++ b/lib/inspec/resources/cpan.rb @@ -51,6 +51,10 @@ module Inspec::Resources info[:version] end + def resource_id + @package_name || "cpan" + end + def to_s "Perl Module #{@package_name}" end diff --git a/lib/inspec/resources/cran.rb b/lib/inspec/resources/cran.rb index 21ae847b6..c2a4de414 100644 --- a/lib/inspec/resources/cran.rb +++ b/lib/inspec/resources/cran.rb @@ -57,6 +57,10 @@ module Inspec::Resources info[:version] end + def resource_id + @package_name || "cran" + end + def to_s "R Module #{@package_name}" end diff --git a/lib/inspec/resources/cron.rb b/lib/inspec/resources/cron.rb index f9d586578..4a1b211f7 100644 --- a/lib/inspec/resources/cron.rb +++ b/lib/inspec/resources/cron.rb @@ -38,6 +38,11 @@ module Inspec::Resources @params.include?(rule) end + def resource_id + user = @user || "current user" + "cron #{user}" + end + def to_s if is_user_crontab? "cron for user #{@user}" diff --git a/lib/inspec/resources/csv.rb b/lib/inspec/resources/csv.rb index 243e0d176..381342358 100644 --- a/lib/inspec/resources/csv.rb +++ b/lib/inspec/resources/csv.rb @@ -19,7 +19,8 @@ module Inspec::Resources def initialize(path, headers = true) @headers = headers - super(path) + @path = path + super(@path) end # override the parse method from JsonConfig @@ -68,6 +69,10 @@ module Inspec::Resources end end + def resource_id + @path || "csv" + end + private # used by JsonConfig to build up a full to_s method diff --git a/lib/inspec/resources/dh_params.rb b/lib/inspec/resources/dh_params.rb index 92ca1d5e0..beabd4e90 100644 --- a/lib/inspec/resources/dh_params.rb +++ b/lib/inspec/resources/dh_params.rb @@ -76,6 +76,10 @@ module Inspec::Resources @dh_params.params_ok? end + def resource_id + @dh_params_path || "dh_params" + end + def to_s "dh_params #{@dh_params_path}" end diff --git a/test/unit/resources/chocolatey_package_test.rb b/test/unit/resources/chocolatey_package_test.rb index d6f6bf1c2..08b5c969a 100644 --- a/test/unit/resources/chocolatey_package_test.rb +++ b/test/unit/resources/chocolatey_package_test.rb @@ -6,6 +6,7 @@ describe "Inspec::Resources::ChocoPkg" do it "can parse output from `choco` when package is installed" do pkg = { name: "git", installed: false, version: nil, type: "chocolatey" } resource = MockLoader.new(:windows).load_resource("chocolatey_package", "git") + _(resource.resource_id).must_equal "git" _(resource.installed?).must_equal pkg[:installed] _(resource.version).must_be_nil _(resource.info).must_equal pkg @@ -18,4 +19,9 @@ describe "Inspec::Resources::ChocoPkg" do _(resource.version).must_equal pkg[:version] _(resource.info).must_equal pkg end + + it "gets the resource_id for the current resource" do + resource = MockLoader.new(:windows).load_resource("chocolatey_package", "nssm") + _(resource.resource_id).must_equal "nssm" + end end diff --git a/test/unit/resources/chrony_conf_test.rb b/test/unit/resources/chrony_conf_test.rb index eabe58647..f9807ebde 100644 --- a/test/unit/resources/chrony_conf_test.rb +++ b/test/unit/resources/chrony_conf_test.rb @@ -15,4 +15,9 @@ describe "Inspec::Resources::ChronyConf" do } assert_nil resource.allow end + + it "gets resource_id for the current resource" do + resource = load_resource("chrony_conf") + _(resource.resource_id).must_equal "/etc/chrony.conf" + end end diff --git a/test/unit/resources/command_test.rb b/test/unit/resources/command_test.rb index fea5a8558..be1ba2990 100644 --- a/test/unit/resources/command_test.rb +++ b/test/unit/resources/command_test.rb @@ -14,10 +14,12 @@ describe Inspec::Resources::Cmd do end it "runs a valid mocked command" do - _(resource("env").result).wont_be_nil - _(resource("env").stdout).must_include "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n" - _(resource("env").stderr).must_equal "" - _(resource("env").exit_status).must_equal 0 + resource = load_resource("command", "env") + _(resource.resource_id).must_equal "Command: `env`" + _(resource.result).wont_be_nil + _(resource.stdout).must_include "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n" + _(resource.stderr).must_equal "" + _(resource.exit_status).must_equal 0 end it "exist? returns false on nil os name" do @@ -42,8 +44,10 @@ describe Inspec::Resources::Cmd do it "redacts output if `redact_regex` is passed with capture groups" do cmd = "command_with_password -p supersecret -d no_redact" + resource = load_resource("command", cmd, redact_regex: /(-p ).*( -d)/) expected_to_s = "Command: `command_with_password -p REDACTED -d no_redact`" - _(resource(cmd, redact_regex: /(-p ).*( -d)/).to_s).must_equal(expected_to_s) + _(resource.resource_id).must_equal(expected_to_s) + _(resource.to_s).must_equal(expected_to_s) end it "redacts output if `redact_regex` is passed without a capture group" do diff --git a/test/unit/resources/cpan_test.rb b/test/unit/resources/cpan_test.rb index 0df08f139..9e87b1368 100644 --- a/test/unit/resources/cpan_test.rb +++ b/test/unit/resources/cpan_test.rb @@ -18,4 +18,8 @@ describe "Inspec::Resources::Cpan" do _(resource.info[:type]).must_equal "cpan" _(resource.info).must_equal pkg end + it "gets the resource_id for the current resource" do + resource = load_resource("cpan", "DBD::Pg") + _(resource.resource_id).must_equal "DBD::Pg" + end end diff --git a/test/unit/resources/cran_test.rb b/test/unit/resources/cran_test.rb index 7964ba269..4adea792b 100644 --- a/test/unit/resources/cran_test.rb +++ b/test/unit/resources/cran_test.rb @@ -16,4 +16,8 @@ describe "Inspec::Resources::Cran" do _(resource.info[:name]).must_equal "DoesNotExist" _(resource.info[:type]).must_equal "cran" end + it "gets resource_id for the current resource" do + resource = load_resource("cran", "DBI") + _(resource.resource_id).must_equal "DBI" + end end diff --git a/test/unit/resources/cron_test.rb b/test/unit/resources/cron_test.rb index 6ea0f6973..3420608a4 100644 --- a/test/unit/resources/cron_test.rb +++ b/test/unit/resources/cron_test.rb @@ -36,4 +36,15 @@ describe "Inspec::Resources::Cron" do _(resource.resource_exception_message).must_include "Error while executing crontab -l -u testuser command:" end end + + describe "resource_id" do + it "generates the resource_id for the current resouce if user is not specified" do + _(cron.resource_id).must_equal "cron current user" + end + + it "generates the resource_id for the current resouce if user is specified" do + resource = load_resource("cron", { user: "testuser" }) + _(resource.resource_id).must_equal "cron testuser" + end + end end diff --git a/test/unit/resources/csv_test.rb b/test/unit/resources/csv_test.rb index 3172c58a0..7308ee228 100644 --- a/test/unit/resources/csv_test.rb +++ b/test/unit/resources/csv_test.rb @@ -9,6 +9,10 @@ describe "Inspec::Resources::CSV" do {} end + it "gets the resource_id for the current resource" do + _(resource.resource_id).must_equal "example.csv" + end + it "captures an array of params" do _(resource.params).must_be_kind_of Array end diff --git a/test/unit/resources/dh_params_test.rb b/test/unit/resources/dh_params_test.rb index e92ee4750..334af6b97 100644 --- a/test/unit/resources/dh_params_test.rb +++ b/test/unit/resources/dh_params_test.rb @@ -71,6 +71,10 @@ describe "Inspec::Resources::DhParams" do EOF end + it "gets the resource_id for the current resource" do + _(resource_dh_params.resource_id).must_equal "dh_params.dh_pem" + end + it "parses the generator used for the Diffie-Hellman operation" do _(resource_dh_params.send("generator")).must_equal 2 end