Files
mc/cmd/client-admin.go
T
Andreas Auernhammer 58f15fca83 add global --resolve HOST[:PORT]=IP flag (#5013)
add global `--resolve HOST[:PORT]:IP` flag

This commit adds support for custom DNS overwrites via one or multiple
`--resolve` flags. It allows `mc` users to define custom DNS mappings
from a HOST (and optional port) to an IP address.

For example:
```
mc --resolve foo.com:9000=10.1.2.3 ls myminio/mybucket
```
The `--resolve` syntax is taken from cURL. Ref. `curl --help dns`

Use cases:

  1. DNS overwrites. If there are no DNS resolvers available, e.g.
     due to a temp. outage, this allows `mc` to connect to a defined
     alias as long as the IP of the one or multiple cluster nodes are
     known. It also allows `mc` to overwrite the current DNS responses.
     This is useful for various tests in dev and staging environments.
  2. TLS certificate verification. Often TLS certificates are issued
     for some DNS names (via SAN) but not for IP addresses. IP addresses
     are usually not static and might change anytime. Accessing a MinIO
     cluster that serves a certificate containing only DNS SANs via an
     IP address results in a TLS certificate verification error - even
     if the certificate is issued by a trusted CA. The current workaround
     is the `--insecure` flag which disables TLS certification completely.

Example for TLS verification:

```
$ mc alias set 'myminio' 'https://192.168.188.118:9000' 'minioadmin' 'minioadmin'
mc: <ERROR> Unable to initialize new alias from the provided credentials.
Get "https://192.168.188.118:9000": tls: failed to verify certificate:
x509: cannot validate certificate for 192.168.188.118 because it doesn't contain any IP SANs.
```

```
mc alias set --resolve foo.com:9000=192.168.188.118 'myminio' 'https://foo.com:9000' 'minioadmin' 'minioadmin'
Added `myminio` successfully.
```

Signed-off-by: Andreas Auernhammer <github@aead.dev>
2024-08-08 17:46:03 -07:00

183 lines
5.2 KiB
Go

// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"sync"
"time"
"github.com/mattn/go-ieproxy"
"github.com/minio/madmin-go/v3"
"github.com/minio/mc/pkg/httptracer"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio-go/v7/pkg/credentials"
)
// 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, e := url.Parse(config.HostURL)
if e != nil {
return nil, probe.NewError(e)
}
hostName := targetURL.Host
confSum := getConfigHash(config)
useTLS := isHostTLS(config)
// Lookup previous cache by hash.
mutex.Lock()
defer mutex.Unlock()
var api *madmin.AdminClient
var found bool
if api, found = clientCache[confSum]; !found {
transport := getTransportForConfig(config, true)
credsChain, err := getCredentialsChainForConfig(config, transport)
if err != nil {
return nil, err
}
creds := credentials.NewChainCredentials(credsChain)
// Not found. Instantiate a new MinIO
var e error
api, e = madmin.NewWithOptions(hostName, &madmin.Options{
Creds: creds,
Secure: useTLS,
})
if e != nil {
return nil, probe.NewError(e)
}
// 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
}
}
// newAdminClient gives a new client interface
func newAdminClient(aliasedURL string) (*madmin.AdminClient, *probe.Error) {
alias, urlStrFull, aliasCfg, 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 aliasCfg == nil && urlRgx.MatchString(aliasedURL) {
return nil, errInvalidAliasedURL(aliasedURL).Trace(aliasedURL)
}
if aliasCfg == nil {
return nil, probe.NewError(fmt.Errorf("No valid configuration found for '%s' host alias", urlStrFull))
}
s3Config := NewS3Config(alias, urlStrFull, aliasCfg)
s3Client, err := s3AdminNew(s3Config)
if err != nil {
return nil, err.Trace(alias, urlStrFull)
}
return s3Client, nil
}
func newAnonymousClient(aliasedURL string) (*madmin.AnonymousClient, *probe.Error) {
_, urlStrFull, aliasCfg, 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 aliasCfg == nil && urlRgx.MatchString(aliasedURL) {
return nil, errInvalidAliasedURL(aliasedURL).Trace(aliasedURL)
}
if aliasCfg == nil {
return nil, probe.NewError(fmt.Errorf("No valid configuration found for '%s' host alias", urlStrFull))
}
// Creates a parsed URL.
targetURL, e := url.Parse(urlStrFull)
if e != nil {
return nil, probe.NewError(e)
}
// By default enable HTTPs.
useTLS := true
if targetURL.Scheme == "http" {
useTLS = false
}
// Construct an anonymous client
anonClient, e := madmin.NewAnonymousClient(targetURL.Host, useTLS)
if e != nil {
return nil, probe.NewError(e)
}
// Set custom transport
var transport http.RoundTripper = &http.Transport{
Proxy: ieproxy.GetProxyFunc(),
DialContext: newCustomDialContext(&Config{}),
DialTLSContext: newCustomDialTLSContext(&tls.Config{
RootCAs: globalRootCAs,
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: globalInsecure,
}),
MaxIdleConnsPerHost: 256,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
// Set this value so that the underlying transport round-tripper
// doesn't try to auto decode the body of objects with
// content-encoding set to `gzip`.
//
// Refer:
// https://golang.org/src/net/http/transport.go?h=roundTrip#L1843
DisableCompression: true,
}
if globalDebug {
transport = httptracer.GetNewTraceTransport(newTraceV4(), transport)
}
anonClient.SetCustomTransport(transport)
return anonClient, nil
}
// s3AdminNew returns an initialized minioAdmin structure. If debug is enabled,
// it also enables an internal trace transport.
var s3AdminNew = NewAdminFactory()