Skip to content

Commit bc4f0a4

Browse files
authored
Merge pull request #8554 from golflimaechoecho/PUP-10997
(PUP-10997) ignore empty lines in /etc/group
2 parents 9026ec7 + d384fb7 commit bc4f0a4

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/puppet/provider/user/useradd.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def localgroups
135135

136136
Puppet::FileSystem.each_line(group_file) do |line|
137137
data = line.chomp.split(':')
138-
if data.last.split(',').include?(user)
138+
if !data.empty? && data.last.split(',').include?(user)
139139
@groups_of[user] << data.first
140140
end
141141
end

spec/unit/provider/user/useradd_spec.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,21 +375,36 @@
375375
before { described_class.has_feature :manages_local_users_and_groups }
376376

377377
let(:content) do
378-
<<~EOF
378+
StringIO.new(<<~EOF)
379379
group1:x:0:myuser
380380
group2:x:999:
381381
group3:x:998:myuser
382382
EOF
383383
end
384384

385+
let(:content_with_empty_line) do
386+
StringIO.new(<<~EOF)
387+
group1:x:0:myuser
388+
group2:x:999:
389+
group3:x:998:myuser
390+
391+
EOF
392+
end
393+
385394
it "should return the local groups string when forcelocal is true" do
386395
resource[:forcelocal] = true
387-
group1, group2, group3 = content.split
388396
allow(Puppet::FileSystem).to receive(:exist?).with('/etc/group').and_return(true)
389-
allow(Puppet::FileSystem).to receive(:each_line).with('/etc/group').and_yield(group1).and_yield(group2).and_yield(group3)
397+
allow(File).to receive(:open).with(Pathname.new('/etc/group')).and_yield(content)
390398
expect(provider.groups).to eq(['group1', 'group3'])
391399
end
392400

401+
it "does not raise when parsing empty lines in /etc/group" do
402+
resource[:forcelocal] = true
403+
allow(Puppet::FileSystem).to receive(:exist?).with('/etc/group').and_return(true)
404+
allow(File).to receive(:open).with(Pathname.new('/etc/group')).and_yield(content_with_empty_line)
405+
expect { provider.groups }.not_to raise_error
406+
end
407+
393408
it "should fall back to nameservice groups when forcelocal is false" do
394409
resource[:forcelocal] = false
395410
allow(Puppet::Util::POSIX).to receive(:groups_of).with('myuser').and_return(['remote groups'])

0 commit comments

Comments
 (0)