Add proper collection of config functions to manage mc configuration.

Not intergrated yet.
This commit is contained in:
Harshavardhana
2015-04-09 18:55:28 -07:00
parent 9e5b24ae41
commit c8292b0391
6 changed files with 318 additions and 0 deletions
+21
View File
@@ -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
}
+40
View File
@@ -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
}
+61
View File
@@ -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
}
+55
View File
@@ -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)
}
+102
View File
@@ -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
}
+39
View File
@@ -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")
}