Skip to content

Commit b0d63e5

Browse files
authored
Merge pull request #9346 from shubhamshinde360/PUP-12041
2 parents 065b4a5 + 6a1db9e commit b0d63e5

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

lib/puppet/provider/group/groupadd.rb

+30-9
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717
value.is_a? Integer
1818
end
1919

20-
optional_commands :localadd => "lgroupadd", :localdelete => "lgroupdel", :localmodify => "lgroupmod"
21-
22-
has_feature :manages_local_users_and_groups, :manages_members if Puppet.features.libuser?
23-
24-
options :members, :flag => '-M', :method => :mem
20+
optional_commands :localadd => "lgroupadd", :localdelete => "lgroupdel", :localmodify => "lgroupmod", :purgemember => "usermod"
21+
22+
has_feature :manages_local_users_and_groups if Puppet.features.libuser?
23+
has_feature :manages_members if Puppet.features.libuser? ||
24+
(Puppet.runtime[:facter].value('os.name') == "Fedora" &&
25+
Puppet.runtime[:facter].value('os.release.major').to_i >= 40)
26+
27+
# Libuser's modify command 'lgroupmod' requires '-M' flag for member additions.
28+
# 'groupmod' command requires the '-aU' flags for it.
29+
if Puppet.features.libuser?
30+
options :members, :flag => '-M', :method => :mem
31+
else
32+
options :members, :flag => '-aU', :method => :mem
33+
end
2534

2635
def exists?
2736
return !!localgid if @resource.forcelocal?
@@ -63,7 +72,8 @@ def create
6372
end
6473

6574
def addcmd
66-
if @resource.forcelocal?
75+
# The localadd command (lgroupadd) must only be called when libuser is supported.
76+
if Puppet.features.libuser? && @resource.forcelocal?
6777
cmd = [command(:localadd)]
6878
@custom_environment = Puppet::Util::Libuser.getenv
6979
else
@@ -91,7 +101,8 @@ def validate_members(members)
91101
end
92102

93103
def modifycmd(param, value)
94-
if @resource.forcelocal? || @resource[:members]
104+
# The localmodify command (lgroupmod) must only be called when libuser is supported.
105+
if Puppet.features.libuser? && (@resource.forcelocal? || @resource[:members])
95106
cmd = [command(:localmodify)]
96107
@custom_environment = Puppet::Util::Libuser.getenv
97108
else
@@ -114,7 +125,8 @@ def modifycmd(param, value)
114125
end
115126

116127
def deletecmd
117-
if @resource.forcelocal?
128+
# The localdelete command (lgroupdel) must only be called when libuser is supported.
129+
if Puppet.features.libuser? && @resource.forcelocal?
118130
@custom_environment = Puppet::Util::Libuser.getenv
119131
[command(:localdelete), @resource[:name]]
120132
else
@@ -133,7 +145,16 @@ def members_to_s(current)
133145
end
134146

135147
def purge_members
136-
localmodify('-m', members_to_s(members), @resource.name)
148+
# The groupadd provider doesn't have the ability currently to remove members from a group, libuser does.
149+
# Use libuser's lgroupmod command to achieve purging members if libuser is supported.
150+
# Otherwise use the 'usermod' command.
151+
if Puppet.features.libuser?
152+
localmodify('-m', members_to_s(members), @resource.name)
153+
else
154+
members.each do |member|
155+
purgemember('-rG', @resource.name, member)
156+
end
157+
end
137158
end
138159

139160
private

spec/unit/provider/group/groupadd_spec.rb

+18-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
end
4545

4646
describe "on systems with libuser" do
47+
before do
48+
allow(Puppet.features).to receive(:libuser?).and_return(true)
49+
end
50+
4751
describe "with forcelocal=true" do
4852
before do
4953
described_class.has_feature(:manages_local_users_and_groups)
@@ -71,7 +75,7 @@
7175
describe "with a list of members" do
7276
before do
7377
members.each { |m| allow(Etc).to receive(:getpwnam).with(m).and_return(true) }
74-
78+
allow(provider).to receive(:flag).and_return('-M')
7579
described_class.has_feature(:manages_members)
7680
resource[:forcelocal] = false
7781
resource[:members] = members
@@ -92,6 +96,10 @@
9296
end
9397

9498
describe "on systems with libuser" do
99+
before do
100+
allow(Puppet.features).to receive(:libuser?).and_return(true)
101+
end
102+
95103
describe "with forcelocal=false" do
96104
before do
97105
described_class.has_feature(:manages_local_users_and_groups)
@@ -156,6 +164,7 @@
156164
before { resource[:auth_membership] = false }
157165

158166
it "should add to the existing users" do
167+
allow(provider).to receive(:flag).and_return('-M')
159168
new_members = ['user1', 'user2', 'user3', 'user4']
160169
allow(provider).to receive(:members).and_return(members)
161170
expect(provider).not_to receive(:localmodify).with('-m', members.join(','), 'mygroup')
@@ -235,6 +244,10 @@
235244
end
236245

237246
describe "on systems with the libuser and forcelocal=false" do
247+
before do
248+
allow(Puppet.features).to receive(:libuser?).and_return(true)
249+
end
250+
238251
before do
239252
described_class.has_feature(:manages_local_users_and_groups)
240253
resource[:forcelocal] = :false
@@ -247,6 +260,10 @@
247260
end
248261

249262
describe "on systems with the libuser and forcelocal=true" do
263+
before do
264+
allow(Puppet.features).to receive(:libuser?).and_return(true)
265+
end
266+
250267
before do
251268
described_class.has_feature(:manages_local_users_and_groups)
252269
resource[:forcelocal] = :true

0 commit comments

Comments
 (0)