From b14d5c410f85d5be1402df7ac9fee47f74887230 Mon Sep 17 00:00:00 2001 From: carabasdaniel Date: Wed, 25 Oct 2023 11:20:08 +0300 Subject: [PATCH] Move config version validation before unmarshal --- pkg/cc/config/config.go | 5 +++++ pkg/cc/config/topaz_config.go | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/cc/config/config.go b/pkg/cc/config/config.go index 1dcb6db2..7d3a03bf 100644 --- a/pkg/cc/config/config.go +++ b/pkg/cc/config/config.go @@ -139,6 +139,11 @@ func NewConfig(configPath Path, log *zerolog.Logger, overrides Overrider, certsG if err := v.ReadConfig(r); err != nil { return nil, errors.Wrapf(err, "failed to parse config file '%s'", file) } + + err = validateVersion(v.GetInt("version")) + if err != nil { + return nil, err + } } v.AutomaticEnv() diff --git a/pkg/cc/config/topaz_config.go b/pkg/cc/config/topaz_config.go index 4b789d78..98f223e5 100644 --- a/pkg/cc/config/topaz_config.go +++ b/pkg/cc/config/topaz_config.go @@ -62,10 +62,14 @@ func (co *CallOptions) ForPath(path string) *Options { func defaults(v *viper.Viper) { } -func (c *Config) validation() error { - if c.Version != ConfigFileVersion { +func validateVersion(version int) error { + if version != ConfigFileVersion { return errors.New("unsupported config version") } + return nil +} + +func (c *Config) validation() error { if _, ok := c.APIConfig.Services["authorizer"]; ok { if c.Command.Mode == CommandModeRun && c.OPA.InstanceID == "" { return errors.New("opa.instance_id not set")