From 7b0b98edac451c5cd726176a105b0945b7491744 Mon Sep 17 00:00:00 2001 From: Nikita Mathur Date: Thu, 2 Jun 2022 17:10:57 +0530 Subject: [PATCH] Resource id added for filesystem, grub, group, gem and other resources Signed-off-by: Nikita Mathur --- lib/inspec/resources/filesystem.rb | 4 ++++ lib/inspec/resources/gem.rb | 4 ++++ lib/inspec/resources/groups.rb | 4 ++++ lib/inspec/resources/grub_conf.rb | 4 ++++ lib/inspec/resources/host.rb | 4 ++++ lib/inspec/resources/http.rb | 4 ++++ test/unit/resources/filesystem_test.rb | 2 ++ test/unit/resources/gem_test.rb | 6 ++++++ test/unit/resources/group_test.rb | 5 +++++ test/unit/resources/grub_conf_test.rb | 7 +++++++ test/unit/resources/host_test.rb | 13 +++++++++++++ test/unit/resources/http_test.rb | 1 + 12 files changed, 58 insertions(+) diff --git a/lib/inspec/resources/filesystem.rb b/lib/inspec/resources/filesystem.rb index 97a652b62..84144f116 100644 --- a/lib/inspec/resources/filesystem.rb +++ b/lib/inspec/resources/filesystem.rb @@ -84,6 +84,10 @@ module Inspec::Resources info = @fsman.info(@partition) info[:name] end + + def resource_id + partition + end end class FsManagement diff --git a/lib/inspec/resources/gem.rb b/lib/inspec/resources/gem.rb index 8bb84e3fb..18148428e 100644 --- a/lib/inspec/resources/gem.rb +++ b/lib/inspec/resources/gem.rb @@ -76,5 +76,9 @@ module Inspec::Resources def to_s "gem package #{@package_name}" end + + def resource_id + "#{@package_name}-#{version}" + end end end diff --git a/lib/inspec/resources/groups.rb b/lib/inspec/resources/groups.rb index 1673d34e5..3b3c4304b 100644 --- a/lib/inspec/resources/groups.rb +++ b/lib/inspec/resources/groups.rb @@ -154,6 +154,10 @@ module Inspec::Resources "Group #{@group}" end + def resource_id + gid ? "#{@group}-#{gid}" : "" + end + private def flatten_entry(group_info, prop) diff --git a/lib/inspec/resources/grub_conf.rb b/lib/inspec/resources/grub_conf.rb index 42e9158ac..35deaa59e 100644 --- a/lib/inspec/resources/grub_conf.rb +++ b/lib/inspec/resources/grub_conf.rb @@ -69,6 +69,10 @@ module Inspec::Resources "Grub Config" end + def resource_id + @conf_path + end + private ###################################################################### diff --git a/lib/inspec/resources/host.rb b/lib/inspec/resources/host.rb index a47f5263f..0e8bc45a3 100644 --- a/lib/inspec/resources/host.rb +++ b/lib/inspec/resources/host.rb @@ -130,6 +130,10 @@ module Inspec::Resources resource_name end + def resource_id + port ? "#{hostname}-#{port}-#{protocol}" : hostname + end + private def ping diff --git a/lib/inspec/resources/http.rb b/lib/inspec/resources/http.rb index fa68c187c..7c6f88cb1 100644 --- a/lib/inspec/resources/http.rb +++ b/lib/inspec/resources/http.rb @@ -125,6 +125,10 @@ module Inspec::Resources def proxy opts.fetch(:proxy, nil) end + + def resource_id + @url + end end class Local < Base diff --git a/test/unit/resources/filesystem_test.rb b/test/unit/resources/filesystem_test.rb index 3817ae4b6..12cfb26e7 100644 --- a/test/unit/resources/filesystem_test.rb +++ b/test/unit/resources/filesystem_test.rb @@ -14,6 +14,7 @@ describe "Inspec::Resources::FileSystemResource" do _(resource.type).must_equal "ext4" _(resource.free_kb).must_equal 20760728 _(resource.percent_free).must_equal 68 + _(resource.resource_id).must_equal "/" end # windows @@ -25,5 +26,6 @@ describe "Inspec::Resources::FileSystemResource" do _(resource.type).must_equal "NTFS" _(resource.free_kb).must_equal 30000000 _(resource.percent_free).must_equal 75 + _(resource.resource_id).must_equal "c:" end end diff --git a/test/unit/resources/gem_test.rb b/test/unit/resources/gem_test.rb index 4afb253d7..f22a9b62c 100644 --- a/test/unit/resources/gem_test.rb +++ b/test/unit/resources/gem_test.rb @@ -20,6 +20,7 @@ describe "Inspec::Resources::Gem" do _(resource.installed?).must_equal true _(resource.info).must_equal pkg _(resource.gem_binary).must_equal "gem" + _(resource.resource_id).must_equal "rubocop-0.33.0" end it "specifying gem binary" do @@ -34,6 +35,7 @@ describe "Inspec::Resources::Gem" do _(resource.installed?).must_equal true _(resource.info).must_equal pkg _(resource.gem_binary).must_equal "/opt/ruby-2.3.1/embedded/bin/gem" + _(resource.resource_id).must_equal "pry-0.10.4" end it "verify gem in :chef" do @@ -48,6 +50,7 @@ describe "Inspec::Resources::Gem" do _(resource.installed?).must_equal true _(resource.info).must_equal pkg _(resource.gem_binary).must_equal "/opt/chef/embedded/bin/gem" + _(resource.resource_id).must_equal "chef-sugar-3.4.0" end it "verifies gem in :chef when multiple versions are installed" do @@ -56,6 +59,7 @@ describe "Inspec::Resources::Gem" do _(resource.versions[0]).must_match(/3\.4/) _(resource.versions).wont_include(/2\.4/) _(resource.gem_binary).must_equal "/opt/chef/embedded/bin/gem" + _(resource.resource_id).must_equal "chef-sugar-3.4.0" end it "verify gem in :chef on windows" do @@ -70,6 +74,7 @@ describe "Inspec::Resources::Gem" do _(resource.installed?).must_equal true _(resource.info).must_equal pkg _(resource.gem_binary).must_equal 'c:\opscode\chef\embedded\bin\gem.bat' + _(resource.resource_id).must_equal "json-1.8.3" end it "verify gem in :chef_server" do @@ -84,5 +89,6 @@ describe "Inspec::Resources::Gem" do _(resource.installed?).must_equal true _(resource.info).must_equal pkg _(resource.gem_binary).must_equal "/opt/opscode/embedded/bin/gem" + _(resource.resource_id).must_equal "knife-backup-0.0.12" end end diff --git a/test/unit/resources/group_test.rb b/test/unit/resources/group_test.rb index a0ac8fcf1..0fc9e4f8c 100644 --- a/test/unit/resources/group_test.rb +++ b/test/unit/resources/group_test.rb @@ -8,24 +8,28 @@ describe "Inspec::Resources::Group" do resource = MockLoader.new(:ubuntu).load_resource("group", "root") _(resource.exists?).must_equal true _(resource.gid).must_equal 0 + _(resource.resource_id).must_equal "root-0" end it "verify group on ubuntu with mixed case" do resource = MockLoader.new(:ubuntu).load_resource("group", "GroupWithCaps") _(resource.exists?).must_equal true _(resource.gid).must_equal 999 + _(resource.resource_id).must_equal "GroupWithCaps-999" end it "verify group on ubuntu with members" do resource = MockLoader.new(:ubuntu).load_resource("group", "www-data") _(resource.exists?).must_equal true _(resource.members).must_equal "www-data,root" + _(resource.resource_id).must_equal "www-data-33" end it "verify group on ubuntu with members_array" do resource = MockLoader.new(:ubuntu).load_resource("group", "www-data") _(resource.exists?).must_equal true _(resource.members_array).must_equal %w{www-data root} + _(resource.resource_id).must_equal "www-data-33" end # ubuntu with non-existent group @@ -33,6 +37,7 @@ describe "Inspec::Resources::Group" do resource = MockLoader.new(:ubuntu).load_resource("group", "nogroup") _(resource.exists?).must_equal false _(resource.gid).must_be_nil + _(resource.resource_id).must_equal "" end # mac diff --git a/test/unit/resources/grub_conf_test.rb b/test/unit/resources/grub_conf_test.rb index af684b3a1..c41701c2e 100644 --- a/test/unit/resources/grub_conf_test.rb +++ b/test/unit/resources/grub_conf_test.rb @@ -10,6 +10,7 @@ describe "Inspec::Resources::GrubConfig" do _(resource.kernel).must_include "/vmlinuz-yup-kernel-works" _(resource.initrd).must_equal "/initramfs-yup-initrd-works" + _(resource.resource_id).must_equal "/boot/grub2/grub.cfg" end # Grub2 with `GRUB_DEFAULT=saved` @@ -25,6 +26,7 @@ describe "Inspec::Resources::GrubConfig" do _(resource.kernel).must_include "/vmlinuz-3.10.0-229.el7.x86_64" _(resource.initrd).must_equal "/initramfs-3.10.0-229.el7.x86_64.img" + _(resource.resource_id).must_equal "/boot/grub2/grub.cfg" end it "parses correctly with grub2 and an invalid grubenv entry" do @@ -44,6 +46,7 @@ describe "Inspec::Resources::GrubConfig" do _(resource.kernel).must_include "/vmlinuz-yup-kernel-works" _(resource.initrd).must_equal "/initramfs-yup-initrd-works" + _(resource.resource_id).must_equal "/boot/grub2/grub.cfg" end # Grub2 with a specified kernel @@ -56,6 +59,7 @@ describe "Inspec::Resources::GrubConfig" do _(resource.kernel).must_include "/vmlinuz-0-rescue" _(resource.initrd).must_equal "/initramfs-0-rescue.img" + _(resource.resource_id).must_equal "/boot/grub2/grub.cfg" end # Legacy Grub @@ -66,6 +70,7 @@ describe "Inspec::Resources::GrubConfig" do _(resource.initrd).must_equal "/initramfs-2.6.32-573.7.1.el6.x86_64.img" _(resource.default).must_equal "0" _(resource.timeout).must_equal "5" + _(resource.resource_id).must_equal "/etc/grub.conf" end # Legacy Grub with a specified kernel @@ -80,6 +85,7 @@ describe "Inspec::Resources::GrubConfig" do _(resource.initrd).must_equal "/initramfs-2.6.32-573.el6.x86_64.img" _(resource.default).must_equal "0" _(resource.timeout).must_equal "5" + _(resource.resource_id).must_equal "/etc/grub.conf" end it "parses data with no identations correctly with grub1" do @@ -93,5 +99,6 @@ describe "Inspec::Resources::GrubConfig" do _(resource.initrd).must_equal "/initramfs-2.6.32-573.el6.x86_64.img" _(resource.default).must_equal "0" _(resource.timeout).must_equal "5" + _(resource.resource_id).must_equal "/etc/non_indented_grub.conf" end end diff --git a/test/unit/resources/host_test.rb b/test/unit/resources/host_test.rb index 38d6940db..753db2d4e 100644 --- a/test/unit/resources/host_test.rb +++ b/test/unit/resources/host_test.rb @@ -10,6 +10,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal true _(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"] _(resource.to_s).must_equal "Host example.com" + _(resource.resource_id).must_equal "example.com" end it "check host ping on centos 7" do @@ -18,6 +19,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal true _(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"] _(resource.to_s).must_equal "Host example.com" + _(resource.resource_id).must_equal "example.com" end it "check host ping on darwin" do @@ -26,6 +28,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal true _(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"] _(resource.to_s).must_equal "Host example.com" + _(resource.resource_id).must_equal "example.com" end it "check host ping on windows" do @@ -34,6 +37,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal false _(resource.ipaddress).must_equal ["134.170.188.221", "2404:6800:4009:827::200e"] _(resource.to_s).must_equal "Host microsoft.com" + _(resource.resource_id).must_equal "microsoft.com" end it "check host ping on unsupported os" do @@ -42,6 +46,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal false _(resource.ipaddress).must_be_nil _(resource.to_s).must_equal "Host example.com" + _(resource.resource_id).must_equal "example.com" end it "check host tcp on ubuntu" do @@ -50,6 +55,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal true _(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"] _(resource.to_s).must_equal "Host example.com port 1234 proto tcp" + _(resource.resource_id).must_equal "example.com-1234-tcp" end it "check host udp on ubuntu" do @@ -58,6 +64,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal true _(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"] _(resource.to_s).must_equal "Host example.com port 1234 proto udp" + _(resource.resource_id).must_equal "example.com-1234-udp" end it "check host tcp on centos 7" do @@ -66,6 +73,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal true _(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"] _(resource.to_s).must_equal "Host example.com port 1234 proto tcp" + _(resource.resource_id).must_equal "example.com-1234-tcp" end it "check host udp on centos 7" do @@ -74,6 +82,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal true _(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"] _(resource.to_s).must_equal "Host example.com port 1234 proto udp" + _(resource.resource_id).must_equal "example.com-1234-udp" end it "check host tcp on darwin" do @@ -82,6 +91,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal true _(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"] _(resource.to_s).must_equal "Host example.com port 1234 proto tcp" + _(resource.resource_id).must_equal "example.com-1234-tcp" end it "check host udp on darwin" do @@ -90,6 +100,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal true _(resource.ipaddress).must_equal ["12.34.56.78", "2606:2800:220:1:248:1893:25c8:1946"] _(resource.to_s).must_equal "Host example.com port 1234 proto udp" + _(resource.resource_id).must_equal "example.com-1234-udp" end it "check host tcp on windows" do @@ -98,6 +109,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal true _(resource.ipaddress).must_equal ["134.170.188.221", "2404:6800:4009:827::200e"] _(resource.to_s).must_equal "Host microsoft.com port 1234 proto tcp" + _(resource.resource_id).must_equal "microsoft.com-1234-tcp" end it "check host tcp on unsupported os" do @@ -106,6 +118,7 @@ describe "Inspec::Resources::Host" do _(resource.reachable?).must_equal false _(resource.ipaddress).must_be_nil _(resource.to_s).must_equal "Host example.com port 1234 proto tcp" + _(resource.resource_id).must_equal "example.com-1234-tcp" end end diff --git a/test/unit/resources/http_test.rb b/test/unit/resources/http_test.rb index dd2c60f8b..dd5e9a8d1 100644 --- a/test/unit/resources/http_test.rb +++ b/test/unit/resources/http_test.rb @@ -16,6 +16,7 @@ describe "Inspec::Resources::Http" do _(worker.status).must_equal 200 _(worker.body).must_equal "pong" + _(worker.resource_id).must_equal "pong" end end