You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.3 KiB
140 lines
3.3 KiB
9 years ago
|
/*
|
||
9 years ago
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
||
9 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
9 years ago
|
package main
|
||
9 years ago
|
|
||
|
import (
|
||
9 years ago
|
"os"
|
||
9 years ago
|
"os/user"
|
||
|
"path/filepath"
|
||
|
|
||
9 years ago
|
"github.com/minio/minio/pkg/probe"
|
||
9 years ago
|
"github.com/minio/minio/pkg/quick"
|
||
|
)
|
||
|
|
||
9 years ago
|
// AuthUser container
|
||
|
type AuthUser struct {
|
||
9 years ago
|
Name string
|
||
|
AccessKeyID string
|
||
|
SecretAccessKey string
|
||
|
}
|
||
|
|
||
9 years ago
|
// AuthConfig auth keys
|
||
|
type AuthConfig struct {
|
||
9 years ago
|
Version string
|
||
9 years ago
|
Users map[string]*AuthUser
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// getAuthConfigPath get users config path
|
||
9 years ago
|
func getAuthConfigPath() (string, *probe.Error) {
|
||
9 years ago
|
if customConfigPath != "" {
|
||
|
return customConfigPath, nil
|
||
|
}
|
||
9 years ago
|
u, err := user.Current()
|
||
|
if err != nil {
|
||
9 years ago
|
return "", probe.NewError(err)
|
||
9 years ago
|
}
|
||
9 years ago
|
authConfigPath := filepath.Join(u.HomeDir, ".minio")
|
||
9 years ago
|
return authConfigPath, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// createAuthConfigPath create users config path
|
||
|
func createAuthConfigPath() *probe.Error {
|
||
|
authConfigPath, err := getAuthConfigPath()
|
||
|
if err != nil {
|
||
|
return err.Trace()
|
||
|
}
|
||
|
if err := os.MkdirAll(authConfigPath, 0700); err != nil {
|
||
|
return probe.NewError(err)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// isAuthConfigFileExists is auth config file exists?
|
||
|
func isAuthConfigFileExists() bool {
|
||
|
if _, err := os.Stat(mustGetAuthConfigFile()); err != nil {
|
||
|
if os.IsNotExist(err) {
|
||
|
return false
|
||
|
}
|
||
|
panic(err)
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// mustGetAuthConfigFile always get users config file, if not panic
|
||
|
func mustGetAuthConfigFile() string {
|
||
|
authConfigFile, err := getAuthConfigFile()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return authConfigFile
|
||
|
}
|
||
|
|
||
|
// getAuthConfigFile get users config file
|
||
|
func getAuthConfigFile() (string, *probe.Error) {
|
||
|
authConfigPath, err := getAuthConfigPath()
|
||
|
if err != nil {
|
||
|
return "", err.Trace()
|
||
|
}
|
||
|
return filepath.Join(authConfigPath, "users.json"), nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// customConfigPath not accessed from outside only allowed through get/set methods
|
||
|
var customConfigPath string
|
||
|
|
||
|
// SetAuthConfigPath - set custom auth config path
|
||
|
func SetAuthConfigPath(configPath string) {
|
||
|
customConfigPath = configPath
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// SaveConfig save auth config
|
||
9 years ago
|
func SaveConfig(a *AuthConfig) *probe.Error {
|
||
9 years ago
|
authConfigFile, err := getAuthConfigFile()
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
return err.Trace()
|
||
9 years ago
|
}
|
||
|
qc, err := quick.New(a)
|
||
|
if err != nil {
|
||
9 years ago
|
return err.Trace()
|
||
9 years ago
|
}
|
||
9 years ago
|
if err := qc.Save(authConfigFile); err != nil {
|
||
9 years ago
|
return err.Trace()
|
||
9 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// LoadConfig load auth config
|
||
9 years ago
|
func LoadConfig() (*AuthConfig, *probe.Error) {
|
||
9 years ago
|
authConfigFile, err := getAuthConfigFile()
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
return nil, err.Trace()
|
||
9 years ago
|
}
|
||
9 years ago
|
if _, err := os.Stat(authConfigFile); err != nil {
|
||
|
return nil, probe.NewError(err)
|
||
|
}
|
||
9 years ago
|
a := &AuthConfig{}
|
||
9 years ago
|
a.Version = "0.0.1"
|
||
9 years ago
|
a.Users = make(map[string]*AuthUser)
|
||
9 years ago
|
qc, err := quick.New(a)
|
||
|
if err != nil {
|
||
9 years ago
|
return nil, err.Trace()
|
||
9 years ago
|
}
|
||
9 years ago
|
if err := qc.Load(authConfigFile); err != nil {
|
||
9 years ago
|
return nil, err.Trace()
|
||
9 years ago
|
}
|
||
9 years ago
|
return qc.Data().(*AuthConfig), nil
|
||
9 years ago
|
}
|