forked from TrueCloudLab/neoneo-go
config: forbid unknown YAML configuration fields
Close #3128 and fix one of the privnet configurations along the way. Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
parent
a4779de375
commit
63de821ff4
3 changed files with 26 additions and 3 deletions
|
@ -80,7 +80,8 @@ ApplicationConfiguration:
|
||||||
- ":20001"
|
- ":20001"
|
||||||
Pprof:
|
Pprof:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
Port: 20011
|
Addresses:
|
||||||
|
- ":20011"
|
||||||
Consensus:
|
Consensus:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
UnlockWallet:
|
UnlockWallet:
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
@ -82,8 +83,9 @@ func LoadFile(configPath string) (Config, error) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
decoder := yaml.NewDecoder(bytes.NewReader(configData))
|
||||||
err = yaml.Unmarshal(configData, &config)
|
decoder.KnownFields(true)
|
||||||
|
err = decoder.Decode(&config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Config{}, fmt.Errorf("failed to unmarshal config YAML: %w", err)
|
return Config{}, fmt.Errorf("failed to unmarshal config YAML: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,3 +15,20 @@ func TestUnexpectedNativeUpdateHistoryContract(t *testing.T) {
|
||||||
_, err := LoadFile(testConfigPath)
|
_, err := LoadFile(testConfigPath)
|
||||||
require.Error(t, err)
|
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")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue