dataproxy.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package api
  2. import (
  3. "bytes"
  4. "io/ioutil"
  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/log"
  12. "github.com/grafana/grafana/pkg/metrics"
  13. "github.com/grafana/grafana/pkg/middleware"
  14. m "github.com/grafana/grafana/pkg/models"
  15. "github.com/grafana/grafana/pkg/setting"
  16. "github.com/grafana/grafana/pkg/util"
  17. )
  18. var (
  19. dataproxyLogger log.Logger = log.New("data-proxy-log")
  20. )
  21. func NewReverseProxy(ds *m.DataSource, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
  22. director := func(req *http.Request) {
  23. req.URL.Scheme = targetUrl.Scheme
  24. req.URL.Host = targetUrl.Host
  25. req.Host = targetUrl.Host
  26. reqQueryVals := req.URL.Query()
  27. if ds.Type == m.DS_INFLUXDB_08 {
  28. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, "db/"+ds.Database+"/"+proxyPath)
  29. reqQueryVals.Add("u", ds.User)
  30. reqQueryVals.Add("p", ds.Password)
  31. req.URL.RawQuery = reqQueryVals.Encode()
  32. } else if ds.Type == m.DS_INFLUXDB {
  33. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
  34. req.URL.RawQuery = reqQueryVals.Encode()
  35. if !ds.BasicAuth {
  36. req.Header.Del("Authorization")
  37. req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.User, ds.Password))
  38. }
  39. } else {
  40. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
  41. }
  42. if ds.BasicAuth {
  43. req.Header.Del("Authorization")
  44. req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword))
  45. }
  46. dsAuth := req.Header.Get("X-DS-Authorization")
  47. if len(dsAuth) > 0 {
  48. req.Header.Del("X-DS-Authorization")
  49. req.Header.Del("Authorization")
  50. req.Header.Add("Authorization", dsAuth)
  51. }
  52. // clear cookie headers
  53. req.Header.Del("Cookie")
  54. req.Header.Del("Set-Cookie")
  55. }
  56. return &httputil.ReverseProxy{Director: director, FlushInterval: time.Millisecond * 200}
  57. }
  58. func getDatasource(id int64, orgId int64) (*m.DataSource, error) {
  59. query := m.GetDataSourceByIdQuery{Id: id, OrgId: orgId}
  60. if err := bus.Dispatch(&query); err != nil {
  61. return nil, err
  62. }
  63. return query.Result, nil
  64. }
  65. func ProxyDataSourceRequest(c *middleware.Context) {
  66. c.TimeRequest(metrics.M_DataSource_ProxyReq_Timer)
  67. ds, err := getDatasource(c.ParamsInt64(":id"), c.OrgId)
  68. if err != nil {
  69. c.JsonApiErr(500, "Unable to load datasource meta data", err)
  70. return
  71. }
  72. if ds.Type == m.DS_CLOUDWATCH {
  73. cloudwatch.HandleRequest(c, ds)
  74. return
  75. }
  76. if ds.Type == m.DS_INFLUXDB {
  77. if c.Query("db") != ds.Database {
  78. c.JsonApiErr(403, "Datasource is not configured to allow this database", nil)
  79. return
  80. }
  81. }
  82. targetUrl, _ := url.Parse(ds.Url)
  83. if len(setting.DataProxyWhiteList) > 0 {
  84. if _, exists := setting.DataProxyWhiteList[targetUrl.Host]; !exists {
  85. c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil)
  86. return
  87. }
  88. }
  89. proxyPath := c.Params("*")
  90. if ds.Type == m.DS_ES {
  91. if c.Req.Request.Method == "DELETE" {
  92. c.JsonApiErr(403, "Deletes not allowed on proxied Elasticsearch datasource", nil)
  93. return
  94. }
  95. if c.Req.Request.Method == "PUT" {
  96. c.JsonApiErr(403, "Puts not allowed on proxied Elasticsearch datasource", nil)
  97. return
  98. }
  99. if c.Req.Request.Method == "POST" && proxyPath != "_msearch" {
  100. c.JsonApiErr(403, "Posts not allowed on proxied Elasticsearch datasource except on /_msearch", nil)
  101. return
  102. }
  103. }
  104. proxy := NewReverseProxy(ds, proxyPath, targetUrl)
  105. proxy.Transport, err = ds.GetHttpTransport()
  106. if err != nil {
  107. c.JsonApiErr(400, "Unable to load TLS certificate", err)
  108. return
  109. }
  110. logProxyRequest(ds.Type, c)
  111. proxy.ServeHTTP(c.Resp, c.Req.Request)
  112. c.Resp.Header().Del("Set-Cookie")
  113. }
  114. func logProxyRequest(dataSourceType string, c *middleware.Context) {
  115. if !setting.DataProxyLogging {
  116. return
  117. }
  118. var body string
  119. if c.Req.Request.Body != nil {
  120. buffer, err := ioutil.ReadAll(c.Req.Request.Body)
  121. if err == nil {
  122. c.Req.Request.Body = ioutil.NopCloser(bytes.NewBuffer(buffer))
  123. body = string(buffer)
  124. }
  125. }
  126. dataproxyLogger.Info("Proxying incoming request",
  127. "userid", c.UserId,
  128. "orgid", c.OrgId,
  129. "username", c.Login,
  130. "datasource", dataSourceType,
  131. "uri", c.Req.RequestURI,
  132. "method", c.Req.Request.Method,
  133. "body", body)
  134. }