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>
This commit is contained in:
Andreas Auernhammer
2024-08-08 17:46:03 -07:00
committed by GitHub
parent a56899e13c
commit 58f15fca83
6 changed files with 89 additions and 42 deletions
+5 -8
View File
@@ -24,7 +24,6 @@ import (
"crypto/x509"
"fmt"
"math/rand"
"net"
"net/http"
"os"
"strings"
@@ -373,25 +372,23 @@ func configurePeerCertificate(s3Config *Config, peerCert *x509.Certificate) {
globalRootCAs.AddCert(peerCert)
}
tr = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 15 * time.Second,
}).DialContext,
Proxy: http.ProxyFromEnvironment,
DialContext: newCustomDialContext(&Config{}),
DialTLSContext: newCustomDialTLSContext(&tls.Config{RootCAs: globalRootCAs}),
MaxIdleConnsPerHost: 256,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
DisableCompression: true,
TLSClientConfig: &tls.Config{RootCAs: globalRootCAs},
}
case tr.TLSClientConfig == nil || tr.TLSClientConfig.RootCAs == nil:
if globalRootCAs != nil {
globalRootCAs.AddCert(peerCert)
}
tr.TLSClientConfig = &tls.Config{RootCAs: globalRootCAs}
tr.DialTLSContext = newCustomDialTLSContext(&tls.Config{RootCAs: globalRootCAs})
default:
tr.TLSClientConfig.RootCAs.AddCert(peerCert)
tr.DialTLSContext = newCustomDialTLSContext(tr.TLSClientConfig)
}
s3Config.Transport = tr
}
+7 -18
View File
@@ -20,7 +20,6 @@ package cmd
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/url"
"sync"
@@ -149,29 +148,19 @@ func newAnonymousClient(aliasedURL string) (*madmin.AnonymousClient, *probe.Erro
return nil, probe.NewError(e)
}
// Keep TLS config.
tlsConfig := &tls.Config{
RootCAs: globalRootCAs,
// Can't use SSLv3 because of POODLE and BEAST
// Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
// Can't use TLSv1.1 because of RC4 cipher usage
MinVersion: tls.VersionTLS12,
}
if globalInsecure {
tlsConfig.InsecureSkipVerify = true
}
// Set custom transport
var transport http.RoundTripper = &http.Transport{
Proxy: ieproxy.GetProxyFunc(),
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 15 * time.Second,
}).DialContext,
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,
TLSClientConfig: tlsConfig,
// 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`.
+38 -12
View File
@@ -106,6 +106,14 @@ func newCustomDialContext(c *Config) dialContext {
KeepAlive: 15 * time.Second,
}
if ip, ok := globalResolvers[addr]; ok {
if _, port, err := net.SplitHostPort(addr); err == nil {
addr = net.JoinHostPort(ip.String(), port)
} else {
addr = ip.String()
}
}
conn, err := dialer.DialContext(ctx, network, addr)
if err != nil {
return nil, err
@@ -119,6 +127,31 @@ func newCustomDialContext(c *Config) dialContext {
}
}
// newCustomDialTLSContext setups a custom TLS dialer for any external communication and proxies.
func newCustomDialTLSContext(tlsConf *tls.Config) dialContext {
return func(ctx context.Context, network, addr string) (net.Conn, error) {
dialer := &tls.Dialer{
NetDialer: &net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 15 * time.Second,
},
Config: tlsConf,
}
if ip, ok := globalResolvers[addr]; ok {
dialer.Config = dialer.Config.Clone()
if host, port, err := net.SplitHostPort(addr); err == nil {
dialer.Config.ServerName = host // Set SNI
addr = net.JoinHostPort(ip.String(), port)
} else {
dialer.Config.ServerName = addr // Set SNI
addr = ip.String()
}
}
return dialer.DialContext(ctx, network, addr)
}
}
var timeSentinel = time.Unix(0, 0).UTC()
// getConfigHash returns the Hash for che *Config
@@ -200,18 +233,11 @@ func getTransportForConfig(config *Config, withS3v2 bool) http.RoundTripper {
DisableCompression: true,
}
if useTLS {
// Keep TLS config.
tlsConfig := &tls.Config{
RootCAs: globalRootCAs,
// Can't use SSLv3 because of POODLE and BEAST
// Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
// Can't use TLSv1.1 because of RC4 cipher usage
MinVersion: tls.VersionTLS12,
}
if config.Insecure {
tlsConfig.InsecureSkipVerify = true
}
tr.TLSClientConfig = tlsConfig
tr.DialTLSContext = newCustomDialTLSContext(&tls.Config{
RootCAs: globalRootCAs,
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: config.Insecure,
})
// Because we create a custom TLSClientConfig, we have to opt-in to HTTP/2.
// See https://github.com/golang/go/issues/14275
+5
View File
@@ -59,6 +59,11 @@ var globalFlags = []cli.Flag{
Usage: "enable debug output",
EnvVar: envPrefix + "DEBUG",
},
cli.StringSliceFlag{
Name: "resolve",
Usage: "resolves HOST[:PORT] to an IP address. Example: minio.local:9000=10.10.75.1",
EnvVar: envPrefix + "RESOLVE",
},
cli.BoolFlag{
Name: "insecure",
Usage: "disable SSL certificate verification",
+30
View File
@@ -21,9 +21,13 @@ package cmd
import (
"context"
"crypto/x509"
"fmt"
"net"
"net/netip"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/charmbracelet/lipgloss"
@@ -69,6 +73,7 @@ var (
globalDebug = false // Debug flag set via command line
globalNoColor = false // No Color flag set via command line
globalInsecure = false // Insecure flag set via command line
globalResolvers map[string]netip.Addr // Custom mappings from HOST[:PORT] to IP
globalAirgapped = false // Airgapped flag set via command line
globalSubnetConfig []madmin.SubsysConfig // Subnet config
@@ -170,5 +175,30 @@ func setGlobalsFromContext(ctx *cli.Context) error {
}
}
dnsEntries := ctx.StringSlice("resolve")
if len(dnsEntries) > 0 {
globalResolvers = make(map[string]netip.Addr, len(dnsEntries))
// Each entry is a HOST[:PORT]=IP pair. This is very similar to cURL's syntax.
for _, e := range dnsEntries {
i := strings.IndexByte(e, '=')
if i < 0 {
return fmt.Errorf("invalid DNS resolve entry %s", e)
}
if strings.ContainsRune(e[:i], ':') {
if _, _, err := net.SplitHostPort(e[:i]); err != nil {
return fmt.Errorf("invalid DNS resolve entry %s: %v", e, err)
}
}
host := e[:i]
addr, err := netip.ParseAddr(e[i+1:])
if err != nil {
return fmt.Errorf("invalid DNS resolve entry %s: %v", e, err)
}
globalResolvers[host] = addr
}
}
return nil
}
+4 -4
View File
@@ -93,9 +93,9 @@ func promptTrustSelfSignedCert(ctx context.Context, endpoint, alias string) (*x5
client := http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
DialTLSContext: newCustomDialTLSContext(&tls.Config{
RootCAs: globalRootCAs, // make sure to use loaded certs before probing
},
}),
},
}
@@ -167,9 +167,9 @@ func fetchPeerCertificate(ctx context.Context, endpoint string) (*x509.Certifica
}
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
DialTLSContext: newCustomDialTLSContext(&tls.Config{
InsecureSkipVerify: true,
},
}),
},
}
resp, e := client.Do(req)