Fix panic when using complex dynamic URLs in app plugin routes (#27977)

* remove unused function to interpolate URLs

* share function to add headers between ds/plugin proxies

* stop performing unnecessary plugin setting lookup

* fix bug causing runtime errors when using complex templated URLs

* lower case util functions not used outside of pluginproxy package

* change test URL to a (valid) dummy URL to make intent clearer

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Victor Cinaglia
2020-11-17 04:56:42 -05:00
committed by GitHub
parent 0f3bebb38d
commit 967e9b39e8
8 changed files with 104 additions and 152 deletions

View File

@ -36,11 +36,18 @@ func TestPluginProxy(t *testing.T) {
return nil
})
header, err := getHeaders(route, 1, "my-app")
So(err, ShouldBeNil)
req := getPluginProxiedRequest(
&models.ReqContext{
SignedInUser: &models.SignedInUser{
Login: "test_user",
},
},
&setting.Cfg{SendUserHeader: true},
route,
)
Convey("Should render header template", func() {
So(header.Get("x-header"), ShouldEqual, "my secret 123")
So(req.Header.Get("x-header"), ShouldEqual, "my secret 123")
})
})
@ -116,11 +123,6 @@ func TestPluginProxy(t *testing.T) {
&setting.Cfg{SendUserHeader: true},
route,
)
Convey("Headers should be updated", func() {
header, err := getHeaders(route, 1, "my-app")
So(err, ShouldBeNil)
So(header.Get("X-Grafana-User"), ShouldEqual, "")
})
Convey("Should set req.URL to be interpolated value from jsonData", func() {
So(req.URL.String(), ShouldEqual, "https://dynamic.grafana.com")
})
@ -128,6 +130,31 @@ func TestPluginProxy(t *testing.T) {
So(route.URL, ShouldEqual, "{{.JsonData.dynamicUrl}}")
})
})
Convey("When getting complex templated url", t, func() {
route := &plugins.AppPluginRoute{
URL: "{{if .JsonData.apiHost}}{{.JsonData.apiHost}}{{else}}https://example.com{{end}}",
Method: "GET",
}
bus.AddHandler("test", func(query *models.GetPluginSettingByIdQuery) error {
query.Result = &models.PluginSetting{}
return nil
})
req := getPluginProxiedRequest(
&models.ReqContext{
SignedInUser: &models.SignedInUser{
Login: "test_user",
},
},
&setting.Cfg{SendUserHeader: true},
route,
)
Convey("Should set req.URL to be interpolated default value", func() {
So(req.URL.String(), ShouldEqual, "https://example.com")
})
})
}
// getPluginProxiedRequest is a helper for easier setup of tests based on global config and ReqContext.
@ -140,10 +167,9 @@ func getPluginProxiedRequest(ctx *models.ReqContext, cfg *setting.Cfg, route *pl
ReqRole: models.ROLE_EDITOR,
}
}
proxy, err := NewApiPluginProxy(ctx, "", route, "", cfg)
So(err, ShouldBeNil)
proxy := NewApiPluginProxy(ctx, "", route, "", cfg)
req, err := http.NewRequest(http.MethodGet, route.URL, nil)
req, err := http.NewRequest(http.MethodGet, "/api/plugin-proxy/grafana-simple-app/api/v4/alerts", nil)
So(err, ShouldBeNil)
proxy.Director(req)
return req