From 638d614d8da171e9691878945242d24f3f5732e4 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Sun, 17 Nov 2024 08:16:26 -0600 Subject: [PATCH] Use the config client_idle_timeout instead of a hardcoded value to allow for long-running fort forard / exec sessions. --- lib/kube/proxy/constants.go | 2 -- lib/kube/proxy/forwarder.go | 3 ++- lib/kube/proxy/portforward_spdy.go | 7 +++++-- lib/kube/proxy/portforward_websocket.go | 9 ++++++--- lib/kube/proxy/remotecommand.go | 3 ++- lib/kube/proxy/remotecommand_websocket.go | 4 +++- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/kube/proxy/constants.go b/lib/kube/proxy/constants.go index 5eb7ff5b9f9b3..5a148276bff69 100644 --- a/lib/kube/proxy/constants.go +++ b/lib/kube/proxy/constants.go @@ -28,8 +28,6 @@ const ( // DefaultStreamCreationTimeout DefaultStreamCreationTimeout = 30 * time.Second - - IdleTimeout = 15 * time.Minute ) // These constants are for remote command execution and port forwarding and are diff --git a/lib/kube/proxy/forwarder.go b/lib/kube/proxy/forwarder.go index ca03e46b557ab..1cfd6351fddce 100644 --- a/lib/kube/proxy/forwarder.go +++ b/lib/kube/proxy/forwarder.go @@ -1667,6 +1667,7 @@ func (f *Forwarder) exec(authCtx *authContext, w http.ResponseWriter, req *http. httpResponseWriter: w, context: ctx, pingPeriod: f.cfg.ConnPingPeriod, + idleTimeout: sess.clientIdleTimeout, onResize: func(remotecommand.TerminalSize) {}, } @@ -1811,6 +1812,7 @@ func (f *Forwarder) portForward(authCtx *authContext, w http.ResponseWriter, req onPortForward: onPortForward, targetDialer: dialer, pingPeriod: f.cfg.ConnPingPeriod, + idleTimeout: sess.clientIdleTimeout, } f.log.Debugf("Starting %v.", request) err = runPortForwarding(request) @@ -2186,7 +2188,6 @@ func (f *Forwarder) getSPDYExecutor(sess *clusterSession, req *http.Request) (re } func (f *Forwarder) getPortForwardDialer(sess *clusterSession, req *http.Request) (httpstream.Dialer, error) { - wsDialer, err := f.getWebsocketDialer(sess, req) if err != nil { return nil, trace.Wrap(err) diff --git a/lib/kube/proxy/portforward_spdy.go b/lib/kube/proxy/portforward_spdy.go index 84fe8d49d4573..561750239250d 100644 --- a/lib/kube/proxy/portforward_spdy.go +++ b/lib/kube/proxy/portforward_spdy.go @@ -46,6 +46,7 @@ type portForwardRequest struct { context context.Context targetDialer httpstream.Dialer pingPeriod time.Duration + idleTimeout time.Duration } func (p portForwardRequest) String() string { @@ -102,8 +103,10 @@ func runPortForwardingHTTPStreams(req portForwardRequest) error { targetConn: targetConn, } defer h.Close() - h.Debugf("Setting port forwarding streaming connection idle timeout to %v", IdleTimeout) - conn.SetIdleTimeout(IdleTimeout) + + h.Debugf("Setting port forwarding streaming connection idle timeout to %s.", req.idleTimeout) + conn.SetIdleTimeout(req.idleTimeout) + h.run() return nil } diff --git a/lib/kube/proxy/portforward_websocket.go b/lib/kube/proxy/portforward_websocket.go index 6e00a5f6299d6..af1cb04ab525a 100644 --- a/lib/kube/proxy/portforward_websocket.go +++ b/lib/kube/proxy/portforward_websocket.go @@ -92,7 +92,8 @@ func runPortForwardingWebSocket(req portForwardRequest) error { Channels: channels, }, }) - conn.SetIdleTimeout(IdleTimeout) + + conn.SetIdleTimeout(req.idleTimeout) // Upgrade the request and create the virtual streams. _, streams, err := conn.Open( @@ -352,8 +353,10 @@ func runPortForwardingTunneledHTTPStreams(req portForwardRequest) error { targetConn: targetConn, } defer h.Close() - h.Debugf("Setting port forwarding streaming connection idle timeout to %v", IdleTimeout) - spdyConn.SetIdleTimeout(IdleTimeout) + + h.Debugf("Setting port forwarding streaming connection idle timeout to %s.", req.idleTimeout) + spdyConn.SetIdleTimeout(req.idleTimeout) + h.run() return nil } diff --git a/lib/kube/proxy/remotecommand.go b/lib/kube/proxy/remotecommand.go index ad8b83a6c5959..09a9c868b43ca 100644 --- a/lib/kube/proxy/remotecommand.go +++ b/lib/kube/proxy/remotecommand.go @@ -56,6 +56,7 @@ type remoteCommandRequest struct { onResize resizeCallback context context.Context pingPeriod time.Duration + idleTimeout time.Duration } func (req remoteCommandRequest) eventPodMeta(ctx context.Context, creds kubeCreds) apievents.KubernetesPodMetadata { @@ -156,7 +157,7 @@ func createSPDYStreams(req remoteCommandRequest) (*remoteCommandProxy, error) { return nil, trace.ConnectionProblem(trace.BadParameter("missing connection"), "missing connection") } - conn.SetIdleTimeout(IdleTimeout) + conn.SetIdleTimeout(req.idleTimeout) var handler protocolHandler switch protocol { diff --git a/lib/kube/proxy/remotecommand_websocket.go b/lib/kube/proxy/remotecommand_websocket.go index 5b08407f79cc2..abc5d3f446fdf 100644 --- a/lib/kube/proxy/remotecommand_websocket.go +++ b/lib/kube/proxy/remotecommand_websocket.go @@ -109,7 +109,9 @@ func createWebSocketStreams(req remoteCommandRequest) (*remoteCommandProxy, erro Channels: channels, }, }) - conn.SetIdleTimeout(IdleTimeout) + + conn.SetIdleTimeout(req.idleTimeout) + negotiatedProtocol, streams, err := conn.Open( responsewriter.GetOriginal(req.httpResponseWriter), req.httpRequest,