From 6a493df5eb839ab9cf1e7625eed1dca293ad3246 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 21 Sep 2015 22:22:27 -0700 Subject: [PATCH] migrate from ConfigV4 to ConfigV5, rename Signature to API --- common-methods.go | 6 +- config-alias-main.go | 6 +- config-host-main.go | 32 +++++----- config-version-main.go | 2 +- config.go | 95 +++++++++++++++++++++++----- config_test.go | 2 +- globals.go | 2 +- hostconfig.go | 4 +- mc_test.go | 6 +- pkg/client/{s3 => s3v4}/httptrace.go | 2 +- pkg/client/{s3 => s3v4}/s3.go | 2 +- pkg/client/{s3 => s3v4}/s3_test.go | 2 +- pkg/client/{s3 => s3v4}/trace.go | 2 +- 13 files changed, 114 insertions(+), 49 deletions(-) rename pkg/client/{s3 => s3v4}/httptrace.go (99%) rename pkg/client/{s3 => s3v4}/s3.go (99%) rename pkg/client/{s3 => s3v4}/s3_test.go (99%) rename pkg/client/{s3 => s3v4}/trace.go (99%) diff --git a/common-methods.go b/common-methods.go index c7a177a9..035137da 100644 --- a/common-methods.go +++ b/common-methods.go @@ -25,8 +25,8 @@ import ( "github.com/minio/mc/pkg/client" "github.com/minio/mc/pkg/client/fs" - "github.com/minio/mc/pkg/client/s3" "github.com/minio/mc/pkg/client/s3v2" + "github.com/minio/mc/pkg/client/s3v4" "github.com/minio/minio/pkg/probe" ) @@ -156,10 +156,10 @@ func getNewClient(urlStr string, auth hostConfig) (client.Client, *probe.Error) var s3Client client.Client var err *probe.Error - if auth.Signature == "v2" { + if auth.API == "S3v2" { s3Client, err = s3v2.New(s3Config) } else { - s3Client, err = s3.New(s3Config) + s3Client, err = s3v4.New(s3Config) } if err != nil { return nil, err.Trace() diff --git a/config-alias-main.go b/config-alias-main.go index fa94bdec..ee616684 100644 --- a/config-alias-main.go +++ b/config-alias-main.go @@ -147,7 +147,7 @@ func listAliases() { fatalIf(err.Trace(configPath), "Unable to load config path") // convert interface{} back to its original struct - newConf := config.Data().(*configV4) + newConf := config.Data().(*configV5) for k, v := range newConf.Aliases { Prints("%s\n", AliasMessage{ op: "list", @@ -173,7 +173,7 @@ func removeAlias(alias string) { } // convert interface{} back to its original struct - newConf := config.Data().(*configV4) + newConf := config.Data().(*configV5) if _, ok := newConf.Aliases[alias]; !ok { fatalIf(errDummy().Trace(), fmt.Sprintf("Alias ‘%s’ does not exist.", alias)) } @@ -209,7 +209,7 @@ func addAlias(alias, url string) { fatalIf(errDummy().Trace(), fmt.Sprintf("Alias name ‘%s’ is invalid, valid examples are: mybucket, Area51, Grand-Nagus", alias)) } // convert interface{} back to its original struct - newConf := config.Data().(*configV4) + newConf := config.Data().(*configV5) if oldURL, ok := newConf.Aliases[alias]; ok { fatalIf(errDummy().Trace(), fmt.Sprintf("Alias ‘%s’ already exists for ‘%s’.", alias, oldURL)) } diff --git a/config-host-main.go b/config-host-main.go index 87248858..34ebbb4a 100644 --- a/config-host-main.go +++ b/config-host-main.go @@ -31,9 +31,9 @@ EXAMPLES: $ mc config {{.Name}} add s3.amazonaws.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12 $ set -o history - 2. Add host configuration for a URL, using old signature v2. For security reasons turn off bash history + 2. Add host configuration for a URL, using s3 api v2. For security reasons turn off bash history $ set +o history - $ mc config {{.Name}} add s3.amazonaws.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12 v2 + $ mc config {{.Name}} add s3.amazonaws.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12 S3v2 $ set -o history 3. List all hosts. @@ -51,7 +51,7 @@ type HostMessage struct { Host string `json:"host"` AccessKeyID string `json:"accessKeyId,omitempty"` SecretAccessKey string `json:"secretAccessKey,omitempty"` - Signature string `json:"signature,omitempty"` + API string `json:"api,omitempty"` } // String colorized host message @@ -61,7 +61,7 @@ func (a HostMessage) String() string { if a.AccessKeyID != "" || a.SecretAccessKey != "" { message += console.Colorize("AccessKeyID", fmt.Sprintf("<- %s,", a.AccessKeyID)) message += console.Colorize("SecretAccessKey", fmt.Sprintf(" %s,", a.SecretAccessKey)) - message += console.Colorize("Signature", fmt.Sprintf(" %s", a.Signature)) + message += console.Colorize("API", fmt.Sprintf(" %s", a.API)) } return message } @@ -115,7 +115,7 @@ func mainConfigHost(ctx *cli.Context) { // set new custom coloring console.SetCustomTheme(map[string]*color.Color{ "Host": color.New(color.FgCyan, color.Bold), - "Signature": color.New(color.FgYellow, color.Bold), + "API": color.New(color.FgYellow, color.Bold), "HostMessage": color.New(color.FgGreen, color.Bold), "AccessKeyID": color.New(color.FgBlue, color.Bold), "SecretAccessKey": color.New(color.FgRed, color.Bold), @@ -143,14 +143,14 @@ func listHosts() { fatalIf(err.Trace(configPath), "Unable to load config path") // convert interface{} back to its original struct - newConf := config.Data().(*configV4) + newConf := config.Data().(*configV5) for k, v := range newConf.Hosts { Prints("%s\n", HostMessage{ op: "list", Host: k, AccessKeyID: v.AccessKeyID, SecretAccessKey: v.SecretAccessKey, - Signature: v.Signature, + API: v.API, }) } @@ -171,7 +171,7 @@ func removeHost(hostGlob string) { fatalIf(err.Trace(configPath), "Unable to load config path") // convert interface{} back to its original struct - newConf := config.Data().(*configV4) + newConf := config.Data().(*configV5) if _, ok := newConf.Hosts[hostGlob]; !ok { fatalIf(errDummy().Trace(), fmt.Sprintf("Host glob ‘%s’ does not exist.", hostGlob)) } @@ -207,15 +207,15 @@ func isValidAccessKey(accessKeyID string) bool { } // addHost - add new host -func addHost(hostGlob, accessKeyID, secretAccessKey, signature string) { +func addHost(hostGlob, accessKeyID, secretAccessKey, api string) { if strings.TrimSpace(hostGlob) == "" { fatalIf(errDummy().Trace(), "Unable to proceed, empty arguments provided.") } - if strings.TrimSpace(signature) == "" { - signature = "v4" + if strings.TrimSpace(api) == "" { + api = "S3v4" } - if strings.ToLower(signature) != "v2" && strings.ToLower(signature) != "v4" { - fatalIf(errInvalidArgument().Trace(), "Unrecognized version name provided, supported inputs are ‘v4’, ‘v2’") + if strings.TrimSpace(api) != "S3v2" && strings.TrimSpace(api) != "S3v4" { + fatalIf(errInvalidArgument().Trace(), "Unrecognized version name provided, supported inputs are ‘S3v4’, ‘S3v2’") } config, err := newConfig() fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.") @@ -235,11 +235,11 @@ func addHost(hostGlob, accessKeyID, secretAccessKey, signature string) { } } // convert interface{} back to its original struct - newConf := config.Data().(*configV4) + newConf := config.Data().(*configV5) newConf.Hosts[hostGlob] = hostConfig{ AccessKeyID: accessKeyID, SecretAccessKey: secretAccessKey, - Signature: signature, + API: "S3v4", } newConfig, err := quick.New(newConf) fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.") @@ -252,6 +252,6 @@ func addHost(hostGlob, accessKeyID, secretAccessKey, signature string) { Host: hostGlob, AccessKeyID: accessKeyID, SecretAccessKey: secretAccessKey, - Signature: signature, + API: api, }) } diff --git a/config-version-main.go b/config-version-main.go index 0ecfad71..a05fe902 100644 --- a/config-version-main.go +++ b/config-version-main.go @@ -51,7 +51,7 @@ func mainConfigVersion(ctx *cli.Context) { fatalIf(err.Trace(configPath), "Unable to load config path") // convert interface{} back to its original struct - newConf := config.Data().(*configV4) + newConf := config.Data().(*configV5) type Version struct { Value string `json:"value"` } diff --git a/config.go b/config.go index 9d4bf111..f7739a90 100644 --- a/config.go +++ b/config.go @@ -28,12 +28,22 @@ import ( "github.com/minio/minio/pkg/quick" ) -type configV4 struct { +type configV5 struct { Version string `json:"version"` Aliases map[string]string `json:"alias"` Hosts map[string]hostConfig `json:"hosts"` } +type configV4 struct { + Version string `json:"version"` + Aliases map[string]string `json:"alias"` + Hosts map[string]struct { + AccessKeyID string `json:"accessKeyId"` + SecretAccessKey string `json:"secretAccessKey"` + Signature string `json:"signature"` + } `json:"hosts"` +} + type configV3 struct { Version string `json:"version"` Aliases map[string]string `json:"alias"` @@ -127,7 +137,7 @@ func mustGetMcConfigPath() string { } // getMcConfig - reads configuration file and returns config -func getMcConfig() (*configV4, *probe.Error) { +func getMcConfig() (*configV5, *probe.Error) { if !isMcConfigExists() { return nil, errInvalidArgument().Trace() } @@ -139,10 +149,10 @@ func getMcConfig() (*configV4, *probe.Error) { // Cached in private global variable. if v := cache.Get(); v != nil { // Use previously cached config. - return v.(quick.Config).Data().(*configV4), nil + return v.(quick.Config).Data().(*configV5), nil } - conf := newConfigV4() + conf := newConfigV5() qconf, err := quick.New(conf) if err != nil { return nil, err.Trace() @@ -153,11 +163,11 @@ func getMcConfig() (*configV4, *probe.Error) { return nil, err.Trace() } cache.Put(qconf) - return qconf.Data().(*configV4), nil + return qconf.Data().(*configV5), nil } // mustGetMcConfig - reads configuration file and returns configs, exits on error -func mustGetMcConfig() *configV4 { +func mustGetMcConfig() *configV5 { config, err := getMcConfig() fatalIf(err.Trace(), "Unable to read mc configuration.") return config @@ -203,6 +213,8 @@ func migrateConfig() { migrateConfigV2ToV3() // Migrate config V3 to V4 migrateConfigV3ToV4() + // Migrate config V4 to V5 + migrateConfigV4ToV5() } func fixConfig() { @@ -325,15 +337,23 @@ func migrateConfigV3ToV4() { if mcConfigV3.Version() == "3" { confV4 := new(configV4) confV4.Aliases = mcConfigV3.Data().(*configV3).Aliases - confV4.Hosts = make(map[string]hostConfig) + confV4.Hosts = make(map[string]struct { + AccessKeyID string `json:"accessKeyId"` + SecretAccessKey string `json:"secretAccessKey"` + Signature string `json:"signature"` + }) for host, hostConf := range mcConfigV3.Data().(*configV3).Hosts { - confV4.Hosts[host] = hostConfig{ + confV4.Hosts[host] = struct { + AccessKeyID string `json:"accessKeyId"` + SecretAccessKey string `json:"secretAccessKey"` + Signature string `json:"signature"` + }{ AccessKeyID: hostConf.AccessKeyID, SecretAccessKey: hostConf.SecretAccessKey, Signature: "v4", } } - confV4.Version = globalMCConfigVersion + confV4.Version = "4" mcNewConfigV4, err := quick.New(confV4) fatalIf(err.Trace(), "Unable to initialize quick config for config version ‘4’.") @@ -345,6 +365,37 @@ func migrateConfigV3ToV4() { } } +func migrateConfigV4ToV5() { + if !isMcConfigExists() { + return + } + mcConfigV4, err := quick.Load(mustGetMcConfigPath(), newConfigV4()) + fatalIf(err.Trace(), "Unable to load mc config V2.") + + // update to newer version + if mcConfigV4.Version() == "4" { + confV5 := new(configV5) + confV5.Aliases = mcConfigV4.Data().(*configV4).Aliases + confV5.Hosts = make(map[string]hostConfig) + for host, hostConf := range mcConfigV4.Data().(*configV4).Hosts { + confV5.Hosts[host] = hostConfig{ + AccessKeyID: hostConf.AccessKeyID, + SecretAccessKey: hostConf.SecretAccessKey, + API: "S3v4", + } + } + confV5.Version = globalMCConfigVersion + + mcNewConfigV5, err := quick.New(confV5) + fatalIf(err.Trace(), "Unable to initialize quick config for config version ‘5’.") + + err = mcNewConfigV5.Save(mustGetMcConfigPath()) + fatalIf(err.Trace(), "Unable to save config version ‘5’.") + + console.Infof("Successfully migrated %s from version ‘4’ to version ‘5’.\n", mustGetMcConfigPath()) + } +} + func fixConfigV3() { if !isMcConfigExists() { return @@ -457,6 +508,20 @@ func newConfigV3() *configV3 { func newConfigV4() *configV4 { conf := new(configV4) + conf.Version = "4" + // make sure to allocate map's otherwise Golang + // exits silently without providing any errors + conf.Hosts = make(map[string]struct { + AccessKeyID string `json:"accessKeyId"` + SecretAccessKey string `json:"secretAccessKey"` + Signature string `json:"signature"` + }) + conf.Aliases = make(map[string]string) + return conf +} + +func newConfigV5() *configV5 { + conf := new(configV5) conf.Version = globalMCConfigVersion // make sure to allocate map's otherwise Golang // exits silently without providing any errors @@ -466,32 +531,32 @@ func newConfigV4() *configV4 { localHostConfig := hostConfig{ AccessKeyID: "", SecretAccessKey: "", - Signature: "v4", + API: "S3v4", } s3HostConf := hostConfig{ AccessKeyID: globalAccessKeyID, SecretAccessKey: globalSecretAccessKey, - Signature: "v4", + API: "S3v4", } // Your example host config exampleHostConf := hostConfig{ AccessKeyID: globalAccessKeyID, SecretAccessKey: globalSecretAccessKey, - Signature: "v4", + API: "S3v4", } playHostConfig := hostConfig{ AccessKeyID: "", SecretAccessKey: "", - Signature: "v4", + API: "S3v4", } dlHostConfig := hostConfig{ AccessKeyID: "", SecretAccessKey: "", - Signature: "v4", + API: "S3v4", } conf.Hosts[globalExampleHostURL] = exampleHostConf @@ -513,7 +578,7 @@ func newConfigV4() *configV4 { // newConfig - get new config interface func newConfig() (config quick.Config, err *probe.Error) { - config, err = quick.New(newConfigV4()) + config, err = quick.New(newConfigV5()) if err != nil { return nil, err.Trace() } diff --git a/config_test.go b/config_test.go index c598dbb0..e8b37c26 100644 --- a/config_test.go +++ b/config_test.go @@ -58,7 +58,7 @@ func (s *TestSuite) TestConfigHostContext(c *C) { "*my-example.com", "AKIKJAA5BMMU2RHO6IBB", "V7f1CCwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr9", - "v2", + "S3v2", }) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) diff --git a/globals.go b/globals.go index ea7c0374..1b9c4d80 100644 --- a/globals.go +++ b/globals.go @@ -26,7 +26,7 @@ var ( // mc configuration related constants. const ( - globalMCConfigVersion = "4" + globalMCConfigVersion = "5" globalMCVersion = Version globalMCConfigDir = ".mc/" diff --git a/hostconfig.go b/hostconfig.go index 5af914eb..348ecedc 100644 --- a/hostconfig.go +++ b/hostconfig.go @@ -26,7 +26,7 @@ import ( type hostConfig struct { AccessKeyID string `json:"accessKeyId"` SecretAccessKey string `json:"secretAccessKey"` - Signature string `json:"signature"` + API string `json:"api"` } // getHostConfig retrieves host specific configuration such as access keys, certs. @@ -41,7 +41,7 @@ func getHostConfig(URL string) (hostConfig, *probe.Error) { hostCfg := hostConfig{ AccessKeyID: "", SecretAccessKey: "", - Signature: "", + API: "fs", } return hostCfg, nil } diff --git a/mc_test.go b/mc_test.go index 601bedc4..99271f0a 100644 --- a/mc_test.go +++ b/mc_test.go @@ -108,12 +108,12 @@ func (s *TestSuite) TestNewConfigV4(c *C) { perr = conf.Save(configFile) c.Assert(perr, IsNil) - confNew := newConfigV4() + confNew := newConfigV5() config, perr := quick.New(confNew) c.Assert(perr, IsNil) perr = config.Load(configFile) c.Assert(perr, IsNil) - data := config.Data().(*configV4) + data := config.Data().(*configV5) type aliases struct { name string @@ -167,7 +167,7 @@ func (s *TestSuite) TestHostConfig(c *C) { c.Assert(err, IsNil) c.Assert(hostcfg.AccessKeyID, Equals, globalAccessKeyID) c.Assert(hostcfg.SecretAccessKey, Equals, globalSecretAccessKey) - c.Assert(hostcfg.Signature, Equals, "v4") + c.Assert(hostcfg.API, Equals, "S3v4") _, err = getHostConfig("http://test.minio.io") c.Assert(err, Not(IsNil)) diff --git a/pkg/client/s3/httptrace.go b/pkg/client/s3v4/httptrace.go similarity index 99% rename from pkg/client/s3/httptrace.go rename to pkg/client/s3v4/httptrace.go index 2cdd886a..36103e85 100644 --- a/pkg/client/s3/httptrace.go +++ b/pkg/client/s3v4/httptrace.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package s3 +package s3v4 import ( "net/http" diff --git a/pkg/client/s3/s3.go b/pkg/client/s3v4/s3.go similarity index 99% rename from pkg/client/s3/s3.go rename to pkg/client/s3v4/s3.go index 320581a3..0dbe3c6e 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3v4/s3.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package s3 +package s3v4 import ( "errors" diff --git a/pkg/client/s3/s3_test.go b/pkg/client/s3v4/s3_test.go similarity index 99% rename from pkg/client/s3/s3_test.go rename to pkg/client/s3v4/s3_test.go index 45b5786f..94030665 100644 --- a/pkg/client/s3/s3_test.go +++ b/pkg/client/s3v4/s3_test.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package s3 +package s3v4 // bucketHandler is an http.Handler that verifies bucket responses and validates incoming requests import ( diff --git a/pkg/client/s3/trace.go b/pkg/client/s3v4/trace.go similarity index 99% rename from pkg/client/s3/trace.go rename to pkg/client/s3v4/trace.go index 6c40b536..cb2003db 100644 --- a/pkg/client/s3/trace.go +++ b/pkg/client/s3v4/trace.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package s3 +package s3v4 import ( "net/http"