Commit 4da39ae7 authored by Robert Pothier's avatar Robert Pothier

Update Kubeadm proxy handling for IPv6

This updates HTTPProxyCheck with brackets around the ipv6 address to handle adding :port
parent aee2cff1
...@@ -425,9 +425,9 @@ func (hst HTTPProxyCheck) Name() string { ...@@ -425,9 +425,9 @@ func (hst HTTPProxyCheck) Name() string {
// Check validates http connectivity type, direct or via proxy. // Check validates http connectivity type, direct or via proxy.
func (hst HTTPProxyCheck) Check() (warnings, errors []error) { func (hst HTTPProxyCheck) Check() (warnings, errors []error) {
url := fmt.Sprintf("%s://%s", hst.Proto, hst.Host) u := (&url.URL{Scheme: hst.Proto, Host: hst.Host}).String()
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", u, nil)
if err != nil { if err != nil {
return nil, []error{err} return nil, []error{err}
} }
...@@ -437,7 +437,7 @@ func (hst HTTPProxyCheck) Check() (warnings, errors []error) { ...@@ -437,7 +437,7 @@ func (hst HTTPProxyCheck) Check() (warnings, errors []error) {
return nil, []error{err} return nil, []error{err}
} }
if proxy != nil { if proxy != nil {
return []error{fmt.Errorf("Connection to %q uses proxy %q. If that is not intended, adjust your proxy settings", url, proxy)}, nil return []error{fmt.Errorf("Connection to %q uses proxy %q. If that is not intended, adjust your proxy settings", u, proxy)}, nil
} }
return nil, nil return nil, nil
} }
......
...@@ -504,38 +504,84 @@ func TestHTTPProxyCIDRCheck(t *testing.T) { ...@@ -504,38 +504,84 @@ func TestHTTPProxyCIDRCheck(t *testing.T) {
} }
// Save current content of *_proxy and *_PROXY variables. // Save current content of *_proxy and *_PROXY variables.
savedEnv := resetProxyEnv() savedEnv := resetProxyEnv(t)
defer restoreEnv(savedEnv) defer restoreEnv(savedEnv)
t.Log("Saved environment: ", savedEnv)
os.Setenv("HTTP_PROXY", "http://proxy.example.com:3128")
os.Setenv("NO_PROXY", "example.com,10.0.0.0/8,2001:db8::/48")
// Check if we can reliably execute tests: for _, rt := range tests {
// ProxyFromEnvironment caches the *_proxy environment variables and warning, _ := rt.check.Check()
// if ProxyFromEnvironment already executed before our test with empty if (warning != nil) != rt.expectWarnings {
// HTTP_PROXY it will make these tests return false positive failures t.Errorf(
req, err := http.NewRequest("GET", "http://host.fake.tld/", nil) "failed HTTPProxyCIDRCheck:\n\texpected: %t\n\t actual: %t (CIDR:%s). Warnings: %v",
if err != nil { rt.expectWarnings,
t.Fatalf("unexpected err: %v", err) (warning != nil),
rt.check.CIDR,
warning,
)
} }
proxy, err := http.ProxyFromEnvironment(req)
if err != nil {
t.Fatalf("unexpected err: %v", err)
} }
if proxy == nil { }
t.Skip("test skipped as ProxyFromEnvironment already initialized in environment without defined HTTP proxy")
func TestHTTPProxyCheck(t *testing.T) {
var tests = []struct {
name string
check HTTPProxyCheck
expectWarnings bool
}{
{
name: "Loopback address",
check: HTTPProxyCheck{
Proto: "https",
Host: "127.0.0.1",
}, // Loopback addresses never should produce proxy warnings
expectWarnings: false,
},
{
name: "IPv4 direct access",
check: HTTPProxyCheck{
Proto: "https",
Host: "10.96.0.1",
}, // Expected to be accessed directly, we set NO_PROXY to 10.0.0.0/8
expectWarnings: false,
},
{
name: "IPv4 via proxy",
check: HTTPProxyCheck{
Proto: "https",
Host: "192.168.0.1",
}, // Expected to go via proxy as this range is not listed in NO_PROXY
expectWarnings: true,
},
{
name: "IPv6 direct access",
check: HTTPProxyCheck{
Proto: "https",
Host: "[2001:db8::1:15]",
}, // Expected to be accessed directly, part of 2001:db8::/48 in NO_PROXY
expectWarnings: false,
},
{
name: "IPv6 via proxy",
check: HTTPProxyCheck{
Proto: "https",
Host: "[2001:db8:1::1:15]",
}, // Expected to go via proxy, range is not in 2001:db8::/48
expectWarnings: true,
},
} }
t.Log("http.ProxyFromEnvironment is usable, continue executing test")
// Save current content of *_proxy and *_PROXY variables.
savedEnv := resetProxyEnv(t)
defer restoreEnv(savedEnv)
for _, rt := range tests { for _, rt := range tests {
warning, _ := rt.check.Check() warning, _ := rt.check.Check()
if (warning != nil) != rt.expectWarnings { if (warning != nil) != rt.expectWarnings {
t.Errorf( t.Errorf(
"failed HTTPProxyCIDRCheck:\n\texpected: %t\n\t actual: %t (CIDR:%s). Warnings: %v", "%s failed HTTPProxyCheck:\n\texpected: %t\n\t actual: %t (Host:%s). Warnings: %v",
rt.name,
rt.expectWarnings, rt.expectWarnings,
(warning != nil), (warning != nil),
rt.check.CIDR, rt.check.Host,
warning, warning,
) )
} }
...@@ -545,7 +591,7 @@ func TestHTTPProxyCIDRCheck(t *testing.T) { ...@@ -545,7 +591,7 @@ func TestHTTPProxyCIDRCheck(t *testing.T) {
// resetProxyEnv is helper function that unsets all *_proxy variables // resetProxyEnv is helper function that unsets all *_proxy variables
// and return previously set values as map. This can be used to restore // and return previously set values as map. This can be used to restore
// original state of the environment. // original state of the environment.
func resetProxyEnv() map[string]string { func resetProxyEnv(t *testing.T) map[string]string {
savedEnv := make(map[string]string) savedEnv := make(map[string]string)
for _, e := range os.Environ() { for _, e := range os.Environ() {
pair := strings.Split(e, "=") pair := strings.Split(e, "=")
...@@ -554,6 +600,26 @@ func resetProxyEnv() map[string]string { ...@@ -554,6 +600,26 @@ func resetProxyEnv() map[string]string {
os.Unsetenv(pair[0]) os.Unsetenv(pair[0])
} }
} }
t.Log("Saved environment: ", savedEnv)
os.Setenv("HTTP_PROXY", "http://proxy.example.com:3128")
os.Setenv("NO_PROXY", "example.com,10.0.0.0/8,2001:db8::/48")
// Check if we can reliably execute tests:
// ProxyFromEnvironment caches the *_proxy environment variables and
// if ProxyFromEnvironment already executed before our test with empty
// HTTP_PROXY it will make these tests return false positive failures
req, err := http.NewRequest("GET", "http://host.fake.tld/", nil)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
proxy, err := http.ProxyFromEnvironment(req)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if proxy == nil {
t.Skip("test skipped as ProxyFromEnvironment already initialized in environment without defined HTTP proxy")
}
t.Log("http.ProxyFromEnvironment is usable, continue executing test")
return savedEnv return savedEnv
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment