dataproxy.go 3.7 KB

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