summaryrefslogtreecommitdiff
path: root/lib/chef/http
diff options
context:
space:
mode:
authorJon Morrow <jon@morrowmail.com>2015-04-10 12:02:30 -0700
committerJon Morrow <jon@morrowmail.com>2015-04-14 16:44:19 -0700
commit4dd42c8ea0c738489f9e4a0df20133a328ce06e5 (patch)
tree32e62d8d36871ab8dd2f480f56a31c532cc196cd /lib/chef/http
parent28620c815fabc1eba6e7a047f571d3be7a1efe52 (diff)
downloadchef-4dd42c8ea0c738489f9e4a0df20133a328ce06e5.tar.gz
Fixes 2140 - Honor Proxy from Env
This change adds support to Chef's core http libs to honor a proxy uri set in the environment not just chef/knife config. It also adds support for username and password both in uri and env. The order of precidence for values is uri, config, env.
Diffstat (limited to 'lib/chef/http')
-rw-r--r--lib/chef/http/basic_client.rb25
1 files changed, 21 insertions, 4 deletions
diff --git a/lib/chef/http/basic_client.rb b/lib/chef/http/basic_client.rb
index f0f5151dbd..076d152d16 100644
--- a/lib/chef/http/basic_client.rb
+++ b/lib/chef/http/basic_client.rb
@@ -97,7 +97,9 @@ class Chef
#adapted from buildr/lib/buildr/core/transports.rb
def proxy_uri
- proxy = Chef::Config["#{url.scheme}_proxy"]
+ proxy = Chef::Config["#{url.scheme}_proxy"] ||
+ env["#{url.scheme.upcase}_PROXY"] || env["#{url.scheme}_proxy"]
+
# Check if the proxy string contains a scheme. If not, add the url's scheme to the
# proxy before parsing. The regex /^.*:\/\// matches, for example, http://.
proxy = if proxy.match(/^.*:\/\//)
@@ -105,7 +107,8 @@ class Chef
else
URI.parse("#{url.scheme}://#{proxy}")
end if String === proxy
- excludes = Chef::Config[:no_proxy].to_s.split(/\s*,\s*/).compact
+ no_proxy = Chef::Config[:no_proxy] || env['NO_PROXY'] || env['no_proxy']
+ excludes = no_proxy.to_s.split(/\s*,\s*/).compact
excludes = excludes.map { |exclude| exclude =~ /:\d+$/ ? exclude : "#{exclude}:*" }
return proxy unless excludes.any? { |exclude| File.fnmatch(exclude, "#{host}:#{port}") }
end
@@ -126,18 +129,32 @@ class Chef
Chef::Config
end
+ def env
+ ENV
+ end
+
def http_client_builder
http_proxy = proxy_uri
if http_proxy.nil?
Net::HTTP
else
Chef::Log.debug("Using #{http_proxy.host}:#{http_proxy.port} for proxy")
- user = Chef::Config["#{url.scheme}_proxy_user"]
- pass = Chef::Config["#{url.scheme}_proxy_pass"]
+ user = http_proxy_user(http_proxy)
+ pass = http_proxy_pass(http_proxy)
Net::HTTP.Proxy(http_proxy.host, http_proxy.port, user, pass)
end
end
+ def http_proxy_user(http_proxy)
+ http_proxy.user || Chef::Config["#{url.scheme}_proxy_user"] ||
+ env["#{url.scheme.upcase}_PROXY_USER"] || env["#{url.scheme}_proxy_user"]
+ end
+
+ def http_proxy_pass(http_proxy)
+ http_proxy.password || Chef::Config["#{url.scheme}_proxy_pass"] ||
+ env["#{url.scheme.upcase}_PROXY_PASS"] || env["#{url.scheme}_proxy_pass"]
+ end
+
def configure_ssl(http_client)
http_client.use_ssl = true
ssl_policy.apply_to(http_client)