dataproxy.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/middleware"
  12. m "github.com/grafana/grafana/pkg/models"
  13. "github.com/grafana/grafana/pkg/setting"
  14. "github.com/grafana/grafana/pkg/util"
  15. )
  16. var dataProxyTransport = &http.Transport{
  17. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  18. Proxy: http.ProxyFromEnvironment,
  19. Dial: (&net.Dialer{
  20. Timeout: 30 * time.Second,
  21. KeepAlive: 30 * time.Second,
  22. }).Dial,
  23. TLSHandshakeTimeout: 10 * time.Second,
  24. }
  25. func NewReverseProxy(ds *m.DataSource, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
  26. director := func(req *http.Request) {
  27. req.URL.Scheme = targetUrl.Scheme
  28. req.URL.Host = targetUrl.Host
  29. req.Host = targetUrl.Host
  30. reqQueryVals := req.URL.Query()
  31. if ds.Type == m.DS_INFLUXDB_08 {
  32. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, "db/"+ds.Database+"/"+proxyPath)
  33. reqQueryVals.Add("u", ds.User)
  34. reqQueryVals.Add("p", ds.Password)
  35. req.URL.RawQuery = reqQueryVals.Encode()
  36. } else if ds.Type == m.DS_INFLUXDB {
  37. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
  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, FlushInterval: time.Millisecond * 200}
  55. }
  56. func getDatasource(id int64, orgId int64) (*m.DataSource, error) {
  57. query := m.GetDataSourceByIdQuery{Id: id, OrgId: orgId}
  58. if err := bus.Dispatch(&query); err != nil {
  59. return nil, err
  60. }
  61. return &query.Result, nil
  62. }
  63. func ProxyDataSourceRequest(c *middleware.Context) {
  64. ds, err := getDatasource(c.ParamsInt64(":id"), c.OrgId)
  65. if err != nil {
  66. c.JsonApiErr(500, "Unable to load datasource meta data", err)
  67. return
  68. }
  69. targetUrl, _ := url.Parse(ds.Url)
  70. if len(setting.DataProxyWhiteList) > 0 {
  71. if _, exists := setting.DataProxyWhiteList[targetUrl.Host]; !exists {
  72. c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil)
  73. return
  74. }
  75. }
  76. if ds.Type == m.DS_CLOUDWATCH {
  77. cloudwatch.HandleRequest(c, ds)
  78. } else {
  79. proxyPath := c.Params("*")
  80. proxy := NewReverseProxy(ds, proxyPath, targetUrl)
  81. proxy.Transport = dataProxyTransport
  82. proxy.ServeHTTP(c.Resp, c.Req.Request)
  83. c.Resp.Header().Del("Set-Cookie")
  84. }
  85. }