Skip to content

Commit c4e4797

Browse files
natikgadzhist0012
andauthored
Allow proxy configuration via HTTP_PROXY env var (#2161)
* Allow proxy config via environment * Unit tests for env[http_proxy] support * Update sentry-ruby/spec/sentry/transport/http_transport_spec.rb Co-authored-by: Stan Lo <stan001212@gmail.com> * Add transport/configuration comments * Fixed Ruby version checks for proxy conf --------- Co-authored-by: Stan Lo <stan001212@gmail.com>
1 parent 19251ea commit c4e4797

File tree

4 files changed

+105
-5
lines changed

4 files changed

+105
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
- Fix breadcrumbs with `warn` level not being ingested [#2150](https://github.com/getsentry/sentry-ruby/pull/2150)
4747
- Fixes [#2145](https://github.com/getsentry/sentry-ruby/issues/2145)
4848
- Don't send negative line numbers in profiles [#2158](https://github.com/getsentry/sentry-ruby/pull/2158)
49+
- Allow transport proxy configuration to be set with `HTTP_PROXY` environment variable [#2161](https://github.com/getsentry/sentry-ruby/pull/2161)
4950

5051
## 5.12.0
5152

sentry-ruby/lib/sentry/transport/configuration.rb

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,80 @@
33
module Sentry
44
class Transport
55
class Configuration
6-
attr_accessor :timeout, :open_timeout, :proxy, :ssl, :ssl_ca_file, :ssl_verification, :encoding
6+
7+
# The timeout in seconds to open a connection to Sentry, in seconds.
8+
# Default value is 2.
9+
#
10+
# @return [Integer]
11+
attr_accessor :timeout
12+
13+
# The timeout in seconds to read data from Sentry, in seconds.
14+
# Default value is 1.
15+
#
16+
# @return [Integer]
17+
attr_accessor :open_timeout
18+
19+
# The proxy configuration to use to connect to Sentry.
20+
# Accepts either a URI formatted string, URI, or a hash with the `uri`,
21+
# `user`, and `password` keys.
22+
#
23+
# @example
24+
# # setup proxy using a string:
25+
# config.transport.proxy = "https://user:password@proxyhost:8080"
26+
#
27+
# # setup proxy using a URI:
28+
# config.transport.proxy = URI("https://user:password@proxyhost:8080")
29+
#
30+
# # setup proxy using a hash:
31+
# config.transport.proxy = {
32+
# uri: URI("https://proxyhost:8080"),
33+
# user: "user",
34+
# password: "password"
35+
# }
36+
#
37+
# If you're using the default transport (`Sentry::HTTPTransport`),
38+
# proxy settings will also automatically be read from tne environment
39+
# variables (`HTTP_PROXY`, `HTTPS_PROXY`, `NO_PROXY`).
40+
#
41+
# @return [String, URI, Hash, nil]
42+
attr_accessor :proxy
43+
44+
# The SSL configuration to use to connect to Sentry.
45+
# You can either pass a `Hash` containing `ca_file` and `verification` keys,
46+
# or you can set those options directly on the `Sentry::HTTPTransport::Configuration` object:
47+
#
48+
# @example
49+
# config.transport.ssl = {
50+
# ca_file: "/path/to/ca_file",
51+
# verification: true
52+
# end
53+
#
54+
# @return [Hash, nil]
55+
attr_accessor :ssl
56+
57+
# The path to the CA file to use to verify the SSL connection.
58+
# Default value is `nil`.
59+
#
60+
# @return [String, nil]
61+
attr_accessor :ssl_ca_file
62+
63+
# Whether to verify that the peer certificate is valid in SSL connections.
64+
# Default value is `true`.
65+
#
66+
# @return [Boolean]
67+
attr_accessor :ssl_verification
68+
69+
# The encoding to use to compress the request body.
70+
# Default value is `Sentry::HTTPTransport::GZIP_ENCODING`.
71+
#
72+
# @return [String]
73+
attr_accessor :encoding
74+
75+
# The class to use as a transport to connect to Sentry.
76+
# If this option not set, it will return `nil`, and Sentry will use
77+
# `Sentry::HTTPTransport` by default.
78+
#
79+
# @return [Class, nil]
780
attr_reader :transport_class
881

982
def initialize

sentry-ruby/lib/sentry/transport/http_transport.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,15 @@ def should_compress?(data)
128128

129129
def conn
130130
server = URI(@dsn.server)
131-
131+
132+
# connection respects proxy setting from @transport_configuration, or environment variables (HTTP_PROXY, HTTPS_PROXY, NO_PROXY)
133+
# Net::HTTP will automatically read the env vars.
134+
# See https://ruby-doc.org/3.2.2/stdlibs/net/Net/HTTP.html#class-Net::HTTP-label-Proxies
132135
connection =
133136
if proxy = normalize_proxy(@transport_configuration.proxy)
134137
::Net::HTTP.new(server.hostname, server.port, proxy[:uri].hostname, proxy[:uri].port, proxy[:user], proxy[:password])
135138
else
136-
::Net::HTTP.new(server.hostname, server.port, nil)
139+
::Net::HTTP.new(server.hostname, server.port)
137140
end
138141

139142
connection.use_ssl = server.scheme == "https"
@@ -148,6 +151,9 @@ def conn
148151
connection
149152
end
150153

154+
# @param proxy [String, URI, Hash] Proxy config value passed into `config.transport`.
155+
# Accepts either a URI formatted string, URI, or a hash with the `uri`, `user`, and `password` keys.
156+
# @return [Hash] Normalized proxy config that will be passed into `Net::HTTP`
151157
def normalize_proxy(proxy)
152158
return proxy unless proxy
153159

sentry-ruby/spec/sentry/transport/http_transport_spec.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
expect(subject.send(:conn).port).to eq(80)
6464
end
6565
end
66-
context "with http DSN" do
66+
context "with https DSN" do
6767
let(:dsn) { "https://12345:67890@sentry.localdomain/sentry/42" }
6868

6969
it "sets port to 443" do
@@ -129,13 +129,33 @@
129129
subject.send_data(data)
130130
end
131131

132+
it "accepts a proxy from ENV[HTTP_PROXY]" do
133+
begin
134+
ENV["http_proxy"] = "https://stan:foobar@example.com:8080"
135+
136+
stub_request(fake_response) do |_, http_obj|
137+
expect(http_obj.proxy_address).to eq("example.com")
138+
expect(http_obj.proxy_port).to eq(8080)
139+
140+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.5")
141+
expect(http_obj.proxy_user).to eq("stan")
142+
expect(http_obj.proxy_pass).to eq("foobar")
143+
end
144+
end
145+
146+
subject.send_data(data)
147+
ensure
148+
ENV["http_proxy"] = nil
149+
end
150+
end
151+
132152
it "accepts custom timeout" do
133153
configuration.transport.timeout = 10
134154

135155
stub_request(fake_response) do |_, http_obj|
136156
expect(http_obj.read_timeout).to eq(10)
137157

138-
if RUBY_VERSION >= "2.6"
158+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6")
139159
expect(http_obj.write_timeout).to eq(10)
140160
end
141161
end

0 commit comments

Comments
 (0)