mirror of
https://github.com/minio/minio.git
synced 2025-01-12 15:33:22 -05:00
Golint cleanup pkg/utils/config
This commit is contained in:
parent
861f13d5bd
commit
b33e2d2f9b
@ -63,15 +63,22 @@ func (h vHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(error.HttpStatusCode)
|
w.WriteHeader(error.HttpStatusCode)
|
||||||
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
||||||
} else {
|
} else {
|
||||||
user := h.conf.GetKey(accessKey)
|
user, ok := h.conf.Users[accessKey]
|
||||||
ok, _ := signers.ValidateRequest(user, r)
|
if ok == false {
|
||||||
if ok {
|
|
||||||
h.handler.ServeHTTP(w, r)
|
|
||||||
} else {
|
|
||||||
error := errorCodeError(AccessDenied)
|
error := errorCodeError(AccessDenied)
|
||||||
errorResponse := getErrorResponse(error, "")
|
errorResponse := getErrorResponse(error, "")
|
||||||
w.WriteHeader(error.HttpStatusCode)
|
w.WriteHeader(error.HttpStatusCode)
|
||||||
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
||||||
|
} else {
|
||||||
|
ok, _ = signers.ValidateRequest(user, r)
|
||||||
|
if ok {
|
||||||
|
h.handler.ServeHTTP(w, r)
|
||||||
|
} else {
|
||||||
|
error := errorCodeError(AccessDenied)
|
||||||
|
errorResponse := getErrorResponse(error, "")
|
||||||
|
w.WriteHeader(error.HttpStatusCode)
|
||||||
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -25,6 +25,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config context
|
||||||
type Config struct {
|
type Config struct {
|
||||||
configPath string
|
configPath string
|
||||||
configFile string
|
configFile string
|
||||||
@ -32,13 +33,14 @@ type Config struct {
|
|||||||
Users map[string]User
|
Users map[string]User
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// User context
|
||||||
type User struct {
|
type User struct {
|
||||||
Name string
|
Name string
|
||||||
AccessKey string
|
AccessKey string
|
||||||
SecretKey string
|
SecretKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize config directory and template config
|
// SetupConfig initialize config directory and template config
|
||||||
func (c *Config) SetupConfig() error {
|
func (c *Config) SetupConfig() error {
|
||||||
u, err := user.Current()
|
u, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -63,12 +65,12 @@ func (c *Config) SetupConfig() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get config file location
|
// GetConfigPath config file location
|
||||||
func (c *Config) GetConfigPath() string {
|
func (c *Config) GetConfigPath() string {
|
||||||
return c.configPath
|
return c.configPath
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify if user exists
|
// IsUserExists verify if user exists
|
||||||
func (c *Config) IsUserExists(username string) bool {
|
func (c *Config) IsUserExists(username string) bool {
|
||||||
for _, user := range c.Users {
|
for _, user := range c.Users {
|
||||||
if user.Name == username {
|
if user.Name == username {
|
||||||
@ -78,16 +80,7 @@ func (c *Config) IsUserExists(username string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user based on accesskey
|
// GetUser - get user from username
|
||||||
func (c *Config) GetKey(accessKey string) User {
|
|
||||||
value, ok := c.Users[accessKey]
|
|
||||||
if !ok {
|
|
||||||
return User{}
|
|
||||||
}
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get user based on username
|
|
||||||
func (c *Config) GetUser(username string) User {
|
func (c *Config) GetUser(username string) User {
|
||||||
for _, user := range c.Users {
|
for _, user := range c.Users {
|
||||||
if user.Name == username {
|
if user.Name == username {
|
||||||
@ -97,7 +90,7 @@ func (c *Config) GetUser(username string) User {
|
|||||||
return User{}
|
return User{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a new user into existing User list
|
// AddUser - add a user into existing User list
|
||||||
func (c *Config) AddUser(user User) {
|
func (c *Config) AddUser(user User) {
|
||||||
var currentUsers map[string]User
|
var currentUsers map[string]User
|
||||||
if len(c.Users) == 0 {
|
if len(c.Users) == 0 {
|
||||||
@ -109,7 +102,7 @@ func (c *Config) AddUser(user User) {
|
|||||||
c.Users = currentUsers
|
c.Users = currentUsers
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write encoded json in config file
|
// WriteConfig - write encoded json in config file
|
||||||
func (c *Config) WriteConfig() error {
|
func (c *Config) WriteConfig() error {
|
||||||
c.configLock.Lock()
|
c.configLock.Lock()
|
||||||
defer c.configLock.Unlock()
|
defer c.configLock.Unlock()
|
||||||
@ -128,7 +121,7 @@ func (c *Config) WriteConfig() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read json config file and decode
|
// ReadConfig - read json config file and decode
|
||||||
func (c *Config) ReadConfig() error {
|
func (c *Config) ReadConfig() error {
|
||||||
c.configLock.RLock()
|
c.configLock.RLock()
|
||||||
defer c.configLock.RUnlock()
|
defer c.configLock.RUnlock()
|
||||||
@ -155,29 +148,3 @@ func (c *Config) ReadConfig() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// helpers
|
|
||||||
|
|
||||||
// Load all users into memory
|
|
||||||
func Loadusers() map[string]User {
|
|
||||||
c := Config{}
|
|
||||||
c.SetupConfig()
|
|
||||||
c.ReadConfig()
|
|
||||||
return c.Users
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load a given user based on accessKey
|
|
||||||
func Loadkey(accessKeyId string) User {
|
|
||||||
c := Config{}
|
|
||||||
c.SetupConfig()
|
|
||||||
c.ReadConfig()
|
|
||||||
return c.GetKey(accessKeyId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load a given user based on username
|
|
||||||
func Loaduser(username string) User {
|
|
||||||
c := Config{}
|
|
||||||
c.SetupConfig()
|
|
||||||
c.ReadConfig()
|
|
||||||
return c.GetUser(username)
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user