From 564d7ecea7eae1239f81c53c60d5650f000e0b15 Mon Sep 17 00:00:00 2001 From: Domas Date: Fri, 18 Sep 2020 14:22:07 +0300 Subject: [PATCH] DataProxy: Ignore empty URL's in plugin routes (#27653) This adds a check to see if plugin route URL is empty, and in such case does not modify request schema and host of the request to be proxied. This behavior is now the same as in the plugin proxy. --- pkg/api/pluginproxy/ds_auth_provider.go | 30 +++++++++++++------------ pkg/api/pluginproxy/ds_proxy_test.go | 15 +++++++++++++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/pkg/api/pluginproxy/ds_auth_provider.go b/pkg/api/pluginproxy/ds_auth_provider.go index d1d8f822d70..1614e7ee455 100644 --- a/pkg/api/pluginproxy/ds_auth_provider.go +++ b/pkg/api/pluginproxy/ds_auth_provider.go @@ -22,22 +22,24 @@ func ApplyRoute(ctx context.Context, req *http.Request, proxyPath string, route SecureJsonData: ds.SecureJsonData.Decrypt(), } - interpolatedURL, err := InterpolateString(route.URL, data) - if err != nil { - logger.Error("Error interpolating proxy url", "error", err) - return - } + if len(route.URL) > 0 { + interpolatedURL, err := InterpolateString(route.URL, data) + if err != nil { + logger.Error("Error interpolating proxy url", "error", err) + return + } - routeURL, err := url.Parse(interpolatedURL) - if err != nil { - logger.Error("Error parsing plugin route url", "error", err) - return - } + routeURL, err := url.Parse(interpolatedURL) + if err != nil { + logger.Error("Error parsing plugin route url", "error", err) + return + } - req.URL.Scheme = routeURL.Scheme - req.URL.Host = routeURL.Host - req.Host = routeURL.Host - req.URL.Path = util.JoinURLFragments(routeURL.Path, proxyPath) + req.URL.Scheme = routeURL.Scheme + req.URL.Host = routeURL.Host + req.Host = routeURL.Host + req.URL.Path = util.JoinURLFragments(routeURL.Path, proxyPath) + } if err := addQueryString(req, route, data); err != nil { logger.Error("Failed to render plugin URL query string", "error", err) diff --git a/pkg/api/pluginproxy/ds_proxy_test.go b/pkg/api/pluginproxy/ds_proxy_test.go index 7f14665ae87..6e138e49270 100644 --- a/pkg/api/pluginproxy/ds_proxy_test.go +++ b/pkg/api/pluginproxy/ds_proxy_test.go @@ -67,6 +67,10 @@ func TestDSRouteRule(t *testing.T) { {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"}, }, }, + { + Path: "api/restricted", + ReqRole: models.ROLE_ADMIN, + }, }, } @@ -116,6 +120,17 @@ func TestDSRouteRule(t *testing.T) { }) }) + Convey("When matching route path with no url", func() { + proxy, err := NewDataSourceProxy(ds, plugin, ctx, "", &setting.Cfg{}) + So(err, ShouldBeNil) + proxy.route = plugin.Routes[4] + ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.route, proxy.ds) + + Convey("Should not replace request url", func() { + So(req.URL.String(), ShouldEqual, "http://localhost/asd") + }) + }) + Convey("Validating request", func() { Convey("plugin route with valid role", func() { proxy, err := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method", &setting.Cfg{})