From 457e544724f0dc22113efd4c3a4b9f41b5223c34 Mon Sep 17 00:00:00 2001 From: Nikita Mathur Date: Mon, 19 Apr 2021 18:40:34 +0530 Subject: [PATCH] Fix for non-existent member for group resource Signed-off-by: Nikita Mathur --- lib/inspec/resources/groups.rb | 2 +- test/fixtures/files/etcgroup | 3 ++- test/unit/resources/etc_group_test.rb | 6 +++--- test/unit/resources/group_test.rb | 16 +++++++++++++++- test/unit/resources/groups_test.rb | 12 ++++++------ 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/inspec/resources/groups.rb b/lib/inspec/resources/groups.rb index f7bfe726a..a354e8e8a 100644 --- a/lib/inspec/resources/groups.rb +++ b/lib/inspec/resources/groups.rb @@ -118,7 +118,7 @@ module Inspec::Resources end def members - flatten_entry(group_info, "members") + flatten_entry(group_info, "members") || [] end def members_array diff --git a/test/fixtures/files/etcgroup b/test/fixtures/files/etcgroup index 3dc879da9..ad92d77a5 100644 --- a/test/fixtures/files/etcgroup +++ b/test/fixtures/files/etcgroup @@ -1,4 +1,5 @@ # comment root:x:0: www-data:x:33:www-data,root -GroupWithCaps:x:999: \ No newline at end of file +GroupWithCaps:x:999: +sftpusers:x:1000:sftponly \ No newline at end of file diff --git a/test/unit/resources/etc_group_test.rb b/test/unit/resources/etc_group_test.rb index b53fecdc6..7075ae814 100644 --- a/test/unit/resources/etc_group_test.rb +++ b/test/unit/resources/etc_group_test.rb @@ -6,9 +6,9 @@ describe "Inspec::Resources::EtcGroup" do let(:resource) { load_resource("etc_group") } it "verify /etc/group config parsing" do - _(resource.gids).must_equal [0, 33, 999] - _(resource.groups).must_equal %w{ root www-data GroupWithCaps } - _(resource.users).must_equal %w{ www-data root } + _(resource.gids).must_equal [0, 33, 999, 1000] + _(resource.groups).must_equal %w{ root www-data GroupWithCaps sftpusers } + _(resource.users).must_equal %w{ www-data root sftponly } end it "verify group filter with no users" do diff --git a/test/unit/resources/group_test.rb b/test/unit/resources/group_test.rb index 3cab79fa3..f5c3826c4 100644 --- a/test/unit/resources/group_test.rb +++ b/test/unit/resources/group_test.rb @@ -86,7 +86,7 @@ describe "Inspec::Resources::Group" do resource = MockLoader.new(:windows).load_resource("group", "dhcp") _(resource.exists?).must_equal false _(resource.gid).must_be_nil - _(resource.members).must_be_nil + _(resource.members).must_equal [] _(resource.members_array).must_equal [] end @@ -96,4 +96,18 @@ describe "Inspec::Resources::Group" do _(resource.exists?).must_equal false _(resource.gid).must_be_nil end + + # centos7 + it "verify group on centos7 with members" do + resource = MockLoader.new(:centos7).load_resource("group", "sftpusers") + _(resource.exists?).must_equal true + _(resource.members).must_include "sftponly" + end + + # centos with non-existent group member + it "verify non-existent group member on centos" do + resource = MockLoader.new(:centos7).load_resource("group", "root") + _(resource.exists?).must_equal true + _(resource.members).must_equal [] + end end diff --git a/test/unit/resources/groups_test.rb b/test/unit/resources/groups_test.rb index 98390e6a8..2dda3a9fc 100644 --- a/test/unit/resources/groups_test.rb +++ b/test/unit/resources/groups_test.rb @@ -7,18 +7,18 @@ describe "groups resource on unix platform" do describe "no arguments" do it "finds all group names" do - _(resource.names.count).must_equal 3 - _(resource.names).must_equal %w{root www-data GroupWithCaps} + _(resource.names.count).must_equal 4 + _(resource.names).must_equal %w{root www-data GroupWithCaps sftpusers} end it "finds all group gids" do - _(resource.gids.count).must_equal 3 - _(resource.gids).must_equal [0, 33, 999] + _(resource.gids.count).must_equal 4 + _(resource.gids).must_equal [0, 33, 999, 1000] end it "finds no group domains" do - _(resource.domains.count).must_equal 3 - _(resource.domains).must_equal [nil, nil, nil] + _(resource.domains.count).must_equal 4 + _(resource.domains).must_equal [nil, nil, nil, nil] end end