|
| 1 | +package http_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/pem" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "net/url" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 17 | + |
| 18 | + httputil "github.com/operator-framework/operator-controller/internal/shared/util/http" |
| 19 | +) |
| 20 | + |
| 21 | +// startRecordingProxy starts a plain-HTTP CONNECT proxy that tunnels HTTPS |
| 22 | +// connections and records the target host of each CONNECT request. |
| 23 | +func startRecordingProxy(proxied chan<- string) *httptest.Server { |
| 24 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 25 | + if r.Method != http.MethodConnect { |
| 26 | + http.Error(w, "only CONNECT supported", http.StatusMethodNotAllowed) |
| 27 | + return |
| 28 | + } |
| 29 | + proxied <- r.Host |
| 30 | + |
| 31 | + dst, err := net.Dial("tcp", r.Host) |
| 32 | + if err != nil { |
| 33 | + http.Error(w, err.Error(), http.StatusBadGateway) |
| 34 | + return |
| 35 | + } |
| 36 | + defer dst.Close() |
| 37 | + |
| 38 | + hj, ok := w.(http.Hijacker) |
| 39 | + if !ok { |
| 40 | + http.Error(w, "hijacking not supported", http.StatusInternalServerError) |
| 41 | + return |
| 42 | + } |
| 43 | + conn, _, err := hj.Hijack() |
| 44 | + if err != nil { |
| 45 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 46 | + return |
| 47 | + } |
| 48 | + defer conn.Close() |
| 49 | + |
| 50 | + if _, err = conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")); err != nil { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + done := make(chan struct{}, 2) |
| 55 | + tunnel := func(dst, src net.Conn) { |
| 56 | + defer func() { done <- struct{}{} }() |
| 57 | + _, _ = io.Copy(dst, src) |
| 58 | + } |
| 59 | + go tunnel(dst, conn) |
| 60 | + go tunnel(conn, dst) |
| 61 | + <-done |
| 62 | + })) |
| 63 | +} |
| 64 | + |
| 65 | +// certPoolWatcherForTLSServer creates a CertPoolWatcher that trusts the given |
| 66 | +// TLS test server's certificate. |
| 67 | +func certPoolWatcherForTLSServer(t *testing.T, server *httptest.Server) *httputil.CertPoolWatcher { |
| 68 | + t.Helper() |
| 69 | + |
| 70 | + dir := t.TempDir() |
| 71 | + certPath := filepath.Join(dir, "server.pem") |
| 72 | + |
| 73 | + certDER := server.TLS.Certificates[0].Certificate[0] |
| 74 | + f, err := os.Create(certPath) |
| 75 | + require.NoError(t, err) |
| 76 | + require.NoError(t, pem.Encode(f, &pem.Block{Type: "CERTIFICATE", Bytes: certDER})) |
| 77 | + require.NoError(t, f.Close()) |
| 78 | + |
| 79 | + cpw, err := httputil.NewCertPoolWatcher(dir, log.FromContext(context.Background())) |
| 80 | + require.NoError(t, err) |
| 81 | + require.NotNil(t, cpw) |
| 82 | + t.Cleanup(cpw.Done) |
| 83 | + require.NoError(t, cpw.Start(context.Background())) |
| 84 | + return cpw |
| 85 | +} |
| 86 | + |
| 87 | +// TestBuildHTTPClientProxyTunnelsConnections verifies end-to-end that the |
| 88 | +// HTTP client produced by BuildHTTPClient correctly tunnels HTTPS connections |
| 89 | +// through an HTTP CONNECT proxy. |
| 90 | +// |
| 91 | +// The test sets transport.Proxy directly to a fixed URL rather than relying on |
| 92 | +// HTTPS_PROXY env-var inspection: httptest servers bind to 127.0.0.1, which |
| 93 | +// http.ProxyFromEnvironment silently excludes from proxying, and env-var changes |
| 94 | +// within the same process are unreliable due to sync.Once caching. Setting |
| 95 | +// transport.Proxy explicitly exercises the same tunnelling code path that |
| 96 | +// HTTPS_PROXY triggers in production. |
| 97 | +func TestBuildHTTPClientProxyTunnelsConnections(t *testing.T) { |
| 98 | + targetServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 99 | + w.WriteHeader(http.StatusOK) |
| 100 | + })) |
| 101 | + defer targetServer.Close() |
| 102 | + |
| 103 | + proxied := make(chan string, 1) |
| 104 | + proxyServer := startRecordingProxy(proxied) |
| 105 | + defer proxyServer.Close() |
| 106 | + |
| 107 | + proxyURL, err := url.Parse(proxyServer.URL) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + cpw := certPoolWatcherForTLSServer(t, targetServer) |
| 111 | + client, err := httputil.BuildHTTPClient(cpw) |
| 112 | + require.NoError(t, err) |
| 113 | + |
| 114 | + // Point the transport directly at our test proxy, bypassing the loopback |
| 115 | + // exclusion and env-var caching of http.ProxyFromEnvironment. |
| 116 | + transport, ok := client.Transport.(*http.Transport) |
| 117 | + require.True(t, ok) |
| 118 | + transport.Proxy = http.ProxyURL(proxyURL) |
| 119 | + |
| 120 | + resp, err := client.Get(targetServer.URL) |
| 121 | + require.NoError(t, err) |
| 122 | + resp.Body.Close() |
| 123 | + |
| 124 | + select { |
| 125 | + case host := <-proxied: |
| 126 | + require.Equal(t, targetServer.Listener.Addr().String(), host, |
| 127 | + "proxy must have received a CONNECT request for the target server address") |
| 128 | + default: |
| 129 | + t.Fatal("HTTPS connection to target server did not go through the proxy") |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// TestBuildHTTPClientProxyBlocksWhenRejected verifies that when the proxy |
| 134 | +// rejects the CONNECT tunnel, the client request fails rather than silently |
| 135 | +// falling back to a direct connection. |
| 136 | +func TestBuildHTTPClientProxyBlocksWhenRejected(t *testing.T) { |
| 137 | + targetServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 138 | + w.WriteHeader(http.StatusOK) |
| 139 | + })) |
| 140 | + defer targetServer.Close() |
| 141 | + |
| 142 | + // A proxy that returns 403 Forbidden for every CONNECT request. |
| 143 | + rejectingProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 144 | + if r.Method == http.MethodConnect { |
| 145 | + http.Error(w, "proxy access denied", http.StatusForbidden) |
| 146 | + return |
| 147 | + } |
| 148 | + http.Error(w, "only CONNECT supported", http.StatusMethodNotAllowed) |
| 149 | + })) |
| 150 | + defer rejectingProxy.Close() |
| 151 | + |
| 152 | + proxyURL, err := url.Parse(rejectingProxy.URL) |
| 153 | + require.NoError(t, err) |
| 154 | + |
| 155 | + cpw := certPoolWatcherForTLSServer(t, targetServer) |
| 156 | + client, err := httputil.BuildHTTPClient(cpw) |
| 157 | + require.NoError(t, err) |
| 158 | + |
| 159 | + transport, ok := client.Transport.(*http.Transport) |
| 160 | + require.True(t, ok) |
| 161 | + transport.Proxy = http.ProxyURL(proxyURL) |
| 162 | + |
| 163 | + resp, err := client.Get(targetServer.URL) |
| 164 | + if resp != nil { |
| 165 | + resp.Body.Close() |
| 166 | + } |
| 167 | + require.Error(t, err, "request should fail when the proxy rejects the CONNECT tunnel") |
| 168 | +} |
0 commit comments