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.
373 lines
8.8 KiB
373 lines
8.8 KiB
9 years ago
|
/*
|
||
8 years ago
|
* Minio Cloud Storage, (C) 2016, 2017 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
8 years ago
|
"errors"
|
||
8 years ago
|
"fmt"
|
||
8 years ago
|
"io/ioutil"
|
||
9 years ago
|
"os"
|
||
8 years ago
|
"strings"
|
||
9 years ago
|
"sync"
|
||
|
|
||
|
"github.com/minio/minio/pkg/quick"
|
||
8 years ago
|
"github.com/tidwall/gjson"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
// Read Write mutex for safe access to ServerConfig.
|
||
|
var serverConfigMu sync.RWMutex
|
||
|
|
||
8 years ago
|
// Config version
|
||
8 years ago
|
var v15 = "15"
|
||
8 years ago
|
|
||
8 years ago
|
// serverConfigV15 server configuration version '15' which is like
|
||
|
// version '14' except it adds support of MySQL notifications.
|
||
|
type serverConfigV15 struct {
|
||
9 years ago
|
Version string `json:"version"`
|
||
|
|
||
|
// S3 API configuration.
|
||
|
Credential credential `json:"credential"`
|
||
|
Region string `json:"region"`
|
||
8 years ago
|
Browser string `json:"browser"`
|
||
9 years ago
|
|
||
|
// Additional error logging configuration.
|
||
8 years ago
|
Logger *logger `json:"logger"`
|
||
9 years ago
|
|
||
8 years ago
|
// Notification queue configuration.
|
||
8 years ago
|
Notify *notifier `json:"notify"`
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func newServerConfigV15() *serverConfigV15 {
|
||
|
srvCfg := &serverConfigV15{
|
||
|
Version: v15,
|
||
8 years ago
|
Region: globalMinioDefaultRegion,
|
||
|
Logger: &logger{},
|
||
|
Notify: ¬ifier{},
|
||
8 years ago
|
}
|
||
8 years ago
|
srvCfg.SetCredential(mustGetNewCredential())
|
||
8 years ago
|
srvCfg.SetBrowser("on")
|
||
8 years ago
|
// Enable console logger by default on a fresh run.
|
||
|
srvCfg.Logger.Console = consoleLogger{
|
||
|
Enable: true,
|
||
|
Level: "error",
|
||
|
}
|
||
|
|
||
|
// Make sure to initialize notification configs.
|
||
|
srvCfg.Notify.AMQP = make(map[string]amqpNotify)
|
||
|
srvCfg.Notify.AMQP["1"] = amqpNotify{}
|
||
|
srvCfg.Notify.ElasticSearch = make(map[string]elasticSearchNotify)
|
||
|
srvCfg.Notify.ElasticSearch["1"] = elasticSearchNotify{}
|
||
|
srvCfg.Notify.Redis = make(map[string]redisNotify)
|
||
|
srvCfg.Notify.Redis["1"] = redisNotify{}
|
||
|
srvCfg.Notify.NATS = make(map[string]natsNotify)
|
||
|
srvCfg.Notify.NATS["1"] = natsNotify{}
|
||
|
srvCfg.Notify.PostgreSQL = make(map[string]postgreSQLNotify)
|
||
|
srvCfg.Notify.PostgreSQL["1"] = postgreSQLNotify{}
|
||
8 years ago
|
srvCfg.Notify.MySQL = make(map[string]mySQLNotify)
|
||
|
srvCfg.Notify.MySQL["1"] = mySQLNotify{}
|
||
8 years ago
|
srvCfg.Notify.Kafka = make(map[string]kafkaNotify)
|
||
|
srvCfg.Notify.Kafka["1"] = kafkaNotify{}
|
||
|
srvCfg.Notify.Webhook = make(map[string]webhookNotify)
|
||
|
srvCfg.Notify.Webhook["1"] = webhookNotify{}
|
||
|
|
||
8 years ago
|
return srvCfg
|
||
|
}
|
||
|
|
||
|
// newConfig - initialize a new server config, saves env parameters if
|
||
|
// found, otherwise use default parameters
|
||
|
func newConfig(envParams envParams) error {
|
||
|
// Initialize server config.
|
||
8 years ago
|
srvCfg := newServerConfigV15()
|
||
8 years ago
|
|
||
|
// If env is set for a fresh start, save them to config file.
|
||
|
if globalIsEnvCreds {
|
||
|
srvCfg.SetCredential(envParams.creds)
|
||
|
}
|
||
|
|
||
|
if globalIsEnvBrowser {
|
||
|
srvCfg.SetBrowser(envParams.browser)
|
||
|
}
|
||
|
|
||
8 years ago
|
// Create config path.
|
||
8 years ago
|
if err := createConfigDir(); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
// hold the mutex lock before a new config is assigned.
|
||
|
// Save the new config globally.
|
||
|
// unlock the mutex.
|
||
|
serverConfigMu.Lock()
|
||
|
serverConfig = srvCfg
|
||
|
serverConfigMu.Unlock()
|
||
|
|
||
|
// Save config into file.
|
||
|
return serverConfig.Save()
|
||
|
}
|
||
|
|
||
8 years ago
|
// loadConfig - loads a new config from disk, overrides params from env
|
||
|
// if found and valid
|
||
|
func loadConfig(envParams envParams) error {
|
||
8 years ago
|
configFile := getConfigFile()
|
||
|
if _, err := os.Stat(configFile); err != nil {
|
||
8 years ago
|
return err
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
srvCfg := &serverConfigV15{}
|
||
8 years ago
|
|
||
9 years ago
|
qc, err := quick.New(srvCfg)
|
||
|
if err != nil {
|
||
8 years ago
|
return err
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
|
if err = qc.Load(configFile); err != nil {
|
||
8 years ago
|
return err
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// If env is set override the credentials from config file.
|
||
|
if globalIsEnvCreds {
|
||
8 years ago
|
srvCfg.SetCredential(envParams.creds)
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
if globalIsEnvBrowser {
|
||
|
srvCfg.SetBrowser(envParams.browser)
|
||
|
}
|
||
|
|
||
|
if strings.ToLower(srvCfg.GetBrowser()) == "off" {
|
||
|
globalIsBrowserEnabled = false
|
||
|
}
|
||
|
|
||
8 years ago
|
// hold the mutex lock before a new config is assigned.
|
||
|
serverConfigMu.Lock()
|
||
9 years ago
|
// Save the loaded config globally.
|
||
9 years ago
|
serverConfig = srvCfg
|
||
8 years ago
|
serverConfigMu.Unlock()
|
||
8 years ago
|
|
||
8 years ago
|
if serverConfig.Version != v15 {
|
||
8 years ago
|
return errors.New("Unsupported config version `" + serverConfig.Version + "`.")
|
||
|
}
|
||
|
|
||
8 years ago
|
return nil
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// doCheckDupJSONKeys recursively detects duplicate json keys
|
||
|
func doCheckDupJSONKeys(key, value gjson.Result) error {
|
||
|
// Key occurrences map of the current scope to count
|
||
|
// if there is any duplicated json key.
|
||
|
keysOcc := make(map[string]int)
|
||
|
|
||
|
// Holds the found error
|
||
|
var checkErr error
|
||
|
|
||
|
// Iterate over keys in the current json scope
|
||
|
value.ForEach(func(k, v gjson.Result) bool {
|
||
|
// If current key is not null, check if its
|
||
|
// value contains some duplicated keys.
|
||
|
if k.Type != gjson.Null {
|
||
|
keysOcc[k.String()]++
|
||
|
checkErr = doCheckDupJSONKeys(k, v)
|
||
|
}
|
||
|
return checkErr == nil
|
||
|
})
|
||
|
|
||
|
// Check found err
|
||
|
if checkErr != nil {
|
||
|
return errors.New(key.String() + " => " + checkErr.Error())
|
||
|
}
|
||
|
|
||
|
// Check for duplicated keys
|
||
|
for k, v := range keysOcc {
|
||
|
if v > 1 {
|
||
|
return errors.New(key.String() + " => `" + k + "` entry is duplicated")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Check recursively if a key is duplicated in the same json scope
|
||
|
// e.g.:
|
||
|
// `{ "key" : { "key" ..` is accepted
|
||
|
// `{ "key" : { "subkey" : "val1", "subkey": "val2" ..` throws subkey duplicated error
|
||
|
func checkDupJSONKeys(json string) error {
|
||
|
// Parse config with gjson library
|
||
|
config := gjson.Parse(json)
|
||
|
|
||
|
// Create a fake rootKey since root json doesn't seem to have representation
|
||
|
// in gjson library.
|
||
|
rootKey := gjson.Result{Type: gjson.String, Str: minioConfigFile}
|
||
|
|
||
|
// Check if loaded json contains any duplicated keys
|
||
|
return doCheckDupJSONKeys(rootKey, config)
|
||
|
}
|
||
|
|
||
|
// validateConfig checks for
|
||
|
func validateConfig() error {
|
||
|
|
||
|
// Get file config path
|
||
|
configFile := getConfigFile()
|
||
|
|
||
8 years ago
|
srvCfg := &serverConfigV15{}
|
||
8 years ago
|
|
||
|
// Load config file
|
||
|
qc, err := quick.New(srvCfg)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err = qc.Load(configFile); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Check if config version is valid
|
||
8 years ago
|
if srvCfg.GetVersion() != v15 {
|
||
|
return errors.New("bad config version, expected: " + v15)
|
||
8 years ago
|
}
|
||
|
|
||
|
// Load config file json and check for duplication json keys
|
||
|
jsonBytes, err := ioutil.ReadFile(configFile)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := checkDupJSONKeys(string(jsonBytes)); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Validate region field
|
||
|
if srvCfg.GetRegion() == "" {
|
||
8 years ago
|
return errors.New("Region config value cannot be empty")
|
||
8 years ago
|
}
|
||
|
|
||
|
// Validate browser field
|
||
|
if b := strings.ToLower(srvCfg.GetBrowser()); b != "on" && b != "off" {
|
||
8 years ago
|
return fmt.Errorf("Browser config value %s is invalid", b)
|
||
8 years ago
|
}
|
||
|
|
||
|
// Validate credential field
|
||
8 years ago
|
if !srvCfg.Credential.IsValid() {
|
||
|
return errors.New("invalid credential")
|
||
8 years ago
|
}
|
||
|
|
||
|
// Validate logger field
|
||
|
if err := srvCfg.Logger.Validate(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Validate notify field
|
||
|
if err := srvCfg.Notify.Validate(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// serverConfig server config.
|
||
8 years ago
|
var serverConfig *serverConfigV15
|
||
9 years ago
|
|
||
|
// GetVersion get current config version.
|
||
8 years ago
|
func (s serverConfigV15) GetVersion() string {
|
||
8 years ago
|
serverConfigMu.RLock()
|
||
|
defer serverConfigMu.RUnlock()
|
||
|
|
||
9 years ago
|
return s.Version
|
||
|
}
|
||
|
|
||
|
// SetRegion set new region.
|
||
8 years ago
|
func (s *serverConfigV15) SetRegion(region string) {
|
||
8 years ago
|
serverConfigMu.Lock()
|
||
|
defer serverConfigMu.Unlock()
|
||
|
|
||
8 years ago
|
// Empty region means "us-east-1" by default from S3 spec.
|
||
|
if region == "" {
|
||
|
region = "us-east-1"
|
||
|
}
|
||
9 years ago
|
s.Region = region
|
||
|
}
|
||
|
|
||
|
// GetRegion get current region.
|
||
8 years ago
|
func (s serverConfigV15) GetRegion() string {
|
||
8 years ago
|
serverConfigMu.RLock()
|
||
|
defer serverConfigMu.RUnlock()
|
||
|
|
||
8 years ago
|
if s.Region != "" {
|
||
|
return s.Region
|
||
|
} // region empty
|
||
|
|
||
|
// Empty region means "us-east-1" by default from S3 spec.
|
||
|
return "us-east-1"
|
||
9 years ago
|
}
|
||
|
|
||
|
// SetCredentials set new credentials.
|
||
8 years ago
|
func (s *serverConfigV15) SetCredential(creds credential) {
|
||
8 years ago
|
serverConfigMu.Lock()
|
||
|
defer serverConfigMu.Unlock()
|
||
|
|
||
8 years ago
|
// Set updated credential.
|
||
8 years ago
|
s.Credential = creds
|
||
9 years ago
|
}
|
||
|
|
||
|
// GetCredentials get current credentials.
|
||
8 years ago
|
func (s serverConfigV15) GetCredential() credential {
|
||
8 years ago
|
serverConfigMu.RLock()
|
||
|
defer serverConfigMu.RUnlock()
|
||
|
|
||
9 years ago
|
return s.Credential
|
||
|
}
|
||
|
|
||
8 years ago
|
// SetBrowser set if browser is enabled.
|
||
8 years ago
|
func (s *serverConfigV15) SetBrowser(v string) {
|
||
8 years ago
|
serverConfigMu.Lock()
|
||
|
defer serverConfigMu.Unlock()
|
||
|
|
||
|
// Set browser param
|
||
8 years ago
|
if v == "" {
|
||
|
v = "on" // Browser is on by default.
|
||
|
}
|
||
|
|
||
|
// Set the new value.
|
||
8 years ago
|
s.Browser = v
|
||
|
}
|
||
|
|
||
|
// GetCredentials get current credentials.
|
||
8 years ago
|
func (s serverConfigV15) GetBrowser() string {
|
||
8 years ago
|
serverConfigMu.RLock()
|
||
|
defer serverConfigMu.RUnlock()
|
||
|
|
||
8 years ago
|
if s.Browser != "" {
|
||
|
return s.Browser
|
||
|
} // empty browser.
|
||
|
|
||
|
// Empty browser means "on" by default.
|
||
|
return "on"
|
||
8 years ago
|
}
|
||
|
|
||
9 years ago
|
// Save config.
|
||
8 years ago
|
func (s serverConfigV15) Save() error {
|
||
8 years ago
|
serverConfigMu.RLock()
|
||
|
defer serverConfigMu.RUnlock()
|
||
9 years ago
|
|
||
|
// get config file.
|
||
8 years ago
|
configFile := getConfigFile()
|
||
9 years ago
|
|
||
|
// initialize quick.
|
||
|
qc, err := quick.New(&s)
|
||
|
if err != nil {
|
||
9 years ago
|
return err
|
||
9 years ago
|
}
|
||
|
|
||
|
// Save config file.
|
||
9 years ago
|
return qc.Save(configFile)
|
||
9 years ago
|
}
|