From 4331bd11432f8458537bee99a7450cb79c143462 Mon Sep 17 00:00:00 2001 From: Alexander Chuprov Date: Fri, 7 Jun 2024 16:22:46 +0300 Subject: [PATCH 1/2] [#1167] adm/morph: Fix set-config parameter validation Signed-off-by: Alexander Chuprov --- .../internal/modules/morph/config/config.go | 27 +++++++++++---- .../modules/morph/config/config_test.go | 34 +++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 cmd/frostfs-adm/internal/modules/morph/config/config_test.go diff --git a/cmd/frostfs-adm/internal/modules/morph/config/config.go b/cmd/frostfs-adm/internal/modules/morph/config/config.go index f35e7aa03..bfcd4ac00 100644 --- a/cmd/frostfs-adm/internal/modules/morph/config/config.go +++ b/cmd/frostfs-adm/internal/modules/morph/config/config.go @@ -147,13 +147,28 @@ func validateConfig(args map[string]any, forceFlag bool) error { } for k, v := range args { - value, ok := v.(int64) - if !ok || value < 0 { - return fmt.Errorf("%s must be >= 0, got %v", k, v) - } + switch k { + case netmap.ContainerFeeConfig, netmap.ContainerAliasFeeConfig, + netmap.EpochDurationConfig, netmap.IrCandidateFeeConfig, + netmap.MaxObjectSizeConfig, netmap.WithdrawFeeConfig, + netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig: + value, ok := v.(int64) + if !ok { + return fmt.Errorf("%s has an invalid type. Expected type: int", k) + } - if k == netmap.MaxECDataCountConfig || k == netmap.MaxECParityCountConfig { - sumEC += value + if value < 0 { + return fmt.Errorf("%s must be >= 0, got %v", k, v) + } + + if k == netmap.MaxECDataCountConfig || k == netmap.MaxECParityCountConfig { + sumEC += value + } + case netmap.HomomorphicHashingDisabledKey, netmap.MaintenanceModeAllowedConfig: + _, ok := v.(bool) + if !ok { + return fmt.Errorf("%s has an invalid type. Expected type: bool", k) + } } } diff --git a/cmd/frostfs-adm/internal/modules/morph/config/config_test.go b/cmd/frostfs-adm/internal/modules/morph/config/config_test.go new file mode 100644 index 000000000..c6d5b2827 --- /dev/null +++ b/cmd/frostfs-adm/internal/modules/morph/config/config_test.go @@ -0,0 +1,34 @@ +package config + +import ( + "testing" + + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap" + "github.com/stretchr/testify/require" +) + +func Test_ValidateConfig(t *testing.T) { + testArgs := make(map[string]any) + + testArgs[netmap.MaxECDataCountConfig] = int64(11) + require.Error(t, validateConfig(testArgs, false)) + + testArgs[netmap.MaxECParityCountConfig] = int64(256) + require.Error(t, validateConfig(testArgs, false)) + require.NoError(t, validateConfig(testArgs, true)) + + testArgs[netmap.MaxECParityCountConfig] = int64(-1) + require.Error(t, validateConfig(testArgs, false)) + + testArgs[netmap.MaxECParityCountConfig] = int64(55) + require.NoError(t, validateConfig(testArgs, false)) + + testArgs[netmap.HomomorphicHashingDisabledKey] = "1" + require.Error(t, validateConfig(testArgs, false)) + + testArgs[netmap.HomomorphicHashingDisabledKey] = true + require.NoError(t, validateConfig(testArgs, false)) + + testArgs["not-well-known-configuration-key"] = "key" + require.NoError(t, validateConfig(testArgs, false)) +} -- 2.45.2 From a9b93676e02e69e85bcc487fd6240ccff43dbe62 Mon Sep 17 00:00:00 2001 From: Alexander Chuprov Date: Fri, 7 Jun 2024 16:33:03 +0300 Subject: [PATCH 2/2] [#1167] adm/morph: Move literal to const Signed-off-by: Alexander Chuprov --- cmd/frostfs-adm/internal/modules/morph/config/config.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cmd/frostfs-adm/internal/modules/morph/config/config.go b/cmd/frostfs-adm/internal/modules/morph/config/config.go index bfcd4ac00..ba6e515c1 100644 --- a/cmd/frostfs-adm/internal/modules/morph/config/config.go +++ b/cmd/frostfs-adm/internal/modules/morph/config/config.go @@ -137,6 +137,8 @@ func SetConfigCmd(cmd *cobra.Command, args []string) error { return wCtx.AwaitTx() } +const maxECSum = 256 + func validateConfig(args map[string]any, forceFlag bool) error { var sumEC int64 _, okData := args[netmap.MaxECDataCountConfig] @@ -172,9 +174,9 @@ func validateConfig(args map[string]any, forceFlag bool) error { } } - if sumEC > 256 && !forceFlag { - return fmt.Errorf("the sum of %s and %s must be <= 256, got %d", - netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig, sumEC) + if sumEC > maxECSum && !forceFlag { + return fmt.Errorf("the sum of %s and %s must be <= %d, got %d", + netmap.MaxECDataCountConfig, netmap.MaxECParityCountConfig, maxECSum, sumEC) } return nil } -- 2.45.2