mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins Proxy: Allow using {{ .URL }} inside "routes" section in plugin.json (#80858)
Signed-off-by: Slach <bloodjazman@gmail.com> Co-authored-by: Andres Martinez Gotor <andres.mgotor@gmail.com>
This commit is contained in:
parent
ea243b536c
commit
702e22806c
@ -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,
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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")
|
||||
|
@ -218,6 +218,7 @@ func (proxy PluginProxy) logRequest() {
|
||||
}
|
||||
|
||||
type templateData struct {
|
||||
URL string
|
||||
JsonData map[string]any
|
||||
SecureJsonData map[string]string
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user