1
0
mirror of https://github.com/minio/mc.git synced 2025-11-14 23:42:27 +03:00

Fix a config corruption bug when config.json has wrong json format, would lead to overwriting the previous config.json

This commit is contained in:
Harshavardhana
2015-07-29 23:11:08 -07:00
parent 4175ca310c
commit 88bef1c60a
4 changed files with 34 additions and 38 deletions

View File

@@ -99,15 +99,15 @@ func (s *CmdTestSuite) TearDownSuite(c *C) {
}
func (s *CmdTestSuite) TestGetNewClient(c *C) {
_, err := getNewClient("http://example.com/bucket1", &hostConfig{})
_, err := getNewClient("http://example.com/bucket1", hostConfig{})
c.Assert(err, IsNil)
_, err = getNewClient("https://example.com/bucket1", &hostConfig{})
_, err = getNewClient("https://example.com/bucket1", hostConfig{})
c.Assert(err, IsNil)
_, err = getNewClient("C:\\Users\\Administrator\\MyDocuments", &hostConfig{})
_, err = getNewClient("C:\\Users\\Administrator\\MyDocuments", hostConfig{})
c.Assert(err, IsNil)
_, err = getNewClient("/usr/bin/pandoc", &hostConfig{})
_, err = getNewClient("/usr/bin/pandoc", hostConfig{})
c.Assert(err, IsNil)
_, err = getNewClient("pkg/client", &hostConfig{})
_, err = getNewClient("pkg/client", hostConfig{})
c.Assert(err, IsNil)
}

View File

