|
|
@ -11,6 +11,7 @@ import ( |
|
|
|
"github.com/grafana/grafana/pkg/bus" |
|
|
|
"github.com/grafana/grafana/pkg/bus" |
|
|
|
"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" |
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/setting" |
|
|
|
"github.com/grafana/grafana/pkg/util" |
|
|
|
"github.com/grafana/grafana/pkg/util" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -24,30 +25,28 @@ var dataProxyTransport = &http.Transport{ |
|
|
|
TLSHandshakeTimeout: 10 * time.Second, |
|
|
|
TLSHandshakeTimeout: 10 * time.Second, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewReverseProxy(ds *m.DataSource, proxyPath string) *httputil.ReverseProxy { |
|
|
|
func NewReverseProxy(ds *m.DataSource, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy { |
|
|
|
target, _ := url.Parse(ds.Url) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
director := func(req *http.Request) { |
|
|
|
director := func(req *http.Request) { |
|
|
|
req.URL.Scheme = target.Scheme |
|
|
|
req.URL.Scheme = targetUrl.Scheme |
|
|
|
req.URL.Host = target.Host |
|
|
|
req.URL.Host = targetUrl.Host |
|
|
|
req.Host = target.Host |
|
|
|
req.Host = targetUrl.Host |
|
|
|
|
|
|
|
|
|
|
|
reqQueryVals := req.URL.Query() |
|
|
|
reqQueryVals := req.URL.Query() |
|
|
|
|
|
|
|
|
|
|
|
if ds.Type == m.DS_INFLUXDB_08 { |
|
|
|
if ds.Type == m.DS_INFLUXDB_08 { |
|
|
|
req.URL.Path = util.JoinUrlFragments(target.Path, "db/"+ds.Database+"/"+proxyPath) |
|
|
|
req.URL.Path = util.JoinUrlFragments(targetUrl.Path, "db/"+ds.Database+"/"+proxyPath) |
|
|
|
reqQueryVals.Add("u", ds.User) |
|
|
|
reqQueryVals.Add("u", ds.User) |
|
|
|
reqQueryVals.Add("p", ds.Password) |
|
|
|
reqQueryVals.Add("p", ds.Password) |
|
|
|
req.URL.RawQuery = reqQueryVals.Encode() |
|
|
|
req.URL.RawQuery = reqQueryVals.Encode() |
|
|
|
} else if ds.Type == m.DS_INFLUXDB { |
|
|
|
} else if ds.Type == m.DS_INFLUXDB { |
|
|
|
req.URL.Path = util.JoinUrlFragments(target.Path, proxyPath) |
|
|
|
req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath) |
|
|
|
reqQueryVals.Add("db", ds.Database) |
|
|
|
reqQueryVals.Add("db", ds.Database) |
|
|
|
req.URL.RawQuery = reqQueryVals.Encode() |
|
|
|
req.URL.RawQuery = reqQueryVals.Encode() |
|
|
|
if !ds.BasicAuth { |
|
|
|
if !ds.BasicAuth { |
|
|
|
req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.User, ds.Password)) |
|
|
|
req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.User, ds.Password)) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
req.URL.Path = util.JoinUrlFragments(target.Path, proxyPath) |
|
|
|
req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ds.BasicAuth { |
|
|
|
if ds.BasicAuth { |
|
|
@ -72,11 +71,20 @@ func ProxyDataSourceRequest(c *middleware.Context) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ds := query.Result |
|
|
|
|
|
|
|
targetUrl, _ := url.Parse(ds.Url) |
|
|
|
|
|
|
|
if len(setting.DataProxyWhiteList) > 0 { |
|
|
|
|
|
|
|
if _, exists := setting.DataProxyWhiteList[targetUrl.Host]; !exists { |
|
|
|
|
|
|
|
c.JsonApiErr(403, "Data proxy hostname and ip are not included in whitelist", nil) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if query.Result.Type == m.DS_CLOUDWATCH { |
|
|
|
if query.Result.Type == m.DS_CLOUDWATCH { |
|
|
|
ProxyCloudWatchDataSourceRequest(c) |
|
|
|
ProxyCloudWatchDataSourceRequest(c) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
proxyPath := c.Params("*") |
|
|
|
proxyPath := c.Params("*") |
|
|
|
proxy := NewReverseProxy(&query.Result, proxyPath) |
|
|
|
proxy := NewReverseProxy(&ds, proxyPath, targetUrl) |
|
|
|
proxy.Transport = dataProxyTransport |
|
|
|
proxy.Transport = dataProxyTransport |
|
|
|
proxy.ServeHTTP(c.RW(), c.Req.Request) |
|
|
|
proxy.ServeHTTP(c.RW(), c.Req.Request) |
|
|
|
} |
|
|
|
} |
|
|
|