Fix missing validation of HTTP client configuration (#284)
authorpasquier-s <spasquie@redhat.com>
Tue, 16 Jan 2018 12:55:51 +0000 (13:55 +0100)
committerBrian Brazil <brian.brazil@robustperception.io>
Tue, 16 Jan 2018 12:55:51 +0000 (12:55 +0000)
config/config.go
config/config_test.go
config/testdata/blackbox-bad2.yml [new file with mode: 0644]
vendor/github.com/prometheus/common/config/http_config.go
vendor/vendor.json

index 299889ecd77a19606c4081f9dffa8e3a44294c85..21a1d65644aaa3c41f85491d4a9c971807e259e8 100644 (file)
@@ -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
        }
index 4042994f26082c466ffa3d57c97cfc2f667f92d1..775a535fbabfe0febef04c7983202cd2472f0a23 100644 (file)
@@ -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 (file)
index 0000000..90c54b2
--- /dev/null
@@ -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"
index ff5837fa5e7f45724c322c5403b489898e76f326..ea231bf8d2c7e9e6a06191a3203162f865c66709 100644 (file)
@@ -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")
 }
index c628563be3b849f02feadd3465edfda5adc1273b..b815568845c5d7d67a285117a31754ab78cc43bf 100644 (file)
                        "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=",