dataproxy.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package api
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "net/http"
  6. "net/http/httputil"
  7. "net/url"
  8. "time"
  9. "github.com/grafana/grafana/pkg/api/cloudwatch"
  10. "github.com/grafana/grafana/pkg/bus"
  11. "github.com/grafana/grafana/pkg/metrics"
  12. "github.com/grafana/grafana/pkg/middleware"
  13. m "github.com/grafana/grafana/pkg/models"
  14. "github.com/grafana/grafana/pkg/setting"
  15. "github.com/grafana/grafana/pkg/util"
  16. )
  17. func dataProxyTransport(ds *m.DataSource) (*http.Transport, error) {
  18. transport := &http.Transport{
  19. TLSClientConfig: &tls.Config{
  20. InsecureSkipVerify: true,
  21. },
  22. Proxy: http.ProxyFromEnvironment,
  23. Dial: (&net.Dialer{
  24. Timeout: 30 * time.Second,
  25. KeepAlive: 30 * time.Second,
  26. }).Dial,
  27. TLSHandshakeTimeout: 10 * time.Second,
  28. }
  29. if ds.TlsAuth {
  30. cert, err := tls.LoadX509KeyPair(ds.TlsClientCert, ds.TlsClientKey)
  31. if err != nil {
  32. return nil, err
  33. }
  34. transport.TLSClientConfig.Certificates = []tls.Certificate{cert}
  35. }
  36. return transport, nil
  37. }
  38. func NewReverseProxy(ds *m.DataSource, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
  39. director := func(req *http.Request) {
  40. req.URL.Scheme = targetUrl.Scheme
  41. req.URL.Host = targetUrl.Host
  42. req.Host = targetUrl.Host
  43. reqQueryVals := req.URL.Query()
  44. if ds.Type == m.DS_INFLUXDB_08 {
  45. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, "db/"+ds.Database+"/"+proxyPath)
  46. reqQueryVals.Add("u", ds.User)
  47. reqQueryVals.Add("p", ds.Password)
  48. req.URL.RawQuery = reqQueryVals.Encode()
  49. } else if ds.Type == m.DS_INFLUXDB {
  50. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
  51. req.URL.RawQuery = reqQueryVals.Encode()
  52. if !ds.BasicAuth {
  53. req.Header.Del("Authorization")
  54. req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.User, ds.Password))
  55. }
  56. } else {
  57. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
  58. }
  59. if ds.BasicAuth {
  60. req.Header.Del("Authorization")
  61. req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword))
  62. }
  63. dsAuth := req.Header.Get("X-DS-Authorization")
  64. if len(dsAuth) > 0 {
  65. req.Header.Del("X-DS-Authorization")
  66. req.Header.Del("Authorization")
  67. req.Header.Add("Authorization", dsAuth)
  68. }
  69. // clear cookie headers
  70. req.Header.Del("Cookie")
  71. req.Header.Del("Set-Cookie")
  72. }
  73. return &httputil.ReverseProxy{Director: director, FlushInterval: time.Millisecond * 200}
  74. }
  75. func getDatasource(id int64, orgId int64) (*m.DataSource, error) {
  76. query := m.GetDataSourceByIdQuery{Id: id, OrgId: orgId}
  77. if err := bus.Dispatch(&query); err != nil {
  78. return nil, err
  79. }
  80. return query.Result, nil
  81. }
  82. func ProxyDataSourceRequest(c *middleware.Context) {
  83. c.TimeRequest(metrics.M_DataSource_ProxyReq_Timer)
  84. ds, err := getDatasource(c.ParamsInt64(":id"), c.OrgId)
  85. if err != nil {
  86. c.JsonApiErr(500, "Unable to load datasource meta data", err)
  87. return
  88. }
  89. if ds.Type == m.DS_CLOUDWATCH {
  90. cloudwatch.HandleRequest(c, ds)
  91. return
  92. }
  93. if ds.Type == m.DS_INFLUXDB {
  94. if c.Query("db") != ds.Database {
  95. c.JsonApiErr(403, "Datasource is not configured to allow this database", nil)
  96. return
  97. }
  98. }
  99. targetUrl, _ := url.Parse(ds.Url)
  100. if len(setting.DataProxyWhiteList) > 0 {
  101. if _, exists := setting.DataProxyWhiteList[targetUrl.Host]; !exists {
  102. c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil)
  103. return
  104. }
  105. }
  106. proxyPath := c.Params("*")
  107. if ds.Type == m.DS_ES {
  108. if c.Req.Request.Method == "DELETE" {
  109. c.JsonApiErr(403, "Deletes not allowed on proxied Elasticsearch datasource", nil)
  110. return
  111. }
  112. if c.Req.Request.Method == "PUT" {
  113. c.JsonApiErr(403, "Puts not allowed on proxied Elasticsearch datasource", nil)
  114. return
  115. }
  116. if c.Req.Request.Method == "POST" && proxyPath != "_msearch" {
  117. c.JsonApiErr(403, "Posts not allowed on proxied Elasticsearch datasource except on /_msearch", nil)
  118. return
  119. }
  120. }
  121. proxy := NewReverseProxy(ds, proxyPath, targetUrl)
  122. proxy.Transport, err = dataProxyTransport(ds)
  123. if err != nil {
  124. c.JsonApiErr(400, "Unable to load TLS certificate", err)
  125. return
  126. }
  127. proxy.ServeHTTP(c.Resp, c.Req.Request)
  128. c.Resp.Header().Del("Set-Cookie")
  129. }