Skip to content

Commit e232770

Browse files
committed
Minimal fix for #4975 -- only call chage when managing password age rules
This is intended to be a minimal fix, with tests, to prevent chage from running unless needed.
1 parent a090e86 commit e232770

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

lib/puppet/provider/nameservice.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ def create
165165

166166
begin
167167
execute(self.addcmd)
168-
execute(self.passcmd) if self.feature? :manages_password_age
168+
if feature?(:manages_password_age) && (cmd = passcmd)
169+
execute(cmd)
170+
end
169171
rescue Puppet::ExecutionFailure => detail
170172
raise Puppet::Error, "Could not create #{@resource.class.name} #{@resource.name}: #{detail}"
171173
end

lib/puppet/provider/user/user_role_add.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def create
8181
run(transition("normal"), "transition role to")
8282
else
8383
run(addcmd, "create")
84-
run(passcmd, "change password policy for")
84+
if cmd = passcmd
85+
run(cmd, "change password policy for")
86+
end
8587
end
8688
# added to handle case when password is specified
8789
self.password = @resource[:password] if @resource[:password]

lib/puppet/provider/user/useradd.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,12 @@ def addcmd
7070
end
7171

7272
def passcmd
73-
cmd = [command(:password)]
74-
[:password_min_age, :password_max_age].each do |property|
75-
if value = @resource.should(property)
76-
cmd << flag(property) << value
77-
end
73+
age_limits = [:password_min_age, :password_max_age].select { |property| @resource.should(property) }
74+
if age_limits.empty?
75+
nil
76+
else
77+
[command(:password),age_limits.collect { |property| [flag(property), @resource.should(property)]}, @resource[:name]].flatten
7878
end
79-
cmd << @resource[:name]
8079
end
8180

8281
def min_age

spec/unit/provider/user/useradd_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,46 @@
131131
@provider.addcmd.must == ["useradd", "-G", "somegroup", "-o", "-m", "someuser"]
132132
end
133133
end
134+
135+
describe "when calling passcmd" do
136+
before do
137+
@resource.stubs(:allowdupe?).returns true
138+
@resource.stubs(:managehome?).returns true
139+
end
140+
141+
it "should call command with :pass" do
142+
@provider.expects(:command).with(:password)
143+
@provider.passcmd
144+
end
145+
146+
it "should return nil if neither min nor max is set" do
147+
@resource.stubs(:should).with(:password_min_age).returns nil
148+
@resource.stubs(:should).with(:password_max_age).returns nil
149+
@provider.passcmd.must == nil
150+
end
151+
152+
it "should return a chage command array with -m <value> and the user name if password_min_age is set" do
153+
@provider.stubs(:command).with(:password).returns("chage")
154+
@resource.stubs(:[]).with(:name).returns("someuser")
155+
@resource.stubs(:should).with(:password_min_age).returns 123
156+
@resource.stubs(:should).with(:password_max_age).returns nil
157+
@provider.passcmd.must == ['chage','-m',123,'someuser']
158+
end
159+
160+
it "should return a chage command array with -M <value> if password_max_age is set" do
161+
@provider.stubs(:command).with(:password).returns("chage")
162+
@resource.stubs(:[]).with(:name).returns("someuser")
163+
@resource.stubs(:should).with(:password_min_age).returns nil
164+
@resource.stubs(:should).with(:password_max_age).returns 999
165+
@provider.passcmd.must == ['chage','-M',999,'someuser']
166+
end
167+
168+
it "should return a chage command array with -M <value> -m <value> if both password_min_age and password_max_age are set" do
169+
@provider.stubs(:command).with(:password).returns("chage")
170+
@resource.stubs(:[]).with(:name).returns("someuser")
171+
@resource.stubs(:should).with(:password_min_age).returns 123
172+
@resource.stubs(:should).with(:password_max_age).returns 999
173+
@provider.passcmd.must == ['chage','-m',123,'-M',999,'someuser']
174+
end
175+
end
134176
end

0 commit comments

Comments
 (0)