Skip to content

Commit

Permalink
Merge pull request #3209 from nspcc-dev/strict-cfg
Browse files Browse the repository at this point in the history
Forbid unknown YAML node configuration fields
  • Loading branch information
roman-khimov authored Nov 21, 2023
2 parents 8e5a724 + 63de821 commit 80fcf81
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config/protocol.privnet.docker.one.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ ApplicationConfiguration:
- ":20001"
Pprof:
Enabled: false
Port: 20011
Addresses:
- ":20011"
Consensus:
Enabled: true
UnlockWallet:
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"bytes"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -82,8 +83,9 @@ func LoadFile(configPath string) (Config, error) {
},
},
}

err = yaml.Unmarshal(configData, &config)
decoder := yaml.NewDecoder(bytes.NewReader(configData))
decoder.KnownFields(true)
err = decoder.Decode(&config)
if err != nil {
return Config{}, fmt.Errorf("failed to unmarshal config YAML: %w", err)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package config

import (
"os"
"path/filepath"
"testing"

"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/stretchr/testify/require"
)

Expand All @@ -12,3 +15,20 @@ func TestUnexpectedNativeUpdateHistoryContract(t *testing.T) {
_, err := LoadFile(testConfigPath)
require.Error(t, err)
}

func TestUnknownConfigFields(t *testing.T) {
tmp := t.TempDir()
cfg := filepath.Join(tmp, "protocol.testnet.yml")
require.NoError(t, os.WriteFile(cfg, []byte(`UnknownConfigurationField: 123`), os.ModePerm))

t.Run("LoadFile", func(t *testing.T) {
_, err := LoadFile(cfg)
require.Error(t, err)
require.Contains(t, err.Error(), "field UnknownConfigurationField not found in type config.Config")
})
t.Run("Load", func(t *testing.T) {
_, err := Load(tmp, netmode.TestNet)
require.Error(t, err)
require.Contains(t, err.Error(), "field UnknownConfigurationField not found in type config.Config")
})
}
4 changes: 3 additions & 1 deletion pkg/core/blockchain_neotest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,9 @@ func TestConfigNativeUpdateHistory(t *testing.T) {
"unit_testnet.single",
}
for _, tc := range testCases {
check(t, tc)
t.Run(fmt.Sprintf("%s", tc), func(t *testing.T) {
check(t, tc)
})
}
}

Expand Down

0 comments on commit 80fcf81

Please sign in to comment.