-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathssh_gateway.go
More file actions
454 lines (402 loc) · 12 KB
/
Copy pathssh_gateway.go
File metadata and controls
454 lines (402 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
sshserver "github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/tools/remotecommand"
transportspdy "k8s.io/client-go/transport/spdy"
spritzv1 "spritz.sh/operator/api/v1"
)
const sshPrincipalDelimiter = ":"
type sshPortForwardActivityStartedKey struct{}
type sshLocalForwardChannelData struct {
DestAddr string
DestPort uint32
OriginAddr string
OriginPort uint32
}
type closeFunc func() error
func (f closeFunc) Close() error {
return f()
}
func formatSSHPrincipal(prefix, namespace, name string) string {
return strings.Join([]string{prefix, namespace, name}, sshPrincipalDelimiter)
}
func parseSSHPrincipal(prefix, principal string) (string, string, bool) {
parts := strings.Split(principal, sshPrincipalDelimiter)
if len(parts) != 3 {
return "", "", false
}
if parts[0] != prefix {
return "", "", false
}
if parts[1] == "" || parts[2] == "" {
return "", "", false
}
return parts[1], parts[2], true
}
func (s *server) startSSHGateway(ctx context.Context) error {
cfg := s.sshGateway
if !cfg.enabled {
return nil
}
server := s.newSSHGatewayServer()
server.AddHostKey(cfg.hostSigner)
errCh := make(chan error, 1)
go func() {
errCh <- server.ListenAndServe()
}()
go func() {
<-ctx.Done()
_ = server.Close()
}()
select {
case err := <-errCh:
return err
case <-time.After(250 * time.Millisecond):
log.Printf("spritz ssh gateway listening on %s", cfg.listenAddr)
return nil
}
}
func (s *server) newSSHGatewayServer() *sshserver.Server {
cfg := s.sshGateway
return &sshserver.Server{
Addr: cfg.listenAddr,
Handler: s.handleSSHSession,
PublicKeyHandler: s.handleSSHAuth,
Version: "spritz",
ChannelHandlers: map[string]sshserver.ChannelHandler{
"session": sshserver.DefaultSessionHandler,
"direct-tcpip": s.handleSSHPortForward,
},
LocalPortForwardingCallback: s.allowSSHPortForwardDestination,
}
}
func (s *server) handleSSHAuth(ctx sshserver.Context, key sshserver.PublicKey) bool {
cert, ok := key.(*gossh.Certificate)
if !ok {
log.Printf("spritz ssh: auth failed user=%s reason=missing-cert", ctx.User())
return false
}
if err := s.sshGateway.certChecker.CheckCert(ctx.User(), cert); err != nil {
log.Printf("spritz ssh: auth failed user=%s key_id=%s err=%v", ctx.User(), cert.KeyId, err)
return false
}
return true
}
func (s *server) handleSSHSession(sess sshserver.Session) {
principal := sess.User()
namespace, name, ok := parseSSHPrincipal(s.sshGateway.principalPrefix, principal)
if !ok {
log.Printf("spritz ssh: invalid principal value=%s", principal)
_, _ = io.WriteString(sess, "invalid ssh principal\n")
_ = sess.Exit(1)
return
}
keyID := ""
if cert, ok := sess.PublicKey().(*gossh.Certificate); ok {
keyID = strings.TrimPrefix(cert.KeyId, "spritz:")
}
log.Printf("spritz ssh: session start name=%s namespace=%s user_id=%s", name, namespace, keyID)
defer log.Printf("spritz ssh: session end name=%s namespace=%s user_id=%s", name, namespace, keyID)
spritz := &spritzv1.Spritz{}
if err := s.client.Get(sess.Context(), clientKey(namespace, name), spritz); err != nil {
log.Printf("spritz ssh: spritz not found name=%s namespace=%s user_id=%s err=%v", name, namespace, keyID, err)
_, _ = io.WriteString(sess, "spritz not ready\n")
_ = sess.Exit(1)
return
}
pod, err := s.findRunningPod(sess.Context(), namespace, name, s.sshGateway.containerName)
if err != nil {
log.Printf("spritz ssh: pod not ready name=%s namespace=%s err=%v", name, namespace, err)
_, _ = io.WriteString(sess, "spritz not ready\n")
_ = sess.Exit(1)
return
}
s.ensureSSHActivityLoop(sess.Context(), spritz)
pty, winCh, hasPty := sess.Pty()
sizeQueue := newTerminalSizeQueue()
if hasPty {
sizeQueue.push(uint16(pty.Window.Width), uint16(pty.Window.Height))
go func() {
for win := range winCh {
sizeQueue.push(uint16(win.Width), uint16(win.Height))
}
}()
}
if err := s.streamSSH(sess.Context(), pod, sess, hasPty, sizeQueue); err != nil {
log.Printf("spritz ssh: stream failed name=%s namespace=%s err=%v", name, namespace, err)
_ = sess.Exit(1)
return
}
_ = sess.Exit(0)
}
func (s *server) handleSSHPortForward(srv *sshserver.Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx sshserver.Context) {
var request sshLocalForwardChannelData
if err := gossh.Unmarshal(newChan.ExtraData(), &request); err != nil {
newChan.Reject(gossh.ConnectionFailed, "error parsing forward data: "+err.Error())
return
}
if srv.LocalPortForwardingCallback == nil || !srv.LocalPortForwardingCallback(ctx, request.DestAddr, request.DestPort) {
newChan.Reject(gossh.Prohibited, "port forwarding is disabled")
return
}
namespace, name, ok := parseSSHPrincipal(s.sshGateway.principalPrefix, ctx.User())
if !ok {
log.Printf("spritz ssh: invalid forward principal value=%s", ctx.User())
newChan.Reject(gossh.Prohibited, "invalid ssh principal")
return
}
spritz := &spritzv1.Spritz{}
if err := s.client.Get(ctx, clientKey(namespace, name), spritz); err != nil {
log.Printf("spritz ssh: forward spritz not found name=%s namespace=%s err=%v", name, namespace, err)
newChan.Reject(gossh.ConnectionFailed, "spritz not ready")
return
}
pod, err := s.findSSHGatewayPod(ctx, namespace, name, s.sshGateway.containerName)
if err != nil {
log.Printf("spritz ssh: forward pod not ready name=%s namespace=%s err=%v", name, namespace, err)
newChan.Reject(gossh.ConnectionFailed, "spritz not ready")
return
}
s.ensureSSHActivityLoop(ctx, spritz)
upstream, cleanup, err := s.openSSHPortForward(ctx, pod, request.DestPort)
if err != nil {
log.Printf("spritz ssh: forward open failed name=%s namespace=%s port=%d err=%v", name, namespace, request.DestPort, err)
newChan.Reject(gossh.ConnectionFailed, "port forward unavailable")
return
}
channel, requests, err := newChan.Accept()
if err != nil {
_ = upstream.Close()
_ = cleanup.Close()
return
}
go gossh.DiscardRequests(requests)
var once sync.Once
closeAll := func() {
once.Do(func() {
_ = channel.Close()
_ = upstream.Close()
_ = cleanup.Close()
})
}
go func() {
defer closeAll()
_, _ = io.Copy(channel, upstream)
}()
go func() {
defer closeAll()
_, _ = io.Copy(upstream, channel)
}()
}
func (s *server) allowSSHPortForwardDestination(ctx sshserver.Context, destinationHost string, destinationPort uint32) bool {
if !isLoopbackSSHForwardHost(destinationHost) {
log.Printf("spritz ssh: rejected forward user=%s host=%s port=%d", ctx.User(), destinationHost, destinationPort)
return false
}
return true
}
func isLoopbackSSHForwardHost(host string) bool {
normalized := strings.TrimSpace(host)
normalized = strings.TrimPrefix(normalized, "[")
normalized = strings.TrimSuffix(normalized, "]")
if normalized == "" {
return false
}
if strings.EqualFold(normalized, "localhost") {
return true
}
ip := net.ParseIP(normalized)
return ip != nil && ip.IsLoopback()
}
func (s *server) ensureSSHActivityLoop(ctx sshserver.Context, spritz *spritzv1.Spritz) {
if s == nil || spritz == nil {
return
}
ctx.Lock()
defer ctx.Unlock()
if started, ok := ctx.Value(sshPortForwardActivityStartedKey{}).(bool); ok && started {
return
}
ctx.SetValue(sshPortForwardActivityStartedKey{}, true)
s.startSSHActivityLoop(ctx, spritz)
}
func (s *server) findSSHGatewayPod(ctx context.Context, namespace, name, container string) (*corev1.Pod, error) {
if s.findRunningPodFunc != nil {
return s.findRunningPodFunc(ctx, namespace, name, container)
}
return s.findRunningPod(ctx, namespace, name, container)
}
func (s *server) openSSHPortForward(ctx context.Context, pod *corev1.Pod, remotePort uint32) (net.Conn, io.Closer, error) {
if s.openSSHPortForwardFunc != nil {
return s.openSSHPortForwardFunc(ctx, pod, remotePort)
}
if s.clientset == nil || s.restConfig == nil {
return nil, nil, errors.New("ssh port forwarding is not configured")
}
req := s.clientset.CoreV1().RESTClient().
Post().
Resource("pods").
Name(pod.Name).
Namespace(pod.Namespace).
SubResource("portforward")
transport, upgrader, err := transportspdy.RoundTripperFor(s.restConfig)
if err != nil {
return nil, nil, err
}
dialer := transportspdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, req.URL())
stopCh := make(chan struct{})
readyCh := make(chan struct{})
errCh := make(chan error, 1)
forwarder, err := portforward.NewOnAddresses(
dialer,
[]string{"127.0.0.1"},
[]string{fmt.Sprintf("0:%d", remotePort)},
stopCh,
readyCh,
io.Discard,
io.Discard,
)
if err != nil {
close(stopCh)
return nil, nil, err
}
go func() {
errCh <- forwarder.ForwardPorts()
}()
select {
case <-readyCh:
case err := <-errCh:
close(stopCh)
return nil, nil, err
case <-ctx.Done():
close(stopCh)
return nil, nil, ctx.Err()
}
ports, err := forwarder.GetPorts()
if err != nil {
close(stopCh)
return nil, nil, err
}
if len(ports) != 1 {
close(stopCh)
return nil, nil, fmt.Errorf("unexpected forwarded port count: %d", len(ports))
}
localAddress := net.JoinHostPort("127.0.0.1", strconv.Itoa(int(ports[0].Local)))
upstream, err := (&net.Dialer{}).DialContext(ctx, "tcp", localAddress)
if err != nil {
close(stopCh)
return nil, nil, err
}
var once sync.Once
cleanup := closeFunc(func() error {
once.Do(func() {
close(stopCh)
})
return nil
})
go func() {
err := <-errCh
if err == nil || errors.Is(err, portforward.ErrLostConnectionToPod) || errors.Is(err, context.Canceled) {
return
}
log.Printf("spritz ssh: port-forward ended pod=%s namespace=%s remote_port=%d err=%v", pod.Name, pod.Namespace, remotePort, err)
}()
return upstream, cleanup, nil
}
func sshActivityRefreshInterval(spec spritzv1.SpritzSpec, fallback time.Duration) time.Duration {
interval := fallback
if interval <= 0 {
interval = time.Minute
}
if raw := strings.TrimSpace(spec.IdleTTL); raw != "" {
if idleTTL, err := time.ParseDuration(raw); err == nil && idleTTL > 0 {
candidate := idleTTL / 2
if candidate <= 0 {
candidate = idleTTL
}
if candidate > 0 && candidate < interval {
interval = candidate
}
}
}
if interval <= 0 {
return time.Minute
}
return interval
}
func (s *server) startSSHActivityLoop(ctx context.Context, spritz *spritzv1.Spritz) {
if s == nil || spritz == nil {
return
}
record := func(when time.Time) {
refreshCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.recordSpritzActivity(refreshCtx, spritz.Namespace, spritz.Name, when); err != nil {
log.Printf("spritz ssh: failed to refresh activity name=%s namespace=%s err=%v", spritz.Name, spritz.Namespace, err)
}
}
record(time.Now())
interval := sshActivityRefreshInterval(spritz.Spec, s.sshGateway.activityRefresh)
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case tick := <-ticker.C:
record(tick)
}
}
}()
}
func (s *server) streamSSH(ctx context.Context, pod *corev1.Pod, sess sshserver.Session, hasPty bool, sizeQueue *terminalSizeQueue) error {
if len(s.sshGateway.command) == 0 {
return fmt.Errorf("ssh command missing")
}
req := s.clientset.CoreV1().RESTClient().
Post().
Resource("pods").
Name(pod.Name).
Namespace(pod.Namespace).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Container: s.sshGateway.containerName,
Command: s.sshGateway.command,
Stdin: true,
Stdout: true,
Stderr: true,
TTY: hasPty,
}, scheme.ParameterCodec)
executor, err := remotecommand.NewSPDYExecutor(s.restConfig, "POST", req.URL())
if err != nil {
return err
}
stdout := sess
stderr := sess.Stderr()
if stderr == nil {
stderr = sess
}
return executor.Stream(remotecommand.StreamOptions{
Stdin: sess,
Stdout: stdout,
Stderr: stderr,
Tty: hasPty,
TerminalSizeQueue: sizeQueue,
})
}