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

fix(http): use default transport consitently #2167

Merged
merged 2 commits into from
Mar 11, 2019
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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## unreleased

### Features

- [#2154](https://github.com/influxdata/kapacitor/pull/2154): Add ability to skip ssl verification with an alert post node. Thanks @itsHabib!

### Bugfixes

- [#2167](https://github.com/influxdata/kapacitor/pull/2167): Use default transport consistently.

## v1.5.2 [2018-12-12]

### Features
Expand All @@ -12,7 +20,6 @@
- [#2101](https://github.com/influxdata/kapacitor/issues/2101): Add multiple field support to the change detect node.
- [#1961](https://github.com/influxdata/kapacitor/pull/1961): Add links to pagerduty2 alerts
- [#1974](https://github.com/influxdata/kapacitor/issues/1974): Add additional metadata to Sensu alerts.
- [#2154](https://github.com/influxdata/kapacitor/pull/2154): Add ability to skip ssl verification with an alert post node.

### Bugfixes

Expand Down
10 changes: 4 additions & 6 deletions client/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/influxdata/influxdb/influxql"
khttp "github.com/influxdata/kapacitor/http"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -176,12 +177,9 @@ func New(conf Config) (*Client, error) {
var tr *http.Transport

if rt == nil {
tr = &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: conf.InsecureSkipVerify,
},
}
tr = khttp.NewDefaultTransportWithTLS(&tls.Config{
InsecureSkipVerify: conf.InsecureSkipVerify,
})
if conf.TLSConfig != nil {
tr.TLSClientConfig = conf.TLSConfig
}
Expand Down
34 changes: 34 additions & 0 deletions http/transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package http

import (
"crypto/tls"
"net"
"net/http"
"time"
)

// NewDefaultTransport creates a new transport with sane defaults.
func NewDefaultTransport() *http.Transport {
// These defaults are copied from http.DefaultTransport.
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
// Below are changes from http.DefaultTransport
MaxIdleConnsPerHost: 100, // increased from 2, services tend to connect to a single host
}
}

// NewDefaultTransportWithTLS creates a new transport with the specified TLS configuration.
func NewDefaultTransportWithTLS(tlsConfig *tls.Config) *http.Transport {
t := NewDefaultTransport()
t.TLSClientConfig = tlsConfig
return t
}
5 changes: 2 additions & 3 deletions influxdb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

imodels "github.com/influxdata/influxdb/models"
khttp "github.com/influxdata/kapacitor/http"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -123,9 +124,7 @@ func NewHTTPClient(conf Config) (*HTTPClient, error) {
return nil, errors.Wrap(err, "invalid URLs")
}
if conf.Transport == nil {
conf.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
}
conf.Transport = khttp.NewDefaultTransport()
}
c := &HTTPClient{
config: conf,
Expand Down
11 changes: 3 additions & 8 deletions services/alerta/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/influxdata/kapacitor/alert"
khttp "github.com/influxdata/kapacitor/http"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/influxdata/kapacitor/models"
"github.com/pkg/errors"
Expand Down Expand Up @@ -45,10 +46,7 @@ func NewService(c Config, d Diagnostic) *Service {
}
s.configValue.Store(c)
s.clientValue.Store(&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.InsecureSkipVerify},
},
Transport: khttp.NewDefaultTransportWithTLS(&tls.Config{InsecureSkipVerify: c.InsecureSkipVerify}),
})
return s
}
Expand Down Expand Up @@ -128,10 +126,7 @@ func (s *Service) Update(newConfig []interface{}) error {
} else {
s.configValue.Store(c)
s.clientValue.Store(&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.InsecureSkipVerify},
},
Transport: khttp.NewDefaultTransportWithTLS(&tls.Config{InsecureSkipVerify: c.InsecureSkipVerify}),
})
}

Expand Down
16 changes: 9 additions & 7 deletions services/httppost/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"

"github.com/influxdata/kapacitor/alert"
khttp "github.com/influxdata/kapacitor/http"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -329,17 +330,18 @@ func (h *handler) Handle(event alert.Event) {
req = req.WithContext(ctx)
}

httpClient := &http.Client{}

// Skip SSL verification?
// Setup HTTP client
var tlsConfig *tls.Config
if h.skipSSLVerification {
httpClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
tlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
}

httpClient := &http.Client{
Transport: khttp.NewDefaultTransportWithTLS(tlsConfig),
}

// Execute the request
resp, err := httpClient.Do(req)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions services/influxdb/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cenkalti/backoff"
"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/influxdb/models"
khttp "github.com/influxdata/kapacitor/http"
"github.com/influxdata/kapacitor/influxdb"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/influxdata/kapacitor/server/vars"
Expand Down Expand Up @@ -501,10 +502,7 @@ func httpConfig(c Config) (influxdb.Config, error) {
if err != nil {
return influxdb.Config{}, errors.Wrap(err, "invalid TLS options")
}
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
}
tr := khttp.NewDefaultTransportWithTLS(tlsConfig)
var credentials influxdb.Credentials
if c.Username != "" {
credentials = influxdb.Credentials{
Expand Down
11 changes: 3 additions & 8 deletions services/k8s/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync"
"sync/atomic"

khttp "github.com/influxdata/kapacitor/http"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -123,10 +124,7 @@ func New(c Config) (Client, error) {
config: c,
urls: urls,
client: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: c.TLSConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(c.TLSConfig),
},
}, nil
}
Expand Down Expand Up @@ -168,10 +166,7 @@ func (c *httpClient) Update(new Config) error {

if old.TLSConfig != new.TLSConfig {
c.client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: new.TLSConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(new.TLSConfig),
}
}
return nil
Expand Down
11 changes: 3 additions & 8 deletions services/slack/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"

"github.com/influxdata/kapacitor/alert"
khttp "github.com/influxdata/kapacitor/http"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/influxdata/kapacitor/tlsconfig"
"github.com/pkg/errors"
Expand All @@ -37,10 +38,7 @@ func NewWorkspace(c Config) (*Workspace, error) {
}

cl := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(tlsConfig),
}

return &Workspace{
Expand Down Expand Up @@ -68,10 +66,7 @@ func (w *Workspace) Update(c Config) error {
}

cl := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(tlsConfig),
}

w.client = cl
Expand Down
9 changes: 3 additions & 6 deletions services/swarm/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
khttp "github.com/influxdata/kapacitor/http"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -52,9 +53,7 @@ func New(c Config) (Client, error) {
config: c,
urls: urls,
client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: c.TLSConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(c.TLSConfig),
},
}, nil
}
Expand Down Expand Up @@ -92,9 +91,7 @@ func (c *httpClient) Update(new Config) error {

if old.TLSConfig != new.TLSConfig {
c.client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: new.TLSConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(new.TLSConfig),
}
}
return nil
Expand Down