dataproxy.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/bus"
  10. "github.com/grafana/grafana/pkg/middleware"
  11. m "github.com/grafana/grafana/pkg/models"
  12. "github.com/grafana/grafana/pkg/setting"
  13. "github.com/grafana/grafana/pkg/util"
  14. )
  15. var dataProxyTransport = &http.Transport{
  16. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  17. Proxy: http.ProxyFromEnvironment,
  18. Dial: (&net.Dialer{
  19. Timeout: 30 * time.Second,
  20. KeepAlive: 30 * time.Second,
  21. }).Dial,
  22. TLSHandshakeTimeout: 10 * time.Second,
  23. }
  24. func NewReverseProxy(ds *m.DataSource, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
  25. director := func(req *http.Request) {
  26. req.URL.Scheme = targetUrl.Scheme
  27. req.URL.Host = targetUrl.Host
  28. req.Host = targetUrl.Host
  29. reqQueryVals := req.URL.Query()
  30. if ds.Type == m.DS_INFLUXDB_08 {
  31. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, "db/"+ds.Database+"/"+proxyPath)
  32. reqQueryVals.Add("u", ds.User)
  33. reqQueryVals.Add("p", ds.Password)
  34. req.URL.RawQuery = reqQueryVals.Encode()
  35. } else if ds.Type == m.DS_INFLUXDB {
  36. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
  37. reqQueryVals.Add("db", ds.Database)
  38. req.URL.RawQuery = reqQueryVals.Encode()
  39. if !ds.BasicAuth {
  40. req.Header.Del("Authorization")
  41. req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.User, ds.Password))
  42. }
  43. } else {
  44. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
  45. }
  46. if ds.BasicAuth {
  47. req.Header.Del("Authorization")
  48. req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword))
  49. }
  50. // clear cookie headers
  51. req.Header.Del("Cookie")
  52. req.Header.Del("Set-Cookie")
  53. }
  54. return &httputil.ReverseProxy{Director: director}
  55. }
  56. //ProxyDataSourceRequest TODO need to cache datasources
  57. func ProxyDataSourceRequest(c *middleware.Context) {
  58. id := c.ParamsInt64(":id")
  59. query := m.GetDataSourceByIdQuery{Id: id, OrgId: c.OrgId}
  60. if err := bus.Dispatch(&query); err != nil {
  61. c.JsonApiErr(500, "Unable to load datasource meta data", err)
  62. return
  63. }
  64. ds := query.Result
  65. targetUrl, _ := url.Parse(ds.Url)
  66. if len(setting.DataProxyWhiteList) > 0 {
  67. if _, exists := setting.DataProxyWhiteList[targetUrl.Host]; !exists {
  68. c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil)
  69. return
  70. }
  71. }
  72. if query.Result.Type == m.DS_CLOUDWATCH {
  73. ProxyCloudWatchDataSourceRequest(c)
  74. } else {
  75. proxyPath := c.Params("*")
  76. proxy := NewReverseProxy(&ds, proxyPath, targetUrl)
  77. proxy.Transport = dataProxyTransport
  78. proxy.ServeHTTP(c.RW(), c.Req.Request)
  79. }
  80. }