diff options
author | tyler-ball <tyleraball@gmail.com> | 2015-12-08 17:02:23 -0700 |
---|---|---|
committer | tyler-ball <tyleraball@gmail.com> | 2015-12-09 14:23:54 -0700 |
commit | 85d0407a16fa4dfc479d550a96355a6d11f4f551 (patch) | |
tree | 5be428017e0883fdcaa19c74308bb73783270071 /chef-config/lib | |
parent | 200c3e5b71ec269efffbfb97e94f1a7aada951af (diff) | |
download | chef-85d0407a16fa4dfc479d550a96355a6d11f4f551.tar.gz |
Moving the proxy export to environment varibles into Chef::Config
Diffstat (limited to 'chef-config/lib')
-rw-r--r-- | chef-config/lib/chef-config/config.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index 49d775232d..113bf481ff 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -26,6 +26,7 @@ require 'chef-config/logger' require 'chef-config/windows' require 'chef-config/path_helper' require 'mixlib/shellout' +require 'uri' module ChefConfig @@ -710,6 +711,66 @@ module ChefConfig config_context :chefdk do end + configurable(:http_proxy) + configurable(:http_proxy_user) + configurable(:http_proxy_pass) + configurable(:https_proxy) + configurable(:https_proxy_user) + configurable(:https_proxy_pass) + configurable(:ftp_proxy) + configurable(:ftp_proxy_user) + configurable(:ftp_proxy_pass) + configurable(:no_proxy) + + # Public method that users should call to export proxies to the appropriate + # environment variables. This method should be called after the config file is + # parsed and loaded. + # TODO add some post-file-parsing logic that automatically calls this so + # users don't have to + def self.export_proxies + export_proxy("http", http_proxy, http_proxy_user, http_proxy_pass) if http_proxy + export_proxy("https", https_proxy, https_proxy_user, https_proxy_pass) if https_proxy + export_proxy("ftp", ftp_proxy, ftp_proxy_user, ftp_proxy_pass) if ftp_proxy + export_no_proxy("no_proxy", no_proxy) if no_proxy + end + + # Builds a proxy uri and exports it to the appropriate environment variables. Examples: + # http://username:password@hostname:port + # https://username@hostname:port + # ftp://hostname:port + # when + # scheme = "http", "https", or "ftp" + # hostport = hostname:port or scheme://hostname:port + # user = username + # pass = password + # @api private + def self.export_proxy(scheme, path, user, pass) + path = "#{scheme}://#{path}" unless path.include?('://') + # URI.split returns the following parts: + # [scheme, userinfo, host, port, registry, path, opaque, query, fragment] + parts = URI.split(URI.encode(path)) + # URI::Generic.build requires an integer for the port, but URI::split gives + # returns a string for the port. + parts[3] = parts[3].to_i if parts[3] + if user && !user.empty? + userinfo = URI.encode(URI.encode(user), '@:') + if pass + userinfo << ":#{URI.encode(URI.encode(pass), '@:')}" + end + parts[1] = userinfo + end + + path = URI::Generic.build(parts).to_s + ENV["#{scheme}_proxy".downcase] = path unless ENV["#{scheme}_proxy".downcase] + ENV["#{scheme}_proxy".upcase] = path unless ENV["#{scheme}_proxy".upcase] + end + + # @api private + def self.export_no_proxy(value) + ENV['no_proxy'] = value unless ENV['no_proxy'] + ENV['NO_PROXY'] = value unless ENV['NO_PROXY'] + end + # Chef requires an English-language UTF-8 locale to function properly. We attempt # to use the 'locale -a' command and search through a list of preferences until we # find one that we can use. On Ubuntu systems we should find 'C.UTF-8' and be |