Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expanded support for proxy config from environment variables #437

Merged
merged 4 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/bugsnag/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,9 @@ def initialize
# Read the API key from the environment
self.api_key = ENV["BUGSNAG_API_KEY"]

# Read NET::HTTP proxy environment variable
if ENV["http_proxy"]
uri = URI.parse(ENV["http_proxy"])
self.proxy_host = uri.host
self.proxy_port = uri.port
self.proxy_user = uri.user
self.proxy_password = uri.password
# Read NET::HTTP proxy environment variables
if (proxy_uri = ENV["https_proxy"] || ENV['http_proxy'])
parse_proxy(proxy_uri)
end

# Set up logging
Expand Down Expand Up @@ -184,6 +180,16 @@ def debug(message)
logger.debug(PROG_NAME) { message }
end

##
# Parses and sets proxy from a uri
def parse_proxy(uri)
proxy = URI.parse(uri)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be any problems if uri was nil at this point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, however this would only occur if parse_proxy was called with nil, in which case URI would spit out a legible error i.e. **URI::InvalidURIError: bad URI(is not URI?):**

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could catch and log that error, but I think failing loudly is more valuable in most cases.

self.proxy_host = proxy.host
self.proxy_port = proxy.port
self.proxy_user = proxy.user
self.proxy_password = proxy.password
end

private

PROG_NAME = "[Bugsnag]"
Expand Down
49 changes: 49 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,55 @@
end
end

describe "set_proxy" do
it "defaults proxy settings to nil" do
expect(subject.proxy_host).to be nil
expect(subject.proxy_port).to be nil
expect(subject.proxy_user).to be nil
expect(subject.proxy_password).to be nil
end

it "allows proxy settings to be set directly" do
subject.proxy_host = "http://localhost"
subject.proxy_port = 34000
subject.proxy_user = "user"
subject.proxy_password = "password"
expect(subject.proxy_host).to eq("http://localhost")
expect(subject.proxy_port).to eq(34000)
expect(subject.proxy_user).to eq("user")
expect(subject.proxy_password).to eq("password")
end

it "parses a uri if provided" do
subject.parse_proxy("http://user:password@localhost:34000")
expect(subject.proxy_host).to eq("localhost")
expect(subject.proxy_port).to eq(34000)
expect(subject.proxy_user).to eq("user")
expect(subject.proxy_password).to eq("password")
end

it "automatically parses http_proxy environment variable" do
ENV['http_proxy'] = "http://user:password@localhost:34000"
expect(subject.proxy_host).to eq("localhost")
expect(subject.proxy_port).to eq(34000)
expect(subject.proxy_user).to eq("user")
expect(subject.proxy_password).to eq("password")
end

it "automatically parses https_proxy environment variable" do
ENV['https_proxy'] = "https://user:password@localhost:34000"
expect(subject.proxy_host).to eq("localhost")
expect(subject.proxy_port).to eq(34000)
expect(subject.proxy_user).to eq("user")
expect(subject.proxy_password).to eq("password")
end

after do
ENV['http_proxy'] = nil
ENV['https_proxy'] = nil
end
end

describe "logger" do
class TestLogger
attr_accessor :logs
Expand Down