dataproxy.go 3.5 KB

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