diff --git a/pkg/api/pluginproxy/ds_auth_provider.go b/pkg/api/pluginproxy/ds_auth_provider.go index 8eeecc8b885..c0fe875b9c4 100644 --- a/pkg/api/pluginproxy/ds_auth_provider.go +++ b/pkg/api/pluginproxy/ds_auth_provider.go @@ -16,6 +16,7 @@ import ( type DSInfo struct { ID int64 Updated time.Time + URL string JSONData map[string]any DecryptedSecureJSONData map[string]string } @@ -24,8 +25,8 @@ type DSInfo struct { func ApplyRoute(ctx context.Context, req *http.Request, proxyPath string, route *plugins.Route, ds DSInfo, cfg *setting.Cfg) { proxyPath = strings.TrimPrefix(proxyPath, route.Path) - data := templateData{ + URL: ds.URL, JsonData: ds.JSONData, SecureJsonData: ds.DecryptedSecureJSONData, } diff --git a/pkg/api/pluginproxy/ds_proxy.go b/pkg/api/pluginproxy/ds_proxy.go index 3bfa77181fc..651d61bb1ce 100644 --- a/pkg/api/pluginproxy/ds_proxy.go +++ b/pkg/api/pluginproxy/ds_proxy.go @@ -252,6 +252,7 @@ func (proxy *DataSourceProxy) director(req *http.Request) { ApplyRoute(req.Context(), req, proxy.proxyPath, proxy.matchedRoute, DSInfo{ ID: proxy.ds.ID, + URL: proxy.ds.URL, Updated: proxy.ds.Updated, JSONData: jsonData, DecryptedSecureJSONData: decryptedValues, diff --git a/pkg/api/pluginproxy/ds_proxy_test.go b/pkg/api/pluginproxy/ds_proxy_test.go index 9bc1739c82f..99ec8bcaaea 100644 --- a/pkg/api/pluginproxy/ds_proxy_test.go +++ b/pkg/api/pluginproxy/ds_proxy_test.go @@ -158,6 +158,36 @@ func TestDataSourceProxy_routeRule(t *testing.T) { assert.Equal(t, "http://localhost/asd", req.URL.String()) }) + t.Run("When matching route path and has setting url", func(t *testing.T) { + ctx, req := setUp() + proxy, err := setupDSProxyTest(t, ctx, ds, routes, "api/common/some/method") + require.NoError(t, err) + proxy.matchedRoute = &plugins.Route{ + Path: "api/common", + URL: "{{.URL}}", + Headers: []plugins.Header{ + {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"}, + }, + URLParams: []plugins.URLParam{ + {Name: "{{.JsonData.queryParam}}", Content: "{{.SecureJsonData.key}}"}, + }, + } + + dsInfo := DSInfo{ + ID: ds.ID, + Updated: ds.Updated, + JSONData: jd, + DecryptedSecureJSONData: map[string]string{ + "key": "123", + }, + URL: "https://dynamic.grafana.com", + } + ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.matchedRoute, dsInfo, proxy.cfg) + + assert.Equal(t, "https://dynamic.grafana.com/some/method?apiKey=123", req.URL.String()) + assert.Equal(t, "my secret 123", req.Header.Get("x-header")) + }) + t.Run("When matching route path and has dynamic body", func(t *testing.T) { ctx, req := setUp() proxy, err := setupDSProxyTest(t, ctx, ds, routes, "api/body") diff --git a/pkg/api/pluginproxy/pluginproxy.go b/pkg/api/pluginproxy/pluginproxy.go index 10c0c38b148..c61a6a284b9 100644 --- a/pkg/api/pluginproxy/pluginproxy.go +++ b/pkg/api/pluginproxy/pluginproxy.go @@ -218,6 +218,7 @@ func (proxy PluginProxy) logRequest() { } type templateData struct { + URL string JsonData map[string]any SecureJsonData map[string]string }