This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
impl_test.go
430 lines (360 loc) · 10.4 KB
/
impl_test.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
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
package sshserver_test
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net"
"strings"
"testing"
"github.com/containerssh/log/standard"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/ssh"
"github.com/containerssh/sshserver"
)
//region Tests
func TestReadyRejection(t *testing.T) {
config := sshserver.DefaultConfig()
if err := config.GenerateHostKey(); err != nil {
assert.Fail(t, "failed to generate host key", err)
return
}
logger := standard.New()
handler := &rejectHandler{}
server, err := sshserver.New(config, handler, logger)
if err != nil {
assert.Fail(t, "failed to create server", err)
return
}
err = server.Run()
if err == nil {
assert.Fail(t, "server.Run() did not result in an error")
} else {
assert.Equal(t, "rejected", err.Error())
}
server.Shutdown(context.Background())
}
func TestAuthFailed(t *testing.T) {
server := newServerHelper(t, "0.0.0.0:2222", map[string][]byte{
"foo": []byte("bar"),
})
hostKey, err := server.start()
if err != nil {
assert.Fail(t, "failed to start ssh server", err)
return
}
defer server.stop()
sshConfig := &ssh.ClientConfig{
User: "foo",
Auth: []ssh.AuthMethod{ssh.Password("invalid")},
}
sshConfig.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
if bytes.Equal(key.Marshal(), hostKey) {
return nil
}
return fmt.Errorf("invalid host")
}
sshConnection, err := ssh.Dial("tcp", "0.0.0.0:2222", sshConfig)
if err != nil {
if !strings.Contains(err.Error(), "unable to authenticate") {
assert.Fail(t, "handshake failed for non-auth reasons", err)
}
} else {
_ = sshConnection.Close()
assert.Fail(t, "authentication succeeded", err)
}
}
func TestSessionSuccess(t *testing.T) {
server := newServerHelper(t, "127.0.0.1:2222", map[string][]byte{
"foo": []byte("bar"),
})
hostKey, err := server.start()
if err != nil {
assert.Fail(t, "failed to start ssh server", err)
return
}
defer server.stop()
reply, exitStatus, err := shellRequestReply("127.0.0.1:2222", "foo", "bar", hostKey, []byte("Hi"))
assert.Equal(t, []byte("Hello world!"), reply)
assert.Equal(t, 0, exitStatus)
assert.Equal(t, nil, err)
}
func TestSessionError(t *testing.T) {
server := newServerHelper(t, "127.0.0.1:2222", map[string][]byte{
"foo": []byte("bar"),
})
hostKey, err := server.start()
if err != nil {
assert.Fail(t, "failed to start ssh server", err)
return
}
defer server.stop()
reply, exitStatus, err := shellRequestReply("127.0.0.1:2222", "foo", "bar", hostKey, []byte("Ho"))
assert.Equal(t, 1, exitStatus)
assert.Equal(t, []byte{}, reply)
assert.Equal(t, nil, err)
}
//endregion
//region Helper
func shellRequestReply(host string, user string, password string, hostKey []byte, request []byte) (reply []byte, exitStatus int, err error) {
sshConfig := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{ssh.Password(password)},
}
sshConfig.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
if bytes.Equal(key.Marshal(), hostKey) {
return nil
}
return fmt.Errorf("invalid host")
}
sshConnection, err := ssh.Dial("tcp", host, sshConfig)
if err != nil {
return nil, -1, fmt.Errorf("handshake failed (%w)", err)
}
defer func() {
if sshConnection != nil {
_ = sshConnection.Close()
}
}()
session, err := sshConnection.NewSession()
if err != nil {
return nil, -1, fmt.Errorf("new session failed (%w)", err)
}
stdin, err := session.StdinPipe()
if err != nil {
return nil, -1, fmt.Errorf("failed to request stdin (%w)", err)
}
stdout, err := session.StdoutPipe()
if err != nil {
return nil, -1, fmt.Errorf("failed to request stdout (%w)", err)
}
if err := session.Shell(); err != nil {
return nil, -1, fmt.Errorf("failed to request shell (%w)", err)
}
if _, err := stdin.Write(request); err != nil {
return nil, -1, fmt.Errorf("failed to write to shell (%w)", err)
}
data := make([]byte, 4096)
n, err := stdout.Read(data)
if err != nil && !errors.Is(err, io.EOF) {
return nil, -1, fmt.Errorf("failed to read from stdout (%w)", err)
}
if err := stdin.Close(); err != nil && !errors.Is(err, io.EOF) {
return data[:n], -1, fmt.Errorf("failed to close stdin (%w)", err)
}
if err := session.Wait(); err != nil {
exitError := &ssh.ExitError{}
if errors.As(err, &exitError) {
exitStatus = exitError.ExitStatus()
} else {
return data[:n], -1, fmt.Errorf("failed to wait for exit (%w)", err)
}
}
if err := session.Close(); err != nil && !errors.Is(err, io.EOF) {
return data[:n], -1, fmt.Errorf("failed to close session (%w)", err)
}
return data[:n], exitStatus, nil
}
func newServerHelper(t *testing.T, listen string, passwords map[string][]byte) *serverHelper {
return &serverHelper{
t: t,
listen: listen,
passwords: passwords,
}
}
type serverHelper struct {
t *testing.T
server sshserver.Server
passwords map[string][]byte
listen string
}
func (h *serverHelper) start() (hostKey []byte, err error) {
if h.server != nil {
return nil, fmt.Errorf("server already running")
}
config := sshserver.DefaultConfig()
config.Listen = h.listen
if err := config.GenerateHostKey(); err != nil {
return nil, err
}
hostKey = config.HostKeys[0].PublicKey().Marshal()
logger := standard.New()
readyChannel := make(chan bool, 1)
errChannel := make(chan error, 1)
handler := newFullHandler(
readyChannel,
h.passwords,
map[string][]byte{},
)
server, err := sshserver.New(config, handler, logger)
if err != nil {
return hostKey, err
}
go func() {
err = server.Run()
if err != nil {
errChannel <- err
}
}()
//Wait for the server to be ready
select {
case err := <-errChannel:
return hostKey, err
case <-readyChannel:
}
h.server = server
return hostKey, nil
}
func (h *serverHelper) stop() {
if h.server != nil {
h.server.Shutdown(context.Background())
}
}
//endregion
//region Handlers
//region Rejection
type rejectHandler struct {
}
func (r *rejectHandler) OnReady() error {
return fmt.Errorf("rejected")
}
func (r *rejectHandler) OnShutdown(_ context.Context) {
}
func (r *rejectHandler) OnNetworkConnection(_ net.Addr) (sshserver.NetworkConnectionHandler, error) {
return nil, fmt.Errorf("not implemented")
}
//endregion
//region Full
func newFullHandler(readyChannel chan bool, passwords map[string][]byte, pubKeys map[string][]byte) sshserver.Handler {
ctx, cancelFunc := context.WithCancel(context.Background())
return &fullHandler{
ctx: ctx,
cancelFunc: cancelFunc,
ready: readyChannel,
passwords: passwords,
pubKeys: pubKeys,
}
}
//region Handler
type fullHandler struct {
ctx context.Context
shutdownContext context.Context
cancelFunc context.CancelFunc
passwords map[string][]byte
pubKeys map[string][]byte
ready chan bool
}
func (f *fullHandler) OnReady() error {
f.ready <- true
return nil
}
func (f *fullHandler) OnShutdown(shutdownContext context.Context) {
f.shutdownContext = shutdownContext
}
func (f *fullHandler) OnNetworkConnection(_ net.Addr) (sshserver.NetworkConnectionHandler, error) {
return &fullNetworkConnectionHandler{
handler: f,
}, nil
}
//endregion
//region Network connection handler
type fullNetworkConnectionHandler struct {
handler *fullHandler
}
func (f *fullNetworkConnectionHandler) OnAuthPassword(username string, password []byte) (response sshserver.AuthResponse, reason error) {
if storedPassword, ok := f.handler.passwords[username]; ok && bytes.Equal(storedPassword, password) {
return sshserver.AuthResponseSuccess, nil
}
return sshserver.AuthResponseFailure, fmt.Errorf("authentication failed")
}
func (f *fullNetworkConnectionHandler) OnAuthPubKey(username string, pubKey []byte) (response sshserver.AuthResponse, reason error) {
if storedPubKey, ok := f.handler.pubKeys[username]; ok && bytes.Equal(storedPubKey, pubKey) {
return sshserver.AuthResponseSuccess, nil
}
return sshserver.AuthResponseFailure, fmt.Errorf("authentication failed")
}
func (f *fullNetworkConnectionHandler) OnHandshakeFailed(_ error) {
}
func (f *fullNetworkConnectionHandler) OnHandshakeSuccess() (connection sshserver.SSHConnectionHandler, failureReason error) {
return &fullSSHConnectionHandler{
handler: f.handler,
}, nil
}
func (f *fullNetworkConnectionHandler) OnDisconnect() {
}
//endregion
//region SSH connection handler
type fullSSHConnectionHandler struct {
handler *fullHandler
}
func (f *fullSSHConnectionHandler) OnUnsupportedGlobalRequest(_ string, _ []byte) {
}
func (f *fullSSHConnectionHandler) OnUnsupportedChannel(_ string, _ []byte) {
}
func (f *fullSSHConnectionHandler) OnSessionChannel(_ []byte) (channel sshserver.SessionChannelHandler, failureReason sshserver.ChannelRejection) {
return &fullSessionChannelHandler{
handler: f.handler,
env: map[string]string{},
}, nil
}
//endregion
//region Session channel handler
type fullSessionChannelHandler struct {
handler *fullHandler
env map[string]string
}
func (f *fullSessionChannelHandler) OnUnsupportedChannelRequest(_ string, _ []byte) {
}
func (f *fullSessionChannelHandler) OnFailedDecodeChannelRequest(_ string, _ []byte, _ error) {
}
func (f *fullSessionChannelHandler) OnEnvRequest(name string, value string) error {
f.env[name] = value
return nil
}
func (f *fullSessionChannelHandler) OnExecRequest(
_ string, _ io.Reader, _ io.Writer, _ io.Writer, _ func(exitStatus uint32),
) error {
return fmt.Errorf("this server does not support exec")
}
func (f *fullSessionChannelHandler) OnPtyRequest(
_ string, _ uint32, _ uint32, _ uint32, _ uint32, _ []byte,
) error {
return fmt.Errorf("this server does not support PTY")
}
func (f *fullSessionChannelHandler) OnShell(
stdin io.Reader, stdout io.Writer, _ io.Writer, onExit func(exitStatus uint32),
) error {
go func() {
data := make([]byte, 4096)
n, err := stdin.Read(data)
if err != nil {
onExit(1)
return
}
if string(data[:n]) != "Hi" {
onExit(1)
return
}
if _, err := stdout.Write([]byte("Hello world!")); err != nil {
onExit(1)
return
}
onExit(0)
}()
return nil
}
func (f *fullSessionChannelHandler) OnSignal(_ string) error {
return nil
}
func (f *fullSessionChannelHandler) OnSubsystem(
_ string, _ io.Reader, _ io.Writer, _ io.Writer, _ func(exitStatus uint32),
) error {
return fmt.Errorf("subsystem not supported")
}
func (f *fullSessionChannelHandler) OnWindow(_ uint32, _ uint32, _ uint32, _ uint32) error {
return nil
}
//endregion
//endregion
//endregion