Validate Whisper DataDir to be relative to the main DataDir (#1335)

This commit is contained in:
Adam Babik 2019-01-04 12:44:01 +01:00 committed by GitHub
parent 66fb99d5d6
commit 944a35a11b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 48 deletions

View File

@ -409,7 +409,8 @@ func (c *NodeConfig) updatePeerLimits() {
}
}
// NewNodeConfig creates new node configuration object with bare-minimum defaults
// NewNodeConfig creates new node configuration object with bare-minimum defaults.
// Important: the returned config is not validated.
func NewNodeConfig(dataDir string, networkID uint64) (*NodeConfig, error) {
var keyStoreDir, wnodeDir string
@ -513,6 +514,7 @@ func loadConfigFromAsset(name string, config *NodeConfig) error {
//
// Key: 'TestStruct.TestField' Error:Field validation for 'TestField' failed on the 'required' tag
//
// nolint: gocyclo
func (c *NodeConfig) Validate() error {
validate := NewValidator()
@ -534,6 +536,14 @@ func (c *NodeConfig) Validate() error {
return err
}
// Whisper's data directory must be relative to the main data directory
// if EnableMailServer is true.
if c.WhisperConfig.Enabled && c.WhisperConfig.EnableMailServer {
if !strings.HasPrefix(c.WhisperConfig.DataDir, c.DataDir) {
return fmt.Errorf("WhisperConfig.DataDir must start with DataDir fragment")
}
}
if !c.NoDiscovery && len(c.ClusterConfig.BootNodes) == 0 {
// No point in running discovery if we don't have bootnodes.
// In case we do have bootnodes, NoDiscovery should be true.
@ -633,10 +643,10 @@ func (c *WhisperConfig) Validate(validate *validator.Validate) error {
if c.DataDir == "" {
return fmt.Errorf("WhisperConfig.DataDir must be specified when WhisperConfig.EnableMailServer is true")
}
if c.MailServerPassword == "" && c.MailServerAsymKey == "" {
return fmt.Errorf("WhisperConfig.MailServerPassword or WhisperConfig.MailServerAsymKey must be specified when WhisperConfig.EnableMailServer is true")
}
if c.MailServerAsymKey != "" {
if _, err := crypto.HexToECDSA(c.MailServerAsymKey); err != nil {
return fmt.Errorf("WhisperConfig.MailServerAsymKey is invalid: %s", c.MailServerAsymKey)

View File

@ -7,7 +7,7 @@ import (
"path/filepath"
"testing"
"gopkg.in/go-playground/validator.v9"
validator "gopkg.in/go-playground/validator.v9"
"github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/status-im/status-go/params"
@ -250,25 +250,6 @@ func TestNodeConfigValidate(t *testing.T) {
}`,
Error: "WhisperConfig.DataDir must be specified when WhisperConfig.EnableMailServer is true",
},
{
Name: "Validate that check for WhisperConfig.DataDir passes if it is not empty and mailserver is enabled",
Config: `{
"NetworkId": 1,
"DataDir": "/some/dir",
"BackupDisabledDataDir": "/some/dir",
"KeyStoreDir": "/some/dir",
"NoDiscovery": true,
"WhisperConfig": {
"Enabled": true,
"EnableMailServer": true,
"DataDir": "/foo",
"MailserverPassword": "foo"
}
}`,
CheckFunc: func(t *testing.T, config *params.NodeConfig) {
require.Equal(t, "foo", config.WhisperConfig.MailServerPassword)
},
},
{
Name: "Validate that WhisperConfig.DataDir is checked to not be empty if mailserver is enabled",
Config: `{
@ -280,10 +261,30 @@ func TestNodeConfigValidate(t *testing.T) {
"WhisperConfig": {
"Enabled": true,
"EnableMailServer": true,
"MailserverPassword": "foo",
"DataDir": "/other/dir"
}
}`,
Error: "WhisperConfig.DataDir must start with DataDir fragment",
},
{
Name: "Validate that check for WhisperConfig.DataDir passes if it is not empty and mailserver is enabled",
Config: `{
"NetworkId": 1,
"DataDir": "/some/dir",
"BackupDisabledDataDir": "/some/dir",
"KeyStoreDir": "/some/dir",
"NoDiscovery": true,
"WhisperConfig": {
"Enabled": true,
"EnableMailServer": true,
"DataDir": "/some/dir",
"MailserverPassword": "foo"
}
}`,
Error: "WhisperConfig.DataDir must be specified when WhisperConfig.EnableMailServer is true",
CheckFunc: func(t *testing.T, config *params.NodeConfig) {
require.Equal(t, "foo", config.WhisperConfig.MailServerPassword)
},
},
{
Name: "Validate that WhisperConfig.MailserverPassword and WhisperConfig.MailServerAsymKey are checked to not be empty if mailserver is enabled",
@ -296,7 +297,7 @@ func TestNodeConfigValidate(t *testing.T) {
"WhisperConfig": {
"Enabled": true,
"EnableMailServer": true,
"DataDir": "/foo"
"DataDir": "/some/dir"
}
}`,
Error: "WhisperConfig.MailServerPassword or WhisperConfig.MailServerAsymKey must be specified when WhisperConfig.EnableMailServer is true",
@ -312,7 +313,7 @@ func TestNodeConfigValidate(t *testing.T) {
"WhisperConfig": {
"Enabled": true,
"EnableMailServer": true,
"DataDir": "/foo",
"DataDir": "/some/dir",
"MailServerAsymKey": "06c365919f1fc8e13ff79a84f1dd14b7e45b869aa5fc0e34940481ee20d32f90"
}
}`,
@ -384,30 +385,30 @@ func TestNodeConfigValidate(t *testing.T) {
}
for _, tc := range testCases {
t.Logf("Test Case %s", tc.Name)
t.Run(tc.Name, func(t *testing.T) {
config, err := params.NewConfigFromJSON(tc.Config)
config, err := params.NewConfigFromJSON(tc.Config)
switch err := err.(type) {
case validator.ValidationErrors:
for _, ve := range err {
require.Contains(t, tc.FieldErrors, ve.Field())
require.Equal(t, tc.FieldErrors[ve.Field()], ve.Tag())
switch err := err.(type) {
case validator.ValidationErrors:
for _, ve := range err {
require.Contains(t, tc.FieldErrors, ve.Field())
require.Equal(t, tc.FieldErrors[ve.Field()], ve.Tag())
}
case error:
if tc.Error == "" {
require.NoError(t, err)
} else {
require.Contains(t, err.Error(), tc.Error)
}
case nil:
if tc.Error != "" {
require.Error(t, err, "Error should be '%v'", tc.Error)
}
require.Nil(t, tc.FieldErrors)
if tc.CheckFunc != nil {
tc.CheckFunc(t, config)
}
}
case error:
if tc.Error == "" {
require.NoError(t, err)
} else {
require.Contains(t, err.Error(), tc.Error)
}
case nil:
if tc.Error != "" {
require.Error(t, err, "Error should be '%v'", tc.Error)
}
require.Nil(t, tc.FieldErrors)
if tc.CheckFunc != nil {
tc.CheckFunc(t, config)
}
}
})
}
}