forked from TrueCloudLab/frostfs-s3-gw
[#493] Fix X-Frostfs-S3-VHS header processing
It is assumed that the X-Frostfs-S3-VHS header will have the value enabled/disabled instead of true/false. Signed-off-by: Roman Loginov <r.loginov@yadro.com>
This commit is contained in:
parent
26baf8a94e
commit
a87c636b4c
2 changed files with 33 additions and 26 deletions
|
@ -3,14 +3,18 @@ package middleware
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
const wildcardPlaceholder = "<wildcard>"
|
const (
|
||||||
|
wildcardPlaceholder = "<wildcard>"
|
||||||
|
|
||||||
|
enabledVHS = "enabled"
|
||||||
|
disabledVHS = "disabled"
|
||||||
|
)
|
||||||
|
|
||||||
type VHSSettings interface {
|
type VHSSettings interface {
|
||||||
Domains() []string
|
Domains() []string
|
||||||
|
@ -26,9 +30,9 @@ func PrepareAddressStyle(settings VHSSettings, log *zap.Logger) Func {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
reqInfo := GetReqInfo(ctx)
|
reqInfo := GetReqInfo(ctx)
|
||||||
reqLogger := reqLogOrDefault(ctx, log)
|
reqLogger := reqLogOrDefault(ctx, log)
|
||||||
headerVHSEnabled := r.Header.Get(settings.VHSHeader())
|
statusVHS := r.Header.Get(settings.VHSHeader())
|
||||||
|
|
||||||
if isVHSAddress(headerVHSEnabled, settings.GlobalVHS(), settings.VHSNamespacesEnabled(), reqInfo.Namespace) {
|
if isVHSAddress(statusVHS, settings.GlobalVHS(), settings.VHSNamespacesEnabled(), reqInfo.Namespace) {
|
||||||
prepareVHSAddress(reqInfo, r, settings)
|
prepareVHSAddress(reqInfo, r, settings)
|
||||||
} else {
|
} else {
|
||||||
preparePathStyleAddress(reqInfo, r, reqLogger)
|
preparePathStyleAddress(reqInfo, r, reqLogger)
|
||||||
|
@ -39,17 +43,20 @@ func PrepareAddressStyle(settings VHSSettings, log *zap.Logger) Func {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isVHSAddress(headerVHSEnabled string, enabledFlag bool, vhsNamespaces map[string]bool, namespace string) bool {
|
func isVHSAddress(statusVHS string, enabledFlag bool, vhsNamespaces map[string]bool, namespace string) bool {
|
||||||
if result, err := strconv.ParseBool(headerVHSEnabled); err == nil {
|
switch statusVHS {
|
||||||
|
case enabledVHS:
|
||||||
|
return true
|
||||||
|
case disabledVHS:
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
result := enabledFlag
|
||||||
|
if v, ok := vhsNamespaces[namespace]; ok {
|
||||||
|
result = v
|
||||||
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
result := enabledFlag
|
|
||||||
if v, ok := vhsNamespaces[namespace]; ok {
|
|
||||||
result = v
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareVHSAddress(reqInfo *ReqInfo, r *http.Request, settings VHSSettings) {
|
func prepareVHSAddress(reqInfo *ReqInfo, r *http.Request, settings VHSSettings) {
|
||||||
|
|
|
@ -41,12 +41,12 @@ func (v *VHSSettingsMock) VHSNamespacesEnabled() map[string]bool {
|
||||||
|
|
||||||
func TestIsVHSAddress(t *testing.T) {
|
func TestIsVHSAddress(t *testing.T) {
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
headerVHSEnabled string
|
headerStatusVHS string
|
||||||
vhsEnabledFlag bool
|
vhsEnabledFlag bool
|
||||||
vhsNamespaced map[string]bool
|
vhsNamespaced map[string]bool
|
||||||
namespace string
|
namespace string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "vhs disabled",
|
name: "vhs disabled",
|
||||||
|
@ -75,9 +75,9 @@ func TestIsVHSAddress(t *testing.T) {
|
||||||
expected: true,
|
expected: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "vhs enabled (header)",
|
name: "vhs enabled (header)",
|
||||||
headerVHSEnabled: "true",
|
headerStatusVHS: enabledVHS,
|
||||||
vhsEnabledFlag: false,
|
vhsEnabledFlag: false,
|
||||||
vhsNamespaced: map[string]bool{
|
vhsNamespaced: map[string]bool{
|
||||||
"kapusta": false,
|
"kapusta": false,
|
||||||
},
|
},
|
||||||
|
@ -85,9 +85,9 @@ func TestIsVHSAddress(t *testing.T) {
|
||||||
expected: true,
|
expected: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "vhs disabled (header)",
|
name: "vhs disabled (header)",
|
||||||
headerVHSEnabled: "false",
|
headerStatusVHS: disabledVHS,
|
||||||
vhsEnabledFlag: true,
|
vhsEnabledFlag: true,
|
||||||
vhsNamespaced: map[string]bool{
|
vhsNamespaced: map[string]bool{
|
||||||
"kapusta": true,
|
"kapusta": true,
|
||||||
},
|
},
|
||||||
|
@ -96,7 +96,7 @@ func TestIsVHSAddress(t *testing.T) {
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
actual := isVHSAddress(tc.headerVHSEnabled, tc.vhsEnabledFlag, tc.vhsNamespaced, tc.namespace)
|
actual := isVHSAddress(tc.headerStatusVHS, tc.vhsEnabledFlag, tc.vhsNamespaced, tc.namespace)
|
||||||
require.Equal(t, tc.expected, actual)
|
require.Equal(t, tc.expected, actual)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue