Skip to content

Commit 55e2884

Browse files
Fixed When the MINIO_BROWSER_REDIRECT_URL parameter contains path, the console cannot be accessed normally.
1 parent 629dd66 commit 55e2884

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

cmd/console/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ func buildServer() (*restapi.Server, error) {
9898
return nil, err
9999
}
100100

101+
subPath := restapi.GetSubPath()
102+
swaggerSpec.Spec().BasePath = subPath + swaggerSpec.Spec().BasePath
103+
swaggerSpec.OrigSpec().BasePath = subPath + swaggerSpec.OrigSpec().BasePath
104+
101105
api := operations.NewConsoleAPI(swaggerSpec)
102106
api.Logger = restapi.LogInfo
103107
server := restapi.NewServer(api)

portal-ui/src/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Api, HttpResponse, Error, FullRequestParams } from "./consoleApi";
22

33
export let api = new Api();
44
const internalRequestFunc = api.request;
5+
api.baseUrl = `${new URL(document.baseURI).pathname}api/v1`;
56
api.request = async <T = any, E = any>({
67
body,
78
secure,

restapi/configure_console.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,18 @@ func AuthenticationMiddleware(next http.Handler) http.Handler {
318318
func FileServerMiddleware(next http.Handler) http.Handler {
319319
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
320320
w.Header().Set("Server", globalAppName) // do not add version information
321+
basePath := GetSubPath()
321322
switch {
322-
case strings.HasPrefix(r.URL.Path, "/ws"):
323+
case strings.HasPrefix(r.URL.Path, basePath+"ws"):
323324
serveWS(w, r)
324-
case strings.HasPrefix(r.URL.Path, "/api"):
325+
case strings.HasPrefix(r.URL.Path, basePath+"api"):
325326
next.ServeHTTP(w, r)
326327
default:
327328
buildFs, err := fs.Sub(portal_ui.GetStaticAssets(), "build")
328329
if err != nil {
329330
panic(err)
330331
}
331-
wrapHandlerSinglePageApplication(requestBounce(http.FileServer(http.FS(buildFs)))).ServeHTTP(w, r)
332+
wrapHandlerSinglePageApplication(requestBounce(http.StripPrefix(basePath, http.FileServer(http.FS(buildFs))))).ServeHTTP(w, r)
332333
}
333334
})
334335
}
@@ -354,7 +355,7 @@ func (w *notFoundRedirectRespWr) Write(p []byte) (int, error) {
354355

355356
// handleSPA handles the serving of the React Single Page Application
356357
func handleSPA(w http.ResponseWriter, r *http.Request) {
357-
basePath := "/"
358+
basePath := GetSubPath()
358359
// For SPA mode we will replace root base with a sub path if configured unless we received cp=y and cpb=/NEW/BASE
359360
if v := r.URL.Query().Get("cp"); v == "y" {
360361
if base := r.URL.Query().Get("cpb"); base != "" {
@@ -419,11 +420,11 @@ func handleSPA(w http.ResponseWriter, r *http.Request) {
419420
}
420421

421422
// if we have a seeded basePath. This should override CONSOLE_SUBPATH every time, thus the `if else`
422-
if basePath != "/" {
423+
if basePath != GetSubPath() {
423424
indexPageBytes = replaceBaseInIndex(indexPageBytes, basePath)
424425
// if we have a custom subpath replace it in
425-
} else if getSubPath() != "/" {
426-
indexPageBytes = replaceBaseInIndex(indexPageBytes, getSubPath())
426+
} else if GetSubPath() != "/" {
427+
indexPageBytes = replaceBaseInIndex(indexPageBytes, GetSubPath())
427428
}
428429
indexPageBytes = replaceLicense(indexPageBytes)
429430

@@ -440,7 +441,7 @@ func handleSPA(w http.ResponseWriter, r *http.Request) {
440441
// wrapHandlerSinglePageApplication handles a http.FileServer returning a 404 and overrides it with index.html
441442
func wrapHandlerSinglePageApplication(h http.Handler) http.HandlerFunc {
442443
return func(w http.ResponseWriter, r *http.Request) {
443-
if r.URL.Path == "/" {
444+
if match, _ := regexp.MatchString(fmt.Sprintf("^%s/?$", GetSubPath()), r.URL.Path); match {
444445
handleSPA(w, r)
445446
return
446447
}
@@ -469,7 +470,7 @@ func configureServer(s *http.Server, _, _ string) {
469470
s.ErrorLog = log.New(&nullWriter{}, "", 0)
470471
}
471472

472-
func getSubPath() string {
473+
func GetSubPath() string {
473474
subPathOnce.Do(func() {
474475
subPath = parseSubPath(env.Get(SubPath, ""))
475476
})

restapi/configure_console_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func Test_parseSubPath(t *testing.T) {
7676
}
7777
}
7878

79-
func Test_getSubPath(t *testing.T) {
79+
func Test_GetSubPath(t *testing.T) {
8080
type args struct {
8181
envValue string
8282
}
@@ -104,22 +104,22 @@ func Test_getSubPath(t *testing.T) {
104104
args: args{
105105
envValue: "/subpath/",
106106
},
107-
want: "/subpath/",
107+
want: "/subpath",
108108
},
109109
{
110110
name: "No starting slash",
111111
args: args{
112112
envValue: "subpath/",
113113
},
114-
want: "/subpath/",
114+
want: "/subpath",
115115
},
116116
}
117117
for _, tt := range tests {
118118
t.Run(tt.name, func(t *testing.T) {
119119
t.Setenv(SubPath, tt.args.envValue)
120120
defer os.Unsetenv(SubPath)
121121
subPathOnce = sync.Once{}
122-
assert.Equalf(t, tt.want, getSubPath(), "getSubPath()")
122+
assert.Equalf(t, tt.want, GetSubPath(), "GetSubPath()")
123123
})
124124
}
125125
}

restapi/ws_handle.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var upgrader = websocket.Upgrader{
4343

4444
const (
4545
// websocket base path
46-
wsBasePath = "/ws"
46+
wsBasePath = "ws"
4747
)
4848

4949
// ConsoleWebsocketAdmin interface of a Websocket Client
@@ -139,7 +139,7 @@ func (c wsConn) readMessage() (messageType int, p []byte, err error) {
139139
// Request should come like ws://<host>:<port>/ws/<api>
140140
func serveWS(w http.ResponseWriter, req *http.Request) {
141141
ctx := req.Context()
142-
wsPath := strings.TrimPrefix(req.URL.Path, wsBasePath)
142+
wsPath := strings.TrimPrefix(req.URL.Path, GetSubPath()+wsBasePath)
143143
// Perform authentication before upgrading to a Websocket Connection
144144
// authenticate WS connection with Console
145145
session, err := auth.GetClaimsFromTokenInRequest(req)

0 commit comments

Comments
 (0)