Skip to content

Commit

Permalink
Merge branch 'CHEF-2130'
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsdeleo committed Mar 25, 2011
2 parents a6fa6f9 + 6da1ab0 commit 2c53cc9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions chef/lib/chef/client.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions chef/lib/chef/config.rb
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions chef/spec/unit/client_spec.rb
Expand Up @@ -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)")
Expand Down

0 comments on commit 2c53cc9

Please sign in to comment.