datasource_cache.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package models
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "errors"
  6. "net"
  7. "net/http"
  8. "sync"
  9. "time"
  10. "github.com/grafana/grafana/pkg/setting"
  11. )
  12. type proxyTransportCache struct {
  13. cache map[int64]cachedTransport
  14. sync.Mutex
  15. }
  16. type cachedTransport struct {
  17. updated time.Time
  18. *http.Transport
  19. }
  20. var ptc = proxyTransportCache{
  21. cache: make(map[int64]cachedTransport),
  22. }
  23. func (ds *DataSource) GetHttpClient() (*http.Client, error) {
  24. transport, err := ds.GetHttpTransport()
  25. if err != nil {
  26. return nil, err
  27. }
  28. return &http.Client{
  29. Timeout: 30 * time.Second,
  30. Transport: transport,
  31. }, nil
  32. }
  33. func (ds *DataSource) GetHttpTransport() (*http.Transport, error) {
  34. ptc.Lock()
  35. defer ptc.Unlock()
  36. if t, present := ptc.cache[ds.Id]; present && ds.Updated.Equal(t.updated) {
  37. return t.Transport, nil
  38. }
  39. tlsConfig, err := ds.GetTLSConfig()
  40. if err != nil {
  41. return nil, err
  42. }
  43. tlsConfig.Renegotiation = tls.RenegotiateFreelyAsClient
  44. transport := &http.Transport{
  45. TLSClientConfig: tlsConfig,
  46. Proxy: http.ProxyFromEnvironment,
  47. Dial: (&net.Dialer{
  48. Timeout: time.Duration(setting.DataProxyTimeout) * time.Second,
  49. KeepAlive: 30 * time.Second,
  50. }).Dial,
  51. TLSHandshakeTimeout: 10 * time.Second,
  52. ExpectContinueTimeout: 1 * time.Second,
  53. MaxIdleConns: 100,
  54. IdleConnTimeout: 90 * time.Second,
  55. }
  56. ptc.cache[ds.Id] = cachedTransport{
  57. Transport: transport,
  58. updated: ds.Updated,
  59. }
  60. return transport, nil
  61. }
  62. func (ds *DataSource) GetTLSConfig() (*tls.Config, error) {
  63. var tlsSkipVerify, tlsClientAuth, tlsAuthWithCACert bool
  64. if ds.JsonData != nil {
  65. tlsClientAuth = ds.JsonData.Get("tlsAuth").MustBool(false)
  66. tlsAuthWithCACert = ds.JsonData.Get("tlsAuthWithCACert").MustBool(false)
  67. tlsSkipVerify = ds.JsonData.Get("tlsSkipVerify").MustBool(false)
  68. }
  69. tlsConfig := &tls.Config{
  70. InsecureSkipVerify: tlsSkipVerify,
  71. }
  72. if tlsClientAuth || tlsAuthWithCACert {
  73. decrypted := ds.SecureJsonData.Decrypt()
  74. if tlsAuthWithCACert && len(decrypted["tlsCACert"]) > 0 {
  75. caPool := x509.NewCertPool()
  76. ok := caPool.AppendCertsFromPEM([]byte(decrypted["tlsCACert"]))
  77. if !ok {
  78. return nil, errors.New("Failed to parse TLS CA PEM certificate")
  79. }
  80. tlsConfig.RootCAs = caPool
  81. }
  82. if tlsClientAuth {
  83. cert, err := tls.X509KeyPair([]byte(decrypted["tlsClientCert"]), []byte(decrypted["tlsClientKey"]))
  84. if err != nil {
  85. return nil, err
  86. }
  87. tlsConfig.Certificates = []tls.Certificate{cert}
  88. }
  89. }
  90. return tlsConfig, nil
  91. }