From b33e2d2f9b5fec060463c0f259197a83ed2ab0ce Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 5 Mar 2015 19:59:26 -0800 Subject: [PATCH] Golint cleanup pkg/utils/config --- pkg/api/minioapi/generic_handlers.go | 17 +++++++--- pkg/utils/config/config.go | 51 +++++----------------------- 2 files changed, 21 insertions(+), 47 deletions(-) diff --git a/pkg/api/minioapi/generic_handlers.go b/pkg/api/minioapi/generic_handlers.go index f8512630d..56f3e704a 100644 --- a/pkg/api/minioapi/generic_handlers.go +++ b/pkg/api/minioapi/generic_handlers.go @@ -63,15 +63,22 @@ func (h vHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(error.HttpStatusCode) w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) } else { - user := h.conf.GetKey(accessKey) - ok, _ := signers.ValidateRequest(user, r) - if ok { - h.handler.ServeHTTP(w, r) - } else { + user, ok := h.conf.Users[accessKey] + if ok == false { error := errorCodeError(AccessDenied) errorResponse := getErrorResponse(error, "") w.WriteHeader(error.HttpStatusCode) 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 { diff --git a/pkg/utils/config/config.go b/pkg/utils/config/config.go index 2173e1a8f..85cafe6f5 100644 --- a/pkg/utils/config/config.go +++ b/pkg/utils/config/config.go @@ -25,6 +25,7 @@ import ( "sync" ) +// Config context type Config struct { configPath string configFile string @@ -32,13 +33,14 @@ type Config struct { Users map[string]User } +// User context type User struct { Name string AccessKey string SecretKey string } -// Initialize config directory and template config +// SetupConfig initialize config directory and template config func (c *Config) SetupConfig() error { u, err := user.Current() if err != nil { @@ -63,12 +65,12 @@ func (c *Config) SetupConfig() error { return nil } -// Get config file location +// GetConfigPath config file location func (c *Config) GetConfigPath() string { return c.configPath } -// Verify if user exists +// IsUserExists verify if user exists func (c *Config) IsUserExists(username string) bool { for _, user := range c.Users { if user.Name == username { @@ -78,16 +80,7 @@ func (c *Config) IsUserExists(username string) bool { return false } -// Get user based on accesskey -func (c *Config) GetKey(accessKey string) User { - value, ok := c.Users[accessKey] - if !ok { - return User{} - } - return value -} - -// Get user based on username +// GetUser - get user from username func (c *Config) GetUser(username string) User { for _, user := range c.Users { if user.Name == username { @@ -97,7 +90,7 @@ func (c *Config) GetUser(username string) 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) { var currentUsers map[string]User if len(c.Users) == 0 { @@ -109,7 +102,7 @@ func (c *Config) AddUser(user User) { c.Users = currentUsers } -// Write encoded json in config file +// WriteConfig - write encoded json in config file func (c *Config) WriteConfig() error { c.configLock.Lock() defer c.configLock.Unlock() @@ -128,7 +121,7 @@ func (c *Config) WriteConfig() error { return nil } -// Read json config file and decode +// ReadConfig - read json config file and decode func (c *Config) ReadConfig() error { c.configLock.RLock() defer c.configLock.RUnlock() @@ -155,29 +148,3 @@ func (c *Config) ReadConfig() error { 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) -}