Skip to content

Commit

Permalink
node: add check for unmarshalling config to validate
Browse files Browse the repository at this point in the history
This is a check in case there is a ",remain" tag in the configuration field, and
if there is an incorrect data format, then the value of the field was ignored,
since then there will be a check to see if this field is correct or not.
This makes some errors clearer after unmarshalling.

Closes #3034.

Signed-off-by: Andrey Butusov <[email protected]>
  • Loading branch information
End-rey committed Nov 29, 2024
1 parent 7b592bd commit 56f54a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cmd/neofs-node/config/internal/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@ package validate

import (
"fmt"
"reflect"

"github.com/mitchellh/mapstructure"
"github.com/nspcc-dev/neofs-node/cmd/internal/configvalidator"
"github.com/spf13/viper"
)

// ValidateStruct validates the viper config structure.
func ValidateStruct(v *viper.Viper) error {
var cfg valideConfig
if err := v.Unmarshal(&cfg); err != nil {
opt := viper.DecoderConfigOption(func(dc *mapstructure.DecoderConfig) {
dc.DecodeHook = mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
func(from reflect.Type, to reflect.Type, data any) (any, error) {
if from.Kind() == reflect.Map && to.Kind() == reflect.String {
_, ok := data.(map[string]any)
if !ok {
return data, nil
}

Check warning on line 24 in cmd/neofs-node/config/internal/validate/validate.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config/internal/validate/validate.go#L23-L24

Added lines #L23 - L24 were not covered by tests
return "", nil
}
return data, nil
})
})
if err := v.Unmarshal(&cfg, opt); err != nil {
return fmt.Errorf("unable to decode config: %w", err)
}

Expand Down
20 changes: 20 additions & 0 deletions cmd/neofs-node/config/internal/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ grpc:
enabled: false
- endpoint: s03.neofs.devenv:8080
- unknown: field
`,
wantErr: true,
},
{
name: "unknown field node.subnet",
config: `
node:
attribute_0: "Price:11"
subnet:
exit_zero: False
`,
wantErr: true,
},
{
name: "unknown field node.attribute_1",
config: `
node:
attribute_0: "Price:11"
attribute_1:
aaa: "Price:11"
`,
wantErr: true,
},
Expand Down

0 comments on commit 56f54a6

Please sign in to comment.