Skip to content

Commit 428b97b

Browse files
Merge pull request #8511 from GabrielNagy/fix-gid-idempotency-main
(PUP-10896) Fix gid idempotency on user resource
2 parents 4c6338b + cdae4c2 commit 428b97b

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

acceptance/tests/resource/user/should_modify_gid.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
confine :except, :platform => 'windows'
33
confine :except, :platform => /aix/ # PUP-5358
44

5-
tag 'audit:medium',
5+
tag 'audit:high',
66
'audit:refactor', # Use block style `test_run`
77
'audit:acceptance' # Could be done as integration tests, but would
88
# require changing the system running the test
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
test_name "verify that we can modify the gid with forcelocal" do
2+
confine :to, :platform => /el|fedora/ # PUP-5358
3+
4+
require 'puppet/acceptance/common_utils'
5+
extend Puppet::Acceptance::PackageUtils
6+
extend Puppet::Acceptance::ManifestUtils
7+
8+
tag 'audit:high'
9+
10+
user = "u#{rand(99999).to_i}"
11+
group1 = "#{user}o"
12+
group2 = "#{user}n"
13+
14+
agents.each do |host|
15+
teardown do
16+
on(host, puppet_resource('user', user, 'ensure=absent'))
17+
on(host, puppet_resource('group', group1, 'ensure=absent'))
18+
on(host, puppet_resource('group', group2, 'ensure=absent'))
19+
end
20+
21+
step "ensure that the groups both exist" do
22+
on(host, puppet_resource('group', group1, 'ensure=present'))
23+
on(host, puppet_resource('group', group2, 'ensure=present'))
24+
end
25+
26+
step "ensure the user exists and has the old group" do
27+
apply_manifest_on(agent, resource_manifest('user', user, ensure: 'present', gid: group1, forcelocal: true))
28+
end
29+
30+
step "verify that the user has the correct gid" do
31+
group_gid1 = host.group_gid(group1)
32+
host.user_get(user) do |result|
33+
user_gid1 = result.stdout.split(':')[3]
34+
35+
fail_test "didn't have the expected old GID #{group_gid1}, but got: #{user_gid1}" unless group_gid1 == user_gid1
36+
end
37+
end
38+
39+
step "modify the GID of the user" do
40+
apply_manifest_on(agent, resource_manifest('user', user, ensure: 'present', gid: group2, forcelocal: true), expect_changes: true)
41+
end
42+
43+
step "verify that the user has the updated gid" do
44+
group_gid2 = host.group_gid(group2)
45+
host.user_get(user) do |result|
46+
user_gid2 = result.stdout.split(':')[3]
47+
48+
fail_test "didn't have the expected old GID #{group_gid}, but got: #{user_gid2}" unless group_gid2 == user_gid2
49+
end
50+
end
51+
52+
step "run again for idempotency" do
53+
apply_manifest_on(agent, resource_manifest('user', user, ensure: 'present', gid: group2, forcelocal: true), catch_changes: true)
54+
end
55+
end
56+
end

lib/puppet/provider/user/useradd.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,14 @@ def localuid
104104

105105
def localgid
106106
user = finduser(:account, resource[:name])
107-
return user[:gid] if user
107+
if user
108+
begin
109+
return Integer(user[:gid])
110+
rescue ArgumentError
111+
Puppet.debug("Non-numeric GID found in /etc/passwd for user #{resource[:name]}")
112+
return user[:gid]
113+
end
114+
end
108115
false
109116
end
110117

spec/unit/provider/user/useradd_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,14 @@
360360
resource[:forcelocal] = true
361361
allow(Puppet::FileSystem).to receive(:exist?).with('/etc/passwd').and_return(true)
362362
allow(Puppet::FileSystem).to receive(:each_line).with('/etc/passwd').and_yield(content)
363-
expect(provider.gid).to eq('999')
363+
expect(provider.gid).to eq(999)
364364
end
365365

366366
it "should fall back to nameservice GID when forcelocal is false" do
367367
resource[:forcelocal] = false
368-
allow(provider).to receive(:get).with(:gid).and_return('1234')
368+
allow(provider).to receive(:get).with(:gid).and_return(1234)
369369
expect(provider).not_to receive(:localgid)
370-
expect(provider.gid).to eq('1234')
370+
expect(provider.gid).to eq(1234)
371371
end
372372
end
373373

0 commit comments

Comments
 (0)