-
Notifications
You must be signed in to change notification settings - Fork 342
Add NewProxyAutoTLSTransport and DialTLSWithBackOff to support TLS proxy
#2479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,9 @@ package network | |
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "net" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
|
|
@@ -26,6 +29,11 @@ import ( | |
| "k8s.io/apimachinery/pkg/util/sets" | ||
| ) | ||
|
|
||
| const ( | ||
| timeoutErr = "timed out dialing" | ||
| connectionRefusedErr = "connection refused" | ||
| ) | ||
|
|
||
| func TestHTTPRoundTripper(t *testing.T) { | ||
| wants := sets.NewString() | ||
| frt := func(key string) http.RoundTripper { | ||
|
|
@@ -69,34 +77,66 @@ func TestHTTPRoundTripper(t *testing.T) { | |
| } | ||
|
|
||
| func TestDialWithBackoff(t *testing.T) { | ||
| // Make the test short. | ||
| bo := backOffTemplate | ||
| bo.Steps = 2 | ||
|
|
||
| // Nobody's listening on a random port. Usually. | ||
| c, err := DialWithBackOff(context.Background(), "tcp4", "127.0.0.1:41482") | ||
| if err == nil { | ||
| c.Close() | ||
| t.Error("Unexpected success dialing") | ||
| c, err := dialBackOffHelper(context.Background(), "tcp4", "127.0.0.1:41482", bo, nil) | ||
| verifyFailedConnection(t, c, err, connectionRefusedErr) | ||
|
|
||
| // Timeout. Use special testing IP address. | ||
| c, err = dialBackOffHelper(context.Background(), "tcp4", "198.18.0.254:8888", bo, nil) | ||
| verifyFailedConnection(t, c, err, timeoutErr) | ||
|
|
||
| s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | ||
| defer s.Close() | ||
|
|
||
| c, err = DialWithBackOff(context.Background(), "tcp4", strings.TrimPrefix(s.URL, "http://")) | ||
| if err != nil { | ||
| t.Fatal("Dial error =", err) | ||
| } | ||
| c.Close() | ||
| } | ||
|
|
||
| func TestDialTLSWithBackoff(t *testing.T) { | ||
| // Make the test short. | ||
| bo := backOffTemplate | ||
| bo.Steps = 2 | ||
|
|
||
| // Timeout. Use special testing IP address. | ||
| c, err = dialBackOffHelper(context.Background(), "tcp4", "198.18.0.254:8888", bo, sleepTO) | ||
| if err == nil { | ||
| c.Close() | ||
| t.Error("Unexpected success dialing") | ||
| } | ||
| const expectedErrPrefix = "timed out dialing" | ||
| if err == nil || !strings.HasPrefix(err.Error(), expectedErrPrefix) { | ||
| t.Errorf("Error = %v, want: %s(...)", err, expectedErrPrefix) | ||
| tlsConf := &tls.Config{ | ||
| InsecureSkipVerify: false, | ||
| ServerName: "example.com", | ||
| MinVersion: tls.VersionTLS12, | ||
| } | ||
|
|
||
| s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | ||
| // Nobody's listening on a random port. Usually. | ||
| c, err := dialBackOffHelper(context.Background(), "tcp4", "127.0.0.1:41482", bo, tlsConf) | ||
| verifyFailedConnection(t, c, err, connectionRefusedErr) | ||
|
|
||
| // Timeout. Use special testing IP address. | ||
| c, err = dialBackOffHelper(context.Background(), "tcp4", "198.18.0.254:8888", bo, tlsConf) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this use a localhost address rather than some Internet IP? Alternately, use one of the documentation IP ranges so our tests aren't accidentally hitting a real IP.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (i.e. use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another PR #2402 is changing it now (and it seems we need a little bit big change for this) so I leave it as it is.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My concern was that this is a real IP owned by Oracle, but I'm not going to block this PR on it. |
||
| verifyFailedConnection(t, c, err, timeoutErr) | ||
|
|
||
| s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | ||
| defer s.Close() | ||
|
|
||
|
nak3 marked this conversation as resolved.
|
||
| c, err = DialWithBackOff(context.Background(), "tcp4", strings.TrimPrefix(s.URL, "http://")) | ||
| rootCAs := x509.NewCertPool() | ||
| rootCAs.AddCert(s.Certificate()) | ||
| tlsConf.RootCAs = rootCAs | ||
|
|
||
| c, err = DialTLSWithBackOff(context.Background(), "tcp4", strings.TrimPrefix(s.URL, "https://"), tlsConf) | ||
| if err != nil { | ||
| t.Fatal("Dial error =", err) | ||
| } | ||
| c.Close() | ||
| } | ||
|
|
||
| func verifyFailedConnection(t *testing.T, c net.Conn, err error, prefix string) { | ||
| if err == nil { | ||
| c.Close() | ||
| t.Error("Unexpected success dialing") | ||
| } else if !strings.Contains(err.Error(), prefix) { | ||
| t.Errorf("Error = %v, want: %s(...)", err, prefix) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.