admin: Simplify client initialization (#1951)
newAdminClient() is added to simplify initialization and take care of ssl connections and tracing http request
This commit is contained in:
committed by
Harshavardhana
parent
a2f25c2f3b
commit
c6cb6595be
+2
-22
@@ -19,7 +19,6 @@ package cmd
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
@@ -116,31 +115,12 @@ func mainAdminLockClear(ctx *cli.Context) error {
|
||||
olderThan, e := time.ParseDuration(ctx.String("older-than"))
|
||||
fatalIf(probe.NewError(e), "Unable to parse the passed older-than flag.")
|
||||
|
||||
// Fetch the server's address stored in config
|
||||
_, _, hostCfg, err := expandAlias(aliasedURL)
|
||||
// Create a new Minio Admin Client
|
||||
client, err := newAdminClient(aliasedURL)
|
||||
if err != nil {
|
||||
return err.ToGoError()
|
||||
}
|
||||
|
||||
// Check if alias exists
|
||||
if hostCfg == nil {
|
||||
fatalIf(errInvalidAliasedURL(aliasedURL).Trace(aliasedURL), "The specified alias is not found.")
|
||||
}
|
||||
|
||||
// Parse the server address
|
||||
url, e := url.Parse(hostCfg.URL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
isSSL := (url.Scheme == "https")
|
||||
|
||||
// Create a new Minio Admin Client
|
||||
client, e := madmin.New(url.Host, hostCfg.AccessKey, hostCfg.SecretKey, isSSL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
aliasedURL = filepath.ToSlash(aliasedURL)
|
||||
|
||||
splits := splitStr(aliasedURL, "/", 3)
|
||||
|
||||
+2
-22
@@ -19,7 +19,6 @@ package cmd
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
@@ -116,31 +115,12 @@ func mainAdminLockList(ctx *cli.Context) error {
|
||||
olderThan, e := time.ParseDuration(ctx.String("older-than"))
|
||||
fatalIf(probe.NewError(e), "Unable to parse the passed older-than flag.")
|
||||
|
||||
// Fetch the server's address stored in config
|
||||
_, _, hostCfg, err := expandAlias(aliasedURL)
|
||||
// Create a new Minio Admin Client
|
||||
client, err := newAdminClient(aliasedURL)
|
||||
if err != nil {
|
||||
return err.ToGoError()
|
||||
}
|
||||
|
||||
// Check if alias exists
|
||||
if hostCfg == nil {
|
||||
fatalIf(errInvalidAliasedURL(aliasedURL).Trace(aliasedURL), "The specified alias is not found.")
|
||||
}
|
||||
|
||||
// Parse the server address
|
||||
url, e := url.Parse(hostCfg.URL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
isSSL := (url.Scheme == "https")
|
||||
|
||||
// Create a new Minio Admin Client
|
||||
client, e := madmin.New(url.Host, hostCfg.AccessKey, hostCfg.SecretKey, isSSL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
aliasedURL = filepath.ToSlash(aliasedURL)
|
||||
|
||||
splits := splitStr(aliasedURL, "/", 3)
|
||||
|
||||
@@ -17,10 +17,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
)
|
||||
|
||||
@@ -69,33 +66,13 @@ func mainAdminServiceRestart(ctx *cli.Context) error {
|
||||
args := ctx.Args()
|
||||
aliasedURL := args.Get(0)
|
||||
|
||||
// Fetch the server's address stored in config
|
||||
_, _, hostCfg, err := expandAlias(aliasedURL)
|
||||
client, err := newAdminClient(aliasedURL)
|
||||
if err != nil {
|
||||
return err.ToGoError()
|
||||
}
|
||||
|
||||
// Check if alias exists
|
||||
if hostCfg == nil {
|
||||
fatalIf(errInvalidAliasedURL(aliasedURL).Trace(aliasedURL), "The specified alias is not found.")
|
||||
}
|
||||
|
||||
// Parse the server address
|
||||
url, e := url.Parse(hostCfg.URL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
isSSL := (url.Scheme == "https")
|
||||
|
||||
// Create a new Minio Admin Client
|
||||
client, e := madmin.New(url.Host, hostCfg.AccessKey, hostCfg.SecretKey, isSSL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
// Restart the specified Minio server
|
||||
e = client.ServiceRestart()
|
||||
e := client.ServiceRestart()
|
||||
fatalIf(probe.NewError(e), "Cannot restart server.")
|
||||
|
||||
return nil
|
||||
|
||||
@@ -19,7 +19,6 @@ package cmd
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
"github.com/minio/cli"
|
||||
@@ -96,31 +95,12 @@ func mainAdminServiceStatus(ctx *cli.Context) error {
|
||||
args := ctx.Args()
|
||||
aliasedURL := args.Get(0)
|
||||
|
||||
// Fetch the server's address stored in config
|
||||
_, _, hostCfg, err := expandAlias(aliasedURL)
|
||||
// Create a new Minio Admin Client
|
||||
client, err := newAdminClient(aliasedURL)
|
||||
if err != nil {
|
||||
return err.ToGoError()
|
||||
}
|
||||
|
||||
// Check if alias exists
|
||||
if hostCfg == nil {
|
||||
fatalIf(errInvalidAliasedURL(aliasedURL).Trace(aliasedURL), "The specified alias is not found.")
|
||||
}
|
||||
|
||||
// Parse the server address
|
||||
url, e := url.Parse(hostCfg.URL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
isSSL := (url.Scheme == "https")
|
||||
|
||||
// Create a new Minio Admin Client
|
||||
client, e := madmin.New(url.Host, hostCfg.AccessKey, hostCfg.SecretKey, isSSL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
// Fetch the storage info of the specified Minio server
|
||||
status, e := client.ServiceStatus()
|
||||
fatalIf(probe.NewError(e), "Cannot get service status.")
|
||||
|
||||
@@ -17,10 +17,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
)
|
||||
|
||||
@@ -69,33 +66,14 @@ func mainAdminServiceStop(ctx *cli.Context) error {
|
||||
args := ctx.Args()
|
||||
aliasedURL := args.Get(0)
|
||||
|
||||
// Fetch the server's address stored in config
|
||||
_, _, hostCfg, err := expandAlias(aliasedURL)
|
||||
// Create a new Minio Admin Client
|
||||
client, err := newAdminClient(aliasedURL)
|
||||
if err != nil {
|
||||
return err.ToGoError()
|
||||
}
|
||||
|
||||
// Check if alias exists
|
||||
if hostCfg == nil {
|
||||
fatalIf(errInvalidAliasedURL(aliasedURL).Trace(aliasedURL), "The specified alias is not found.")
|
||||
}
|
||||
|
||||
// Parse the server address
|
||||
url, e := url.Parse(hostCfg.URL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
isSSL := (url.Scheme == "https")
|
||||
|
||||
// Create a new Minio Admin Client
|
||||
client, e := madmin.New(url.Host, hostCfg.AccessKey, hostCfg.SecretKey, isSSL)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
// Stop the specified Minio server
|
||||
e = client.ServiceStop()
|
||||
e := client.ServiceStop()
|
||||
fatalIf(probe.NewError(e), "Cannot stop server.")
|
||||
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Minio Client (C) 2017 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 cmd
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"hash/fnv"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
|
||||
"github.com/minio/mc/pkg/httptracer"
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
)
|
||||
|
||||
// newAdminFactory encloses New function with client cache.
|
||||
func newAdminFactory() func(config *Config) (*madmin.AdminClient, *probe.Error) {
|
||||
clientCache := make(map[uint32]*madmin.AdminClient)
|
||||
mutex := &sync.Mutex{}
|
||||
|
||||
// Return New function.
|
||||
return func(config *Config) (*madmin.AdminClient, *probe.Error) {
|
||||
// Creates a parsed URL.
|
||||
targetURL, _ := url.Parse(config.HostURL)
|
||||
// By default enable HTTPs.
|
||||
useTLS := true
|
||||
if targetURL.Scheme == "http" {
|
||||
useTLS = false
|
||||
}
|
||||
|
||||
// Save if target supports virtual host style.
|
||||
hostName := targetURL.Host
|
||||
|
||||
// Generate a hash out of s3Conf.
|
||||
confHash := fnv.New32a()
|
||||
confHash.Write([]byte(hostName + config.AccessKey + config.SecretKey))
|
||||
confSum := confHash.Sum32()
|
||||
|
||||
// Lookup previous cache by hash.
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
var api *madmin.AdminClient
|
||||
var found bool
|
||||
if api, found = clientCache[confSum]; !found {
|
||||
// Not found. Instantiate a new minio
|
||||
var e error
|
||||
api, e = madmin.New(hostName, config.AccessKey, config.SecretKey, useTLS)
|
||||
if e != nil {
|
||||
return nil, probe.NewError(e)
|
||||
}
|
||||
|
||||
transport := http.DefaultTransport
|
||||
|
||||
if useTLS {
|
||||
tlsConfig := &tls.Config{RootCAs: globalRootCAs}
|
||||
if config.Insecure {
|
||||
tlsConfig.InsecureSkipVerify = true
|
||||
}
|
||||
transport = &http.Transport{
|
||||
TLSClientConfig: tlsConfig,
|
||||
}
|
||||
}
|
||||
|
||||
if config.Debug {
|
||||
transport = httptracer.GetNewTraceTransport(newTraceV4(), transport)
|
||||
}
|
||||
|
||||
// Set custom transport.
|
||||
api.SetCustomTransport(transport)
|
||||
|
||||
// Set app info.
|
||||
api.SetAppInfo(config.AppName, config.AppVersion)
|
||||
|
||||
// Cache the new minio client with hash of config as key.
|
||||
clientCache[confSum] = api
|
||||
}
|
||||
|
||||
// Store the new api object.
|
||||
return api, nil
|
||||
}
|
||||
}
|
||||
|
||||
// newAdminClientFromAlias gives a new admin client interface for matching
|
||||
// alias entry in the mc config file. If no matching host config entry
|
||||
// is found, fs client is returned.
|
||||
func newAdminClientFromAlias(alias string, urlStr string) (*madmin.AdminClient, *probe.Error) {
|
||||
s3Config := buildConfig(alias, urlStr)
|
||||
s3Client, err := s3AdminNew(s3Config)
|
||||
if err != nil {
|
||||
return nil, err.Trace(alias, urlStr)
|
||||
}
|
||||
return s3Client, nil
|
||||
}
|
||||
|
||||
// newAdminClient gives a new client interface
|
||||
func newAdminClient(aliasedURL string) (*madmin.AdminClient, *probe.Error) {
|
||||
alias, urlStrFull, hostCfg, err := expandAlias(aliasedURL)
|
||||
if err != nil {
|
||||
return nil, err.Trace(aliasedURL)
|
||||
}
|
||||
// Verify if the aliasedURL is a real URL, fail in those cases
|
||||
// indicating the user to add alias.
|
||||
if hostCfg == nil && urlRgx.MatchString(aliasedURL) {
|
||||
return nil, errInvalidAliasedURL(aliasedURL).Trace(aliasedURL)
|
||||
}
|
||||
return newAdminClientFromAlias(alias, urlStrFull)
|
||||
}
|
||||
|
||||
// s3AdminNew returns an initialized minioAdmin structure. If debug is enabled,
|
||||
// it also enables an internal trace transport.
|
||||
var s3AdminNew = newAdminFactory()
|
||||
+2
-41
@@ -18,13 +18,10 @@ package cmd
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
)
|
||||
|
||||
@@ -149,8 +146,8 @@ func uploadSourceToTargetURL(urls URLs, progress io.Reader) URLs {
|
||||
// alias entry in the mc config file. If no matching host config entry
|
||||
// is found, fs client is returned.
|
||||
func newClientFromAlias(alias string, urlStr string) (Client, *probe.Error) {
|
||||
hostCfg := mustGetHostConfig(alias)
|
||||
if hostCfg == nil {
|
||||
s3Config := buildConfig(alias, urlStr)
|
||||
if s3Config == nil {
|
||||
// No matching host config. So we treat it like a
|
||||
// filesystem.
|
||||
fsClient, err := fsNew(urlStr)
|
||||
@@ -160,42 +157,6 @@ func newClientFromAlias(alias string, urlStr string) (Client, *probe.Error) {
|
||||
return fsClient, nil
|
||||
}
|
||||
|
||||
// We have a valid alias and hostConfig. We populate the
|
||||
// credentials from the match found in the config file.
|
||||
s3Config := new(Config)
|
||||
|
||||
// secretKey retrieved from the environement overrides the one
|
||||
// present in the config file
|
||||
keysPairEnv := os.Getenv("MC_SECRET_" + alias)
|
||||
keysPairArray := strings.Split(keysPairEnv, ":")
|
||||
var accessKeyEnv, secretKeyEnv string
|
||||
if len(keysPairArray) >= 1 {
|
||||
accessKeyEnv = keysPairArray[0]
|
||||
}
|
||||
if len(keysPairArray) >= 2 {
|
||||
secretKeyEnv = keysPairArray[1]
|
||||
}
|
||||
if len(keysPairEnv) > 0 &&
|
||||
isValidAccessKey(accessKeyEnv) && isValidSecretKey(secretKeyEnv) {
|
||||
s3Config.AccessKey = accessKeyEnv
|
||||
s3Config.SecretKey = secretKeyEnv
|
||||
} else {
|
||||
if len(keysPairEnv) > 0 {
|
||||
console.Errorln("Access/Secret keys associated to `" + alias + "' " +
|
||||
"are found in your environment but not suitable for use. " +
|
||||
"Falling back to the standard config.")
|
||||
}
|
||||
s3Config.AccessKey = hostCfg.AccessKey
|
||||
s3Config.SecretKey = hostCfg.SecretKey
|
||||
}
|
||||
|
||||
s3Config.Signature = hostCfg.API
|
||||
s3Config.AppName = "mc"
|
||||
s3Config.AppVersion = Version
|
||||
s3Config.AppComments = []string{os.Args[0], runtime.GOOS, runtime.GOARCH}
|
||||
s3Config.HostURL = urlStr
|
||||
s3Config.Debug = globalDebug
|
||||
s3Config.Insecure = globalInsecure
|
||||
s3Client, err := s3New(s3Config)
|
||||
if err != nil {
|
||||
return nil, err.Trace(alias, urlStr)
|
||||
|
||||
+5
-5
@@ -69,11 +69,11 @@ var (
|
||||
|
||||
// Set global states. NOTE: It is deliberately kept monolithic to ensure we dont miss out any flags.
|
||||
func setGlobals(quiet, debug, json, noColor, insecure bool) {
|
||||
globalQuiet = quiet
|
||||
globalDebug = debug
|
||||
globalJSON = json
|
||||
globalNoColor = noColor
|
||||
globalInsecure = insecure
|
||||
globalQuiet = globalQuiet || quiet
|
||||
globalDebug = globalDebug || debug
|
||||
globalJSON = globalJSON || json
|
||||
globalNoColor = globalNoColor || noColor
|
||||
globalInsecure = globalInsecure || insecure
|
||||
|
||||
// Enable debug messages if requested.
|
||||
if globalDebug {
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -87,3 +88,44 @@ func splitStr(path, sep string, n int) []string {
|
||||
}
|
||||
return splits
|
||||
}
|
||||
|
||||
// buildConfig fetches config related to the specified alias
|
||||
// to create a new config structure
|
||||
func buildConfig(alias, urlStr string) *Config {
|
||||
hostCfg := mustGetHostConfig(alias)
|
||||
if hostCfg == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// We have a valid alias and hostConfig. We populate the
|
||||
// credentials from the match found in the config file.
|
||||
s3Config := new(Config)
|
||||
|
||||
// Fetch keys from the environnement, otherwise, get them from the config file
|
||||
keys := splitStr(os.Getenv("MC_SECRET_"+alias), ":", 2)
|
||||
if isValidAccessKey(keys[0]) && isValidSecretKey(keys[1]) {
|
||||
s3Config.AccessKey = keys[0]
|
||||
s3Config.SecretKey = keys[1]
|
||||
} else {
|
||||
if keys[0] != "" {
|
||||
console.Errorln("Access/Secret keys associated to `" + alias + "' " +
|
||||
"are found in your environment but not suitable for use. " +
|
||||
"Falling back to the standard config.")
|
||||
}
|
||||
}
|
||||
|
||||
if s3Config.AccessKey == "" {
|
||||
s3Config.AccessKey = hostCfg.AccessKey
|
||||
s3Config.SecretKey = hostCfg.SecretKey
|
||||
}
|
||||
|
||||
s3Config.Signature = hostCfg.API
|
||||
s3Config.AppName = "mc"
|
||||
s3Config.AppVersion = Version
|
||||
s3Config.AppComments = []string{os.Args[0], runtime.GOOS, runtime.GOARCH}
|
||||
s3Config.HostURL = urlStr
|
||||
s3Config.Debug = globalDebug
|
||||
s3Config.Insecure = globalInsecure
|
||||
|
||||
return s3Config
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user