/
main.go
170 lines (142 loc) · 3.73 KB
/
main.go
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
package main
import (
"fmt"
"net"
"os"
"strings"
"github.com/tg123/docker-sshd/pkg/bridge"
"github.com/tg123/docker-sshd/pkg/kubesshd"
"k8s.io/client-go/tools/clientcmd"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ssh"
)
func main() {
config := struct {
ListenAddr string
Port int
KeyFile string
Cmd string
Namespace string
}{}
app := &cli.App{
Name: "docker-sshd",
Usage: "make pod container sshable",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "address",
Aliases: []string{"l"},
Value: "0.0.0.0",
Usage: "listening address",
Destination: &config.ListenAddr,
},
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Value: 2232,
Usage: "listening port",
Destination: &config.Port,
},
&cli.StringFlag{
Name: "server-key",
Aliases: []string{"i"},
Usage: "server key files, support wildcard",
Value: "/etc/ssh/ssh_host_ed25519_key",
Destination: &config.KeyFile,
},
&cli.StringFlag{
Name: "command",
Aliases: []string{"c"},
Usage: "default exec command",
Value: "/bin/sh",
Destination: &config.Cmd,
},
&cli.StringFlag{
Name: "namespace",
Usage: "kubernetes namespace",
Value: "default",
Destination: &config.Namespace,
},
},
Action: func(c *cli.Context) error {
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)
kubeClientConfig, err := kubeConfig.ClientConfig()
if err != nil {
return err
}
privateBytes, err := os.ReadFile(config.KeyFile)
if err != nil {
return err
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
return err
}
sshserver := &ssh.ServerConfig{
NoClientAuth: true,
NoClientAuthCallback: func(cm ssh.ConnMetadata) (*ssh.Permissions, error) {
return nil, nil
},
PasswordCallback: func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
return nil, nil
},
PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
return nil, nil
},
KeyboardInteractiveCallback: func(conn ssh.ConnMetadata, client ssh.KeyboardInteractiveChallenge) (*ssh.Permissions, error) {
return nil, nil
},
}
sshserver.AddHostKey(private)
addr := net.JoinHostPort(config.ListenAddr, fmt.Sprintf("%d", config.Port))
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
defer listener.Close()
log.Printf("kube-sshd started, listening at %v", addr)
for {
c, err := listener.Accept()
if err != nil {
log.Printf("failed to accept connection: %v", err)
continue
}
b, err := bridge.New(c, sshserver, &bridge.BridgeConfig{
DefaultCmd: config.Cmd,
}, func(sc *ssh.ServerConn) (bridge.SessionProvider, error) {
full := sc.User()
parts := strings.Split(full, "/")
ns := config.Namespace
pod := full
container := ""
switch len(parts) {
case 2: // Format: pod/container
pod = parts[0]
container = parts[1]
case 3: // Format: namespace/pod/container
ns = parts[0]
pod = parts[1]
container = parts[2]
default:
}
kube, err := kubesshd.New(kubeClientConfig, ns, pod, container)
if err != nil {
return nil, err
}
return kube, nil
})
if err != nil {
log.Printf("failed to establish ssh connection: %v", err)
continue
}
go b.Start()
}
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}