Allow ssl option specification for httpjson plugin

closes #769
This commit is contained in:
Cameron Sparr 2016-03-01 17:13:26 +00:00 committed by Michele Fadda
parent 40d5e3c6bd
commit f6e98c9d88
3 changed files with 54 additions and 6 deletions

View File

@ -6,6 +6,7 @@
- [#692](https://github.com/influxdata/telegraf/pull/770): Support InfluxDB retention policies - [#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! - [#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! - [#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 ### Bugfixes
- [#748](https://github.com/influxdata/telegraf/issues/748): Fix sensor plugin split on ":" - [#748](https://github.com/influxdata/telegraf/issues/748): Fix sensor plugin split on ":"

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/plugins/parsers"
) )
@ -23,6 +24,15 @@ type HttpJson struct {
Parameters map[string]string Parameters map[string]string
Headers 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 client HTTPClient
} }
@ -36,6 +46,9 @@ type HTTPClient interface {
// http.Response: HTTP respons object // http.Response: HTTP respons object
// error : Any error that may have occurred // error : Any error that may have occurred
MakeRequest(req *http.Request) (*http.Response, error) MakeRequest(req *http.Request) (*http.Response, error)
SetHTTPClient(client *http.Client)
HTTPClient() *http.Client
} }
type RealHTTPClient struct { type RealHTTPClient struct {
@ -46,6 +59,14 @@ func (c RealHTTPClient) MakeRequest(req *http.Request) (*http.Response, error) {
return c.client.Do(req) 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 = ` var sampleConfig = `
## NOTE This plugin only reads numerical measurements, strings and booleans ## NOTE This plugin only reads numerical measurements, strings and booleans
## will be ignored. ## will be ignored.
@ -77,6 +98,13 @@ var sampleConfig = `
# [inputs.httpjson.headers] # [inputs.httpjson.headers]
# X-Auth-Token = "my-xauth-token" # X-Auth-Token = "my-xauth-token"
# apiVersion = "v1" # 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 { func (h *HttpJson) SampleConfig() string {
@ -91,6 +119,23 @@ func (h *HttpJson) Description() string {
func (h *HttpJson) Gather(acc telegraf.Accumulator) error { func (h *HttpJson) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup 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)) errorChannel := make(chan error, len(h.Servers))
for _, server := range h.Servers { for _, server := range h.Servers {
@ -244,11 +289,6 @@ func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) {
func init() { func init() {
inputs.Add("httpjson", func() telegraf.Input { inputs.Add("httpjson", func() telegraf.Input {
tr := &http.Transport{ResponseHeaderTimeout: time.Duration(3 * time.Second)} return &HttpJson{client: RealHTTPClient{}}
client := &http.Client{
Transport: tr,
Timeout: time.Duration(4 * time.Second),
}
return &HttpJson{client: RealHTTPClient{client: client}}
}) })
} }

View File

@ -147,6 +147,13 @@ func (c mockHTTPClient) MakeRequest(req *http.Request) (*http.Response, error) {
return &resp, nil 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. // Generates a pointer to an HttpJson object that uses a mock HTTP client.
// Parameters: // Parameters:
// response : Body of the response that the mock HTTP client should return // response : Body of the response that the mock HTTP client should return