diff --git a/config/aliases.go b/pkg/config/aliases.go similarity index 84% rename from config/aliases.go rename to pkg/config/aliases.go index 6b958d9e..9b39aacf 100644 --- a/config/aliases.go +++ b/pkg/config/aliases.go @@ -1,11 +1,14 @@ package config +// Aliases - keep a map type Aliases map[string]string +// Set - set an alias func (a Aliases) Set(aliasName, urlName string) { a[aliasName] = urlName } +// Get - get an alias func (a Aliases) Get(aliasName string) string { return a[aliasName] } diff --git a/config/auth.go b/pkg/config/auth.go similarity index 75% rename from config/auth.go rename to pkg/config/auth.go index a24fdf18..85056ab6 100644 --- a/config/auth.go +++ b/pkg/config/auth.go @@ -1,13 +1,20 @@ package config +// Auth - authorization keys type Auth struct { AccessKeyID string SecretAccessKey string } +// exact key length +const ( + AccessKeyLength = 20 + SecretAccessLength = 40 +) + // IsValidSecretKey - validate secret key func (a Auth) IsValidSecretKey() bool { - if len(a.SecretAccessKey) != 40 { + if len(a.SecretAccessKey) != SecretAccessLength { return false } return true @@ -15,7 +22,7 @@ func (a Auth) IsValidSecretKey() bool { // IsValidAccessKey - validate access key func (a Auth) IsValidAccessKey() bool { - if len(a.AccessKeyID) != 20 { + if len(a.AccessKeyID) != AccessKeyLength { return false } // Is alphanumeric? diff --git a/config/config.go b/pkg/config/config.go similarity index 89% rename from config/config.go rename to pkg/config/config.go index 45f513be..2c7ab1b4 100644 --- a/config/config.go +++ b/pkg/config/config.go @@ -18,7 +18,7 @@ package config import "github.com/minio-io/minio/pkg/iodine" -// Config +// Config - configuration type Config struct { Version uint // Config version Hosts map[string]Auth // URL and their respective Auth map @@ -27,7 +27,6 @@ type Config struct { // AddAlias - add a alias into existing alias list func (c *Config) AddAlias(aliasName string, aliasURL string) error { - c.Aliases = make(Aliases) if c.Aliases.IsExists(aliasName) { return iodine.New(AliasExists{Name: aliasName}, nil) } @@ -35,6 +34,7 @@ func (c *Config) AddAlias(aliasName string, aliasURL string) error { return nil } +// HostExists - check if host exists func (c *Config) HostExists(hostURL string) bool { for host := range c.Hosts { if host == hostURL { @@ -44,7 +44,8 @@ func (c *Config) HostExists(hostURL string) bool { return false } -func (c *Config) AddAuth(hostURL, accessKeyID, secretAccessKey string) error { +// AddHostAuth - add host authorization +func (c *Config) AddHostAuth(hostURL, accessKeyID, secretAccessKey string) error { if c.HostExists(hostURL) { return iodine.New(HostExists{Name: hostURL}, nil) } @@ -55,6 +56,7 @@ func (c *Config) AddAuth(hostURL, accessKeyID, secretAccessKey string) error { if !auth.IsValidAccessKey() || !auth.IsValidSecretKey() { return iodine.New(InvalidAuthKeys{}, nil) } + c.Aliases = make(Aliases) c.Hosts = make(map[string]Auth) c.Hosts[hostURL] = auth return nil diff --git a/config/config_test.go b/pkg/config/config_test.go similarity index 70% rename from config/config_test.go rename to pkg/config/config_test.go index d9da7fb9..0a8d6921 100644 --- a/config/config_test.go +++ b/pkg/config/config_test.go @@ -31,25 +31,36 @@ var _ = Suite(&MySuite{}) func Test(t *testing.T) { TestingT(t) } func (s *MySuite) TestConfig(c *C) { + // create new config.json configPath, _ := ioutil.TempDir("/tmp", "minio-test-") ctx, err := New(configPath, "config.json") c.Assert(err, IsNil) defer os.RemoveAll(configPath) + // initialize config ctx.Config = new(Config) - err = ctx.Config.AddAuth("http*://s3*.amazonaws.com", "YOUR-ACCESS-KEY-ID-HERE", "YOUR-SECRET-ACCESS-KEY-HERE") + // error on invalid auth keys + err = ctx.Config.AddHostAuth("http*://s3*.amazonaws.com", "YOUR-ACCESS-KEY-ID-HERE", "YOUR-SECRET-ACCESS-KEY-HERE") c.Assert(err, Not(IsNil)) - err = ctx.Config.AddAuth("http*://s3*.amazonaws.com", "AC5NH40NQLTL4D2W92PM", "0nAMx5oJbWx5IgCmOJJneXM8w/ohTz2b0QAb2xvN") + // success on valid auth keys + err = ctx.Config.AddHostAuth("http*://s3*.amazonaws.com", "AC5NH40NQLTL4D2W92PM", "0nAMx5oJbWx5IgCmOJJneXM8w/ohTz2b0QAb2xvN") c.Assert(err, IsNil) + // success on add new alias err = ctx.Config.AddAlias("s3", "https://s3.amazonaws.com") c.Assert(err, IsNil) + // error on duplicate alias + err = ctx.Config.AddAlias("s3", "https://s3.amazonaws.com") + c.Assert(err, Not(IsNil)) + + // save config err = ctx.SaveConfig() c.Assert(err, IsNil) + // load config err = ctx.LoadConfig() c.Assert(err, IsNil) } diff --git a/config/context.go b/pkg/config/context.go similarity index 97% rename from config/context.go rename to pkg/config/context.go index 3ad9f7fe..a94d7fd1 100644 --- a/config/context.go +++ b/pkg/config/context.go @@ -81,6 +81,7 @@ func (c *Context) LoadConfig() error { } } +// New - instantiate a new config context func New(configPath, configFileName string) (*Context, error) { if configPath == "" || strings.TrimSpace(configPath) == "" { return nil, iodine.New(InvalidArgument{}, nil) diff --git a/config/errors.go b/pkg/config/errors.go similarity index 78% rename from config/errors.go rename to pkg/config/errors.go index 3fa052a0..a8f7a245 100644 --- a/config/errors.go +++ b/pkg/config/errors.go @@ -2,6 +2,7 @@ package config import "fmt" +// AliasExists - alias exists type AliasExists struct { Name string } @@ -10,6 +11,7 @@ func (e AliasExists) Error() string { return fmt.Sprintf("alias: %s exists", e.Name) } +// AliasNotFound - alias not found type AliasNotFound struct { Name string } @@ -18,12 +20,14 @@ func (e AliasNotFound) Error() string { return fmt.Sprintf("alias: %s exists", e.Name) } +// InvalidAuthKeys - invalid authorization keys type InvalidAuthKeys struct{} func (e InvalidAuthKeys) Error() string { return fmt.Sprintf("invalid authorization key") } +// HostExists - host exists type HostExists struct { Name string } @@ -32,6 +36,7 @@ func (e HostExists) Error() string { return fmt.Sprintf("host: %s exists", e.Name) } +// InvalidArgument - invalid argument type InvalidArgument struct{} func (e InvalidArgument) Error() string {