fix: handle redirects for specific resources (#12629)

This commit is contained in:
Harshavardhana 2021-07-07 12:04:16 -07:00 committed by GitHub
parent 25f55d6051
commit c99d399d09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 47 deletions

View File

@ -114,13 +114,16 @@ func minioConfigToConsoleFeatures() {
os.Setenv("CONSOLE_MINIO_SERVER", getAPIEndpoints()[0])
if value := env.Get("MINIO_LOG_QUERY_URL", ""); value != "" {
os.Setenv("CONSOLE_LOG_QUERY_URL", value)
}
if value := env.Get("MINIO_LOG_QUERY_AUTH_TOKEN", ""); value != "" {
os.Setenv("CONSOLE_LOG_QUERY_AUTH_TOKEN", value)
if value := env.Get("MINIO_LOG_QUERY_AUTH_TOKEN", ""); value != "" {
os.Setenv("CONSOLE_LOG_QUERY_AUTH_TOKEN", value)
}
}
// Enable if prometheus URL is set.
if value := env.Get("MINIO_PROMETHEUS_URL", ""); value != "" {
os.Setenv("CONSOLE_PROMETHEUS_URL", value)
if value := env.Get("MINIO_PROMETHEUS_JOB_ID", "minio-job"); value != "" {
os.Setenv("CONSOLE_PROMETHEUS_JOB_ID", value)
}
}
// Enable if LDAP is enabled.
if globalLDAPConfig.Enabled {
@ -413,11 +416,6 @@ func handleCommonEnvVars() {
logger.Fatal(config.ErrInvalidBrowserValue(err), "Invalid MINIO_BROWSER value in environment variable")
}
globalBrowserRedirect, err = config.ParseBool(env.Get(config.EnvBrowserRedirect, config.EnableOn))
if err != nil {
logger.Fatal(config.ErrInvalidBrowserValue(err), "Invalid MINIO_BROWSER_REDIRECT value in environment variable")
}
globalFSOSync, err = config.ParseBool(env.Get(config.EnvFSOSync, config.EnableOff))
if err != nil {
logger.Fatal(config.ErrInvalidFSOSyncValue(err), "Invalid MINIO_FS_OSYNC value in environment variable")

View File

@ -22,6 +22,7 @@ import (
"net"
"net/http"
"net/url"
"path"
"strings"
"sync/atomic"
"time"
@ -161,10 +162,9 @@ func guessIsBrowserReq(r *http.Request) bool {
func setBrowserRedirectHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Re-direction is handled specifically for browser requests.
if guessIsBrowserReq(r) && globalBrowserRedirect {
if guessIsBrowserReq(r) {
// Fetch the redirect location if any.
u := getRedirectLocation(r)
if u != nil {
if u := getRedirectLocation(r); u != nil {
// Employ a temporary re-direct.
http.Redirect(w, r, u.String(), http.StatusTemporaryRedirect)
return
@ -184,33 +184,36 @@ func shouldProxy() bool {
// serves only limited purpose on redirect-handler for
// browser requests.
func getRedirectLocation(r *http.Request) *url.URL {
hostname, _, _ := net.SplitHostPort(r.Host)
if hostname == "" {
hostname = r.Host
resource, err := getResource(r.URL.Path, r.Host, globalDomainNames)
if err != nil {
return nil
}
var rurl = &url.URL{
Host: net.JoinHostPort(hostname, globalMinioConsolePort),
for _, prefix := range []string{
"favicon-16x16.png",
"favicon-32x32.png",
"favicon-96x96.png",
"index.html",
minioReservedBucket,
} {
bucket, _ := path2BucketObject(resource)
if path.Clean(bucket) == prefix || resource == slashSeparator {
hostname, _, _ := net.SplitHostPort(r.Host)
if hostname == "" {
hostname = r.Host
}
return &url.URL{
Host: net.JoinHostPort(hostname, globalMinioConsolePort),
Scheme: func() string {
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
return scheme
}(),
}
}
}
urlPath := r.URL.Path
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
rurl.Scheme = scheme
if urlPath == minioReservedBucketPath {
rurl.Path = minioReservedBucketPath + SlashSeparator
}
if contains([]string{
SlashSeparator,
"/webrpc",
"/login",
"/favicon-16x16.png",
"/favicon-32x32.png",
"/favicon-96x96.png",
}, urlPath) {
rurl.Path = minioReservedBucketPath + urlPath
}
return rurl
return nil
}
// guessIsHealthCheckReq - returns true if incoming request looks

View File

@ -137,9 +137,6 @@ var (
// This flag is set to 'true' by default
globalBrowserEnabled = true
// This flag is set to 'true' by default.
globalBrowserRedirect = true
// This flag is set to 'true' when MINIO_UPDATE env is set to 'off'. Default is false.
globalInplaceUpdateDisabled = false

View File

@ -29,14 +29,13 @@ const (
EnvRootUser = "MINIO_ROOT_USER"
EnvRootPassword = "MINIO_ROOT_PASSWORD"
EnvBrowser = "MINIO_BROWSER"
EnvBrowserRedirect = "MINIO_BROWSER_REDIRECT"
EnvDomain = "MINIO_DOMAIN"
EnvRegionName = "MINIO_REGION_NAME"
EnvPublicIPs = "MINIO_PUBLIC_IPS"
EnvFSOSync = "MINIO_FS_OSYNC"
EnvArgs = "MINIO_ARGS"
EnvDNSWebhook = "MINIO_DNS_WEBHOOK_ENDPOINT"
EnvBrowser = "MINIO_BROWSER"
EnvDomain = "MINIO_DOMAIN"
EnvRegionName = "MINIO_REGION_NAME"
EnvPublicIPs = "MINIO_PUBLIC_IPS"
EnvFSOSync = "MINIO_FS_OSYNC"
EnvArgs = "MINIO_ARGS"
EnvDNSWebhook = "MINIO_DNS_WEBHOOK_ENDPOINT"
EnvRootDiskThresholdSize = "MINIO_ROOTDISK_THRESHOLD_SIZE"