2024-07-31 06:45:46 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-09-16 10:30:33 +00:00
|
|
|
const (
|
|
|
|
wildcardPlaceholder = "<wildcard>"
|
|
|
|
|
|
|
|
enabledVHS = "enabled"
|
|
|
|
disabledVHS = "disabled"
|
|
|
|
)
|
2024-07-31 06:45:46 +00:00
|
|
|
|
|
|
|
type VHSSettings interface {
|
|
|
|
Domains() []string
|
|
|
|
GlobalVHS() bool
|
2024-08-01 13:24:47 +00:00
|
|
|
VHSHeader() string
|
|
|
|
ServernameHeader() string
|
2024-07-31 06:45:46 +00:00
|
|
|
VHSNamespacesEnabled() map[string]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrepareAddressStyle(settings VHSSettings, log *zap.Logger) Func {
|
|
|
|
return func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
reqInfo := GetReqInfo(ctx)
|
|
|
|
reqLogger := reqLogOrDefault(ctx, log)
|
2024-09-16 10:30:33 +00:00
|
|
|
statusVHS := r.Header.Get(settings.VHSHeader())
|
2024-07-31 06:45:46 +00:00
|
|
|
|
2024-09-16 10:30:33 +00:00
|
|
|
if isVHSAddress(statusVHS, settings.GlobalVHS(), settings.VHSNamespacesEnabled(), reqInfo.Namespace) {
|
2024-07-31 06:45:46 +00:00
|
|
|
prepareVHSAddress(reqInfo, r, settings)
|
|
|
|
} else {
|
|
|
|
preparePathStyleAddress(reqInfo, r, reqLogger)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 10:30:33 +00:00
|
|
|
func isVHSAddress(statusVHS string, enabledFlag bool, vhsNamespaces map[string]bool, namespace string) bool {
|
|
|
|
switch statusVHS {
|
|
|
|
case enabledVHS:
|
|
|
|
return true
|
|
|
|
case disabledVHS:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
result := enabledFlag
|
|
|
|
if v, ok := vhsNamespaces[namespace]; ok {
|
|
|
|
result = v
|
|
|
|
}
|
2024-07-31 06:45:46 +00:00
|
|
|
|
2024-09-16 10:30:33 +00:00
|
|
|
return result
|
2024-07-31 06:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareVHSAddress(reqInfo *ReqInfo, r *http.Request, settings VHSSettings) {
|
|
|
|
reqInfo.RequestVHSEnabled = true
|
2024-08-01 13:24:47 +00:00
|
|
|
bktName, match := checkDomain(r.Host, getDomains(r, settings))
|
2024-07-31 06:45:46 +00:00
|
|
|
if match {
|
|
|
|
if bktName == "" {
|
|
|
|
reqInfo.RequestType = noneType
|
|
|
|
} else {
|
|
|
|
if objName := strings.TrimPrefix(r.URL.Path, "/"); objName != "" {
|
|
|
|
reqInfo.RequestType = objectType
|
|
|
|
reqInfo.ObjectName = objName
|
|
|
|
reqInfo.BucketName = bktName
|
|
|
|
} else {
|
|
|
|
reqInfo.RequestType = bucketType
|
|
|
|
reqInfo.BucketName = bktName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
parts := strings.Split(r.Host, ".")
|
|
|
|
reqInfo.BucketName = parts[0]
|
|
|
|
|
|
|
|
if objName := strings.TrimPrefix(r.URL.Path, "/"); objName != "" {
|
|
|
|
reqInfo.RequestType = objectType
|
|
|
|
reqInfo.ObjectName = objName
|
|
|
|
} else {
|
|
|
|
reqInfo.RequestType = bucketType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 13:24:47 +00:00
|
|
|
func getDomains(r *http.Request, settings VHSSettings) []string {
|
|
|
|
if headerServername := r.Header.Get(settings.ServernameHeader()); headerServername != "" {
|
|
|
|
return []string{headerServername}
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings.Domains()
|
|
|
|
}
|
|
|
|
|
2024-07-31 06:45:46 +00:00
|
|
|
func preparePathStyleAddress(reqInfo *ReqInfo, r *http.Request, reqLogger *zap.Logger) {
|
|
|
|
bktObj := strings.TrimPrefix(r.URL.Path, "/")
|
|
|
|
if bktObj == "" {
|
|
|
|
reqInfo.RequestType = noneType
|
|
|
|
} else if ind := strings.IndexByte(bktObj, '/'); ind != -1 && bktObj[ind+1:] != "" {
|
|
|
|
reqInfo.RequestType = objectType
|
|
|
|
reqInfo.BucketName = bktObj[:ind]
|
|
|
|
reqInfo.ObjectName = bktObj[ind+1:]
|
|
|
|
|
|
|
|
if r.URL.RawPath != "" {
|
|
|
|
// we have to do this because of
|
|
|
|
// https://github.com/go-chi/chi/issues/641
|
|
|
|
// https://github.com/go-chi/chi/issues/642
|
|
|
|
if obj, err := url.PathUnescape(reqInfo.ObjectName); err != nil {
|
|
|
|
reqLogger.Warn(logs.FailedToUnescapeObjectName, zap.Error(err))
|
|
|
|
} else {
|
|
|
|
reqInfo.ObjectName = obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reqInfo.RequestType = bucketType
|
|
|
|
reqInfo.BucketName = strings.TrimSuffix(bktObj, "/")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDomain(host string, domains []string) (bktName string, match bool) {
|
|
|
|
partsHost := strings.Split(host, ".")
|
|
|
|
for _, pattern := range domains {
|
|
|
|
partsPattern := strings.Split(pattern, ".")
|
|
|
|
bktName, match = compareMatch(partsHost, partsPattern)
|
|
|
|
if match {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareMatch(host, pattern []string) (bktName string, match bool) {
|
|
|
|
if len(host) < len(pattern) {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
i, j := len(host)-1, len(pattern)-1
|
|
|
|
for j >= 0 && (pattern[j] == wildcardPlaceholder || host[i] == pattern[j]) {
|
|
|
|
i--
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case i == -1:
|
|
|
|
return "", true
|
|
|
|
case i == 0 && (j != 0 || host[i] == pattern[j]):
|
|
|
|
return host[0], true
|
|
|
|
default:
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
}
|