forked from ContainerSSH/libcontainerssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_framework_test.go
608 lines (544 loc) · 15.6 KB
/
e2e_framework_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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
package libcontainerssh_test
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io"
"net"
"strings"
"sync"
"testing"
"time"
containerssh "go.containerssh.io/libcontainerssh"
auth2 "go.containerssh.io/libcontainerssh/auth"
"go.containerssh.io/libcontainerssh/auth/webhook"
"go.containerssh.io/libcontainerssh/config"
internalssh "go.containerssh.io/libcontainerssh/internal/ssh"
"go.containerssh.io/libcontainerssh/internal/test"
"go.containerssh.io/libcontainerssh/log"
"go.containerssh.io/libcontainerssh/message"
"go.containerssh.io/libcontainerssh/metadata"
"go.containerssh.io/libcontainerssh/service"
"golang.org/x/crypto/ssh"
)
func NewT(t *testing.T) T {
cfg := config.AppConfig{}
cfg.Default()
return &testContext{
t,
cfg,
&sync.Mutex{},
NewAuthUserStorage(),
0,
0,
nil,
nil,
nil,
nil,
}
}
type T interface {
// StartContainerSSH starts a ContainerSSH instance on a random port. If no authentication has been previously
// configured, this configures ContainerSSH for webhook authentication with an internal user database.
StartContainerSSH()
// ConfigureBackend configures ContainerSSH to use a specific backend.
ConfigureBackend(backend config.Backend)
// LoginViaSSH logs creates a temporary user and logs in via SSH. After this
// has been called new session channels can be requested.
LoginViaSSH()
// StartSessionChannel starts a new session channel in ContainerSSH. Later commands will
// run in the context of this session channel. If multiple session channels are desired,
// each one should be run in a separate subtest.
StartSessionChannel()
// RequestCommandExecution attempts to run the specified command in a previously-opened
// session channel. If no session channel has been opened, this command will fail.
RequestCommandExecution(cmd string)
// RequestShell requests a shell to be executed.
RequestShell()
// AssertStdoutHas waits for the specified output string to be sent from the output.
AssertStdoutHas(output string)
// SendStdin sends the specified string to the SSH server via the standard input.
SendStdin(data string)
// CloseChannel closes the current channel.
CloseChannel()
Parallel()
Run(name string, f func(t T)) bool
Cleanup(func())
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fail()
FailNow()
Failed() bool
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Log(args ...interface{})
Logf(format string, args ...interface{})
Skip(args ...interface{})
SkipNow()
Skipf(format string, args ...interface{})
}
type testContext struct {
*testing.T
cfg config.AppConfig
lock *sync.Mutex
users AuthUserStorage
authPort int
sshPort int
lifecycle service.Lifecycle
sshConn *ssh.Client
channel ssh.Channel
requests <-chan *ssh.Request
}
func (c *testContext) StartContainerSSH() {
t := c.T
t.Helper()
c.lock.Lock()
defer c.lock.Unlock()
if c.lifecycle != nil {
t.Fatalf("ContainerSSH is already running.")
}
t.Logf("Starting ContainerSSH...")
c.authPort = test.GetNextPort(c.T, "auth server")
c.authServer(c.T, c.users, c.authPort)
c.cfg.Auth.PasswordAuth.Method = config.PasswordAuthMethodWebhook
c.cfg.Auth.PasswordAuth.Webhook.URL = fmt.Sprintf("http://127.0.0.1:%d", c.authPort)
c.cfg.Auth.PublicKeyAuth.Method = config.PubKeyAuthMethodWebhook
c.cfg.Auth.PublicKeyAuth.Webhook.URL = fmt.Sprintf("http://127.0.0.1:%d", c.authPort)
c.sshPort = test.GetNextPort(c.T, "ContainerSSH")
c.cfg.SSH.Listen = fmt.Sprintf("127.0.0.1:%d", c.sshPort)
if err := c.cfg.SSH.GenerateHostKey(); err != nil {
t.Fatalf("Failed to generate host keys (%v)", err)
}
c.cfg.Log.T = c.T
c.cfg.Log.Destination = config.LogDestinationTest
cssh, lifecycle, err := containerssh.New(c.cfg, log.NewLoggerFactory())
if err != nil {
t.Fatalf("Failed to start ContainerSSH (%v)", err)
}
c.lifecycle = lifecycle
running := make(chan struct{})
stopped := make(chan struct{})
crashed := make(chan struct{})
lifecycle.OnRunning(
func(s service.Service, l service.Lifecycle) {
close(running)
},
)
lifecycle.OnStopping(
func(s service.Service, l service.Lifecycle, shutdownContext context.Context) {
close(stopped)
},
)
lifecycle.OnCrashed(
func(s service.Service, l service.Lifecycle, err error) {
close(crashed)
},
)
go func() {
_ = cssh.RunWithLifecycle(lifecycle)
}()
c.T.Cleanup(func() {
lifecycle.Stop(context.Background())
})
select {
case <-running:
t.Logf("ContainerSSH is now running.")
case <-stopped:
t.Fatalf("ContainerSSH unexpectedly stopped.")
case <-crashed:
t.Fatalf("ContainerSSH unexpectedly crashed.")
}
t.Logf("Started ContainerSSH.")
}
func (c *testContext) ConfigureBackend(backend config.Backend) {
t := c.T
t.Helper()
t.Logf("Configuring %s backend...", backend)
c.cfg.Backend = backend
switch backend {
case config.BackendKubernetes:
kube := test.Kubernetes(t)
c.cfg.Kubernetes.Connection.ServerName = kube.ServerName
c.cfg.Kubernetes.Connection.Host = kube.Host
c.cfg.Kubernetes.Connection.CAData = kube.CACert
c.cfg.Kubernetes.Connection.KeyData = kube.UserKey
c.cfg.Kubernetes.Connection.CertData = kube.UserCert
case config.BackendSSHProxy:
proxy := test.SSH(t)
c.cfg.SSHProxy.Server = proxy.Host()
c.cfg.SSHProxy.Port = uint16(proxy.Port())
c.cfg.SSHProxy.Username = proxy.Username()
c.cfg.SSHProxy.Password = proxy.Password()
c.cfg.SSHProxy.AllowedHostKeyFingerprints = []string{
proxy.FingerprintSHA256(),
}
c.cfg.SSHProxy.HostKeyAlgorithms = config.MustSSHKeyAlgoListFromStringList(
proxy.HostKeyAlgorithms(),
)
}
t.Logf("Configured %s backend.", backend)
}
func (c *testContext) LoginViaSSH() {
t := c.T
t.Helper()
c.lock.Lock()
defer c.lock.Unlock()
if c.sshConn != nil {
t.Fatalf("Already logged in via SSH.")
}
t.Logf("Logging in via SSH...")
cfg := ssh.ClientConfig{}
cfg.SetDefaults()
username := c.T.Name()
user := c.users.AddUser(username)
password := "test-login"
user.SetPassword(password)
cfg.Auth = append(cfg.Auth, ssh.Password(password))
hostKeys, err := c.cfg.SSH.LoadHostKeys()
if err != nil {
t.Fatalf("Failed to read back the host keys (%v)", err)
}
hostKeyAlgorithms := make([]string, len(hostKeys))
marshalledHostKeys := make([]string, len(hostKeys))
for i, hostKey := range hostKeys {
hostKeyAlgorithms[i] = hostKey.PublicKey().Type()
marshalledHostKeys[i] = string(ssh.MarshalAuthorizedKey(hostKey.PublicKey()))
}
cfg.HostKeyAlgorithms = hostKeyAlgorithms
cfg.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
marshalledHostKey := string(ssh.MarshalAuthorizedKey(key))
for _, hostKey := range marshalledHostKeys {
if hostKey == marshalledHostKey {
return nil
}
}
return fmt.Errorf("invalid host key: %s", marshalledHostKey)
}
cfg.User = username
sshConn, err := ssh.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", c.sshPort), &cfg)
if err != nil {
t.Fatalf("Failed to log in via SSH (%v)", err)
}
c.sshConn = sshConn
t.Logf("SSH login successful.")
}
func (c *testContext) StartSessionChannel() {
t := c.T
t.Helper()
c.lock.Lock()
defer c.lock.Unlock()
if c.sshConn == nil {
t.Fatalf("SSH connection is not running.")
}
if c.channel != nil {
t.Fatalf("A channel is already open.")
}
t.Logf("Starting a new session channel...")
channel, requests, err := c.sshConn.OpenChannel("session", nil)
if err != nil {
t.Fatalf("Failed to open channel (%v)", err)
}
c.channel = channel
c.requests = requests
// We use c.T here so the cleanup happens in the parent test.
c.T.Cleanup(func() {
_ = channel.Close()
c.channel = nil
})
t.Logf("Started a new session channel.")
}
func (c *testContext) RequestCommandExecution(cmd string) {
t := c.T
t.Helper()
c.lock.Lock()
defer c.lock.Unlock()
if c.channel == nil {
t.Fatalf("No channel opened.")
}
t.Logf("Starting program %s ...", cmd)
success, err := c.channel.SendRequest(
string(internalssh.RequestTypeExec),
true,
ssh.Marshal(internalssh.ExecRequestPayload{Exec: cmd}),
)
if err != nil {
t.Fatalf("Failed to send exec request. (%v)", err)
}
if !success {
t.Fatalf("Server rejected exec request. (%v)", err)
}
t.Logf("Started program.")
}
func (c *testContext) RequestShell() {
t := c.T
t.Helper()
c.lock.Lock()
defer c.lock.Unlock()
if c.channel == nil {
t.Fatalf("No channel opened.")
}
t.Logf("Starting shell...")
success, err := c.channel.SendRequest(
string(internalssh.RequestTypeShell),
true,
nil,
)
if err != nil {
t.Fatalf("Failed to send exec request. (%v)", err)
}
if !success {
t.Fatalf("Server rejected exec request. (%v)", err)
}
t.Logf("Started shell.")
}
func (c *testContext) AssertStdoutHas(output string) {
t := c.T
t.Helper()
c.lock.Lock()
defer c.lock.Unlock()
if c.channel == nil {
t.Fatalf("No channel opened.")
}
t.Logf("Waiting for output...")
// Wait a second to allow the output to arrive
time.Sleep(1 * time.Second)
data := make([]byte, 16*1024)
n, err := c.channel.Read(data)
if err != nil {
t.Fatalf("Failed to read channel stdout. (%v)", err)
}
if !strings.Contains(string(data[:n]), output) {
t.Fatalf("Output does not contain '%s' (output was: %s)", output, string(data[:n]))
}
t.Logf("Output check complete.")
}
func (c *testContext) SendStdin(data string) {
t := c.T
t.Helper()
c.lock.Lock()
defer c.lock.Unlock()
if c.channel == nil {
t.Fatalf("No channel opened.")
}
t.Logf("Sending stdin data...")
if _, err := c.channel.Write([]byte(data)); err != nil {
t.Fatalf("Failed to send stdin data (%v)", err)
}
t.Logf("Sent stdin data.")
}
func (c *testContext) CloseChannel() {
t := c.T
t.Helper()
c.lock.Lock()
defer c.lock.Unlock()
if c.channel == nil {
t.Fatalf("No channel opened.")
}
t.Logf("Closing channel...")
if err := c.channel.Close(); err != nil && !errors.Is(err, io.EOF) {
t.Fatalf("Failed to close channel. (%v)", err)
}
c.channel = nil
t.Logf("Closed channel.")
}
func (c *testContext) Run(name string, f func(t T)) bool {
c.T.Helper()
return c.T.Run(name, func(t *testing.T) {
t.Helper()
f(&testContext{
t,
c.cfg,
c.lock,
c.users,
c.authPort,
c.sshPort,
c.lifecycle,
c.sshConn,
nil,
nil,
})
})
}
type authHandler struct {
userdb AuthUserStorage
}
func (a *authHandler) OnPassword(meta metadata.ConnectionAuthPendingMetadata, Password []byte) (
bool,
metadata.ConnectionAuthenticatedMetadata,
error,
) {
user, err := a.userdb.GetUser(meta.Username)
if err != nil {
return false, meta.AuthFailed(), err
}
if pw := user.GetPassword(); pw != nil && *pw == string(Password) {
return true, meta.Authenticated(meta.Username), nil
}
return false, meta.AuthFailed(), fmt.Errorf("incorrect password")
}
func (a *authHandler) OnPubKey(meta metadata.ConnectionAuthPendingMetadata, publicKey auth2.PublicKey) (
bool,
metadata.ConnectionAuthenticatedMetadata,
error,
) {
user, err := a.userdb.GetUser(meta.Username)
if err != nil {
return false, meta.AuthFailed(), err
}
for _, key := range user.GetAuthorizedKeys() {
if key == publicKey.PublicKey {
return true, meta.Authenticated(meta.Username), nil
}
}
return false, meta.AuthFailed(), fmt.Errorf("authentication failed")
}
func (a *authHandler) OnAuthorization(meta metadata.ConnectionAuthenticatedMetadata) (
bool,
metadata.ConnectionAuthenticatedMetadata,
error,
) {
return true, meta, nil
}
func (c *testContext) authServer(t *testing.T, userdb AuthUserStorage, port int) {
srv, err := webhook.NewServer(
config.HTTPServerConfiguration{
Listen: fmt.Sprintf("127.0.0.1:%d", port),
},
&authHandler{
userdb: userdb,
},
log.NewTestLogger(t),
)
if err != nil {
t.Fatalf("Failed to start authentication webhook server. (%v)", err)
}
lifecycle := service.NewLifecycle(srv)
go func() {
_ = lifecycle.Run()
}()
t.Cleanup(func() {
shutdownContext, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
lifecycle.Stop(shutdownContext)
lastError := lifecycle.Wait()
if lastError != nil {
t.Fatalf("Failed to stop authentication webhook server. (%v)", lastError)
}
})
}
// AuthUser is an entry in the in-memory AuthUserStorage. It can be used to modify the user used for testing.
type AuthUser interface {
SetPassword(password string)
// GetPassword
GetPassword() *string
// AddKey adds a new private and public key to this user.
AddKey() ssh.Signer
// GetKeys returns a list of signers containing the private and public key for this user.
GetKeys() []ssh.Signer
// GetAuthorizedKeys returns a list of public keys in the OpenSSH Authorized Keys format.
GetAuthorizedKeys() []string
}
// AuthUserStorage is a storage interface for creating and managing in-memory users used for test
// authentications.
type AuthUserStorage interface {
// AddUser adds a new user to the in-memory database. You can then add credentials for the
// SSH connection to the user. If the user already exists, this function throws a panic.
AddUser(username string) AuthUser
// GetUser returns a user with a specific username. If the user is not found, this function
// throws a panic.
GetUser(username string) (AuthUser, error)
// RemoveUser removes a user from the internal database. If the user is not found, this
// function throws a panic.
RemoveUser(username string)
}
type authUserStorage struct {
lock *sync.Mutex
users map[string]AuthUser
}
func (a *authUserStorage) AddUser(username string) AuthUser {
a.lock.Lock()
defer a.lock.Unlock()
if _, ok := a.users[username]; ok {
panic(message.NewMessage(message.MTest, "User %s already exists in test user database.", username))
}
a.users[username] = &authUser{
lock: &sync.Mutex{},
}
return a.users[username]
}
func (a *authUserStorage) GetUser(username string) (AuthUser, error) {
a.lock.Lock()
defer a.lock.Unlock()
if user, ok := a.users[username]; ok {
return user, nil
}
return nil, message.NewMessage(message.MTest, "User %s not found in test database.", username)
}
func (a *authUserStorage) RemoveUser(username string) {
a.lock.Lock()
defer a.lock.Unlock()
delete(a.users, username)
}
type authUser struct {
lock *sync.Mutex
password *string
keys []ssh.Signer
}
func (a *authUser) SetPassword(password string) {
a.password = &password
}
func (a *authUser) GetPassword() *string {
return a.password
}
func (a *authUser) AddKey() ssh.Signer {
a.lock.Lock()
defer a.lock.Unlock()
reader := rand.Reader
bitSize := 4096
key, err := rsa.GenerateKey(reader, bitSize)
if err != nil {
panic(message.Wrap(err, message.MTest, "Failed to generate RSA key."))
}
var pemBlock = &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}
var pemBytes bytes.Buffer
if err := pem.Encode(&pemBytes, pemBlock); err != nil {
panic(message.Wrap(err, message.MTest, "Failed to marshal private key."))
}
sshPrivateKey, err := ssh.ParsePrivateKey(pemBytes.Bytes())
if err != nil {
panic(message.Wrap(err, message.MTest, "Failed to parse SSH private key."))
}
a.keys = append(a.keys, sshPrivateKey)
return sshPrivateKey
}
func (a *authUser) GetKeys() []ssh.Signer {
a.lock.Lock()
defer a.lock.Unlock()
result := make([]ssh.Signer, len(a.keys))
copy(result, a.keys)
return result
}
func (a *authUser) GetAuthorizedKeys() []string {
a.lock.Lock()
defer a.lock.Unlock()
result := make([]string, len(a.keys))
for i, key := range a.keys {
result[i] = fmt.Sprintf("ssh-rsa %s", ssh.MarshalAuthorizedKey(key.PublicKey()))
}
return result
}
// NewAuthUserStorage creates a new in-memory user storage for authentication.
func NewAuthUserStorage() AuthUserStorage {
return &authUserStorage{
lock: &sync.Mutex{},
users: map[string]AuthUser{},
}
}