Module: Chef::Mixin::PathSanity

Included in:
Client, Knife, ShellOut
Defined in:
lib/chef/mixin/path_sanity.rb

Instance Method Summary collapse

Instance Method Details

#enforce_path_sanity(env = ENV) ⇒ Object



23
24
25
26
27
# File 'lib/chef/mixin/path_sanity.rb', line 23

def enforce_path_sanity(env = ENV)
  if Chef::Config[:enforce_path_sanity]
    env["PATH"] = sanitized_path(env)
  end
end

#sanitized_path(env = ENV) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chef/mixin/path_sanity.rb', line 29

def sanitized_path(env = ENV)
  env_path = env["PATH"].nil? ? "" : env["PATH"].dup
  path_separator = Chef::Platform.windows? ? ";" : ":"
  # ensure the Ruby and Gem bindirs are included
  # mainly for 'full-stack' Chef installs
  new_paths = env_path.split(path_separator)
  [ ruby_bindir, gem_bindir ].compact.each do |path|
    new_paths = [ path ] + new_paths unless new_paths.include?(path)
  end
  sane_paths.each do |path|
    new_paths << path unless new_paths.include?(path)
  end
  new_paths.join(path_separator).encode("utf-8", invalid: :replace, undef: :replace)
end