mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Preserve trailing slash in plugin proxy (#86859)
* Plugins: Preserve trailing slash in plugin proxy * enable toggle by default
This commit is contained in:
committed by
GitHub
parent
17f03882d4
commit
fe3e5917f1
@@ -104,6 +104,10 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
|
||||
URL: "http://www.test.com",
|
||||
Body: []byte(`{ "url": "{{.JsonData.dynamicUrl}}", "secret": "{{.SecureJsonData.key}}" }`),
|
||||
},
|
||||
{
|
||||
Path: "mypath",
|
||||
URL: "https://example.com/api/v1/",
|
||||
},
|
||||
}
|
||||
|
||||
ds := &datasources.DataSource{
|
||||
@@ -209,6 +213,16 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
|
||||
require.Equal(t, `{ "url": "https://dynamic.grafana.com", "secret": "123" }`, string(content))
|
||||
})
|
||||
|
||||
t.Run("When matching route path ending with a slash", func(t *testing.T) {
|
||||
ctx, req := setUp()
|
||||
proxy, err := setupDSProxyTest(t, ctx, ds, routes, "mypath/some-route/")
|
||||
require.NoError(t, err)
|
||||
proxy.matchedRoute = routes[6]
|
||||
ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.matchedRoute, dsInfo, proxy.cfg)
|
||||
|
||||
assert.Equal(t, "https://example.com/api/v1/some-route/", req.URL.String())
|
||||
})
|
||||
|
||||
t.Run("Validating request", func(t *testing.T) {
|
||||
t.Run("plugin route with valid role", func(t *testing.T) {
|
||||
ctx, _ := setUp()
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
|
||||
@@ -76,7 +77,12 @@ func (proxy *PluginProxy) HandleRequest() {
|
||||
}
|
||||
|
||||
if path, exists := params["*"]; exists {
|
||||
hasSlash := strings.HasSuffix(proxy.proxyPath, "/")
|
||||
proxy.proxyPath = path
|
||||
|
||||
if hasSlash && !strings.HasSuffix(path, "/") && proxy.features.IsEnabled(proxy.ctx.Req.Context(), featuremgmt.FlagPluginProxyPreserveTrailingSlash) {
|
||||
proxy.proxyPath += "/"
|
||||
}
|
||||
} else {
|
||||
proxy.proxyPath = ""
|
||||
}
|
||||
|
||||
@@ -316,10 +316,16 @@ func TestPluginProxyRoutes(t *testing.T) {
|
||||
Method: "GET",
|
||||
URL: "http://localhost/api/v2/instances",
|
||||
},
|
||||
{
|
||||
Path: "/mypath/*",
|
||||
Method: "GET",
|
||||
URL: "https://example.com/api/v1/",
|
||||
},
|
||||
}
|
||||
|
||||
tcs := []struct {
|
||||
proxyPath string
|
||||
withFeatures []any
|
||||
expectedURLPath string
|
||||
expectedStatus int
|
||||
}{
|
||||
@@ -362,6 +368,17 @@ func TestPluginProxyRoutes(t *testing.T) {
|
||||
expectedURLPath: "/api/v2/instances/instance-one",
|
||||
expectedStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
proxyPath: "/mypath/some-route/",
|
||||
expectedURLPath: "/api/v1/some-route",
|
||||
expectedStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
proxyPath: "/mypath/some-route/",
|
||||
withFeatures: []any{featuremgmt.FlagPluginProxyPreserveTrailingSlash},
|
||||
expectedURLPath: "/api/v1/some-route/",
|
||||
expectedStatus: http.StatusOK,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tcs {
|
||||
@@ -404,7 +421,7 @@ func TestPluginProxyRoutes(t *testing.T) {
|
||||
SecureJSONData: map[string][]byte{},
|
||||
}
|
||||
cfg := &setting.Cfg{}
|
||||
proxy, err := NewPluginProxy(ps, testRoutes, ctx, tc.proxyPath, cfg, secretsService, tracing.InitializeTracerForTest(), &http.Transport{}, acimpl.ProvideAccessControl(featuremgmt.WithFeatures()), featuremgmt.WithFeatures())
|
||||
proxy, err := NewPluginProxy(ps, testRoutes, ctx, tc.proxyPath, cfg, secretsService, tracing.InitializeTracerForTest(), &http.Transport{}, acimpl.ProvideAccessControl(featuremgmt.WithFeatures()), featuremgmt.WithFeatures(tc.withFeatures...))
|
||||
require.NoError(t, err)
|
||||
proxy.HandleRequest()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user