Skip to content

Commit

Permalink
Allow Setting Proxy without Using HTTP_PROXY, etc. (#180)
Browse files Browse the repository at this point in the history
* Added more options for setting proxy

* The dogapi now allows setting env variables such as DOGAPI_HTTP_PROXY instead
of only allow HTTP_PROXY and the like

* incrementing version tag

* Revert "incrementing version tag"

This reverts commit 1a75c67.

* Removed an unused requires statement

* Making changes suggested by PR comments

* fixed a type

Co-authored-by: Jiri Kuncar <[email protected]>
Co-authored-by: Hippolyte HENRY <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2020
1 parent 0f2fa56 commit 39599ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/dogapi/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ def initialize(api_key, application_key, silent=true, timeout=nil, endpoint=nil)
def connect
connection = Net::HTTP

# After ruby 2.0 Net::HTTP looks for the env variable but not ruby 1.9
if RUBY_VERSION < '2.0.0'
proxy = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy']
if proxy
proxy_uri = URI.parse(proxy)
connection = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
end
# Expose using a proxy without setting the HTTPS_PROXY or HTTP_PROXY variables
proxy = Dogapi.find_proxy()

if proxy
proxy_uri = URI.parse(proxy)
connection = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
end

uri = URI.parse(@api_host)
Expand Down Expand Up @@ -211,6 +210,12 @@ def Dogapi.find_localhost
@@hostname = Addrinfo.getaddrinfo(Socket.gethostname, nil, nil, nil, nil, Socket::AI_CANONNAME).first.canonname
end

def Dogapi.find_proxy
ENV['DD_PROXY_HTTPS'] || ENV['dd_proxy_https'] ||
ENV['DD_PROXY_HTTP'] || ENV['dd_proxy_http'] ||
ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy']
end

def Dogapi.validate_tags(tags)
unless tags.is_a? Array
raise ArgumentError, "The tags parameter needs to be an array of string. Current value: #{tags}"
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@
ENV['http_proxy'] = nil
end

it 'uses-the dogapi specific proxy if set' do
service = Dogapi::APIService.new('api_key', 'app_key')

service.connect do |conn|
expect(conn.proxy_address).to be(nil)
expect(conn.proxy_port).to be(nil)
end

ENV['https_proxy'] = 'https://www.proxy.com:443'
ENV['dd_proxy_https'] = 'https://www.otherproxy.com:443'

service.connect do |conn|
expect(conn.proxy_address).to eq 'www.otherproxy.com'
expect(conn.proxy_port).to eq 443
end

ENV['https_proxy'] = nil
ENV['dd_proxy_https'] = nil
end

it 'respects the endpoint configuration' do
service = Dogapi::APIService.new('api_key', 'app_key', true, nil, 'https://app.example.com')

Expand Down

0 comments on commit 39599ca

Please sign in to comment.