Skip to content

Commit

Permalink
Fixed When the MINIO_BROWSER_REDIRECT_URL parameter contains path, th…
Browse files Browse the repository at this point in the history
…e console cannot be accessed normally.
  • Loading branch information
polaris-phecda committed May 22, 2023
1 parent 629dd66 commit 55e2884
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
4 changes: 4 additions & 0 deletions cmd/console/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions portal-ui/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T = any, E = any>({
body,
secure,
Expand Down
19 changes: 10 additions & 9 deletions restapi/configure_console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -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)

Expand All @@ -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
}
Expand Down Expand Up @@ -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, ""))
})
Expand Down
8 changes: 4 additions & 4 deletions restapi/configure_console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -104,22 +104,22 @@ 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 {
t.Run(tt.name, func(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()")
})
}
}
4 changes: 2 additions & 2 deletions restapi/ws_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var upgrader = websocket.Upgrader{

const (
// websocket base path
wsBasePath = "/ws"
wsBasePath = "ws"
)

// ConsoleWebsocketAdmin interface of a Websocket Client
Expand Down Expand Up @@ -139,7 +139,7 @@ func (c wsConn) readMessage() (messageType int, p []byte, err error) {
// Request should come like ws://<host>:<port>/ws/<api>
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)
Expand Down

0 comments on commit 55e2884

Please sign in to comment.