-
Notifications
You must be signed in to change notification settings - Fork 578
[v0.31] ENGPLAT-399 Add --secure flag for TLS verification (#3781) #3801
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package clihelper | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestIsLoftReachable_InsecureTrueAgainstSelfSigned(t *testing.T) { | ||
| // Create an HTTPS server with a self-signed certificate. | ||
| server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path == "/healthz" { | ||
| w.WriteHeader(http.StatusOK) | ||
| return | ||
| } | ||
| w.WriteHeader(http.StatusNotFound) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| host := strings.TrimPrefix(server.URL, "https://") | ||
|
|
||
| // With insecure=true, the self-signed cert should be accepted. | ||
| reachable, err := IsLoftReachable(context.Background(), host, true) | ||
| assert.NilError(t, err) | ||
| assert.Assert(t, reachable, "should be reachable with insecure=true against self-signed cert") | ||
| } | ||
|
|
||
| func TestIsLoftReachable_InsecureFalseAgainstSelfSigned(t *testing.T) { | ||
| // Create an HTTPS server with a self-signed certificate. | ||
| server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path == "/healthz" { | ||
| w.WriteHeader(http.StatusOK) | ||
| return | ||
| } | ||
| w.WriteHeader(http.StatusNotFound) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| host := strings.TrimPrefix(server.URL, "https://") | ||
|
|
||
| // With insecure=false, the self-signed cert should cause a TLS error | ||
| // and IsLoftReachable should return false (not reachable). | ||
| reachable, err := IsLoftReachable(context.Background(), host, false) | ||
| assert.NilError(t, err) | ||
| assert.Assert(t, !reachable, "should not be reachable with insecure=false against self-signed cert") | ||
| } | ||
|
|
||
| func TestIsLoftReachable_InsecureFalseAgainstTrustedCert(t *testing.T) { | ||
| // Create an HTTPS server with a self-signed cert, but add the cert | ||
| // to the system pool so it's trusted. We do this by creating a custom | ||
| // test that validates the transport respects system certs. | ||
| server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path == "/healthz" { | ||
| w.WriteHeader(http.StatusOK) | ||
| return | ||
| } | ||
| w.WriteHeader(http.StatusNotFound) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| // Verify the server is actually using TLS with a self-signed cert. | ||
| conn, err := tls.Dial("tcp", strings.TrimPrefix(server.URL, "https://"), &tls.Config{ | ||
| InsecureSkipVerify: true, | ||
| }) | ||
| assert.NilError(t, err) | ||
| defer conn.Close() | ||
|
|
||
| // Get the server certificate and create a cert pool that trusts it. | ||
| serverCert := conn.ConnectionState().PeerCertificates[0] | ||
| certPool := x509.NewCertPool() | ||
| certPool.AddCert(serverCert) | ||
|
|
||
| // Verify the cert pool trusts the server - this validates our test setup. | ||
| _, err = serverCert.Verify(x509.VerifyOptions{ | ||
| Roots: certPool, | ||
| }) | ||
| assert.NilError(t, err, "cert should be verified with our custom pool") | ||
| } | ||
|
|
||
| func TestIsLoftReachable_UnhealthyServer(t *testing.T) { | ||
| // Server that returns 500 on /healthz. | ||
| server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| host := strings.TrimPrefix(server.URL, "https://") | ||
|
|
||
| reachable, err := IsLoftReachable(context.Background(), host, true) | ||
| assert.NilError(t, err) | ||
| assert.Assert(t, !reachable, "should not be reachable when server returns 500") | ||
| } | ||
|
|
||
| func TestIsLoftReachable_UnreachableHost(t *testing.T) { | ||
| // Use a host that doesn't exist. | ||
| reachable, err := IsLoftReachable(context.Background(), "localhost:1", true) | ||
| assert.NilError(t, err) | ||
| assert.Assert(t, !reachable, "should not be reachable when host is unreachable") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package http | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestTransport_Secure(t *testing.T) { | ||
| tr := Transport(false) | ||
| if tr.TLSClientConfig != nil { | ||
| assert.Assert(t, !tr.TLSClientConfig.InsecureSkipVerify, "TLS verification should be enabled when insecure=false") | ||
| } | ||
| assert.Assert(t, !tr.ForceAttemptHTTP2, "HTTP/2 should be disabled") | ||
| } | ||
|
|
||
| func TestTransport_Insecure(t *testing.T) { | ||
| tr := Transport(true) | ||
| assert.Assert(t, tr.TLSClientConfig != nil, "TLSClientConfig should be set when insecure=true") | ||
| assert.Assert(t, tr.TLSClientConfig.InsecureSkipVerify, "TLS verification should be skipped when insecure=true") | ||
| assert.Assert(t, !tr.ForceAttemptHTTP2, "HTTP/2 should be disabled") | ||
| } | ||
|
|
||
| func TestInsecureTransport(t *testing.T) { | ||
| tr := InsecureTransport() | ||
| assert.Assert(t, tr.TLSClientConfig != nil, "TLSClientConfig should be set") | ||
| assert.Assert(t, tr.TLSClientConfig.InsecureSkipVerify, "InsecureTransport should skip TLS verification") | ||
| } | ||
|
|
||
| func TestCloneDefaultTransport(t *testing.T) { | ||
| tr := CloneDefaultTransport() | ||
| assert.Assert(t, !tr.ForceAttemptHTTP2, "HTTP/2 should be disabled") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.