@@ -135,16 +135,13 @@ func putTargets(targetURLs []string, length int64, reader io.Reader) error {
}
// getNewClient gives a new client interface
func getNewClient(urlStr string, auth *hostConfig) (clnt client.Client, err error) {
func getNewClient(urlStr string, auth hostConfig) (clnt client.Client, err error) {
url, err := client.Parse(urlStr)
if err != nil {
return nil, NewIodine(iodine.New(errInvalidURL{URL: urlStr}, map[string]string{"URL": urlStr}))
return nil, iodine.New(errInvalidURL{URL: urlStr}, map[string]string{"URL": urlStr})
}
switch url.Type {
case client.Object: // Minio and S3 compatible cloud storage
if auth == nil {
return nil, NewIodine(iodine.New(errInvalidArgument{}, nil))
}
s3Config := new(s3.Config)
s3Config.AccessKeyID = func() string {
if auth.AccessKeyID == globalAccessKeyID {
@@ -167,19 +164,19 @@ func getNewClient(urlStr string, auth *hostConfig) (clnt client.Client, err erro
case client.Filesystem:
return fs.New(urlStr)
}
return nil, NewIodine(iodine.New(errInvalidURL{URL: urlStr}, nil))
return nil, iodine.New(errInvalidURL{URL: urlStr}, nil)
}
// url2Stat - Returns client, config and its stat Content from the URL
func url2Stat(urlStr string) (client client.Client, content *client.Content, err error) {
client, err = url2Client(urlStr)
if err != nil {
return nil, nil, NewIodine(iodine.New(err, map[string]string{"URL": urlStr}))
return nil, nil, iodine.New(err, map[string]string{"URL": urlStr})
}
content, err = client.Stat()
if err != nil {
return nil, nil, NewIodine(iodine.New(err, map[string]string{"URL": urlStr}))
return nil, nil, iodine.New(err, map[string]string{"URL": urlStr})
}
return client, content, nil
@@ -189,21 +186,21 @@ func url2Client(url string) (client.Client, error) {
// Empty source arg?
urlParse, err := client.Parse(url)
if err != nil {
return nil, NewIodine(iodine.New(err, map[string]string{"URL": url}))
return nil, iodine.New(err, map[string]string{"URL": url})
}
if urlParse.Path == "" {
return nil, NewIodine(iodine.New(errInvalidURL{URL: url}, map[string]string{"URL": url}))
return nil, iodine.New(errInvalidURL{URL: url}, map[string]string{"URL": url})
}
urlconfig, err := getHostConfig(url)
if err != nil {
return nil, NewIodine(iodine.New(err, map[string]string{"URL": url}))
return nil, iodine.New(err, map[string]string{"URL": url})
}
client, err := getNewClient(url, urlconfig)
if err != nil {
return nil, NewIodine(iodine.New(err, map[string]string{"URL": url}))
return nil, iodine.New(err, map[string]string{"URL": url})
}
return client, nil
@@ -213,7 +210,7 @@ func url2Client(url string) (client.Client, error) {
func source2Client(sourceURL string) (client.Client, error) {
sourceClient, err := url2Client(sourceURL)
if err != nil {
return nil, NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, map[string]string{"URL": sourceURL}))
return nil, iodine.New(errInvalidSource{URL: sourceURL}, map[string]string{"URL": sourceURL})
}
return sourceClient, nil
}
@@ -222,7 +219,7 @@ func source2Client(sourceURL string) (client.Client, error) {
func target2Client(targetURL string) (client.Client, error) {
targetClient, err := url2Client(targetURL)
if err != nil {
return nil, NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, map[string]string{"URL": targetURL}))
return nil, iodine.New(errInvalidTarget{URL: targetURL}, map[string]string{"URL": targetURL})
}
return targetClient, nil
}

View File

@@ -31,7 +31,7 @@ import (
type configV1 struct {
Version string
Aliases map[string]string
Hosts map[string]*hostConfig
Hosts map[string]hostConfig
}
// cached variables should *NEVER* be accessed directly from outside this file.
@@ -186,7 +186,9 @@ func migrateConfigV1ToV101() {
if err != nil {
console.Fatalln(NewIodine(iodine.New(err, nil)))
}
config.Load(mustGetMcConfigPath())
if err := config.Load(mustGetMcConfigPath()); err != nil {
console.Fatalln(NewIodine(iodine.New(err, nil)))
}
conf = config.Data().(*configV1)
// version is the same return
if conf.Version == mcCurrentConfigVersion {
@@ -194,7 +196,7 @@ func migrateConfigV1ToV101() {
}
conf.Version = mcCurrentConfigVersion
localHostConfig := new(hostConfig)
localHostConfig := hostConfig{}
localHostConfig.AccessKeyID = ""
localHostConfig.SecretAccessKey = ""
@@ -218,7 +220,7 @@ func newConfigV1() *configV1 {
conf.Version = mcPreviousConfigVersion
// make sure to allocate map's otherwise Golang
// exits silently without providing any errors
conf.Hosts = make(map[string]*hostConfig)
conf.Hosts = make(map[string]hostConfig)
conf.Aliases = make(map[string]string)
return conf
}
@@ -229,27 +231,27 @@ func newConfigV101() *configV1 {
conf.Version = mcCurrentConfigVersion
// make sure to allocate map's otherwise Golang
// exits silently without providing any errors
conf.Hosts = make(map[string]*hostConfig)
conf.Hosts = make(map[string]hostConfig)
conf.Aliases = make(map[string]string)
localHostConfig := new(hostConfig)
localHostConfig := hostConfig{}
localHostConfig.AccessKeyID = ""
localHostConfig.SecretAccessKey = ""
s3HostConf := new(hostConfig)
s3HostConf := hostConfig{}
s3HostConf.AccessKeyID = globalAccessKeyID
s3HostConf.SecretAccessKey = globalSecretAccessKey
// Your example host config
exampleHostConf := new(hostConfig)
exampleHostConf := hostConfig{}
exampleHostConf.AccessKeyID = globalAccessKeyID
exampleHostConf.SecretAccessKey = globalSecretAccessKey
playHostConfig := new(hostConfig)
playHostConfig := hostConfig{}
playHostConfig.AccessKeyID = ""
playHostConfig.SecretAccessKey = ""
dlHostConfig := new(hostConfig)
dlHostConfig := hostConfig{}
dlHostConfig.AccessKeyID = ""
dlHostConfig.SecretAccessKey = ""

View File

@@ -29,18 +29,18 @@ type hostConfig struct {
}
// getHostConfig retrieves host specific configuration such as access keys, certs.
func getHostConfig(URL string) (*hostConfig, error) {
func getHostConfig(URL string) (hostConfig, error) {
config, err := getMcConfig()
if err != nil {
return nil, NewIodine(iodine.New(err, nil))
return hostConfig{}, NewIodine(iodine.New(err, nil))
}
url, err := client.Parse(URL)
if err != nil {
return nil, NewIodine(iodine.New(errInvalidURL{URL: URL}, nil))
return hostConfig{}, NewIodine(iodine.New(errInvalidURL{URL: URL}, nil))
}
// No host matching or keys needed for filesystem requests
if url.Type == client.Filesystem {
hostCfg := &hostConfig{
hostCfg := hostConfig{
AccessKeyID: "",
SecretAccessKey: "",
}
@@ -50,14 +50,11 @@ func getHostConfig(URL string) (*hostConfig, error) {
for globURL, hostCfg := range config.Hosts {
match, err := filepath.Match(globURL, url.Host)
if err != nil {
return nil, NewIodine(iodine.New(errInvalidGlobURL{glob: globURL, request: URL}, nil))
return hostConfig{}, NewIodine(iodine.New(errInvalidGlobURL{glob: globURL, request: URL}, nil))
}
if match {
if hostCfg == nil {
return nil, NewIodine(iodine.New(errInvalidAuth{}, nil))
}
return hostCfg, nil
}
}
return nil, NewIodine(iodine.New(errNoMatchingHost{}, nil))
return hostConfig{}, NewIodine(iodine.New(errNoMatchingHost{}, nil))
}