diff --git a/CHANGELOG.md b/CHANGELOG.md index c390add59..d2181787a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - [#692](https://github.com/influxdata/telegraf/pull/770): Support InfluxDB retention policies - [#771](https://github.com/influxdata/telegraf/pull/771): Default timeouts for input plugns. Thanks @PierreF! - [#758](https://github.com/influxdata/telegraf/pull/758): UDP Listener input plugin, thanks @whatyouhide! +- [#769](https://github.com/influxdata/telegraf/issues/769): httpjson plugin: allow specifying SSL configuration. ### Bugfixes - [#748](https://github.com/influxdata/telegraf/issues/748): Fix sensor plugin split on ":" diff --git a/plugins/inputs/httpjson/httpjson.go b/plugins/inputs/httpjson/httpjson.go index c07a9602a..061995892 100644 --- a/plugins/inputs/httpjson/httpjson.go +++ b/plugins/inputs/httpjson/httpjson.go @@ -11,6 +11,7 @@ import ( "time" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/parsers" ) @@ -23,6 +24,15 @@ type HttpJson struct { Parameters map[string]string Headers map[string]string + // Path to CA file + SSLCA string `toml:"ssl_ca"` + // Path to host cert file + SSLCert string `toml:"ssl_cert"` + // Path to cert key file + SSLKey string `toml:"ssl_key"` + // Use SSL but skip chain & host verification + InsecureSkipVerify bool + client HTTPClient } @@ -36,6 +46,9 @@ type HTTPClient interface { // http.Response: HTTP respons object // error : Any error that may have occurred MakeRequest(req *http.Request) (*http.Response, error) + + SetHTTPClient(client *http.Client) + HTTPClient() *http.Client } type RealHTTPClient struct { @@ -46,6 +59,14 @@ func (c RealHTTPClient) MakeRequest(req *http.Request) (*http.Response, error) { return c.client.Do(req) } +func (c RealHTTPClient) SetHTTPClient(client *http.Client) { + c.client = client +} + +func (c RealHTTPClient) HTTPClient() *http.Client { + return c.client +} + var sampleConfig = ` ## NOTE This plugin only reads numerical measurements, strings and booleans ## will be ignored. @@ -77,6 +98,13 @@ var sampleConfig = ` # [inputs.httpjson.headers] # X-Auth-Token = "my-xauth-token" # apiVersion = "v1" + + ## Optional SSL Config + # ssl_ca = "/etc/telegraf/ca.pem" + # ssl_cert = "/etc/telegraf/cert.pem" + # ssl_key = "/etc/telegraf/key.pem" + ## Use SSL but skip chain & host verification + # insecure_skip_verify = false ` func (h *HttpJson) SampleConfig() string { @@ -91,6 +119,23 @@ func (h *HttpJson) Description() string { func (h *HttpJson) Gather(acc telegraf.Accumulator) error { var wg sync.WaitGroup + if h.client.HTTPClient() == nil { + tlsCfg, err := internal.GetTLSConfig( + h.SSLCert, h.SSLKey, h.SSLCA, h.InsecureSkipVerify) + if err != nil { + return err + } + tr := &http.Transport{ + ResponseHeaderTimeout: time.Duration(3 * time.Second), + TLSClientConfig: tlsCfg, + } + client := &http.Client{ + Transport: tr, + Timeout: time.Duration(4 * time.Second), + } + h.client.SetHTTPClient(client) + } + errorChannel := make(chan error, len(h.Servers)) for _, server := range h.Servers { @@ -244,11 +289,6 @@ func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) { func init() { inputs.Add("httpjson", func() telegraf.Input { - tr := &http.Transport{ResponseHeaderTimeout: time.Duration(3 * time.Second)} - client := &http.Client{ - Transport: tr, - Timeout: time.Duration(4 * time.Second), - } - return &HttpJson{client: RealHTTPClient{client: client}} + return &HttpJson{client: RealHTTPClient{}} }) } diff --git a/plugins/inputs/httpjson/httpjson_test.go b/plugins/inputs/httpjson/httpjson_test.go index b6b57a167..1a1187d44 100644 --- a/plugins/inputs/httpjson/httpjson_test.go +++ b/plugins/inputs/httpjson/httpjson_test.go @@ -147,6 +147,13 @@ func (c mockHTTPClient) MakeRequest(req *http.Request) (*http.Response, error) { return &resp, nil } +func (c mockHTTPClient) SetHTTPClient(_ *http.Client) { +} + +func (c mockHTTPClient) HTTPClient() *http.Client { + return nil +} + // Generates a pointer to an HttpJson object that uses a mock HTTP client. // Parameters: // response : Body of the response that the mock HTTP client should return