From c8292b03910f6ca25e653a193b0b7f22d88bf9ee Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 9 Apr 2015 18:54:25 -0700 Subject: [PATCH] Add proper collection of config functions to manage mc configuration. Not intergrated yet. --- config/aliases.go | 21 +++++++++ config/auth.go | 40 +++++++++++++++++ config/config.go | 61 +++++++++++++++++++++++++ config/config_test.go | 55 +++++++++++++++++++++++ config/context.go | 102 ++++++++++++++++++++++++++++++++++++++++++ config/errors.go | 39 ++++++++++++++++ 6 files changed, 318 insertions(+) create mode 100644 config/aliases.go create mode 100644 config/auth.go create mode 100644 config/config.go create mode 100644 config/config_test.go create mode 100644 config/context.go create mode 100644 config/errors.go diff --git a/config/aliases.go b/config/aliases.go new file mode 100644 index 00000000..6b958d9e --- /dev/null +++ b/config/aliases.go @@ -0,0 +1,21 @@ +package config + +type Aliases map[string]string + +func (a Aliases) Set(aliasName, urlName string) { + a[aliasName] = urlName +} + +func (a Aliases) Get(aliasName string) string { + return a[aliasName] +} + +// IsExists verify if alias exists +func (a Aliases) IsExists(aliasName string) bool { + for alias := range a { + if alias == aliasName { + return true + } + } + return false +} diff --git a/config/auth.go b/config/auth.go new file mode 100644 index 00000000..a24fdf18 --- /dev/null +++ b/config/auth.go @@ -0,0 +1,40 @@ +package config + +type Auth struct { + AccessKeyID string + SecretAccessKey string +} + +// IsValidSecretKey - validate secret key +func (a Auth) IsValidSecretKey() bool { + if len(a.SecretAccessKey) != 40 { + return false + } + return true +} + +// IsValidAccessKey - validate access key +func (a Auth) IsValidAccessKey() bool { + if len(a.AccessKeyID) != 20 { + return false + } + // Is alphanumeric? + isalnum := func(c rune) bool { + return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' + } + for _, char := range a.AccessKeyID { + if isalnum(char) { + continue + } + switch char { + case '-': + case '.': + case '_': + case '~': + continue + default: + return false + } + } + return true +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 00000000..45f513be --- /dev/null +++ b/config/config.go @@ -0,0 +1,61 @@ +/* + * Minimalist Object Storage, (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package config + +import "github.com/minio-io/minio/pkg/iodine" + +// Config +type Config struct { + Version uint // Config version + Hosts map[string]Auth // URL and their respective Auth map + Aliases Aliases // aliasName and URL map +} + +// 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) + } + c.Aliases.Set(aliasName, aliasURL) + return nil +} + +func (c *Config) HostExists(hostURL string) bool { + for host := range c.Hosts { + if host == hostURL { + return true + } + } + return false +} + +func (c *Config) AddAuth(hostURL, accessKeyID, secretAccessKey string) error { + if c.HostExists(hostURL) { + return iodine.New(HostExists{Name: hostURL}, nil) + } + auth := Auth{ + AccessKeyID: accessKeyID, + SecretAccessKey: secretAccessKey, + } + if !auth.IsValidAccessKey() || !auth.IsValidSecretKey() { + return iodine.New(InvalidAuthKeys{}, nil) + } + c.Hosts = make(map[string]Auth) + c.Hosts[hostURL] = auth + return nil +} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 00000000..d9da7fb9 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,55 @@ +/* + * Minimalist Object Storage, (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package config + +import ( + "io/ioutil" + "os" + "testing" + + . "github.com/minio-io/check" +) + +type MySuite struct{} + +var _ = Suite(&MySuite{}) + +func Test(t *testing.T) { TestingT(t) } + +func (s *MySuite) TestConfig(c *C) { + configPath, _ := ioutil.TempDir("/tmp", "minio-test-") + ctx, err := New(configPath, "config.json") + c.Assert(err, IsNil) + defer os.RemoveAll(configPath) + + ctx.Config = new(Config) + + err = ctx.Config.AddAuth("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") + c.Assert(err, IsNil) + + err = ctx.Config.AddAlias("s3", "https://s3.amazonaws.com") + c.Assert(err, IsNil) + + err = ctx.SaveConfig() + c.Assert(err, IsNil) + + err = ctx.LoadConfig() + c.Assert(err, IsNil) +} diff --git a/config/context.go b/config/context.go new file mode 100644 index 00000000..3ad9f7fe --- /dev/null +++ b/config/context.go @@ -0,0 +1,102 @@ +package config + +import ( + "encoding/json" + "io" + "os" + "path" + "strings" + "sync" + + "github.com/minio-io/minio/pkg/iodine" +) + +// Context - config context +type Context struct { + // copy of Config + Config *Config + + // file and path for config + configFile string + configPath string + + // lock + lock *sync.RWMutex +} + +// GetConfigPath path to config file +func (c *Context) GetConfigPath() string { + return c.configPath +} + +// GetConfigFile config file +func (c *Context) GetConfigFile() string { + return c.configFile +} + +// SaveConfig - write encoded json in config file +func (c *Context) SaveConfig() error { + c.lock.Lock() + defer c.lock.Unlock() + + var file *os.File + var err error + + file, err = os.OpenFile(c.configFile, os.O_WRONLY, 0666) + defer file.Close() + if err != nil { + return iodine.New(err, nil) + } + + encoder := json.NewEncoder(file) + encoder.Encode(c.Config) + return nil +} + +// LoadConfig - read json config file and decode +func (c *Context) LoadConfig() error { + c.lock.RLock() + defer c.lock.RUnlock() + + var file *os.File + var err error + + file, err = os.OpenFile(c.configFile, os.O_RDONLY, 0666) + defer file.Close() + if err != nil { + return iodine.New(err, nil) + } + + config := new(Config) + decoder := json.NewDecoder(file) + err = decoder.Decode(&config) + switch err { + case io.EOF: + return nil + case nil: + c.Config = config + return nil + default: + return iodine.New(err, nil) + } +} + +func New(configPath, configFileName string) (*Context, error) { + if configPath == "" || strings.TrimSpace(configPath) == "" { + return nil, iodine.New(InvalidArgument{}, nil) + } + if configFileName == "" || strings.TrimSpace(configFileName) == "" { + return nil, iodine.New(InvalidArgument{}, nil) + } + c := new(Context) + c.configPath = configPath + c.configFile = path.Join(c.configPath, configFileName) + if _, err := os.Stat(c.configFile); os.IsNotExist(err) { + _, err = os.Create(c.configFile) + if err != nil { + return nil, iodine.New(err, nil) + } + } + c.lock = new(sync.RWMutex) + return c, nil +} diff --git a/config/errors.go b/config/errors.go new file mode 100644 index 00000000..3fa052a0 --- /dev/null +++ b/config/errors.go @@ -0,0 +1,39 @@ +package config + +import "fmt" + +type AliasExists struct { + Name string +} + +func (e AliasExists) Error() string { + return fmt.Sprintf("alias: %s exists", e.Name) +} + +type AliasNotFound struct { + Name string +} + +func (e AliasNotFound) Error() string { + return fmt.Sprintf("alias: %s exists", e.Name) +} + +type InvalidAuthKeys struct{} + +func (e InvalidAuthKeys) Error() string { + return fmt.Sprintf("invalid authorization key") +} + +type HostExists struct { + Name string +} + +func (e HostExists) Error() string { + return fmt.Sprintf("host: %s exists", e.Name) +} + +type InvalidArgument struct{} + +func (e InvalidArgument) Error() string { + return fmt.Sprintf("invalid argument") +}