@ -19,7 +19,6 @@ package cmd
import (
"errors"
"fmt"
"io/ioutil"
"reflect"
"sync"
@ -27,7 +26,6 @@ import (
"github.com/minio/minio/pkg/event"
"github.com/minio/minio/pkg/event/target"
"github.com/minio/minio/pkg/quick"
"github.com/tidwall/gjson"
)
// Steps to move from version N to version N+1
@ -254,57 +252,6 @@ func newConfig() error {
return globalServerConfig . Save ( )
}
// 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 )
}
// getValidConfig - returns valid server configuration
func getValidConfig ( ) ( * serverConfig , error ) {
srvCfg := & serverConfig {
@ -312,8 +259,7 @@ func getValidConfig() (*serverConfig, error) {
Browser : true ,
}
configFile := getConfigFile ( )
if _ , err := quick . Load ( configFile , srvCfg ) ; err != nil {
if _ , err := quick . Load ( getConfigFile ( ) , srvCfg ) ; err != nil {
return nil , err
}
@ -321,21 +267,11 @@ func getValidConfig() (*serverConfig, error) {
return nil , fmt . Errorf ( "configuration version mismatch. Expected: ‘%s’, Got: ‘%s’" , serverConfigVersion , srvCfg . Version )
}
// Load config file json and check for duplication json keys
jsonBytes , err := ioutil . ReadFile ( configFile )
if err != nil {
return nil , err
}
if err = checkDupJSONKeys ( string ( jsonBytes ) ) ; err != nil {
return nil , err
}
// Validate credential fields only when
// they are not set via the environment
// Error out if global is env credential is not set and config has invalid credential
if ! globalIsEnvCreds && ! srvCfg . Credential . IsValid ( ) {
return nil , errors . New ( "invalid credential in config file " + configFile )
return nil , errors . New ( "invalid credential in config file " + getConfigFile ( ) )
}
return srvCfg , nil