2015-07-22 08:48:22 -07:00
|
|
|
// 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 httputil
|
|
|
|
|
|
|
|
import (
|
2015-09-06 16:07:44 -07:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-07-22 08:48:22 -07:00
|
|
|
"net/http"
|
2017-03-20 06:17:04 -07:00
|
|
|
"strings"
|
2015-07-22 08:48:22 -07:00
|
|
|
"time"
|
2016-09-19 13:47:51 -07:00
|
|
|
|
2017-10-06 03:22:19 -07:00
|
|
|
"github.com/mwitkow/go-conntrack"
|
2018-01-11 07:10:25 -08:00
|
|
|
config_util "github.com/prometheus/common/config"
|
2015-07-22 08:48:22 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewClient returns a http.Client using the specified http.RoundTripper.
|
2017-10-06 03:22:19 -07:00
|
|
|
func newClient(rt http.RoundTripper) *http.Client {
|
2015-07-22 08:48:22 -07:00
|
|
|
return &http.Client{Transport: rt}
|
|
|
|
}
|
|
|
|
|
2017-03-20 06:17:04 -07:00
|
|
|
// NewClientFromConfig returns a new HTTP client configured for the
|
2017-10-06 03:22:19 -07:00
|
|
|
// given config.HTTPClientConfig. The name is used as go-conntrack metric label.
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
func NewClientFromConfig(cfg config_util.HTTPClientConfig, name string) (*http.Client, error) {
|
2018-04-05 01:08:18 -07:00
|
|
|
rt, err := NewRoundTripperFromConfig(cfg, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newClient(rt), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRoundTripperFromConfig returns a new HTTP RoundTripper configured for the
|
|
|
|
// given config.HTTPClientConfig. The name is used as go-conntrack metric label.
|
|
|
|
func NewRoundTripperFromConfig(cfg config_util.HTTPClientConfig, name string) (http.RoundTripper, error) {
|
2017-03-20 06:17:04 -07:00
|
|
|
tlsConfig, err := NewTLSConfig(cfg.TLSConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// The only timeout we care about is the configured scrape timeout.
|
|
|
|
// It is applied on request. So we leave out any timings here.
|
|
|
|
var rt http.RoundTripper = &http.Transport{
|
2017-12-17 07:15:04 -08:00
|
|
|
Proxy: http.ProxyURL(cfg.ProxyURL.URL),
|
|
|
|
MaxIdleConns: 20000,
|
|
|
|
MaxIdleConnsPerHost: 1000, // see https://github.com/golang/go/issues/13801
|
|
|
|
DisableKeepAlives: false,
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
DisableCompression: true,
|
2017-08-09 07:30:49 -07:00
|
|
|
// 5 minutes is typically above the maximum sane scrape interval. So we can
|
|
|
|
// use keepalive for all configurations.
|
|
|
|
IdleConnTimeout: 5 * time.Minute,
|
2017-10-06 03:22:19 -07:00
|
|
|
DialContext: conntrack.NewDialContextFunc(
|
|
|
|
conntrack.DialWithTracing(),
|
|
|
|
conntrack.DialWithName(name),
|
|
|
|
),
|
2017-03-20 06:17:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a bearer token is provided, create a round tripper that will set the
|
|
|
|
// Authorization header correctly on each request.
|
2018-01-19 03:36:21 -08:00
|
|
|
if len(cfg.BearerToken) > 0 {
|
|
|
|
rt = NewBearerAuthRoundTripper(string(cfg.BearerToken), rt)
|
|
|
|
} else if len(cfg.BearerTokenFile) > 0 {
|
|
|
|
rt = NewBearerAuthFileRoundTripper(cfg.BearerTokenFile, rt)
|
2017-03-20 06:17:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.BasicAuth != nil {
|
2017-05-29 04:46:23 -07:00
|
|
|
rt = NewBasicAuthRoundTripper(cfg.BasicAuth.Username, string(cfg.BasicAuth.Password), rt)
|
2017-03-20 06:17:04 -07:00
|
|
|
}
|
|
|
|
|
2018-04-05 01:08:18 -07:00
|
|
|
// Return a new configured RoundTripper.
|
|
|
|
return rt, nil
|
2015-07-22 08:48:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type bearerAuthRoundTripper struct {
|
|
|
|
bearerToken string
|
|
|
|
rt http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBearerAuthRoundTripper adds the provided bearer token to a request unless the authorization
|
|
|
|
// header has already been set.
|
|
|
|
func NewBearerAuthRoundTripper(bearer string, rt http.RoundTripper) http.RoundTripper {
|
|
|
|
return &bearerAuthRoundTripper{bearer, 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 "+rt.bearerToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rt.rt.RoundTrip(req)
|
|
|
|
}
|
|
|
|
|
2018-01-19 03:36:21 -08:00
|
|
|
type bearerAuthFileRoundTripper struct {
|
|
|
|
bearerFile string
|
|
|
|
rt http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBearerAuthFileRoundTripper adds the bearer token read from the provided file to a request unless
|
|
|
|
// the authorization header has already been set. This file is read for every request.
|
|
|
|
func NewBearerAuthFileRoundTripper(bearerFile string, rt http.RoundTripper) http.RoundTripper {
|
|
|
|
return &bearerAuthFileRoundTripper{bearerFile, rt}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *bearerAuthFileRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
if len(req.Header.Get("Authorization")) == 0 {
|
|
|
|
b, err := ioutil.ReadFile(rt.bearerFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read bearer token file %s: %s", rt.bearerFile, err)
|
|
|
|
}
|
|
|
|
bearerToken := strings.TrimSpace(string(b))
|
|
|
|
|
|
|
|
req = cloneRequest(req)
|
|
|
|
req.Header.Set("Authorization", "Bearer "+bearerToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rt.rt.RoundTrip(req)
|
|
|
|
}
|
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
type basicAuthRoundTripper struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
rt http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBasicAuthRoundTripper will apply a BASIC auth authorization header to a request unless it has
|
|
|
|
// already been set.
|
|
|
|
func NewBasicAuthRoundTripper(username, password string, rt http.RoundTripper) http.RoundTripper {
|
|
|
|
return &basicAuthRoundTripper{username, password, rt}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
if len(req.Header.Get("Authorization")) != 0 {
|
|
|
|
return rt.rt.RoundTrip(req)
|
|
|
|
}
|
|
|
|
req = cloneRequest(req)
|
|
|
|
req.SetBasicAuth(rt.username, rt.password)
|
|
|
|
return rt.rt.RoundTrip(req)
|
|
|
|
}
|
|
|
|
|
2015-07-22 08:48:22 -07:00
|
|
|
// 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
|
|
|
|
}
|
2015-09-06 16:07:44 -07:00
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
// NewTLSConfig creates a new tls.Config from the given config_util.TLSConfig.
|
|
|
|
func NewTLSConfig(cfg config_util.TLSConfig) (*tls.Config, error) {
|
2016-09-19 13:47:51 -07:00
|
|
|
tlsConfig := &tls.Config{InsecureSkipVerify: cfg.InsecureSkipVerify}
|
2015-09-06 16:07:44 -07:00
|
|
|
|
|
|
|
// If a CA cert is provided then let's read it in so we can validate the
|
|
|
|
// scrape target's certificate properly.
|
2016-09-19 13:47:51 -07:00
|
|
|
if len(cfg.CAFile) > 0 {
|
2015-09-06 16:07:44 -07:00
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
// Load CA cert.
|
2016-09-19 13:47:51 -07:00
|
|
|
caCert, err := ioutil.ReadFile(cfg.CAFile)
|
2015-09-06 16:07:44 -07:00
|
|
|
if err != nil {
|
2016-09-19 13:47:51 -07:00
|
|
|
return nil, fmt.Errorf("unable to use specified CA cert %s: %s", cfg.CAFile, err)
|
2015-09-06 16:07:44 -07:00
|
|
|
}
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
tlsConfig.RootCAs = caCertPool
|
|
|
|
}
|
|
|
|
|
2016-09-19 13:47:51 -07:00
|
|
|
if len(cfg.ServerName) > 0 {
|
|
|
|
tlsConfig.ServerName = cfg.ServerName
|
2016-05-26 14:24:49 -07:00
|
|
|
}
|
2015-09-06 16:07:44 -07:00
|
|
|
// If a client cert & key is provided then configure TLS config accordingly.
|
2016-09-19 13:47:51 -07:00
|
|
|
if len(cfg.CertFile) > 0 && len(cfg.KeyFile) > 0 {
|
|
|
|
cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile)
|
2015-09-06 16:07:44 -07:00
|
|
|
if err != nil {
|
2016-09-19 13:47:51 -07:00
|
|
|
return nil, fmt.Errorf("unable to use specified client cert (%s) & key (%s): %s", cfg.CertFile, cfg.KeyFile, err)
|
2015-09-06 16:07:44 -07:00
|
|
|
}
|
|
|
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
|
|
|
}
|
|
|
|
tlsConfig.BuildNameToCertificate()
|
|
|
|
|
|
|
|
return tlsConfig, nil
|
|
|
|
}
|