|
@@ -1,6 +1,8 @@
|
|
|
package api
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "bytes"
|
|
|
|
|
+ "io/ioutil"
|
|
|
"net/http"
|
|
"net/http"
|
|
|
"net/http/httputil"
|
|
"net/http/httputil"
|
|
|
"net/url"
|
|
"net/url"
|
|
@@ -8,6 +10,7 @@ import (
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/cloudwatch"
|
|
"github.com/grafana/grafana/pkg/api/cloudwatch"
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
|
+ "github.com/grafana/grafana/pkg/log"
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
|
"github.com/grafana/grafana/pkg/metrics"
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
@@ -15,6 +18,10 @@ import (
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+var (
|
|
|
|
|
+ dataproxyLogger log.Logger = log.New("data-proxy-log")
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
func NewReverseProxy(ds *m.DataSource, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
|
|
func NewReverseProxy(ds *m.DataSource, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
|
|
|
director := func(req *http.Request) {
|
|
director := func(req *http.Request) {
|
|
|
req.URL.Scheme = targetUrl.Scheme
|
|
req.URL.Scheme = targetUrl.Scheme
|
|
@@ -121,6 +128,32 @@ func ProxyDataSourceRequest(c *middleware.Context) {
|
|
|
c.JsonApiErr(400, "Unable to load TLS certificate", err)
|
|
c.JsonApiErr(400, "Unable to load TLS certificate", err)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ logProxyRequest(ds.Type, c)
|
|
|
proxy.ServeHTTP(c.Resp, c.Req.Request)
|
|
proxy.ServeHTTP(c.Resp, c.Req.Request)
|
|
|
c.Resp.Header().Del("Set-Cookie")
|
|
c.Resp.Header().Del("Set-Cookie")
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func logProxyRequest(dataSourceType string, c *middleware.Context) {
|
|
|
|
|
+ if !setting.DataProxyLogging {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var body string
|
|
|
|
|
+ if c.Req.Request.Body != nil {
|
|
|
|
|
+ buffer, err := ioutil.ReadAll(c.Req.Request.Body)
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ c.Req.Request.Body = ioutil.NopCloser(bytes.NewBuffer(buffer))
|
|
|
|
|
+ body = string(buffer)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ dataproxyLogger.Info("Proxying incoming request",
|
|
|
|
|
+ "userid", c.UserId,
|
|
|
|
|
+ "orgid", c.OrgId,
|
|
|
|
|
+ "username", c.Login,
|
|
|
|
|
+ "datasource", dataSourceType,
|
|
|
|
|
+ "uri", c.Req.RequestURI,
|
|
|
|
|
+ "method", c.Req.Request.Method,
|
|
|
|
|
+ "body", body)
|
|
|
|
|
+}
|