2016-12-07 04:10:42 -06:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2017-09-28 08:55:32 -05:00
|
|
|
"errors"
|
2016-12-07 04:10:42 -06:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2019-02-11 06:42:05 -06:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-12-07 04:10:42 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type proxyTransportCache struct {
|
|
|
|
cache map[int64]cachedTransport
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type cachedTransport struct {
|
|
|
|
updated time.Time
|
|
|
|
|
|
|
|
*http.Transport
|
|
|
|
}
|
|
|
|
|
|
|
|
var ptc = proxyTransportCache{
|
|
|
|
cache: make(map[int64]cachedTransport),
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *DataSource) GetHttpClient() (*http.Client, error) {
|
|
|
|
transport, err := ds.GetHttpTransport()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Client{
|
2018-04-16 13:04:58 -05:00
|
|
|
Timeout: 30 * time.Second,
|
2016-12-07 04:10:42 -06:00
|
|
|
Transport: transport,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *DataSource) GetHttpTransport() (*http.Transport, error) {
|
|
|
|
ptc.Lock()
|
|
|
|
defer ptc.Unlock()
|
|
|
|
|
|
|
|
if t, present := ptc.cache[ds.Id]; present && ds.Updated.Equal(t.updated) {
|
|
|
|
return t.Transport, nil
|
|
|
|
}
|
|
|
|
|
2019-01-28 12:38:56 -06:00
|
|
|
tlsConfig, err := ds.GetTLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-09-28 08:10:14 -05:00
|
|
|
}
|
|
|
|
|
2019-01-28 12:38:56 -06:00
|
|
|
tlsConfig.Renegotiation = tls.RenegotiateFreelyAsClient
|
|
|
|
|
2016-12-07 04:10:42 -06:00
|
|
|
transport := &http.Transport{
|
2019-01-28 12:38:56 -06:00
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2016-12-07 04:10:42 -06:00
|
|
|
Dial: (&net.Dialer{
|
2019-02-11 06:42:05 -06:00
|
|
|
Timeout: time.Duration(setting.DataProxyTimeout) * time.Second,
|
2016-12-07 04:10:42 -06:00
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
}
|
|
|
|
|
2019-01-28 12:38:56 -06:00
|
|
|
ptc.cache[ds.Id] = cachedTransport{
|
|
|
|
Transport: transport,
|
|
|
|
updated: ds.Updated,
|
|
|
|
}
|
|
|
|
|
|
|
|
return transport, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *DataSource) GetTLSConfig() (*tls.Config, error) {
|
|
|
|
var tlsSkipVerify, tlsClientAuth, tlsAuthWithCACert bool
|
|
|
|
if ds.JsonData != nil {
|
|
|
|
tlsClientAuth = ds.JsonData.Get("tlsAuth").MustBool(false)
|
|
|
|
tlsAuthWithCACert = ds.JsonData.Get("tlsAuthWithCACert").MustBool(false)
|
|
|
|
tlsSkipVerify = ds.JsonData.Get("tlsSkipVerify").MustBool(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: tlsSkipVerify,
|
|
|
|
}
|
|
|
|
|
2017-09-28 05:04:01 -05:00
|
|
|
if tlsClientAuth || tlsAuthWithCACert {
|
2016-12-07 04:10:42 -06:00
|
|
|
decrypted := ds.SecureJsonData.Decrypt()
|
|
|
|
if tlsAuthWithCACert && len(decrypted["tlsCACert"]) > 0 {
|
|
|
|
caPool := x509.NewCertPool()
|
|
|
|
ok := caPool.AppendCertsFromPEM([]byte(decrypted["tlsCACert"]))
|
2017-09-28 08:55:32 -05:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Failed to parse TLS CA PEM certificate")
|
2016-12-07 04:10:42 -06:00
|
|
|
}
|
2019-01-28 12:38:56 -06:00
|
|
|
tlsConfig.RootCAs = caPool
|
2016-12-07 04:10:42 -06:00
|
|
|
}
|
|
|
|
|
2017-09-28 05:04:01 -05:00
|
|
|
if tlsClientAuth {
|
|
|
|
cert, err := tls.X509KeyPair([]byte(decrypted["tlsClientCert"]), []byte(decrypted["tlsClientKey"]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-28 12:38:56 -06:00
|
|
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
2016-12-07 04:10:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 12:38:56 -06:00
|
|
|
return tlsConfig, nil
|
2016-12-07 04:10:42 -06:00
|
|
|
}
|