From 0471e64ad11ad8fadf063960bbe93bc715539afe Mon Sep 17 00:00:00 2001 From: Shubheksha Jalan Date: Thu, 11 Jan 2018 20:40:25 +0530 Subject: [PATCH] Use shared types from the `common` repo (#3674) * refactor: use shared types from common repo, remove util/config * vendor: add common/config * fix nit --- cmd/promtool/main.go | 2 +- config/config.go | 2 +- config/config_test.go | 2 +- discovery/azure/azure.go | 2 +- discovery/consul/consul.go | 2 +- discovery/ec2/ec2.go | 2 +- discovery/kubernetes/kubernetes.go | 2 +- discovery/marathon/marathon.go | 2 +- discovery/openstack/openstack.go | 2 +- discovery/triton/triton.go | 2 +- discovery/triton/triton_test.go | 2 +- notifier/notifier_test.go | 2 +- retrieval/target_test.go | 2 +- storage/remote/client.go | 2 +- storage/remote/client_test.go | 2 +- util/config/config.go | 138 --------- util/httputil/client.go | 2 +- util/httputil/client_test.go | 2 +- .../prometheus/common/config/config.go | 47 +++ .../prometheus/common/config/http_config.go | 281 ++++++++++++++++++ vendor/vendor.json | 6 + 21 files changed, 351 insertions(+), 155 deletions(-) delete mode 100644 util/config/config.go create mode 100644 vendor/github.com/prometheus/common/config/config.go create mode 100644 vendor/github.com/prometheus/common/config/http_config.go diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 86fa690756..9e78c1085d 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -23,12 +23,12 @@ import ( "gopkg.in/alecthomas/kingpin.v2" "gopkg.in/yaml.v2" + config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/common/version" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/rulefmt" "github.com/prometheus/prometheus/promql" - config_util "github.com/prometheus/prometheus/util/config" "github.com/prometheus/prometheus/util/promlint" ) diff --git a/config/config.go b/config/config.go index cab3d5b74f..9c80d4f63a 100644 --- a/config/config.go +++ b/config/config.go @@ -22,9 +22,9 @@ import ( "strings" "time" + config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" sd_config "github.com/prometheus/prometheus/discovery/config" - config_util "github.com/prometheus/prometheus/util/config" yaml_util "github.com/prometheus/prometheus/util/yaml" "gopkg.in/yaml.v2" ) diff --git a/config/config_test.go b/config/config_test.go index 795e7cb457..88f3b4d3eb 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -34,9 +34,9 @@ import ( "github.com/prometheus/prometheus/discovery/triton" "github.com/prometheus/prometheus/discovery/zookeeper" + config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" sd_config "github.com/prometheus/prometheus/discovery/config" - config_util "github.com/prometheus/prometheus/util/config" "github.com/prometheus/prometheus/util/testutil" "gopkg.in/yaml.v2" ) diff --git a/discovery/azure/azure.go b/discovery/azure/azure.go index eaf5501e20..78fc6714db 100644 --- a/discovery/azure/azure.go +++ b/discovery/azure/azure.go @@ -27,10 +27,10 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/prometheus/client_golang/prometheus" + config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/discovery/targetgroup" - config_util "github.com/prometheus/prometheus/util/config" "github.com/prometheus/prometheus/util/strutil" yaml_util "github.com/prometheus/prometheus/util/yaml" ) diff --git a/discovery/consul/consul.go b/discovery/consul/consul.go index 02045faee8..af13efa550 100644 --- a/discovery/consul/consul.go +++ b/discovery/consul/consul.go @@ -27,9 +27,9 @@ import ( consul "github.com/hashicorp/consul/api" "github.com/mwitkow/go-conntrack" "github.com/prometheus/client_golang/prometheus" + config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/discovery/targetgroup" - config_util "github.com/prometheus/prometheus/util/config" "github.com/prometheus/prometheus/util/httputil" "github.com/prometheus/prometheus/util/strutil" yaml_util "github.com/prometheus/prometheus/util/yaml" diff --git a/discovery/ec2/ec2.go b/discovery/ec2/ec2.go index 6ac985ef40..51500e633b 100644 --- a/discovery/ec2/ec2.go +++ b/discovery/ec2/ec2.go @@ -32,8 +32,8 @@ import ( "github.com/prometheus/common/model" "github.com/aws/aws-sdk-go/service/ec2" + config_util "github.com/prometheus/common/config" "github.com/prometheus/prometheus/discovery/targetgroup" - config_util "github.com/prometheus/prometheus/util/config" "github.com/prometheus/prometheus/util/strutil" yaml_util "github.com/prometheus/prometheus/util/yaml" ) diff --git a/discovery/kubernetes/kubernetes.go b/discovery/kubernetes/kubernetes.go index 699ab17c97..6ccc40f748 100644 --- a/discovery/kubernetes/kubernetes.go +++ b/discovery/kubernetes/kubernetes.go @@ -23,9 +23,9 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/prometheus/client_golang/prometheus" + config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/discovery/targetgroup" - config_util "github.com/prometheus/prometheus/util/config" yaml_util "github.com/prometheus/prometheus/util/yaml" "k8s.io/client-go/kubernetes" diff --git a/discovery/marathon/marathon.go b/discovery/marathon/marathon.go index 8239212dac..492a1da026 100644 --- a/discovery/marathon/marathon.go +++ b/discovery/marathon/marathon.go @@ -29,9 +29,9 @@ import ( "github.com/go-kit/kit/log/level" conntrack "github.com/mwitkow/go-conntrack" "github.com/prometheus/client_golang/prometheus" + config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/discovery/targetgroup" - config_util "github.com/prometheus/prometheus/util/config" "github.com/prometheus/prometheus/util/httputil" "github.com/prometheus/prometheus/util/strutil" yaml_util "github.com/prometheus/prometheus/util/yaml" diff --git a/discovery/openstack/openstack.go b/discovery/openstack/openstack.go index 127154dab0..85a6aff100 100644 --- a/discovery/openstack/openstack.go +++ b/discovery/openstack/openstack.go @@ -23,9 +23,9 @@ import ( "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack" "github.com/prometheus/client_golang/prometheus" + config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/discovery/targetgroup" - config_util "github.com/prometheus/prometheus/util/config" yaml_util "github.com/prometheus/prometheus/util/yaml" ) diff --git a/discovery/triton/triton.go b/discovery/triton/triton.go index 90622afcee..47aed98c00 100644 --- a/discovery/triton/triton.go +++ b/discovery/triton/triton.go @@ -27,8 +27,8 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" + config_util "github.com/prometheus/common/config" "github.com/prometheus/prometheus/discovery/targetgroup" - config_util "github.com/prometheus/prometheus/util/config" "github.com/prometheus/prometheus/util/httputil" yaml_util "github.com/prometheus/prometheus/util/yaml" ) diff --git a/discovery/triton/triton_test.go b/discovery/triton/triton_test.go index faccfe5e32..a108ed04b7 100644 --- a/discovery/triton/triton_test.go +++ b/discovery/triton/triton_test.go @@ -26,9 +26,9 @@ import ( "github.com/stretchr/testify/assert" + "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/discovery/targetgroup" - "github.com/prometheus/prometheus/util/config" ) var ( diff --git a/notifier/notifier_test.go b/notifier/notifier_test.go index bff958f3b9..7ffc8d55eb 100644 --- a/notifier/notifier_test.go +++ b/notifier/notifier_test.go @@ -27,11 +27,11 @@ import ( old_ctx "golang.org/x/net/context" + config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/pkg/labels" - config_util "github.com/prometheus/prometheus/util/config" "github.com/prometheus/prometheus/util/httputil" ) diff --git a/retrieval/target_test.go b/retrieval/target_test.go index eebacfb1cc..a21f3fcbfa 100644 --- a/retrieval/target_test.go +++ b/retrieval/target_test.go @@ -28,8 +28,8 @@ import ( "github.com/prometheus/common/model" + config_util "github.com/prometheus/common/config" "github.com/prometheus/prometheus/pkg/labels" - config_util "github.com/prometheus/prometheus/util/config" "github.com/prometheus/prometheus/util/httputil" ) diff --git a/storage/remote/client.go b/storage/remote/client.go index ed5591ffee..d3e5d2a056 100644 --- a/storage/remote/client.go +++ b/storage/remote/client.go @@ -28,8 +28,8 @@ import ( "github.com/prometheus/common/model" "golang.org/x/net/context/ctxhttp" + config_util "github.com/prometheus/common/config" "github.com/prometheus/prometheus/prompb" - config_util "github.com/prometheus/prometheus/util/config" "github.com/prometheus/prometheus/util/httputil" ) diff --git a/storage/remote/client_test.go b/storage/remote/client_test.go index f045489969..b0b93aad84 100644 --- a/storage/remote/client_test.go +++ b/storage/remote/client_test.go @@ -23,9 +23,9 @@ import ( "testing" "time" + config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/prompb" - config_util "github.com/prometheus/prometheus/util/config" ) var longErrMessage = strings.Repeat("error message", maxErrMsgLen) diff --git a/util/config/config.go b/util/config/config.go deleted file mode 100644 index df81dd4113..0000000000 --- a/util/config/config.go +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright 2013 The Prometheus Authors -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package config - -import ( - "fmt" - "net/url" - - yaml_util "github.com/prometheus/prometheus/util/yaml" -) - -// Secret special type for storing secrets. -type Secret string - -// UnmarshalYAML implements the yaml.Unmarshaler interface for Secrets. -func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error { - type plain Secret - return unmarshal((*plain)(s)) -} - -// MarshalYAML implements the yaml.Marshaler interface for Secrets. -func (s Secret) MarshalYAML() (interface{}, error) { - if s != "" { - return "", nil - } - return nil, nil -} - -// TLSConfig configures the options for TLS connections. -type TLSConfig struct { - // The CA cert to use for the targets. - CAFile string `yaml:"ca_file,omitempty"` - // The client cert file for the targets. - CertFile string `yaml:"cert_file,omitempty"` - // The client key file for the targets. - KeyFile string `yaml:"key_file,omitempty"` - // Used to verify the hostname for the targets. - ServerName string `yaml:"server_name,omitempty"` - // Disable target certificate validation. - InsecureSkipVerify bool `yaml:"insecure_skip_verify"` - - // Catches all undefined fields and must be empty after parsing. - XXX map[string]interface{} `yaml:",inline"` -} - -// UnmarshalYAML implements the yaml.Unmarshaler interface. -func (c *TLSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { - type plain TLSConfig - if err := unmarshal((*plain)(c)); err != nil { - return err - } - return yaml_util.CheckOverflow(c.XXX, "TLS config") -} - -// BasicAuth contains basic HTTP authentication credentials. -type BasicAuth struct { - Username string `yaml:"username"` - Password Secret `yaml:"password"` - - // Catches all undefined fields and must be empty after parsing. - XXX map[string]interface{} `yaml:",inline"` -} - -// UnmarshalYAML implements the yaml.Unmarshaler interface. -func (a *BasicAuth) UnmarshalYAML(unmarshal func(interface{}) error) error { - type plain BasicAuth - err := unmarshal((*plain)(a)) - if err != nil { - return err - } - return yaml_util.CheckOverflow(a.XXX, "basic_auth") -} - -// URL is a custom URL type that allows validation at configuration load time. -type URL struct { - *url.URL -} - -// UnmarshalYAML implements the yaml.Unmarshaler interface for URLs. -func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error { - var s string - if err := unmarshal(&s); err != nil { - return err - } - - urlp, err := url.Parse(s) - if err != nil { - return err - } - u.URL = urlp - return nil -} - -// MarshalYAML implements the yaml.Marshaler interface for URLs. -func (u URL) MarshalYAML() (interface{}, error) { - if u.URL != nil { - return u.String(), nil - } - return nil, nil -} - -// HTTPClientConfig configures an HTTP client. -type HTTPClientConfig struct { - // The HTTP basic authentication credentials for the targets. - BasicAuth *BasicAuth `yaml:"basic_auth,omitempty"` - // The bearer token for the targets. - BearerToken Secret `yaml:"bearer_token,omitempty"` - // The bearer token file for the targets. - BearerTokenFile string `yaml:"bearer_token_file,omitempty"` - // HTTP proxy server to use to connect to the targets. - ProxyURL URL `yaml:"proxy_url,omitempty"` - // TLSConfig to use to connect to the targets. - TLSConfig TLSConfig `yaml:"tls_config,omitempty"` - - // Catches all undefined fields and must be empty after parsing. - XXX map[string]interface{} `yaml:",inline"` -} - -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") - } - if c.BasicAuth != nil && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) { - return fmt.Errorf("at most one of basic_auth, bearer_token & bearer_token_file must be configured") - } - return nil -} diff --git a/util/httputil/client.go b/util/httputil/client.go index 9cbddb8753..f9eed8f4d7 100644 --- a/util/httputil/client.go +++ b/util/httputil/client.go @@ -23,7 +23,7 @@ import ( "time" "github.com/mwitkow/go-conntrack" - config_util "github.com/prometheus/prometheus/util/config" + config_util "github.com/prometheus/common/config" ) // NewClient returns a http.Client using the specified http.RoundTripper. diff --git a/util/httputil/client_test.go b/util/httputil/client_test.go index a537b9da0e..49e54adc98 100644 --- a/util/httputil/client_test.go +++ b/util/httputil/client_test.go @@ -24,7 +24,7 @@ import ( "strings" "testing" - config_util "github.com/prometheus/prometheus/util/config" + config_util "github.com/prometheus/common/config" "github.com/prometheus/prometheus/util/testutil" ) diff --git a/vendor/github.com/prometheus/common/config/config.go b/vendor/github.com/prometheus/common/config/config.go new file mode 100644 index 0000000000..9195c34bfd --- /dev/null +++ b/vendor/github.com/prometheus/common/config/config.go @@ -0,0 +1,47 @@ +// Copyright 2016 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package config + +import ( + "fmt" + "strings" +) + +func checkOverflow(m map[string]interface{}, ctx string) error { + if len(m) > 0 { + var keys []string + for k := range m { + keys = append(keys, k) + } + return fmt.Errorf("unknown fields in %s: %s", ctx, strings.Join(keys, ", ")) + } + return nil +} + +// Secret special type for storing secrets. +type Secret string + +// MarshalYAML implements the yaml.Marshaler interface for Secrets. +func (s Secret) MarshalYAML() (interface{}, error) { + if s != "" { + return "", nil + } + return nil, nil +} + +//UnmarshalYAML implements the yaml.Unmarshaler interface for Secrets. +func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error { + type plain Secret + return unmarshal((*plain)(s)) +} diff --git a/vendor/github.com/prometheus/common/config/http_config.go b/vendor/github.com/prometheus/common/config/http_config.go new file mode 100644 index 0000000000..ea231bf8d2 --- /dev/null +++ b/vendor/github.com/prometheus/common/config/http_config.go @@ -0,0 +1,281 @@ +// Copyright 2016 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package config + +import ( + "crypto/tls" + "crypto/x509" + "fmt" + "io/ioutil" + "net/http" + "net/url" + "strings" + + "gopkg.in/yaml.v2" +) + +// BasicAuth contains basic HTTP authentication credentials. +type BasicAuth struct { + Username string `yaml:"username"` + Password Secret `yaml:"password"` + + // Catches all undefined fields and must be empty after parsing. + XXX map[string]interface{} `yaml:",inline"` +} + +// URL is a custom URL type that allows validation at configuration load time. +type URL struct { + *url.URL +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface for URLs. +func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error { + var s string + if err := unmarshal(&s); err != nil { + return err + } + + urlp, err := url.Parse(s) + if err != nil { + return err + } + u.URL = urlp + return nil +} + +// MarshalYAML implements the yaml.Marshaler interface for URLs. +func (u URL) MarshalYAML() (interface{}, error) { + if u.URL != nil { + return u.String(), nil + } + return nil, nil +} + +// HTTPClientConfig configures an HTTP client. +type HTTPClientConfig struct { + // The HTTP basic authentication credentials for the targets. + BasicAuth *BasicAuth `yaml:"basic_auth,omitempty"` + // The bearer token for the targets. + BearerToken Secret `yaml:"bearer_token,omitempty"` + // The bearer token file for the targets. + BearerTokenFile string `yaml:"bearer_token_file,omitempty"` + // HTTP proxy server to use to connect to the targets. + ProxyURL URL `yaml:"proxy_url,omitempty"` + // TLSConfig to use to connect to the targets. + TLSConfig TLSConfig `yaml:"tls_config,omitempty"` + + // Catches all undefined fields and must be empty after parsing. + XXX map[string]interface{} `yaml:",inline"` +} + +// 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") + } + if c.BasicAuth != nil && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) { + return fmt.Errorf("at most one of basic_auth, bearer_token & bearer_token_file must be configured") + } + return nil +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface +func (c *HTTPClientConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + type plain HTTPClientConfig + err := unmarshal((*plain)(c)) + if err != nil { + return err + } + err = c.Validate() + if err != nil { + return c.Validate() + } + return checkOverflow(c.XXX, "http_client_config") +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface. +func (a *BasicAuth) UnmarshalYAML(unmarshal func(interface{}) error) error { + type plain BasicAuth + err := unmarshal((*plain)(a)) + if err != nil { + return err + } + return checkOverflow(a.XXX, "basic_auth") +} + +// NewHTTPClientFromConfig returns a new HTTP client configured for the +// given config.HTTPClientConfig. +func NewHTTPClientFromConfig(cfg *HTTPClientConfig) (*http.Client, error) { + tlsConfig, err := NewTLSConfig(&cfg.TLSConfig) + if err != nil { + return nil, err + } + + // It's the caller's job to handle timeouts + var rt http.RoundTripper = &http.Transport{ + Proxy: http.ProxyURL(cfg.ProxyURL.URL), + DisableKeepAlives: true, + TLSClientConfig: tlsConfig, + } + + // If a bearer token is provided, create a round tripper that will set the + // Authorization header correctly on each request. + bearerToken := cfg.BearerToken + if len(bearerToken) == 0 && len(cfg.BearerTokenFile) > 0 { + b, err := ioutil.ReadFile(cfg.BearerTokenFile) + if err != nil { + return nil, fmt.Errorf("unable to read bearer token file %s: %s", cfg.BearerTokenFile, err) + } + bearerToken = Secret(strings.TrimSpace(string(b))) + } + + if len(bearerToken) > 0 { + rt = NewBearerAuthRoundTripper(bearerToken, rt) + } + + if cfg.BasicAuth != nil { + rt = NewBasicAuthRoundTripper(cfg.BasicAuth.Username, Secret(cfg.BasicAuth.Password), rt) + } + + // Return a new client with the configured round tripper. + return &http.Client{Transport: rt}, nil +} + +type bearerAuthRoundTripper struct { + bearerToken Secret + rt http.RoundTripper +} + +type basicAuthRoundTripper struct { + username string + password Secret + rt http.RoundTripper +} + +// NewBasicAuthRoundTripper will apply a BASIC auth authorization header to a request unless it has +// already been set. +func NewBasicAuthRoundTripper(username string, password Secret, rt http.RoundTripper) http.RoundTripper { + return &basicAuthRoundTripper{username, password, rt} +} + +func (rt *bearerAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + if len(req.Header.Get("Authorization")) == 0 { + req = cloneRequest(req) + req.Header.Set("Authorization", "Bearer "+string(rt.bearerToken)) + } + + return rt.rt.RoundTrip(req) +} + +// NewBearerAuthRoundTripper adds the provided bearer token to a request unless the authorization +// header has already been set. +func NewBearerAuthRoundTripper(bearer Secret, rt http.RoundTripper) http.RoundTripper { + return &bearerAuthRoundTripper{bearer, rt} +} + +func (rt *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + if len(req.Header.Get("Authorization")) != 0 { + return rt.RoundTrip(req) + } + req = cloneRequest(req) + req.SetBasicAuth(rt.username, string(rt.password)) + return rt.rt.RoundTrip(req) +} + +// cloneRequest returns a clone of the provided *http.Request. +// The clone is a shallow copy of the struct and its Header map. +func cloneRequest(r *http.Request) *http.Request { + // Shallow copy of the struct. + r2 := new(http.Request) + *r2 = *r + // Deep copy of the Header. + r2.Header = make(http.Header) + for k, s := range r.Header { + r2.Header[k] = s + } + return r2 +} + +// NewTLSConfig creates a new tls.Config from the given config.TLSConfig. +func NewTLSConfig(cfg *TLSConfig) (*tls.Config, error) { + tlsConfig := &tls.Config{InsecureSkipVerify: cfg.InsecureSkipVerify} + + // If a CA cert is provided then let's read it in so we can validate the + // scrape target's certificate properly. + if len(cfg.CAFile) > 0 { + caCertPool := x509.NewCertPool() + // Load CA cert. + caCert, err := ioutil.ReadFile(cfg.CAFile) + if err != nil { + return nil, fmt.Errorf("unable to use specified CA cert %s: %s", cfg.CAFile, err) + } + caCertPool.AppendCertsFromPEM(caCert) + tlsConfig.RootCAs = caCertPool + } + + if len(cfg.ServerName) > 0 { + tlsConfig.ServerName = cfg.ServerName + } + + // If a client cert & key is provided then configure TLS config accordingly. + if len(cfg.CertFile) > 0 && len(cfg.KeyFile) == 0 { + return nil, fmt.Errorf("client cert file %q specified without client key file", cfg.CertFile) + } else if len(cfg.KeyFile) > 0 && len(cfg.CertFile) == 0 { + return nil, fmt.Errorf("client key file %q specified without client cert file", cfg.KeyFile) + } else if len(cfg.CertFile) > 0 && len(cfg.KeyFile) > 0 { + cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile) + if err != nil { + return nil, fmt.Errorf("unable to use specified client cert (%s) & key (%s): %s", cfg.CertFile, cfg.KeyFile, err) + } + tlsConfig.Certificates = []tls.Certificate{cert} + } + tlsConfig.BuildNameToCertificate() + + return tlsConfig, nil +} + +// TLSConfig configures the options for TLS connections. +type TLSConfig struct { + // The CA cert to use for the targets. + CAFile string `yaml:"ca_file,omitempty"` + // The client cert file for the targets. + CertFile string `yaml:"cert_file,omitempty"` + // The client key file for the targets. + KeyFile string `yaml:"key_file,omitempty"` + // Used to verify the hostname for the targets. + ServerName string `yaml:"server_name,omitempty"` + // Disable target certificate validation. + InsecureSkipVerify bool `yaml:"insecure_skip_verify"` + + // Catches all undefined fields and must be empty after parsing. + XXX map[string]interface{} `yaml:",inline"` +} + +// UnmarshalYAML implements the yaml.Unmarshaler interface. +func (c *TLSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + type plain TLSConfig + if err := unmarshal((*plain)(c)); err != nil { + return err + } + return checkOverflow(c.XXX, "TLS config") +} + +func (c HTTPClientConfig) String() string { + b, err := yaml.Marshal(c) + if err != nil { + return fmt.Sprintf("", err) + } + return string(b) +} diff --git a/vendor/vendor.json b/vendor/vendor.json index 055025f6f3..2ac589b30c 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -745,6 +745,12 @@ "revision": "fa8ad6fec33561be4280a8f0514318c79d7f6cb6", "revisionTime": "2015-02-12T10:17:44Z" }, + { + "checksumSHA1": "i+0TxE6bOpJdPNOeNHpO0vMzFh4=", + "path": "github.com/prometheus/common/config", + "revision": "89604d197083d4781071d3c65855d24ecfb0a563", + "revisionTime": "2018-01-10T21:49:58Z" + }, { "checksumSHA1": "xfnn0THnqNwjwimeTClsxahYrIo=", "path": "github.com/prometheus/common/expfmt",