From 58f15fca837e65b55c93ec11291827ae1de3ffb5 Mon Sep 17 00:00:00 2001 From: Andreas Auernhammer Date: Fri, 9 Aug 2024 02:46:03 +0200 Subject: [PATCH] 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: 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 --- cmd/alias-set.go | 13 +++++------- cmd/client-admin.go | 25 +++++++---------------- cmd/client-s3.go | 50 ++++++++++++++++++++++++++++++++++----------- cmd/flags.go | 5 +++++ cmd/globals.go | 30 +++++++++++++++++++++++++++ cmd/tofu.go | 8 ++++---- 6 files changed, 89 insertions(+), 42 deletions(-) diff --git a/cmd/alias-set.go b/cmd/alias-set.go index 1f8f2297..8b3ba21a 100644 --- a/cmd/alias-set.go +++ b/cmd/alias-set.go @@ -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 } diff --git a/cmd/client-admin.go b/cmd/client-admin.go index 0c180331..845acbed 100644 --- a/cmd/client-admin.go +++ b/cmd/client-admin.go @@ -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`. diff --git a/cmd/client-s3.go b/cmd/client-s3.go index f9512ca3..3039f33c 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -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 diff --git a/cmd/flags.go b/cmd/flags.go index a84228a6..39b5fb4e 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -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", diff --git a/cmd/globals.go b/cmd/globals.go index a9101362..319830db 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -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 } diff --git a/cmd/tofu.go b/cmd/tofu.go index 1a4ebad0..b1340ac5 100644 --- a/cmd/tofu.go +++ b/cmd/tofu.go @@ -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)