Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration validation #1380

Merged
merged 2 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/sirupsen/logrus"
tmconfig "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto/ed25519"
"gopkg.in/go-playground/validator.v9"
)

const (
Expand All @@ -30,46 +31,44 @@ const (

// Config contains all the configuration needed.
type Config struct {
Name string
Path string
Name string `validate:"required"`
Path string `validate:"required"`

Server struct {
Address string
Address string `validate:"required"`
}

Log struct {
Format string
Format string `validate:"required"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add oneof=text json > https://godoc.org/gopkg.in/go-playground/validator.v9#hdr-One_Of

With that, we can remove l149 and we could do the same for the logLevel

ForceColors bool
Level string
Level string `validate:"required"`
}

Database struct {
ServiceRelativePath string
InstanceRelativePath string
ExecutionRelativePath string
ProcessRelativePath string
ServiceRelativePath string `validate:"required"`
InstanceRelativePath string `validate:"required"`
ExecutionRelativePath string `validate:"required"`
ProcessRelativePath string `validate:"required"`
}

Tendermint struct {
*tmconfig.Config
Path string
*tmconfig.Config `validate:"required"`
Path string `validate:"required"`
}

Cosmos CosmosConfig
}

// CosmosConfig is the struct to hold cosmos related configs.
type CosmosConfig struct {
Path string
ChainID string
GenesisTime time.Time

GenesisValidatorTx StdTx

ValidatorPubKey PubKeyEd25519
Path string `validate:"required"`
ChainID string `validate:"required"`
GenesisTime time.Time `validate:"required"`
GenesisValidatorTx StdTx `validate:"required"`
ValidatorPubKey PubKeyEd25519 `validate:"required"`
}

// New creates a new config with default values.
// Default creates a new config with default values.
func Default() (*Config, error) {
home, err := homedir.Dir()
if err != nil {
Expand Down Expand Up @@ -148,12 +147,15 @@ func (c *Config) Prepare() error {
// Validate checks values and return an error if any validation failed.
func (c *Config) Validate() error {
if !xstrings.SliceContains([]string{"text", "json"}, c.Log.Format) {
return fmt.Errorf("value %q is not an allowed", c.Log.Format)
return fmt.Errorf("config.Log.Format value %q is not an allowed", c.Log.Format)
}
if _, err := logrus.ParseLevel(c.Log.Level); err != nil {
return err
return fmt.Errorf("config.Log.Level error: %w", err)
}
return nil
if err := authtypes.StdTx(c.Cosmos.GenesisValidatorTx).ValidateBasic(); err != nil {
return fmt.Errorf("config.Cosmos.GenesisValidatorTx error: %w", err.Stacktrace())
}
return validator.New().Struct(c)
}

// PubKeyEd25519 is type used to parse value provided by envconfig.
Expand Down Expand Up @@ -183,13 +185,18 @@ type StdTx authtypes.StdTx

// Decode parses string value as hex ed25519 key.
func (tx *StdTx) Decode(value string) error {
if value == "" {
return nil
}
cdc := codec.New()
codec.RegisterCrypto(cdc)
sdktypes.RegisterCodec(cdc)
stakingtypes.RegisterCodec(cdc)

if err := cdc.UnmarshalJSON([]byte(value), tx); err != nil {
return fmt.Errorf("unmarshal genesis validator error: %s", err)
}
if err := authtypes.StdTx(*tx).ValidateBasic(); err != nil {
return err.Stacktrace()
}
return nil
}
21 changes: 7 additions & 14 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@ func TestDefaultValue(t *testing.T) {
require.Equal(t, "engine", c.Name)
}

func TestNew(t *testing.T) {
c, err := New()
require.NoError(t, err)
require.NotNil(t, c)
}

func TestLoad(t *testing.T) {
snapsnot := map[string]string{
"MESG_SERVER_ADDRESS": "",
"MESG_LOG_FORMAT": "",
"MESG_LOG_LEVEL": "",
"MESG_LOG_FORCECOLORS": "",
"MESG_TENDERMINT_P2P_PERSISTENTPEERS": "",
"MESG_COSMOS_VALIDATOR_PUB_KEY": "",
"MESG_COSMOS_VALIDATORPUBKEY": "",
"MESG_COSMOS_GENESISVALIDATORTX": "",
}
for key := range snapsnot {
snapsnot[key] = os.Getenv(key)
Expand Down Expand Up @@ -70,13 +65,11 @@ func TestLoad(t *testing.T) {

func TestValidate(t *testing.T) {
c, _ := Default()
require.NoError(t, c.Validate())

c, _ = Default()
c.Log.Format = "wrongValue"
c.Load()
require.Error(t, c.Validate())
}

c, _ = Default()
c.Log.Level = "wrongValue"
require.Error(t, c.Validate())
func TestStdTXDecodeNoSignersError(t *testing.T) {
var tx StdTx
require.Error(t, tx.Decode(`{"msg":[{"type":"cosmos-sdk/MsgCreateValidator","value":{"description":{"identity":"","website":"","details":"create-first-validator"},"pubkey":"cosmosvalconspub1zcjduepqq0a87y3pur6vvzyp99t92me2zyxywz46kyq5vt7x2n4g987acmxszqey9p","value":{"denom":"stake","amount":"100000000"}}}],"fee":{"amount":[],"gas":"200000"}}`))
}