From: pasquier-s Date: Tue, 16 Jan 2018 12:55:51 +0000 (+0100) Subject: Fix missing validation of HTTP client configuration (#284) X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d4a66090c1e6b962cc44d504661811d206e6eb42;p=blackbox_exporter.git Fix missing validation of HTTP client configuration (#284) --- diff --git a/config/config.go b/config/config.go index 299889e..21a1d65 100644 --- a/config/config.go +++ b/config/config.go @@ -166,6 +166,9 @@ func (s *HTTPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error { if err := unmarshal((*plain)(s)); err != nil { return err } + if err := s.HTTPClientConfig.Validate(); err != nil { + return err + } if err := checkOverflow(s.XXX, "http probe"); err != nil { return err } diff --git a/config/config_test.go b/config/config_test.go index 4042994..775a535 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -30,6 +30,10 @@ func TestLoadBadConfigs(t *testing.T) { ConfigFile: "testdata/blackbox-bad.yml", ExpectedError: "Error parsing config file: unknown fields in dns probe: invalid_extra_field", }, + { + ConfigFile: "testdata/blackbox-bad2.yml", + ExpectedError: "Error parsing config file: at most one of bearer_token & bearer_token_file must be configured", + }, { ConfigFile: "testdata/invalid-dns-module.yml", ExpectedError: "Error parsing config file: Query name must be set for DNS module", @@ -37,6 +41,10 @@ func TestLoadBadConfigs(t *testing.T) { } for i, test := range tests { err := sc.ReloadConfig(test.ConfigFile) + if err == nil { + t.Errorf("In case %v:\nExpected:\n%v\nGot:\nnil", i, test.ExpectedError) + continue + } if err.Error() != test.ExpectedError { t.Errorf("In case %v:\nExpected:\n%v\nGot:\n%v", i, test.ExpectedError, err.Error()) } diff --git a/config/testdata/blackbox-bad2.yml b/config/testdata/blackbox-bad2.yml new file mode 100644 index 0000000..90c54b2 --- /dev/null +++ b/config/testdata/blackbox-bad2.yml @@ -0,0 +1,11 @@ +modules: + http_post_2xx: + prober: http + timeout: 5s + http: + method: POST + bearer_token: foo + bearer_token_file: foo + basic_auth: + username: "username" + password: "mysecret" diff --git a/vendor/github.com/prometheus/common/config/http_config.go b/vendor/github.com/prometheus/common/config/http_config.go index ff5837f..ea231bf 100644 --- a/vendor/github.com/prometheus/common/config/http_config.go +++ b/vendor/github.com/prometheus/common/config/http_config.go @@ -22,7 +22,7 @@ import ( "net/url" "strings" - yaml "gopkg.in/yaml.v2" + "gopkg.in/yaml.v2" ) // BasicAuth contains basic HTTP authentication credentials. @@ -79,7 +79,9 @@ type HTTPClientConfig struct { XXX map[string]interface{} `yaml:",inline"` } -func (c *HTTPClientConfig) validate() error { +// Validate validates the HTTPClientConfig to check only one of BearerToken, +// BasicAuth and BearerTokenFile is configured. +func (c *HTTPClientConfig) Validate() error { if len(c.BearerToken) > 0 && len(c.BearerTokenFile) > 0 { return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured") } @@ -96,9 +98,9 @@ func (c *HTTPClientConfig) UnmarshalYAML(unmarshal func(interface{}) error) erro if err != nil { return err } - err = c.validate() + err = c.Validate() if err != nil { - return c.validate() + return c.Validate() } return checkOverflow(c.XXX, "http_client_config") } diff --git a/vendor/vendor.json b/vendor/vendor.json index c628563..b815568 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -99,10 +99,10 @@ "revisionTime": "2017-02-16T18:52:47Z" }, { - "checksumSHA1": "4TLgSCgJZuS5gtytxNvcVk4h8/g=", + "checksumSHA1": "i+0TxE6bOpJdPNOeNHpO0vMzFh4=", "path": "github.com/prometheus/common/config", - "revision": "2f17f4a9d485bf34b4bfaccc273805040e4f86c8", - "revisionTime": "2017-09-08T16:18:22Z" + "revision": "89604d197083d4781071d3c65855d24ecfb0a563", + "revisionTime": "2018-01-10T21:49:58Z" }, { "checksumSHA1": "xfnn0THnqNwjwimeTClsxahYrIo=",