From 55e288481847fde47a75a5db68e1af11b94f085c Mon Sep 17 00:00:00 2001 From: houshuai Date: Mon, 20 Mar 2023 19:46:31 +0800 Subject: [PATCH 1/2] Fixed When the MINIO_BROWSER_REDIRECT_URL parameter contains path, the console cannot be accessed normally. --- cmd/console/server.go | 4 ++++ portal-ui/src/api/index.ts | 1 + restapi/configure_console.go | 19 ++++++++++--------- restapi/configure_console_test.go | 8 ++++---- restapi/ws_handle.go | 4 ++-- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/cmd/console/server.go b/cmd/console/server.go index 92ebbf7324..10ae60520e 100644 --- a/cmd/console/server.go +++ b/cmd/console/server.go @@ -98,6 +98,10 @@ func buildServer() (*restapi.Server, error) { return nil, err } + subPath := restapi.GetSubPath() + swaggerSpec.Spec().BasePath = subPath + swaggerSpec.Spec().BasePath + swaggerSpec.OrigSpec().BasePath = subPath + swaggerSpec.OrigSpec().BasePath + api := operations.NewConsoleAPI(swaggerSpec) api.Logger = restapi.LogInfo server := restapi.NewServer(api) diff --git a/portal-ui/src/api/index.ts b/portal-ui/src/api/index.ts index 992f56f8ec..27e6373770 100644 --- a/portal-ui/src/api/index.ts +++ b/portal-ui/src/api/index.ts @@ -2,6 +2,7 @@ import { Api, HttpResponse, Error, FullRequestParams } from "./consoleApi"; export let api = new Api(); const internalRequestFunc = api.request; +api.baseUrl = `${new URL(document.baseURI).pathname}api/v1`; api.request = async ({ body, secure, diff --git a/restapi/configure_console.go b/restapi/configure_console.go index 27a6dd9840..62bc252c8d 100644 --- a/restapi/configure_console.go +++ b/restapi/configure_console.go @@ -318,17 +318,18 @@ func AuthenticationMiddleware(next http.Handler) http.Handler { func FileServerMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Server", globalAppName) // do not add version information + basePath := GetSubPath() switch { - case strings.HasPrefix(r.URL.Path, "/ws"): + case strings.HasPrefix(r.URL.Path, basePath+"ws"): serveWS(w, r) - case strings.HasPrefix(r.URL.Path, "/api"): + case strings.HasPrefix(r.URL.Path, basePath+"api"): next.ServeHTTP(w, r) default: buildFs, err := fs.Sub(portal_ui.GetStaticAssets(), "build") if err != nil { panic(err) } - wrapHandlerSinglePageApplication(requestBounce(http.FileServer(http.FS(buildFs)))).ServeHTTP(w, r) + wrapHandlerSinglePageApplication(requestBounce(http.StripPrefix(basePath, http.FileServer(http.FS(buildFs))))).ServeHTTP(w, r) } }) } @@ -354,7 +355,7 @@ func (w *notFoundRedirectRespWr) Write(p []byte) (int, error) { // handleSPA handles the serving of the React Single Page Application func handleSPA(w http.ResponseWriter, r *http.Request) { - basePath := "/" + basePath := GetSubPath() // For SPA mode we will replace root base with a sub path if configured unless we received cp=y and cpb=/NEW/BASE if v := r.URL.Query().Get("cp"); v == "y" { if base := r.URL.Query().Get("cpb"); base != "" { @@ -419,11 +420,11 @@ func handleSPA(w http.ResponseWriter, r *http.Request) { } // if we have a seeded basePath. This should override CONSOLE_SUBPATH every time, thus the `if else` - if basePath != "/" { + if basePath != GetSubPath() { indexPageBytes = replaceBaseInIndex(indexPageBytes, basePath) // if we have a custom subpath replace it in - } else if getSubPath() != "/" { - indexPageBytes = replaceBaseInIndex(indexPageBytes, getSubPath()) + } else if GetSubPath() != "/" { + indexPageBytes = replaceBaseInIndex(indexPageBytes, GetSubPath()) } indexPageBytes = replaceLicense(indexPageBytes) @@ -440,7 +441,7 @@ func handleSPA(w http.ResponseWriter, r *http.Request) { // wrapHandlerSinglePageApplication handles a http.FileServer returning a 404 and overrides it with index.html func wrapHandlerSinglePageApplication(h http.Handler) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/" { + if match, _ := regexp.MatchString(fmt.Sprintf("^%s/?$", GetSubPath()), r.URL.Path); match { handleSPA(w, r) return } @@ -469,7 +470,7 @@ func configureServer(s *http.Server, _, _ string) { s.ErrorLog = log.New(&nullWriter{}, "", 0) } -func getSubPath() string { +func GetSubPath() string { subPathOnce.Do(func() { subPath = parseSubPath(env.Get(SubPath, "")) }) diff --git a/restapi/configure_console_test.go b/restapi/configure_console_test.go index 07f897d2ca..cfdf7cc30d 100644 --- a/restapi/configure_console_test.go +++ b/restapi/configure_console_test.go @@ -76,7 +76,7 @@ func Test_parseSubPath(t *testing.T) { } } -func Test_getSubPath(t *testing.T) { +func Test_GetSubPath(t *testing.T) { type args struct { envValue string } @@ -104,14 +104,14 @@ func Test_getSubPath(t *testing.T) { args: args{ envValue: "/subpath/", }, - want: "/subpath/", + want: "/subpath", }, { name: "No starting slash", args: args{ envValue: "subpath/", }, - want: "/subpath/", + want: "/subpath", }, } for _, tt := range tests { @@ -119,7 +119,7 @@ func Test_getSubPath(t *testing.T) { t.Setenv(SubPath, tt.args.envValue) defer os.Unsetenv(SubPath) subPathOnce = sync.Once{} - assert.Equalf(t, tt.want, getSubPath(), "getSubPath()") + assert.Equalf(t, tt.want, GetSubPath(), "GetSubPath()") }) } } diff --git a/restapi/ws_handle.go b/restapi/ws_handle.go index da2985b47b..cefc557e74 100644 --- a/restapi/ws_handle.go +++ b/restapi/ws_handle.go @@ -43,7 +43,7 @@ var upgrader = websocket.Upgrader{ const ( // websocket base path - wsBasePath = "/ws" + wsBasePath = "ws" ) // ConsoleWebsocketAdmin interface of a Websocket Client @@ -139,7 +139,7 @@ func (c wsConn) readMessage() (messageType int, p []byte, err error) { // Request should come like ws://:/ws/ func serveWS(w http.ResponseWriter, req *http.Request) { ctx := req.Context() - wsPath := strings.TrimPrefix(req.URL.Path, wsBasePath) + wsPath := strings.TrimPrefix(req.URL.Path, GetSubPath()+wsBasePath) // Perform authentication before upgrading to a Websocket Connection // authenticate WS connection with Console session, err := auth.GetClaimsFromTokenInRequest(req) From 0a2234d7b8e85569b94c9a85521751455852f57d Mon Sep 17 00:00:00 2001 From: polaris-phecda <92513748+polaris-phecda@users.noreply.github.com> Date: Sun, 25 Jun 2023 20:35:38 +0800 Subject: [PATCH 2/2] Update index.ts --- portal-ui/src/api/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/portal-ui/src/api/index.ts b/portal-ui/src/api/index.ts index 85b0e4d9b1..9232095f68 100644 --- a/portal-ui/src/api/index.ts +++ b/portal-ui/src/api/index.ts @@ -3,7 +3,6 @@ import { Api, HttpResponse, Error, FullRequestParams } from "./consoleApi"; export let api = new Api(); api.baseUrl = `${new URL(document.baseURI).pathname}api/v1`; const internalRequestFunc = api.request; -api.baseUrl = `${new URL(document.baseURI).pathname}api/v1`; api.request = async ({ body, secure,