Skip to content

Commit

Permalink
Allow ssl option specification for httpjson plugin
Browse files Browse the repository at this point in the history
closes #769
  • Loading branch information
sparrc committed Mar 1, 2016
1 parent 74aaf4f commit b2a4d4a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ":"
Expand Down
52 changes: 46 additions & 6 deletions plugins/inputs/httpjson/httpjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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{}}
})
}
7 changes: 7 additions & 0 deletions plugins/inputs/httpjson/httpjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b2a4d4a

Please sign in to comment.