@@ -49,16 +49,16 @@ type KubeSession struct {
49
49
}
50
50
51
51
// NewKubeSession joins a live kubernetes session.
52
- func NewKubeSession (ctx context.Context , tc * TeleportClient , meta types.SessionTracker , kubeAddr string , tlsServer string , mode types.SessionParticipantMode , tlsConfig * tls.Config ) (* KubeSession , error ) {
52
+ func NewKubeSession (ctx context.Context , tc * TeleportClient , meta types.SessionTracker , tlsServer string , mode types.SessionParticipantMode , tlsConfig * tls.Config ) (* KubeSession , error ) {
53
53
ctx , cancel := context .WithCancel (ctx )
54
- joinEndpoint := "wss://" + kubeAddr + "/api/v1/teleport/join/" + meta .GetSessionID ()
54
+ joinEndpoint := "wss://" + tc . KubeProxyAddr + "/api/v1/teleport/join/" + meta .GetSessionID ()
55
55
56
56
if tlsServer != "" {
57
57
tlsConfig .ServerName = tlsServer
58
58
}
59
59
60
60
dialer := & websocket.Dialer {
61
- NetDialContext : kubeSessionNetDialer (ctx , tc , kubeAddr ).DialContext ,
61
+ NetDialContext : kubeSessionNetDialer (ctx , tc ).DialContext ,
62
62
TLSClientConfig : tlsConfig ,
63
63
}
64
64
@@ -93,6 +93,10 @@ func NewKubeSession(ctx context.Context, tc *TeleportClient, meta types.SessionT
93
93
return nil , trace .Wrap (err )
94
94
}
95
95
96
+ context .AfterFunc (ctx , func () {
97
+ _ = stream .Close ()
98
+ })
99
+
96
100
term , err := terminal .New (tc .Stdin , tc .Stdout , tc .Stderr )
97
101
if err != nil {
98
102
cancel ()
@@ -108,26 +112,25 @@ func NewKubeSession(ctx context.Context, tc *TeleportClient, meta types.SessionT
108
112
stdout := utils .NewSyncWriter (term .Stdout ())
109
113
110
114
go handleOutgoingResizeEvents (ctx , stream , term )
111
- go handleIncomingResizeEvents (stream , term )
115
+ go handleIncomingResizeEvents (ctx , stream , term )
112
116
113
117
s := & KubeSession {stream , term , ctx , cancel , meta , sync.WaitGroup {}}
114
- err = s .handleMFA (ctx , tc , mode , stdout )
115
- if err != nil {
118
+ if err := s .handleMFA (ctx , tc , mode , stdout ); err != nil {
116
119
return nil , trace .Wrap (err )
117
120
}
118
121
119
- s .pipeInOut (stdout , tc .EnableEscapeSequences , mode )
122
+ s .pipeInOut (ctx , stdout , tc .EnableEscapeSequences , mode )
120
123
return s , nil
121
124
}
122
125
123
- func kubeSessionNetDialer (ctx context.Context , tc * TeleportClient , kubeAddr string ) client.ContextDialer {
126
+ func kubeSessionNetDialer (ctx context.Context , tc * TeleportClient ) client.ContextDialer {
124
127
dialOpts := []client.DialOption {
125
128
client .WithInsecureSkipVerify (tc .InsecureSkipVerify ),
126
129
}
127
130
128
131
// Add options for ALPN connection upgrade only if kube is served at Proxy
129
132
// web address.
130
- if tc .WebProxyAddr == kubeAddr && tc .TLSRoutingConnUpgradeRequired {
133
+ if tc .WebProxyAddr == tc . KubeProxyAddr && tc .TLSRoutingConnUpgradeRequired {
131
134
dialOpts = append (dialOpts ,
132
135
client .WithALPNConnUpgrade (tc .TLSRoutingConnUpgradeRequired ),
133
136
client .WithALPNConnUpgradePing (true ), // Use Ping protocol for long-lived connections.
@@ -157,27 +160,30 @@ func handleOutgoingResizeEvents(ctx context.Context, stream *streamproto.Session
157
160
}
158
161
}
159
162
160
- func handleIncomingResizeEvents (stream * streamproto.SessionStream , term * terminal.Terminal ) {
163
+ func handleIncomingResizeEvents (ctx context. Context , stream * streamproto.SessionStream , term * terminal.Terminal ) {
161
164
events := term .Subscribe ()
162
165
163
166
for {
164
- event , more := <- events
165
- _ , ok := event .(terminal.ResizeEvent )
166
- if ok {
167
- w , h , err := term .Size ()
168
- if err != nil {
169
- fmt .Printf ("Error attempting to fetch terminal size: %v\n \r " , err )
170
- }
167
+ select {
168
+ case <- ctx .Done ():
169
+ return
170
+ case event , more := <- events :
171
+ _ , ok := event .(terminal.ResizeEvent )
172
+ if ok {
173
+ w , h , err := term .Size ()
174
+ if err != nil {
175
+ fmt .Printf ("Error attempting to fetch terminal size: %v\n \r " , err )
176
+ }
171
177
172
- size := remotecommand.TerminalSize {Width : uint16 (w ), Height : uint16 (h )}
173
- err = stream .Resize (& size )
174
- if err != nil {
175
- fmt . Printf ( "Error attempting to resize terminal: %v \n \r " , err )
178
+ size := remotecommand.TerminalSize {Width : uint16 (w ), Height : uint16 (h )}
179
+ if err : = stream .Resize (& size ); err != nil {
180
+ fmt . Printf ( "Error attempting to resize terminal: %v \n \r " , err )
181
+ }
176
182
}
177
- }
178
183
179
- if ! more {
180
- break
184
+ if ! more {
185
+ return
186
+ }
181
187
}
182
188
}
183
189
}
@@ -205,14 +211,15 @@ func (s *KubeSession) handleMFA(ctx context.Context, tc *TeleportClient, mode ty
205
211
}
206
212
207
213
// pipeInOut starts background tasks that copy input to and from the terminal.
208
- func (s * KubeSession ) pipeInOut (stdout io.Writer , enableEscapeSequences bool , mode types.SessionParticipantMode ) {
214
+ func (s * KubeSession ) pipeInOut (ctx context. Context , stdout io.Writer , enableEscapeSequences bool , mode types.SessionParticipantMode ) {
209
215
// wait for the session to copy everything
210
216
s .wg .Add (1 )
211
217
go func () {
212
- defer s .wg .Done ()
213
- defer s .cancel ()
214
- _ , err := io .Copy (stdout , s .stream )
215
- if err != nil {
218
+ defer func () {
219
+ s .wg .Done ()
220
+ s .cancel ()
221
+ }()
222
+ if _ , err := io .Copy (stdout , s .stream ); err != nil {
216
223
fmt .Printf ("Error while reading remote stream: %v\n \r " , err .Error ())
217
224
}
218
225
}()
@@ -225,9 +232,8 @@ func (s *KubeSession) pipeInOut(stdout io.Writer, enableEscapeSequences bool, mo
225
232
handlePeerControls (s .term , enableEscapeSequences , s .stream )
226
233
default :
227
234
handleNonPeerControls (mode , s .term , func () {
228
- err := s .stream .ForceTerminate ()
229
- if err != nil {
230
- log .DebugContext (context .Background (), "Error sending force termination request" , "error" , err )
235
+ if err := s .stream .ForceTerminate (); err != nil {
236
+ log .DebugContext (ctx , "Error sending force termination request" , "error" , err )
231
237
fmt .Print ("\n \r Error while sending force termination request\n \r " )
232
238
}
233
239
})
0 commit comments