@@ -11,6 +11,7 @@ import (
11
11
"github.com/grafana/grafana/pkg/bus"
12
12
"github.com/grafana/grafana/pkg/middleware"
13
13
m "github.com/grafana/grafana/pkg/models"
14
+ "github.com/grafana/grafana/pkg/setting"
14
15
"github.com/grafana/grafana/pkg/util"
15
16
)
16
17
@@ -24,30 +25,28 @@ var dataProxyTransport = &http.Transport{
24
25
TLSHandshakeTimeout : 10 * time .Second ,
25
26
}
26
27
27
- func NewReverseProxy (ds * m.DataSource , proxyPath string ) * httputil.ReverseProxy {
28
- target , _ := url .Parse (ds .Url )
29
-
28
+ func NewReverseProxy (ds * m.DataSource , proxyPath string , targetUrl * url.URL ) * httputil.ReverseProxy {
30
29
director := func (req * http.Request ) {
31
- req .URL .Scheme = target .Scheme
32
- req .URL .Host = target .Host
33
- req .Host = target .Host
30
+ req .URL .Scheme = targetUrl .Scheme
31
+ req .URL .Host = targetUrl .Host
32
+ req .Host = targetUrl .Host
34
33
35
34
reqQueryVals := req .URL .Query ()
36
35
37
36
if ds .Type == m .DS_INFLUXDB_08 {
38
- req .URL .Path = util .JoinUrlFragments (target .Path , "db/" + ds .Database + "/" + proxyPath )
37
+ req .URL .Path = util .JoinUrlFragments (targetUrl .Path , "db/" + ds .Database + "/" + proxyPath )
39
38
reqQueryVals .Add ("u" , ds .User )
40
39
reqQueryVals .Add ("p" , ds .Password )
41
40
req .URL .RawQuery = reqQueryVals .Encode ()
42
41
} else if ds .Type == m .DS_INFLUXDB {
43
- req .URL .Path = util .JoinUrlFragments (target .Path , proxyPath )
42
+ req .URL .Path = util .JoinUrlFragments (targetUrl .Path , proxyPath )
44
43
reqQueryVals .Add ("db" , ds .Database )
45
44
req .URL .RawQuery = reqQueryVals .Encode ()
46
45
if ! ds .BasicAuth {
47
46
req .Header .Add ("Authorization" , util .GetBasicAuthHeader (ds .User , ds .Password ))
48
47
}
49
48
} else {
50
- req .URL .Path = util .JoinUrlFragments (target .Path , proxyPath )
49
+ req .URL .Path = util .JoinUrlFragments (targetUrl .Path , proxyPath )
51
50
}
52
51
53
52
if ds .BasicAuth {
@@ -72,11 +71,20 @@ func ProxyDataSourceRequest(c *middleware.Context) {
72
71
return
73
72
}
74
73
74
+ ds := query .Result
75
+ targetUrl , _ := url .Parse (ds .Url )
76
+ if len (setting .DataProxyWhiteList ) > 0 {
77
+ if _ , exists := setting .DataProxyWhiteList [targetUrl .Host ]; ! exists {
78
+ c .JsonApiErr (403 , "Data proxy hostname and ip are not included in whitelist" , nil )
79
+ return
80
+ }
81
+ }
82
+
75
83
if query .Result .Type == m .DS_CLOUDWATCH {
76
84
ProxyCloudWatchDataSourceRequest (c )
77
85
} else {
78
86
proxyPath := c .Params ("*" )
79
- proxy := NewReverseProxy (& query . Result , proxyPath )
87
+ proxy := NewReverseProxy (& ds , proxyPath , targetUrl )
80
88
proxy .Transport = dataProxyTransport
81
89
proxy .ServeHTTP (c .RW (), c .Req .Request )
82
90
}
0 commit comments