Fix for #439 - simplify config handling
This commit is contained in:
+21
-88
@@ -17,17 +17,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/minio-io/cli"
|
||||
"github.com/minio-io/mc/pkg/client"
|
||||
"github.com/minio-io/mc/pkg/console"
|
||||
"github.com/minio-io/mc/pkg/quick"
|
||||
"github.com/minio-io/minio/pkg/iodine"
|
||||
@@ -165,91 +162,6 @@ func newConfig() (config quick.Config) {
|
||||
return config
|
||||
}
|
||||
|
||||
// getHostURL -
|
||||
func getHostURL(u *url.URL) string {
|
||||
return u.Scheme + "://" + u.Host
|
||||
}
|
||||
|
||||
func getHostConfigs(requestURLs []string) (hostConfigs map[string]*hostConfig, err error) {
|
||||
hostConfigs = make(map[string]*hostConfig)
|
||||
for _, requestURL := range requestURLs {
|
||||
hostConfigs[requestURL], err = getHostConfig(requestURL)
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
}
|
||||
return hostConfigs, nil
|
||||
}
|
||||
|
||||
// getHostConfig retrieves host specific configuration such as access keys, certs.
|
||||
func getHostConfig(requestURL string) (*hostConfig, error) {
|
||||
config, err := getMcConfig()
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
u, err := url.Parse(requestURL)
|
||||
if err != nil {
|
||||
return nil, iodine.New(errInvalidURL{url: requestURL}, nil)
|
||||
}
|
||||
// No host matching or keys needed for filesystem requests
|
||||
if client.GetType(requestURL) == client.Filesystem {
|
||||
hostCfg := &hostConfig{
|
||||
AccessKeyID: "",
|
||||
SecretAccessKey: "",
|
||||
}
|
||||
return hostCfg, nil
|
||||
}
|
||||
|
||||
// No host matching or keys needed for localhost and 127.0.0.1 URL's skip them
|
||||
if strings.Contains(getHostURL(u), "localhost") || strings.Contains(getHostURL(u), "127.0.0.1") {
|
||||
hostCfg := &hostConfig{
|
||||
AccessKeyID: "",
|
||||
SecretAccessKey: "",
|
||||
}
|
||||
return hostCfg, nil
|
||||
}
|
||||
for globURL, hostCfg := range config.Hosts {
|
||||
match, err := filepath.Match(globURL, getHostURL(u))
|
||||
if err != nil {
|
||||
return nil, iodine.New(errInvalidGlobURL{glob: globURL, request: requestURL}, nil)
|
||||
}
|
||||
if match {
|
||||
if hostCfg == nil {
|
||||
return nil, iodine.New(errInvalidAuth{}, nil)
|
||||
}
|
||||
// verify Auth key validity for all hosts
|
||||
if hostCfg.AccessKeyID != globalAccessKeyID && hostCfg.SecretAccessKey != globalSecretAccessKey {
|
||||
if !client.IsValidAccessKey(hostCfg.AccessKeyID) || !client.IsValidSecretKey(hostCfg.SecretAccessKey) {
|
||||
return nil, iodine.New(errInvalidAuthKeys{}, nil)
|
||||
}
|
||||
}
|
||||
return hostCfg, nil
|
||||
}
|
||||
}
|
||||
return nil, iodine.New(errNoMatchingHost{}, nil)
|
||||
}
|
||||
|
||||
// saveConfig writes configuration data in json format to config file.
|
||||
func saveConfig(arg string, alias []string) error {
|
||||
switch arg {
|
||||
case "generate":
|
||||
if isMcConfigExist() {
|
||||
return iodine.New(errConfigExists{}, nil)
|
||||
}
|
||||
err := writeConfig(newConfig())
|
||||
if err != nil {
|
||||
return iodine.New(err, nil)
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
config, err := addAlias(alias)
|
||||
if err != nil {
|
||||
return iodine.New(err, nil)
|
||||
}
|
||||
return writeConfig(config)
|
||||
}
|
||||
}
|
||||
|
||||
func addAlias(alias []string) (quick.Config, error) {
|
||||
if len(alias) < 2 {
|
||||
return nil, iodine.New(errInvalidArgument{}, nil)
|
||||
@@ -294,6 +206,27 @@ func runConfigCmd(ctx *cli.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// saveConfig writes configuration data in json format to config file.
|
||||
func saveConfig(arg string, alias []string) error {
|
||||
switch arg {
|
||||
case "generate":
|
||||
if isMcConfigExist() {
|
||||
return iodine.New(errConfigExists{}, nil)
|
||||
}
|
||||
err := writeConfig(newConfig())
|
||||
if err != nil {
|
||||
return iodine.New(err, nil)
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
config, err := addAlias(alias)
|
||||
if err != nil {
|
||||
return iodine.New(err, nil)
|
||||
}
|
||||
return writeConfig(config)
|
||||
}
|
||||
}
|
||||
|
||||
// doConfig is the handler for "mc config" sub-command.
|
||||
func doConfig(arg string, alias []string) (string, error) {
|
||||
configPath, err := getMcConfigPath()
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Mini Copy (C) 2015 Minio, Inc.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Mini Copy (C) 2015 Minio, Inc.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/minio-io/mc/pkg/client"
|
||||
"github.com/minio-io/minio/pkg/iodine"
|
||||
)
|
||||
|
||||
// getHostURL -
|
||||
func getHostURL(u *url.URL) string {
|
||||
return u.Scheme + "://" + u.Host
|
||||
}
|
||||
|
||||
func getHostConfigs(requestURLs []string) (hostConfigs map[string]*hostConfig, err error) {
|
||||
hostConfigs = make(map[string]*hostConfig)
|
||||
for _, requestURL := range requestURLs {
|
||||
hostConfigs[requestURL], err = getHostConfig(requestURL)
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
}
|
||||
return hostConfigs, nil
|
||||
}
|
||||
|
||||
// getHostConfig retrieves host specific configuration such as access keys, certs.
|
||||
func getHostConfig(requestURL string) (*hostConfig, error) {
|
||||
config, err := getMcConfig()
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
u, err := url.Parse(requestURL)
|
||||
if err != nil {
|
||||
return nil, iodine.New(errInvalidURL{url: requestURL}, nil)
|
||||
}
|
||||
// No host matching or keys needed for filesystem requests
|
||||
if client.GetType(requestURL) == client.Filesystem {
|
||||
hostCfg := &hostConfig{
|
||||
AccessKeyID: "",
|
||||
SecretAccessKey: "",
|
||||
}
|
||||
return hostCfg, nil
|
||||
}
|
||||
|
||||
// No host matching or keys needed for localhost and 127.0.0.1 URL's skip them
|
||||
if strings.Contains(getHostURL(u), "localhost") || strings.Contains(getHostURL(u), "127.0.0.1") {
|
||||
hostCfg := &hostConfig{
|
||||
AccessKeyID: "",
|
||||
SecretAccessKey: "",
|
||||
}
|
||||
return hostCfg, nil
|
||||
}
|
||||
for globURL, hostCfg := range config.Hosts {
|
||||
match, err := filepath.Match(globURL, getHostURL(u))
|
||||
if err != nil {
|
||||
return nil, iodine.New(errInvalidGlobURL{glob: globURL, request: requestURL}, nil)
|
||||
}
|
||||
if match {
|
||||
if hostCfg == nil {
|
||||
return nil, iodine.New(errInvalidAuth{}, nil)
|
||||
}
|
||||
// verify Auth key validity for all hosts
|
||||
if hostCfg.AccessKeyID != globalAccessKeyID && hostCfg.SecretAccessKey != globalSecretAccessKey {
|
||||
if !client.IsValidAccessKey(hostCfg.AccessKeyID) || !client.IsValidSecretKey(hostCfg.SecretAccessKey) {
|
||||
return nil, iodine.New(errInvalidAuthKeys{}, nil)
|
||||
}
|
||||
}
|
||||
return hostCfg, nil
|
||||
}
|
||||
}
|
||||
return nil, iodine.New(errNoMatchingHost{}, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user