dataproxy.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.Add("Authorization", util.GetBasicAuthHeader(ds.User, ds.Password))
  41. }
  42. } else {
  43. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
  44. }
  45. if ds.BasicAuth {
  46. req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword))
  47. }
  48. // clear cookie headers
  49. req.Header.Del("Cookie")
  50. req.Header.Del("Set-Cookie")
  51. }
  52. return &httputil.ReverseProxy{Director: director}
  53. }
  54. //ProxyDataSourceRequest TODO need to cache datasources
  55. func ProxyDataSourceRequest(c *middleware.Context) {
  56. id := c.ParamsInt64(":id")
  57. query := m.GetDataSourceByIdQuery{Id: id, OrgId: c.OrgId}
  58. if err := bus.Dispatch(&query); err != nil {
  59. c.JsonApiErr(500, "Unable to load datasource meta data", err)
  60. return
  61. }
  62. ds := query.Result
  63. targetUrl, _ := url.Parse(ds.Url)
  64. if len(setting.DataProxyWhiteList) > 0 {
  65. if _, exists := setting.DataProxyWhiteList[targetUrl.Host]; !exists {
  66. c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil)
  67. return
  68. }
  69. }
  70. if query.Result.Type == m.DS_CLOUDWATCH {
  71. ProxyCloudWatchDataSourceRequest(c)
  72. } else {
  73. proxyPath := c.Params("*")
  74. proxy := NewReverseProxy(&ds, proxyPath, targetUrl)
  75. proxy.Transport = dataProxyTransport
  76. proxy.ServeHTTP(c.RW(), c.Req.Request)
  77. }
  78. }