mirror of
https://github.com/minio/minio.git
synced 2025-11-07 12:52:58 -05:00
Refactor to move config handling into api/
This commit is contained in:
@@ -20,7 +20,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/minio-io/minio/pkg/utils/config"
|
||||
"github.com/minio-io/minio/pkg/api/config"
|
||||
)
|
||||
|
||||
type vHandler struct {
|
||||
|
||||
@@ -20,9 +20,9 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
x "github.com/gorilla/mux"
|
||||
router "github.com/gorilla/mux"
|
||||
"github.com/minio-io/minio/pkg/api/config"
|
||||
mstorage "github.com/minio-io/minio/pkg/storage"
|
||||
"github.com/minio-io/minio/pkg/utils/config"
|
||||
)
|
||||
|
||||
// private use
|
||||
@@ -32,7 +32,7 @@ type minioAPI struct {
|
||||
}
|
||||
|
||||
// Path based routing
|
||||
func pathMux(api minioAPI, mux *x.Router) *x.Router {
|
||||
func pathMux(api minioAPI, mux *router.Router) *router.Router {
|
||||
mux.HandleFunc("/", api.listBucketsHandler).Methods("GET")
|
||||
mux.HandleFunc("/{bucket}", api.listObjectsHandler).Methods("GET")
|
||||
mux.HandleFunc("/{bucket}", api.putBucketHandler).Methods("PUT")
|
||||
@@ -44,7 +44,7 @@ func pathMux(api minioAPI, mux *x.Router) *x.Router {
|
||||
}
|
||||
|
||||
// Domain based routing
|
||||
func domainMux(api minioAPI, mux *x.Router) *x.Router {
|
||||
func domainMux(api minioAPI, mux *router.Router) *router.Router {
|
||||
mux.HandleFunc("/",
|
||||
api.listObjectsHandler).Host("{bucket}" + "." + api.domain).Methods("GET")
|
||||
mux.HandleFunc("/{object:.*}",
|
||||
@@ -60,7 +60,7 @@ func domainMux(api minioAPI, mux *x.Router) *x.Router {
|
||||
}
|
||||
|
||||
// Get proper router based on domain availability
|
||||
func getMux(api minioAPI, mux *x.Router) *x.Router {
|
||||
func getMux(api minioAPI, mux *router.Router) *router.Router {
|
||||
switch true {
|
||||
case api.domain == "":
|
||||
return pathMux(api, mux)
|
||||
@@ -73,12 +73,12 @@ func getMux(api minioAPI, mux *x.Router) *x.Router {
|
||||
|
||||
// HTTPHandler - http wrapper handler
|
||||
func HTTPHandler(domain string, storage mstorage.Storage) http.Handler {
|
||||
var mux *x.Router
|
||||
var mux *router.Router
|
||||
var api = minioAPI{}
|
||||
api.storage = storage
|
||||
api.domain = domain
|
||||
|
||||
r := x.NewRouter()
|
||||
r := router.NewRouter()
|
||||
mux = getMux(api, r)
|
||||
|
||||
var conf = config.Config{}
|
||||
|
||||
@@ -18,20 +18,21 @@ package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio-io/minio/pkg/utils/config"
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/minio-io/minio/pkg/api/config"
|
||||
)
|
||||
|
||||
// SignRequest - a given http request using HMAC style signatures
|
||||
|
||||
150
pkg/api/config/config.go
Normal file
150
pkg/api/config/config.go
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Mini 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 (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Config context
|
||||
type Config struct {
|
||||
ConfigPath string
|
||||
ConfigFile string
|
||||
ConfigLock *sync.RWMutex
|
||||
Users map[string]User
|
||||
}
|
||||
|
||||
// User context
|
||||
type User struct {
|
||||
Name string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
}
|
||||
|
||||
// SetupConfig initialize config directory and template config
|
||||
func (c *Config) SetupConfig() error {
|
||||
u, err := user.Current()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
confPath := path.Join(u.HomeDir, ".minio")
|
||||
if err := os.MkdirAll(confPath, os.ModeDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.ConfigPath = confPath
|
||||
c.ConfigFile = path.Join(c.ConfigPath, "config.json")
|
||||
if _, err := os.Stat(c.ConfigFile); os.IsNotExist(err) {
|
||||
_, err = os.Create(c.ConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
c.ConfigLock = new(sync.RWMutex)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConfigPath config file location
|
||||
func (c *Config) GetConfigPath() string {
|
||||
return c.ConfigPath
|
||||
}
|
||||
|
||||
// IsUserExists verify if user exists
|
||||
func (c *Config) IsUserExists(username string) bool {
|
||||
for _, user := range c.Users {
|
||||
if user.Name == username {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// GetUser - get user from username
|
||||
func (c *Config) GetUser(username string) User {
|
||||
for _, user := range c.Users {
|
||||
if user.Name == username {
|
||||
return user
|
||||
}
|
||||
}
|
||||
return User{}
|
||||
}
|
||||
|
||||
// AddUser - add a user into existing User list
|
||||
func (c *Config) AddUser(user User) {
|
||||
var currentUsers map[string]User
|
||||
if len(c.Users) == 0 {
|
||||
currentUsers = make(map[string]User)
|
||||
} else {
|
||||
currentUsers = c.Users
|
||||
}
|
||||
currentUsers[user.AccessKey] = user
|
||||
c.Users = currentUsers
|
||||
}
|
||||
|
||||
// WriteConfig - write encoded json in config file
|
||||
func (c *Config) WriteConfig() error {
|
||||
c.ConfigLock.Lock()
|
||||
defer c.ConfigLock.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 err
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.Encode(c.Users)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadConfig - read json config file and decode
|
||||
func (c *Config) ReadConfig() error {
|
||||
c.ConfigLock.RLock()
|
||||
defer c.ConfigLock.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 err
|
||||
}
|
||||
|
||||
users := make(map[string]User)
|
||||
decoder := json.NewDecoder(file)
|
||||
err = decoder.Decode(&users)
|
||||
switch err {
|
||||
case io.EOF:
|
||||
return nil
|
||||
case nil:
|
||||
c.Users = users
|
||||
return nil
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
75
pkg/api/config/config_test.go
Normal file
75
pkg/api/config/config_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Mini 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"
|
||||
"path"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/minio-io/minio/pkg/utils/crypto/keys"
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
type MySuite struct{}
|
||||
|
||||
var _ = Suite(&MySuite{})
|
||||
|
||||
func Test(t *testing.T) { TestingT(t) }
|
||||
|
||||
func (s *MySuite) TestConfig(c *C) {
|
||||
conf := Config{}
|
||||
conf.ConfigLock = new(sync.RWMutex)
|
||||
conf.ConfigPath, _ = ioutil.TempDir("/tmp", "minio-test-")
|
||||
defer os.RemoveAll(conf.ConfigPath)
|
||||
conf.ConfigFile = path.Join(conf.ConfigPath, "config.json")
|
||||
if _, err := os.Stat(conf.ConfigFile); os.IsNotExist(err) {
|
||||
_, err = os.Create(conf.ConfigFile)
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
accesskey, _ := keys.GenerateRandomAlphaNumeric(keys.MinioAccessID)
|
||||
secretkey, _ := keys.GenerateRandomBase64(keys.MinioSecretID)
|
||||
|
||||
user := User{
|
||||
Name: "gnubot",
|
||||
AccessKey: string(accesskey),
|
||||
SecretKey: string(secretkey),
|
||||
}
|
||||
|
||||
conf.AddUser(user)
|
||||
err := conf.WriteConfig()
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
err = conf.ReadConfig()
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
accesskey, _ = keys.GenerateRandomAlphaNumeric(keys.MinioAccessID)
|
||||
secretkey, _ = keys.GenerateRandomBase64(keys.MinioSecretID)
|
||||
user = User{
|
||||
Name: "minio",
|
||||
AccessKey: string(accesskey),
|
||||
SecretKey: string(secretkey),
|
||||
}
|
||||
conf.AddUser(user)
|
||||
err = conf.WriteConfig()
|
||||
c.Assert(err, IsNil)
|
||||
}
|
||||
@@ -24,7 +24,7 @@ import (
|
||||
"path"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/minio-io/minio/pkg/utils/config"
|
||||
"github.com/minio-io/minio/pkg/api/config"
|
||||
"github.com/minio-io/minio/pkg/utils/crypto/keys"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user