Browse files

Merge branch 'CHEF-2130'

  • Loading branch information...
2 parents a6fa6f9 + 6da1ab0 commit 2c53cc9338880de629b41d4d807e4be1010ca8c0 @danielsdeleo danielsdeleo committed Mar 25, 2011
Showing with 36 additions and 0 deletions.
  1. +15 −0 chef/lib/chef/client.rb
  2. +3 −0 chef/lib/chef/config.rb
  3. +18 −0 chef/spec/unit/client_spec.rb
View
15 chef/lib/chef/client.rb
@@ -42,6 +42,8 @@ class Chef
# syncs cookbooks if necessary, and triggers convergence.
class Client
+ SANE_PATHS = %w[/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin]
+
# Clears all notifications for client run status events.
# Primarily for testing purposes.
def self.clear_notifications
@@ -141,6 +143,7 @@ def initialize(json_attribs=nil)
def run
run_context = nil
+ enforce_path_sanity
run_ohai
register unless Chef::Config[:solo]
build_node
@@ -308,6 +311,18 @@ def converge(run_context)
true
end
+ def enforce_path_sanity(env=ENV)
+ if Chef::Config[:enforce_path_sanity] && RUBY_PLATFORM !~ /mswin|mingw32|windows/
+ existing_paths = env["PATH"].split(':')
+ SANE_PATHS.each do |sane_path|
+ unless existing_paths.include?(sane_path)
+ env["PATH"] << ':' unless env["PATH"].empty?
+ env["PATH"] << sane_path
+ end
+ end
+ end
+ end
+
private
def directory_not_empty?(path)
View
3 chef/lib/chef/config.rb
@@ -101,6 +101,9 @@ def self.inspect
providers
end
+ # Turn on "path sanity" by default. See also: http://wiki.opscode.com/display/chef/User+Environment+PATH+Sanity
+ enforce_path_sanity(true)
+
# Used when OpenID authentication is enabled in the Web UI
authorized_openid_identifiers nil
authorized_openid_providers nil
View
18 chef/spec/unit/client_spec.rb
@@ -50,6 +50,24 @@
@client.node = @node
end
+ describe "when enforcing path sanity" do
+ before do
+ Chef::Config[:enforce_path_sanity] = true
+ end
+
+ it "adds all useful PATHs that are not yet in PATH to PATH" do
+ env = {"PATH" => ""}
+ @client.enforce_path_sanity(env)
+ env["PATH"].should == "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
+ end
+
+ it "does not re-add paths that already exist in PATH" do
+ env = {"PATH" => "/usr/bin:/sbin:/bin"}
+ @client.enforce_path_sanity(env)
+ env["PATH"].should == "/usr/bin:/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin"
+ end
+ end
+
describe "run" do
it "should identify the node and run ohai, then register the client" do
mock_chef_rest_for_node = mock("Chef::REST (node)")

0 comments on commit 2c53cc9

Please sign in to comment.