Skip to content

Commit c77f9e4

Browse files
seans3k8s-publishing-bot
authored andcommitted
Websocket HTTPS proxy support
Kubernetes-commit: f73945aae56b51078318199ff2f0ecae91bc489e
1 parent e782ad6 commit c77f9e4

File tree

4 files changed

+148
-63
lines changed

4 files changed

+148
-63
lines changed

go.mod

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/google/gnostic-models v0.6.9
1313
github.com/google/go-cmp v0.7.0
1414
github.com/google/uuid v1.6.0
15-
github.com/gorilla/websocket v1.5.3
15+
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674
1616
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
1717
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822
1818
github.com/peterbourgon/diskv v2.0.1+incompatible
@@ -25,8 +25,8 @@ require (
2525
golang.org/x/time v0.9.0
2626
google.golang.org/protobuf v1.36.5
2727
gopkg.in/evanphx/json-patch.v4 v4.12.0
28-
k8s.io/api v0.0.0-20250320031247-741ca7705f8e
29-
k8s.io/apimachinery v0.0.0-20250319092800-e8a77bd768fd
28+
k8s.io/api v0.0.0
29+
k8s.io/apimachinery v0.0.0
3030
k8s.io/klog/v2 v2.130.1
3131
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff
3232
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738
@@ -62,3 +62,8 @@ require (
6262
gopkg.in/inf.v0 v0.9.1 // indirect
6363
gopkg.in/yaml.v3 v3.0.1 // indirect
6464
)
65+
66+
replace (
67+
k8s.io/api => ../api
68+
k8s.io/apimachinery => ../apimachinery
69+
)

go.sum

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k=
2+
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
13
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
24
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
5+
github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk=
36
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
47
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
58
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -22,6 +25,7 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v
2225
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
2326
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
2427
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
28+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
2529
github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg=
2630
github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
2731
github.com/google/gnostic-models v0.6.9 h1:MU/8wDLif2qCXZmzncUQ/BOfxWfthHi63KqpoNbWqVw=
@@ -34,10 +38,11 @@ github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db h1:097atOisP2aRj7vFgY
3438
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
3539
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
3640
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
37-
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
38-
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
41+
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 h1:JeSE6pjso5THxAzdVpqr6/geYxZytqFMBCOtn/ujyeo=
42+
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674/go.mod h1:r4w70xmWCQKmi1ONH4KIaBptdivuRPyosB9RmPlGEwA=
3943
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA=
4044
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
45+
github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw=
4146
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
4247
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
4348
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -93,13 +98,16 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
9398
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
9499
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
95100
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
101+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
96102
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
97103
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
98104
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
99105
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
100106
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
107+
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
101108
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
102109
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
110+
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
103111
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
104112
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
105113
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -111,11 +119,13 @@ golang.org/x/oauth2 v0.27.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT
111119
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
112120
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
113121
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
122+
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
114123
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
115124
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
116125
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
117126
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
118127
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
128+
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
119129
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
120130
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=
121131
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -146,10 +156,7 @@ gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
146156
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
147157
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
148158
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
149-
k8s.io/api v0.0.0-20250320031247-741ca7705f8e h1:O3P0nY3fp1Hj0yurtChVY3bTcCVY0QVwgtEVjCxZu9E=
150-
k8s.io/api v0.0.0-20250320031247-741ca7705f8e/go.mod h1:JO0tyTI0qSXXaGVhLdqwfi3RMbS2g9hcYvzBmZP5wVk=
151-
k8s.io/apimachinery v0.0.0-20250319092800-e8a77bd768fd h1:KoXgjwEokLM8o95kMxowg5vp5iQ4v46Kk+zobsqeTgU=
152-
k8s.io/apimachinery v0.0.0-20250319092800-e8a77bd768fd/go.mod h1:D2UW665TVSpInyOuG6C+PMtC1MZheP0KQz65UPQEiI4=
159+
k8s.io/gengo/v2 v2.0.0-20240826214909-a7b603a56eb7/go.mod h1:EJykeLsmFC60UQbYJezXkEsG2FLrt0GPNkU5iK5GWxU=
153160
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
154161
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
155162
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff h1:/usPimJzUKKu+m+TE36gUyGcf03XZEP0ZIKgKj35LS4=

tools/remotecommand/fallback_test.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ KR8NJEkK99Vh/tew6jAMll70xFrE7aF8VLXJVE7w4sQzuvHxl9Q=
288288
`)
289289

290290
// See (https://github.com/kubernetes/kubernetes/issues/126134).
291-
func TestFallbackClient_WebSocketHTTPSProxyCausesSPDYFallback(t *testing.T) {
291+
func TestFallbackClient_WebSocketHTTPSProxyNoFallback(t *testing.T) {
292292
cert, err := tls.X509KeyPair(localhostCert, localhostKey)
293293
if err != nil {
294294
t.Errorf("https (valid hostname): proxy_test: %v", err)
@@ -309,42 +309,40 @@ func TestFallbackClient_WebSocketHTTPSProxyCausesSPDYFallback(t *testing.T) {
309309
proxyLocation, err := url.Parse(proxyServer.URL)
310310
require.NoError(t, err)
311311

312-
// Create fake SPDY server. Copy received STDIN data back onto STDOUT stream.
313-
spdyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
314-
var stdin, stdout bytes.Buffer
315-
ctx, err := createHTTPStreams(w, req, &StreamOptions{
316-
Stdin: &stdin,
317-
Stdout: &stdout,
318-
})
312+
// Create fake WebSocket server. Copy received STDIN data back onto STDOUT stream.
313+
websocketServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
314+
conns, err := webSocketServerStreams(req, w, streamOptionsFromRequest(req))
319315
if err != nil {
320316
w.WriteHeader(http.StatusForbidden)
321317
return
322318
}
323-
defer ctx.conn.Close() //nolint:errcheck
324-
_, err = io.Copy(ctx.stdoutStream, ctx.stdinStream)
319+
defer conns.conn.Close() //nolint:errcheck
320+
// Loopback the STDIN stream onto the STDOUT stream.
321+
_, err = io.Copy(conns.stdoutStream, conns.stdinStream)
325322
if err != nil {
326-
t.Fatalf("error copying STDIN to STDOUT: %v", err)
323+
t.Fatalf("websocket copy error: %v", err)
327324
}
328325
}))
329-
defer spdyServer.Close() //nolint:errcheck
326+
defer websocketServer.Close() //nolint:errcheck
330327

331-
backendLocation, err := url.Parse(spdyServer.URL)
328+
// Now create the WebSocket client (executor), and point it to the TLS proxy server.
329+
// The proxy server should open a websocket connection to the fake websocket server.
330+
websocketServer.URL = websocketServer.URL + "?" + "stdin=true" + "&" + "stdout=true"
331+
websocketLocation, err := url.Parse(websocketServer.URL)
332332
require.NoError(t, err)
333-
334333
clientConfig := &rest.Config{
335-
Host: spdyServer.URL,
334+
Host: websocketLocation.Host,
336335
TLSClientConfig: rest.TLSClientConfig{CAData: localhostCert},
337336
Proxy: func(req *http.Request) (*url.URL, error) {
338337
return proxyLocation, nil
339338
},
340339
}
341-
342-
// Websocket with https proxy will fail in dialing (falling back to SPDY).
343-
websocketExecutor, err := NewWebSocketExecutor(clientConfig, "GET", backendLocation.String())
340+
websocketExecutor, err := NewWebSocketExecutor(clientConfig, "GET", websocketServer.URL)
344341
require.NoError(t, err)
345-
spdyExecutor, err := NewSPDYExecutor(clientConfig, "POST", backendLocation)
342+
emptyURL, _ := url.Parse("")
343+
spdyExecutor, err := NewSPDYExecutor(clientConfig, "POST", emptyURL)
346344
require.NoError(t, err)
347-
// Fallback to spdyExecutor with websocket https proxy error; spdyExecutor succeeds against fake spdy server.
345+
// No fallback to spdyExecutor with websocket.
348346
sawHTTPSProxyError := false
349347
exec, err := NewFallbackExecutor(websocketExecutor, spdyExecutor, func(err error) bool {
350348
if httpstream.IsUpgradeFailure(err) {
@@ -396,9 +394,9 @@ func TestFallbackClient_WebSocketHTTPSProxyCausesSPDYFallback(t *testing.T) {
396394
t.Errorf("unexpected data received: %d sent: %d", len(data), len(randomData))
397395
}
398396

399-
// Ensure the https proxy error was observed
400-
if !sawHTTPSProxyError {
401-
t.Errorf("expected to see https proxy error")
397+
// Ensure the https proxy error was *not* observed
398+
if sawHTTPSProxyError {
399+
t.Errorf("expected to *not* see https proxy error")
402400
}
403401
// Ensure the proxy was called once
404402
if e, a := int64(1), proxyCalled.Load(); e != a {

tools/remotecommand/websocket_test.go

Lines changed: 106 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"context"
2222
"crypto/rand"
23+
"crypto/tls"
2324
"encoding/json"
2425
"fmt"
2526
"io"
@@ -31,16 +32,18 @@ import (
3132
"reflect"
3233
"strings"
3334
"sync"
35+
"sync/atomic"
3436
"testing"
3537
"time"
3638

3739
gwebsocket "github.com/gorilla/websocket"
40+
"github.com/stretchr/testify/require"
3841

3942
v1 "k8s.io/api/core/v1"
4043
apierrors "k8s.io/apimachinery/pkg/api/errors"
4144
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
42-
"k8s.io/apimachinery/pkg/util/httpstream"
4345
"k8s.io/apimachinery/pkg/util/httpstream/wsstream"
46+
utilnettesting "k8s.io/apimachinery/pkg/util/net/testing"
4447
"k8s.io/apimachinery/pkg/util/remotecommand"
4548
"k8s.io/apimachinery/pkg/util/wait"
4649
"k8s.io/client-go/rest"
@@ -1342,38 +1345,110 @@ func createWebSocketStreams(req *http.Request, w http.ResponseWriter, opts *opti
13421345
return wsStreams, nil
13431346
}
13441347

1345-
// See (https://github.com/kubernetes/kubernetes/issues/126134).
1346-
func TestWebSocketClient_HTTPSProxyErrorExpected(t *testing.T) {
1347-
urlStr := "http://127.0.0.1/never-used" + "?" + "stdin=true" + "&" + "stdout=true"
1348-
websocketLocation, err := url.Parse(urlStr)
1349-
if err != nil {
1350-
t.Fatalf("Unable to parse WebSocket server URL: %s", urlStr)
1351-
}
1352-
// proxy url with https scheme will trigger websocket dialing error.
1353-
httpsProxyFunc := func(req *http.Request) (*url.URL, error) { return url.Parse("https://127.0.0.1") }
1354-
exec, err := NewWebSocketExecutor(&rest.Config{Host: websocketLocation.Host, Proxy: httpsProxyFunc}, "GET", urlStr)
1355-
if err != nil {
1356-
t.Errorf("unexpected error creating websocket executor: %v", err)
1357-
}
1358-
var stdout bytes.Buffer
1359-
options := &StreamOptions{
1360-
Stdout: &stdout,
1361-
}
1362-
errorChan := make(chan error)
1363-
go func() {
1364-
// Start the streaming on the WebSocket "exec" client.
1365-
errorChan <- exec.StreamWithContext(context.Background(), *options)
1366-
}()
1348+
func TestWebSocketClient_ProxySucceeds(t *testing.T) {
1349+
// Validate websocket proxy succeeds for each of the enumerated schemes.
1350+
proxySchemes := []string{"http", "https"}
1351+
for _, proxyScheme := range proxySchemes {
1352+
// Create the proxy handler, keeping track of how many times it was called.
1353+
var proxyCalled atomic.Int64
1354+
proxyHandler := utilnettesting.NewHTTPProxyHandler(t, func(req *http.Request) bool {
1355+
proxyCalled.Add(1)
1356+
return true
1357+
})
1358+
defer proxyHandler.Wait()
1359+
// Create/Start the proxy server, adding TLS functionality depending on scheme.
1360+
proxyServer := httptest.NewUnstartedServer(proxyHandler)
1361+
if proxyScheme == "https" {
1362+
cert, err := tls.X509KeyPair(localhostCert, localhostKey)
1363+
if err != nil {
1364+
t.Errorf("https (valid hostname): proxy_test: %v", err)
1365+
}
1366+
proxyServer.TLS = &tls.Config{Certificates: []tls.Certificate{cert}}
1367+
proxyServer.StartTLS()
1368+
} else {
1369+
proxyServer.Start()
1370+
}
1371+
defer proxyServer.Close() //nolint:errcheck
1372+
proxyLocation, err := url.Parse(proxyServer.URL)
1373+
require.NoError(t, err)
1374+
t.Logf("Proxy URL: %s", proxyLocation.String())
13671375

1368-
select {
1369-
case <-time.After(wait.ForeverTestTimeout):
1370-
t.Fatalf("expect stream to be closed after connection is closed.")
1371-
case err := <-errorChan:
1372-
if err == nil {
1373-
t.Errorf("expected error but received none")
1376+
// Create fake WebSocket server. Copy received STDIN data back onto STDOUT stream.
1377+
websocketServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
1378+
conns, err := webSocketServerStreams(req, w, streamOptionsFromRequest(req))
1379+
if err != nil {
1380+
t.Fatalf("error on webSocketServerStreams: %v", err)
1381+
}
1382+
defer conns.conn.Close() //nolint:errcheck
1383+
// Loopback the STDIN stream onto the STDOUT stream.
1384+
_, err = io.Copy(conns.stdoutStream, conns.stdinStream)
1385+
if err != nil {
1386+
t.Fatalf("error copying STDIN to STDOUT: %v", err)
1387+
}
1388+
}))
1389+
defer websocketServer.Close() //nolint:errcheck
1390+
1391+
// Now create the WebSocket client (executor), and point it to the TLS proxy server.
1392+
// The proxy server should open a websocket connection to the fake websocket server.
1393+
websocketServer.URL = websocketServer.URL + "?" + "stdin=true" + "&" + "stdout=true"
1394+
websocketLocation, err := url.Parse(websocketServer.URL)
1395+
require.NoError(t, err)
1396+
clientConfig := &rest.Config{
1397+
Host: websocketLocation.Host,
1398+
// Unused if "http" scheme.
1399+
TLSClientConfig: rest.TLSClientConfig{CAData: localhostCert},
1400+
Proxy: func(req *http.Request) (*url.URL, error) {
1401+
return proxyLocation, nil
1402+
},
1403+
}
1404+
exec, err := NewWebSocketExecutor(clientConfig, "GET", websocketServer.URL)
1405+
require.NoError(t, err)
1406+
1407+
// Generate random data, and set it up to stream on STDIN. The data will be
1408+
// returned on the STDOUT buffer.
1409+
randomSize := 1024 * 1024
1410+
randomData := make([]byte, randomSize)
1411+
if _, err := rand.Read(randomData); err != nil {
1412+
t.Errorf("unexpected error reading random data: %v", err)
1413+
}
1414+
var stdout bytes.Buffer
1415+
options := &StreamOptions{
1416+
Stdin: bytes.NewReader(randomData),
1417+
Stdout: &stdout,
1418+
}
1419+
errorChan := make(chan error)
1420+
go func() {
1421+
// Start the streaming on the WebSocket "exec" client.
1422+
errorChan <- exec.StreamWithContext(context.Background(), *options)
1423+
}()
1424+
1425+
select {
1426+
case <-time.After(wait.ForeverTestTimeout):
1427+
t.Fatalf("expect stream to be closed after connection is closed.")
1428+
case err := <-errorChan:
1429+
if err != nil {
1430+
t.Fatalf("unexpected error: %v", err)
1431+
}
1432+
// Validate remote command v5 protocol was negotiated.
1433+
streamExec := exec.(*wsStreamExecutor)
1434+
if remotecommand.StreamProtocolV5Name != streamExec.negotiated {
1435+
t.Fatalf("expected remote command v5 protocol, got (%s)", streamExec.negotiated)
1436+
}
1437+
}
1438+
data, err := io.ReadAll(bytes.NewReader(stdout.Bytes()))
1439+
if err != nil {
1440+
t.Fatalf("error reading the stream: %v", err)
1441+
}
1442+
// Check the random data sent on STDIN was the same returned on STDOUT.
1443+
t.Logf("comparing %d random bytes sent data versus received", len(randomData))
1444+
if !bytes.Equal(randomData, data) {
1445+
t.Errorf("unexpected data received: %d sent: %d", len(data), len(randomData))
1446+
} else {
1447+
t.Log("success--random bytes are the same")
13741448
}
1375-
if !httpstream.IsHTTPSProxyError(err) {
1376-
t.Errorf("expected https proxy error, got (%s)", err)
1449+
// Ensure the proxy was called once
1450+
if e, a := int64(1), proxyCalled.Load(); e != a {
1451+
t.Errorf("expected %d proxy call, got %d", e, a)
13771452
}
13781453
}
13791454
}

0 commit comments

Comments
 (0)