-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addition of stdlib::getpwnam to return
/etc/passwd
entry.
Given a username `stdlib::getpwnam` returns the password entry for that user. Example ```puppet $passwd_entry = stdlib::getpwnam('root') ``` then `$passwd_entry` would contain: ```puppet { 'name'=>'root', 'passwd'=>'x', 'uid'=>0, 'gid'=>0, 'gecos'=>'root', 'dir'=>'/root', 'shell'=>'/bin/bash', } ```
- Loading branch information
1 parent
ed474d2
commit 4c6f5e6
Showing
3 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
# @summary | ||
# Given a username returns the user's entry from the `/etc/passwd` file. | ||
# | ||
# | ||
Puppet::Functions.create_function(:'stdlib::getpwnam') do | ||
# @param user | ||
# The username to fetch password record for. | ||
# | ||
# @example Get a password entry for user steve as a hash. | ||
# $passwd_entry = stdlib::getpwnam('steve') | ||
# | ||
# @example Get the UID of user steve | ||
# $uid = stdlib::getpwnam('steve')["uid"] | ||
# | ||
# @return | ||
# [Hash] For example {"name"=>"root", "passwd"=>"x", "uid"=>0, "gid"=>0, "gecos"=>"root", "dir"=>"/root", "shell"=>"/bin/bash"} | ||
dispatch :getpwnam do | ||
param 'String', :user | ||
return_type 'Hash' | ||
end | ||
|
||
def getpwnam(user) | ||
Etc.getpwnam(user).to_h.first(7).map { |k, v| { k.to_s => v } }.reduce(:merge) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'stdlib::getpwnam' do | ||
it { is_expected.not_to be_nil } | ||
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects 1 argument, got none}i) } | ||
|
||
it { | ||
Check failure on line 9 in spec/functions/getpwnam_spec.rb
|
||
passwd_entry = Etc::Passwd.new( | ||
'name' => 'steve', | ||
'passwd' => 'x', | ||
'uid' => 1000, | ||
'gid' => 1001, | ||
'gecos' => 'Steve', | ||
'dir' => '/home/steve', | ||
'shell' => '/bin/fish', | ||
) | ||
allow(Etc).to receive(:getpwnam).with(anything, anything).and_call_original | ||
allow(Etc).to receive(:getpwnam).with('steve').and_return(passwd_entry) | ||
is_expected.to run.with_params('steve').and_return( | ||
{ | ||
'name' => 'steve', | ||
'passwd' => 'x', | ||
'uid' => 1000, | ||
'gid' => 1001, | ||
'gecos' => 'Steve', | ||
'dir' => '/home/steve', | ||
'shell' => '/bin/fish' | ||
}, | ||
) | ||
} | ||
|
||
it { is_expected.to run.with_params('notexisting').and_raise_error(ArgumentError, %r{can't find user for notexisting}i) } | ||
end |