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.
128 lines
3.1 KiB
128 lines
3.1 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
||
10 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
|
||
10 years ago
|
|
||
|
import (
|
||
9 years ago
|
"os"
|
||
10 years ago
|
"os/user"
|
||
|
"path/filepath"
|
||
|
|
||
9 years ago
|
"github.com/minio/minio-xl/pkg/probe"
|
||
|
"github.com/minio/minio-xl/pkg/quick"
|
||
10 years ago
|
)
|
||
|
|
||
9 years ago
|
// AuthConfig auth keys
|
||
|
type AuthConfig struct {
|
||
9 years ago
|
Version string `json:"version"`
|
||
|
AccessKeyID string `json:"accessKeyId"`
|
||
|
SecretAccessKey string `json:"secretAccessKey"`
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
// getAuthConfigPath get users config path
|
||
10 years ago
|
func getAuthConfigPath() (string, *probe.Error) {
|
||
10 years ago
|
if customConfigPath != "" {
|
||
|
return customConfigPath, nil
|
||
|
}
|
||
10 years ago
|
u, err := user.Current()
|
||
|
if err != nil {
|
||
10 years ago
|
return "", probe.NewError(err)
|
||
10 years ago
|
}
|
||
9 years ago
|
authConfigPath := filepath.Join(u.HomeDir, ".minio")
|
||
10 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()
|
||
|
}
|
||
9 years ago
|
return filepath.Join(authConfigPath, "fsUsers.json"), nil
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// customConfigPath for custom config path only for testing purposes
|
||
10 years ago
|
var customConfigPath string
|
||
|
|
||
9 years ago
|
// saveAuthConfig save auth config
|
||
|
func saveAuthConfig(a *AuthConfig) *probe.Error {
|
||
9 years ago
|
authConfigFile, err := getAuthConfigFile()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return err.Trace()
|
||
10 years ago
|
}
|
||
|
qc, err := quick.New(a)
|
||
|
if err != nil {
|
||
10 years ago
|
return err.Trace()
|
||
10 years ago
|
}
|
||
9 years ago
|
if err := qc.Save(authConfigFile); err != nil {
|
||
10 years ago
|
return err.Trace()
|
||
10 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// loadAuthConfig load auth config
|
||
|
func loadAuthConfig() (*AuthConfig, *probe.Error) {
|
||
9 years ago
|
authConfigFile, err := getAuthConfigFile()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return nil, err.Trace()
|
||
10 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 = "1"
|
||
10 years ago
|
qc, err := quick.New(a)
|
||
|
if err != nil {
|
||
10 years ago
|
return nil, err.Trace()
|
||
10 years ago
|
}
|
||
9 years ago
|
if err := qc.Load(authConfigFile); err != nil {
|
||
10 years ago
|
return nil, err.Trace()
|
||
10 years ago
|
}
|
||
9 years ago
|
return qc.Data().(*AuthConfig), nil
|
||
10 years ago
|
}
|