From 77c1998a3830f12a7fe29a7b07d6cd34f9c57c1b Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Wed, 1 Mar 2017 18:17:04 +0100 Subject: [PATCH] config: Fix creating new config with wrong version (#3821) Simplify a little config code to avoid making mistake next time. --- cmd/config-migrate_test.go | 2 +- cmd/config-v14.go | 62 +++++++++++++++++++++----------------- cmd/config-v14_test.go | 4 +-- cmd/globals.go | 1 - 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/cmd/config-migrate_test.go b/cmd/config-migrate_test.go index 35f2d802d..c35b2ef89 100644 --- a/cmd/config-migrate_test.go +++ b/cmd/config-migrate_test.go @@ -151,7 +151,7 @@ func TestServerConfigMigrateV2toV14(t *testing.T) { } // Check the version number in the upgraded config file - expectedVersion := globalMinioConfigVersion + expectedVersion := v14 if serverConfig.Version != expectedVersion { t.Fatalf("Expect version "+expectedVersion+", found: %v", serverConfig.Version) } diff --git a/cmd/config-v14.go b/cmd/config-v14.go index 1ae69405b..fa4e3b0b3 100644 --- a/cmd/config-v14.go +++ b/cmd/config-v14.go @@ -17,6 +17,7 @@ package cmd import ( + "errors" "os" "strings" "sync" @@ -27,6 +28,9 @@ import ( // Read Write mutex for safe access to ServerConfig. var serverConfigMu sync.RWMutex +// Config version +var v14 = "14" + // serverConfigV14 server configuration version '14' which is like // version '13' except it adds support of browser param. type serverConfigV14 struct { @@ -44,30 +48,15 @@ type serverConfigV14 struct { Notify *notifier `json:"notify"` } -// newConfig - initialize a new server config, saves env parameters if -// found, otherwise use default parameters -func newConfig(envParams envParams) error { - // Initialize server config. +func newServerConfigV14() *serverConfigV14 { srvCfg := &serverConfigV14{ - Logger: &logger{}, - Notify: ¬ifier{}, - } - srvCfg.Version = globalMinioConfigVersion - srvCfg.Region = globalMinioDefaultRegion - - // If env is set for a fresh start, save them to config file. - if globalIsEnvCreds { - srvCfg.SetCredential(envParams.creds) - } else { - srvCfg.SetCredential(newCredential()) - } - - if globalIsEnvBrowser { - srvCfg.SetBrowser(envParams.browser) - } else { - srvCfg.SetBrowser("on") + Version: v14, + Region: globalMinioDefaultRegion, + Logger: &logger{}, + Notify: ¬ifier{}, } - + srvCfg.SetCredential(newCredential()) + srvCfg.SetBrowser("on") // Enable console logger by default on a fresh run. srvCfg.Logger.Console = consoleLogger{ Enable: true, @@ -90,6 +79,24 @@ func newConfig(envParams envParams) error { srvCfg.Notify.Webhook = make(map[string]webhookNotify) srvCfg.Notify.Webhook["1"] = webhookNotify{} + return srvCfg +} + +// newConfig - initialize a new server config, saves env parameters if +// found, otherwise use default parameters +func newConfig(envParams envParams) error { + // Initialize server config. + srvCfg := newServerConfigV14() + + // If env is set for a fresh start, save them to config file. + if globalIsEnvCreds { + srvCfg.SetCredential(envParams.creds) + } + + if globalIsEnvBrowser { + srvCfg.SetBrowser(envParams.browser) + } + // Create config path. if err := createConfigPath(); err != nil { return err @@ -117,8 +124,9 @@ func loadConfig(envParams envParams) error { if _, err = os.Stat(configFile); err != nil { return err } + srvCfg := &serverConfigV14{} - srvCfg.Version = globalMinioConfigVersion + qc, err := quick.New(srvCfg) if err != nil { return err @@ -131,8 +139,6 @@ func loadConfig(envParams envParams) error { // If env is set override the credentials from config file. if globalIsEnvCreds { srvCfg.SetCredential(envParams.creds) - } else { - srvCfg.SetCredential(srvCfg.Credential) } if globalIsEnvBrowser { @@ -149,8 +155,10 @@ func loadConfig(envParams envParams) error { serverConfig = srvCfg serverConfigMu.Unlock() - // Set the version properly after the unmarshalled json is loaded. - serverConfig.Version = globalMinioConfigVersion + if serverConfig.Version != v14 { + return errors.New("Unsupported config version `" + serverConfig.Version + "`.") + } + return nil } diff --git a/cmd/config-v14_test.go b/cmd/config-v14_test.go index bbf786c3a..63f0c4ebd 100644 --- a/cmd/config-v14_test.go +++ b/cmd/config-v14_test.go @@ -102,8 +102,8 @@ func TestServerConfig(t *testing.T) { }) // Match version. - if serverConfig.GetVersion() != globalMinioConfigVersion { - t.Errorf("Expecting version %s found %s", serverConfig.GetVersion(), globalMinioConfigVersion) + if serverConfig.GetVersion() != v14 { + t.Errorf("Expecting version %s found %s", serverConfig.GetVersion(), v14) } // Attempt to save. diff --git a/cmd/globals.go b/cmd/globals.go index deff7ae90..8ffd08712 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -31,7 +31,6 @@ import ( // minio configuration related constants. const ( - globalMinioConfigVersion = "13" globalMinioConfigDir = ".minio" globalMinioCertsDir = "certs" globalMinioCertsCADir = "CAs"