Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/api unknown path return error 2190 #2382

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/api/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
extensionValidationExcludeBody = "x-validation-exclude-body"
)

var ErrInvalidAPIEndpoint = errors.New("invalid API endpoint")

type responseError struct {
Message string `json:"message"`
}
Expand Down Expand Up @@ -85,6 +87,7 @@ func Serve(
r.Mount("/_pprof/", httputil.ServePPROF("/_pprof/"))
r.Mount("/swagger.json", http.HandlerFunc(swaggerSpecHandler))
r.Mount("/", uiHandler)
r.Mount(BaseURL, http.HandlerFunc(InvalidAPIEndpointHandler))
return r
}

Expand Down Expand Up @@ -162,3 +165,10 @@ func validateRequest(r *http.Request, router routers.Router, options *openapi3fi
}
return http.StatusOK, nil
}

// InvalidAPIEndpointHandler returns ErrInvalidAPIEndpoint, and is currently being used to ensure
// that routes under the pattern it is used with in chi.Router.Mount (i.e. /api/v1) are
// not accessible.
func InvalidAPIEndpointHandler(w http.ResponseWriter, _ *http.Request) {
writeError(w, http.StatusInternalServerError, ErrInvalidAPIEndpoint)
}
31 changes: 31 additions & 0 deletions pkg/api/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,34 @@ func setupClientWithAdmin(t testing.TB, blockstoreType string, opts ...testutil.
clt = setupClientByEndpoint(t, server.URL, cred.AccessKeyID, cred.SecretAccessKey)
return clt, deps
}

func TestInvalidRoute(t *testing.T) {
handler, _ := setupHandler(t, "")
server := setupServer(t, handler)
clt := setupClientByEndpoint(t, server.URL, "", "")
cred := createDefaultAdminUser(t, clt)

// setup client with invalid endpoint base url
basicAuthProvider, err := securityprovider.NewSecurityProviderBasicAuth(cred.AccessKeyID, cred.SecretAccessKey)
if err != nil {
t.Fatal("basic auth security provider", err)
}
clt, err = api.NewClientWithResponses(server.URL+api.BaseURL+"//", api.WithRequestEditorFn(basicAuthProvider.Intercept))
if err != nil {
t.Fatal("failed to create api client:", err)
}

ctx := context.Background()
resp, err := clt.ListRepositoriesWithResponse(ctx, &api.ListRepositoriesParams{})
if err != nil {
t.Fatalf("failed to get lakefs server version")
}
if resp.JSONDefault == nil {
t.Fatalf("client api call expected default error, got nil")
}
expectedErrMsg := api.ErrInvalidAPIEndpoint.Error()
errMsg := resp.JSONDefault.Message
if errMsg != expectedErrMsg {
t.Fatalf("client response error message: %s, expected: %s", errMsg, expectedErrMsg)
}
}