From c8079f0d7bd25ae6feec9a4d24539c1142526d82 Mon Sep 17 00:00:00 2001 From: Matt Robenolt Date: Fri, 7 Dec 2018 10:52:42 -0800 Subject: [PATCH] Provide the ability to override HTTPTransport configuration --- client.go | 58 ++++++++++++++++++++++++++++++++++---------------- client_test.go | 2 +- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/client.go b/client.go index a2c9a6c..4bcdb95 100644 --- a/client.go +++ b/client.go @@ -6,6 +6,7 @@ import ( "compress/zlib" "crypto/rand" "crypto/tls" + "crypto/x509" "encoding/base64" "encoding/hex" "encoding/json" @@ -13,7 +14,6 @@ import ( "fmt" "io" "io/ioutil" - "log" mrand "math/rand" "net/http" "net/url" @@ -336,25 +336,9 @@ func (c *context) interfaces() []Interface { // Packets will be dropped if the buffer is full. Used by NewClient. var MaxQueueBuffer = 100 -func newTransport() Transport { - t := &HTTPTransport{} - rootCAs, err := gocertifi.CACerts() - if err != nil { - log.Println("raven: failed to load root TLS certificates:", err) - } else { - t.Client = &http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, - TLSClientConfig: &tls.Config{RootCAs: rootCAs}, - }, - } - } - return t -} - func newClient(tags map[string]string) *Client { client := &Client{ - Transport: newTransport(), + Transport: NewHTTPTransport(nil), Tags: tags, context: &context{}, sampleRate: 1.0, @@ -528,6 +512,16 @@ func (client *Client) SetSampleRate(rate float32) error { return nil } +// SetTransport sets the client's Transport +func (client *Client) SetTransport(t Transport) { + client.mu.Lock() + defer client.mu.Unlock() + client.Transport = t +} + +// SetTransport sets the Transport on the default *Client +func SetTransport(t Transport) { DefaultClient.SetTransport(t) } + // SetRelease sets the "release" tag on the default *Client func SetRelease(release string) { DefaultClient.SetRelease(release) } @@ -922,6 +916,34 @@ type HTTPTransport struct { *http.Client } +// HTTPTransportOptions are options to configure the HTTPTransport +type HTTPTransportOptions struct { + InsecureSkipVerify bool + RootCAs *x509.CertPool +} + +func NewHTTPTransport(o *HTTPTransportOptions) *HTTPTransport { + if o == nil { + o = &HTTPTransportOptions{} + } + if o.RootCAs == nil { + // This can't actually error, so there's no point. + rootCAs, _ := gocertifi.CACerts() + o.RootCAs = rootCAs + } + return &HTTPTransport{ + Client: &http.Client{ + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + TLSClientConfig: &tls.Config{ + RootCAs: o.RootCAs, + InsecureSkipVerify: o.InsecureSkipVerify, + }, + }, + }, + } +} + func (t *HTTPTransport) Send(url, authHeader string, packet *Packet) error { if url == "" { return nil diff --git a/client_test.go b/client_test.go index eb342e4..ba0dced 100644 --- a/client_test.go +++ b/client_test.go @@ -16,7 +16,7 @@ func TestShouldExcludeErr(t *testing.T) { regexpStrs := []string{"ERR_TIMEOUT", "should.exclude", "(?i)^big$"} client := &Client{ - Transport: newTransport(), + Transport: NewHTTPTransport(nil), Tags: nil, context: &context{}, queue: make(chan *outgoingPacket, MaxQueueBuffer),