summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian C. Dunn <jdunn@getchef.com>2014-08-14 15:12:12 -0400
committerJulian C. Dunn <jdunn@getchef.com>2014-08-14 15:12:12 -0400
commitfec9467a0c93b203e97447207352745536c2b471 (patch)
tree2eaa0cc04792c3c16766ca8dcd07aa787560136b
parentb1f8060ae763d9c8e2304522a94cda292274fa5e (diff)
downloadohai-fec9467a0c93b203e97447207352745536c2b471.tar.gz
getlogin() often lies, especially when run under "su" -- it still returns the
calling user, so node['current_user'] is set incorrectly in this case. According to the Ruby documentation, it's safer to use getpwuid().
-rw-r--r--lib/ohai/plugins/passwd.rb2
-rw-r--r--spec/unit/plugins/passwd_spec.rb3
2 files changed, 3 insertions, 2 deletions
diff --git a/lib/ohai/plugins/passwd.rb b/lib/ohai/plugins/passwd.rb
index 1e10f708..9a5cd06f 100644
--- a/lib/ohai/plugins/passwd.rb
+++ b/lib/ohai/plugins/passwd.rb
@@ -32,7 +32,7 @@ Ohai.plugin(:Passwd) do
end
unless current_user
- current_user fix_encoding(Etc.getlogin)
+ current_user fix_encoding(Etc.getpwuid(Process.euid).name)
end
end
end
diff --git a/spec/unit/plugins/passwd_spec.rb b/spec/unit/plugins/passwd_spec.rb
index 33b57005..335ba6b7 100644
--- a/spec/unit/plugins/passwd_spec.rb
+++ b/spec/unit/plugins/passwd_spec.rb
@@ -24,7 +24,8 @@ describe Ohai::System, "plugin etc" do
end
it "should set the current user" do
- Etc.should_receive(:getlogin).and_return('chef')
+ Process.should_receive(:euid).and_return('31337')
+ Etc.should_receive(:getpwuid).and_return(PasswdEntry.new('chef', 31337, 31337, '/home/chef', '/bin/ksh', 'Julia Child'))
@plugin.run
@plugin[:current_user].should == 'chef'
end