diff --git a/api/middleware/address_style.go b/api/middleware/address_style.go index 12b1cdda..dd5efb43 100644 --- a/api/middleware/address_style.go +++ b/api/middleware/address_style.go @@ -3,14 +3,18 @@ package middleware import ( "net/http" "net/url" - "strconv" "strings" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs" "go.uber.org/zap" ) -const wildcardPlaceholder = "" +const ( + wildcardPlaceholder = "" + + enabledVHS = "enabled" + disabledVHS = "disabled" +) type VHSSettings interface { Domains() []string @@ -26,9 +30,9 @@ func PrepareAddressStyle(settings VHSSettings, log *zap.Logger) Func { ctx := r.Context() reqInfo := GetReqInfo(ctx) 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) } else { 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 { - if result, err := strconv.ParseBool(headerVHSEnabled); err == nil { +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 + } + return result } - - result := enabledFlag - if v, ok := vhsNamespaces[namespace]; ok { - result = v - } - - return result } func prepareVHSAddress(reqInfo *ReqInfo, r *http.Request, settings VHSSettings) { diff --git a/api/middleware/address_style_test.go b/api/middleware/address_style_test.go index 4089bfe3..09e2d542 100644 --- a/api/middleware/address_style_test.go +++ b/api/middleware/address_style_test.go @@ -41,12 +41,12 @@ func (v *VHSSettingsMock) VHSNamespacesEnabled() map[string]bool { func TestIsVHSAddress(t *testing.T) { for _, tc := range []struct { - name string - headerVHSEnabled string - vhsEnabledFlag bool - vhsNamespaced map[string]bool - namespace string - expected bool + name string + headerStatusVHS string + vhsEnabledFlag bool + vhsNamespaced map[string]bool + namespace string + expected bool }{ { name: "vhs disabled", @@ -75,9 +75,9 @@ func TestIsVHSAddress(t *testing.T) { expected: true, }, { - name: "vhs enabled (header)", - headerVHSEnabled: "true", - vhsEnabledFlag: false, + name: "vhs enabled (header)", + headerStatusVHS: enabledVHS, + vhsEnabledFlag: false, vhsNamespaced: map[string]bool{ "kapusta": false, }, @@ -85,9 +85,9 @@ func TestIsVHSAddress(t *testing.T) { expected: true, }, { - name: "vhs disabled (header)", - headerVHSEnabled: "false", - vhsEnabledFlag: true, + name: "vhs disabled (header)", + headerStatusVHS: disabledVHS, + vhsEnabledFlag: true, vhsNamespaced: map[string]bool{ "kapusta": true, }, @@ -96,7 +96,7 @@ func TestIsVHSAddress(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) }) }