DS apiserver: Fix resource path (#85494)

pull/85506/head
Marcus Efraimsson 1 year ago committed by GitHub
parent b47f8b429e
commit c9ab4e3a9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 35
      pkg/registry/apis/datasource/sub_resource.go
  2. 71
      pkg/registry/apis/datasource/sub_resource_test.go

@ -55,31 +55,23 @@ func (r *subResourceREST) Connect(ctx context.Context, name string, opts runtime
ctx = contextualMiddlewares(ctx)
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
body, err := io.ReadAll(req.Body)
clonedReq, err := resourceRequest(req)
if err != nil {
responder.Error(err)
return
}
idx := strings.LastIndex(req.URL.Path, "/resource")
if idx < 0 {
responder.Error(fmt.Errorf("expected resource path")) // 400?
body, err := io.ReadAll(req.Body)
if err != nil {
responder.Error(err)
return
}
clonedReq := req.Clone(req.Context())
rawURL := req.URL.Path[idx+len("/resource"):]
clonedReq.URL = &url.URL{
Path: rawURL,
RawQuery: clonedReq.URL.RawQuery,
}
err = r.builder.client.CallResource(ctx, &backend.CallResourceRequest{
PluginContext: pluginCtx,
Path: clonedReq.URL.Path,
Method: req.Method,
URL: req.URL.String(),
URL: clonedReq.URL.String(),
Body: body,
Headers: req.Header,
}, httpresponsesender.New(w))
@ -89,3 +81,20 @@ func (r *subResourceREST) Connect(ctx context.Context, name string, opts runtime
}
}), nil
}
func resourceRequest(req *http.Request) (*http.Request, error) {
idx := strings.LastIndex(req.URL.Path, "/resource")
if idx < 0 {
return nil, fmt.Errorf("expected resource path") // 400?
}
clonedReq := req.Clone(req.Context())
rawURL := strings.TrimLeft(req.URL.Path[idx+len("/resource"):], "/")
clonedReq.URL = &url.URL{
Path: rawURL,
RawQuery: clonedReq.URL.RawQuery,
}
return clonedReq, nil
}

@ -0,0 +1,71 @@
package datasource
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestResourceRequest(t *testing.T) {
testCases := []struct {
desc string
url string
error bool
expectedPath string
expectedURL string
}{
{
desc: "no resource path",
url: "http://localhost:6443/apis/test.datasource.grafana.app/v0alpha1/namespaces/default/connections/abc",
error: true,
},
{
desc: "root resource path",
url: "http://localhost:6443/apis/test.datasource.grafana.app/v0alpha1/namespaces/default/connections/abc/resource",
expectedPath: "",
expectedURL: "",
},
{
desc: "root resource path",
url: "http://localhost:6443/apis/test.datasource.grafana.app/v0alpha1/namespaces/default/connections/abc/resource/",
expectedPath: "",
expectedURL: "",
},
{
desc: "resource sub path",
url: "http://localhost:6443/apis/test.datasource.grafana.app/v0alpha1/namespaces/default/connections/abc/resource/test",
expectedPath: "test",
expectedURL: "test",
},
{
desc: "resource sub path with colon",
url: "http://localhost:6443/apis/test.datasource.grafana.app/v0alpha1/namespaces/default/connections/abc/resource/test-*,*:test-*/_mapping",
expectedPath: "test-*,*:test-*/_mapping",
expectedURL: "./test-%2A,%2A:test-%2A/_mapping",
},
{
desc: "resource sub path with query params",
url: "http://localhost:6443/apis/test.datasource.grafana.app/v0alpha1/namespaces/default/connections/abc/resource/test?k1=v1&k2=v2",
expectedPath: "test",
expectedURL: "test?k1=v1&k2=v2",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tc.url, nil)
clonedReq, err := resourceRequest(req)
if tc.error {
require.Error(t, err)
require.Nil(t, clonedReq)
} else {
require.NoError(t, err)
require.NotNil(t, clonedReq)
require.Equal(t, tc.expectedPath, clonedReq.URL.Path)
require.Equal(t, tc.expectedURL, clonedReq.URL.String())
}
})
}
}
Loading…
Cancel
Save