|
|
|
|
@ -81,7 +81,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Convey("When matching route path", func() { |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method", &setting.Cfg{}) |
|
|
|
|
proxy.route = plugin.Routes[0] |
|
|
|
|
ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.route, proxy.ds) |
|
|
|
|
|
|
|
|
|
@ -92,7 +92,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("When matching route path and has dynamic url", func() { |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/common/some/method") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/common/some/method", &setting.Cfg{}) |
|
|
|
|
proxy.route = plugin.Routes[3] |
|
|
|
|
ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.route, proxy.ds) |
|
|
|
|
|
|
|
|
|
@ -104,20 +104,20 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
|
|
|
|
|
Convey("Validating request", func() { |
|
|
|
|
Convey("plugin route with valid role", func() { |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method", &setting.Cfg{}) |
|
|
|
|
err := proxy.validateRequest() |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("plugin route with admin role and user is editor", func() { |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin", &setting.Cfg{}) |
|
|
|
|
err := proxy.validateRequest() |
|
|
|
|
So(err, ShouldNotBeNil) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("plugin route with admin role and user is admin", func() { |
|
|
|
|
ctx.SignedInUser.OrgRole = m.ROLE_ADMIN |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin", &setting.Cfg{}) |
|
|
|
|
err := proxy.validateRequest() |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
}) |
|
|
|
|
@ -186,7 +186,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
client = newFakeHTTPClient(json) |
|
|
|
|
proxy1 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1") |
|
|
|
|
proxy1 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1", &setting.Cfg{}) |
|
|
|
|
proxy1.route = plugin.Routes[0] |
|
|
|
|
ApplyRoute(proxy1.ctx.Req.Context(), req, proxy1.proxyPath, proxy1.route, proxy1.ds) |
|
|
|
|
|
|
|
|
|
@ -200,7 +200,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "http://localhost/asd", nil) |
|
|
|
|
client = newFakeHTTPClient(json2) |
|
|
|
|
proxy2 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken2") |
|
|
|
|
proxy2 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken2", &setting.Cfg{}) |
|
|
|
|
proxy2.route = plugin.Routes[1] |
|
|
|
|
ApplyRoute(proxy2.ctx.Req.Context(), req, proxy2.proxyPath, proxy2.route, proxy2.ds) |
|
|
|
|
|
|
|
|
|
@ -215,7 +215,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
req, _ := http.NewRequest("GET", "http://localhost/asd", nil) |
|
|
|
|
|
|
|
|
|
client = newFakeHTTPClient([]byte{}) |
|
|
|
|
proxy3 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1") |
|
|
|
|
proxy3 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1", &setting.Cfg{}) |
|
|
|
|
proxy3.route = plugin.Routes[0] |
|
|
|
|
ApplyRoute(proxy3.ctx.Req.Context(), req, proxy3.proxyPath, proxy3.route, proxy3.ds) |
|
|
|
|
|
|
|
|
|
@ -236,7 +236,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
ds := &m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE} |
|
|
|
|
ctx := &m.ReqContext{} |
|
|
|
|
|
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "/render") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "/render", &setting.Cfg{}) |
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
@ -261,7 +261,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ctx := &m.ReqContext{} |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "", &setting.Cfg{}) |
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
@ -291,7 +291,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ctx := &m.ReqContext{} |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "", &setting.Cfg{}) |
|
|
|
|
|
|
|
|
|
requestURL, _ := url.Parse("http://grafana.com/sub") |
|
|
|
|
req := http.Request{URL: requestURL, Header: make(http.Header)} |
|
|
|
|
@ -317,7 +317,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ctx := &m.ReqContext{} |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "", &setting.Cfg{}) |
|
|
|
|
|
|
|
|
|
requestURL, _ := url.Parse("http://grafana.com/sub") |
|
|
|
|
req := http.Request{URL: requestURL, Header: make(http.Header)} |
|
|
|
|
@ -347,7 +347,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ctx := &m.ReqContext{} |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "", &setting.Cfg{}) |
|
|
|
|
|
|
|
|
|
requestURL, _ := url.Parse("http://grafana.com/sub") |
|
|
|
|
req := http.Request{URL: requestURL, Header: make(http.Header)} |
|
|
|
|
@ -369,7 +369,7 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
Url: "http://host/root/", |
|
|
|
|
} |
|
|
|
|
ctx := &m.ReqContext{} |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "/path/to/folder/") |
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "/path/to/folder/", &setting.Cfg{}) |
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil) |
|
|
|
|
req.Header.Add("Origin", "grafana.com") |
|
|
|
|
req.Header.Add("Referer", "grafana.com") |
|
|
|
|
@ -388,9 +388,55 @@ func TestDSRouteRule(t *testing.T) { |
|
|
|
|
So(req.Header.Get("X-Canary"), ShouldEqual, "stillthere") |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("When SendUserHeader config is enabled", func() { |
|
|
|
|
req := getDatasourceProxiedRequest( |
|
|
|
|
&m.ReqContext{ |
|
|
|
|
SignedInUser: &m.SignedInUser{ |
|
|
|
|
Login: "test_user", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
&setting.Cfg{SendUserHeader: true}, |
|
|
|
|
) |
|
|
|
|
Convey("Should add header with username", func() { |
|
|
|
|
So(req.Header.Get("X-Grafana-User"), ShouldEqual, "test_user") |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
Convey("When SendUserHeader config is disabled", func() { |
|
|
|
|
req := getDatasourceProxiedRequest( |
|
|
|
|
&m.ReqContext{ |
|
|
|
|
SignedInUser: &m.SignedInUser{ |
|
|
|
|
Login: "test_user", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
&setting.Cfg{SendUserHeader: false}, |
|
|
|
|
) |
|
|
|
|
Convey("Should not add header with username", func() { |
|
|
|
|
// Get will return empty string even if header is not set
|
|
|
|
|
So(req.Header.Get("X-Grafana-User"), ShouldEqual, "") |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// getDatasourceProxiedRequest is a helper for easier setup of tests based on global config and ReqContext.
|
|
|
|
|
func getDatasourceProxiedRequest(ctx *m.ReqContext, cfg *setting.Cfg) *http.Request { |
|
|
|
|
plugin := &plugins.DataSourcePlugin{} |
|
|
|
|
|
|
|
|
|
ds := &m.DataSource{ |
|
|
|
|
Type: "custom", |
|
|
|
|
Url: "http://host/root/", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
proxy := NewDataSourceProxy(ds, plugin, ctx, "", cfg) |
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil) |
|
|
|
|
So(err, ShouldBeNil) |
|
|
|
|
|
|
|
|
|
proxy.getDirector()(req) |
|
|
|
|
return req |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type httpClientStub struct { |
|
|
|
|
fakeBody []byte |
|
|
|
|
} |
|
|
|
|
|