Add missing comments
This commit is contained in:
@@ -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]
|
||||
}
|
||||
@@ -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?
|
||||
@@ -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
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
@@ -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 {
|
||||
Reference in New Issue
Block a user