-
Notifications
You must be signed in to change notification settings - Fork 95
/
replay_test.go
624 lines (579 loc) · 16.7 KB
/
replay_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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"github.com/jaksi/sshutils"
"golang.org/x/crypto/ssh"
)
type replayTest struct {
User string `json:"user"`
Events []replayTestEvent `json:"events"`
PlainLogs []string `json:"plain_logs"`
JSONLogs []map[string]interface{} `json:"json_logs"`
}
type replayTestEvent struct {
Source source `json:"source"`
Type string `json:"type"`
Entry replayTestEntry `json:"entry"`
}
func (event *replayTestEvent) UnmarshalJSON(data []byte) error {
var rawEvent struct {
Source source `json:"source"`
Type string `json:"type"`
Entry interface{} `json:"entry"`
}
if err := json.Unmarshal(data, &rawEvent); err != nil {
return err
}
event.Source = rawEvent.Source
event.Type = rawEvent.Type
entryData, err := json.Marshal(rawEvent.Entry)
if err != nil {
return err
}
var entry replayTestEntry
switch rawEvent.Type {
case "global_request":
entry = &globalRequestReplayEntry{}
case "new_channel":
entry = &newChannelReplayEntry{}
case "channel_data":
entry = &channelDataReplayEntry{}
case "channel_eof":
entry = &channelEOFReplayEntry{}
case "channel_close":
entry = &channelCloseReplayEntry{}
case "connection_close":
entry = &connectionCloseReplayEntry{}
case "channel_request":
entry = &channelRequestReplayEntry{}
case "channel_error":
entry = &channelErrorReplayEntry{}
default:
return fmt.Errorf("unsuported event type %s", event.Type)
}
if err = json.Unmarshal(entryData, &entry); err != nil {
return err
}
event.Entry = entry
return nil
}
type source int
const (
client source = iota
server
)
func (src source) String() string {
switch src {
case client:
return "client"
case server:
return "server"
default:
return "unknown"
}
}
func (src *source) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"client"`:
*src = client
case `"server"`:
*src = server
default:
return fmt.Errorf("unknown source: %q", string(data))
}
return nil
}
type replayTestEntry interface {
Execute(t *testing.T, context *clientContext) error
Wait(t *testing.T, context *clientContext) error
}
type clientChannel struct {
channel ssh.Channel
requests <-chan *ssh.Request
}
type clientContext struct {
conn ssh.Conn
requests <-chan *ssh.Request
channels <-chan ssh.NewChannel
clientChannels []clientChannel
}
type globalRequestReplayEntry struct {
Type string `json:"type"`
WantReply bool `json:"want_reply"`
Payload string `json:"payload"`
Accepted bool `json:"accepted"`
Response string `json:"response"`
}
func (event globalRequestReplayEntry) Execute(t *testing.T, context *clientContext) error {
rawPayload, err := base64.RawStdEncoding.DecodeString(event.Payload)
if err != nil {
return err
}
accepted, response, err := context.conn.SendRequest(event.Type, event.WantReply, rawPayload)
if err != nil {
return err
}
if accepted != event.Accepted {
t.Errorf("Accepted mismatch: got %v, want %v", accepted, event.Accepted)
}
if event.Type != "tcpip-forward" {
encodedResponse := base64.RawStdEncoding.EncodeToString(response)
if encodedResponse != event.Response {
t.Errorf("Response mismatch: got %v, want %v", encodedResponse, event.Response)
}
}
return nil
}
func (event globalRequestReplayEntry) Wait(t *testing.T, context *clientContext) error {
rawResponse, err := base64.RawStdEncoding.DecodeString(event.Response)
if err != nil {
return err
}
select {
case request, ok := <-context.requests:
if !ok {
return fmt.Errorf("Connection was closed")
}
if request.WantReply {
if err := request.Reply(event.Accepted, rawResponse); err != nil {
return err
}
}
if request.Type != event.Type {
t.Errorf("Type mismatch: got %v, want %v", request.Type, event.Type)
}
if request.WantReply != event.WantReply {
t.Errorf("WantReply mismatch: got %v, want %v", request.WantReply, event.WantReply)
}
if request.Type != "[email protected]" {
encodedPayload := base64.RawStdEncoding.EncodeToString(request.Payload)
if encodedPayload != event.Payload {
t.Errorf("Payload mismatch: got %v, want %v", encodedPayload, event.Payload)
}
}
case <-time.After(100 * time.Millisecond):
t.Errorf("Timed out waiting for request")
}
return nil
}
type newChannelReplayEntry struct {
Type string `json:"type"`
ExtraData string `json:"extra_data"`
Accepted bool `json:"accepted"`
RejectReason uint32 `json:"reject_reason"`
Message string `json:"message"`
}
func (event newChannelReplayEntry) Execute(t *testing.T, context *clientContext) error {
rawExtraData, err := base64.RawStdEncoding.DecodeString(event.ExtraData)
if err != nil {
return err
}
channel, requests, err := context.conn.OpenChannel(event.Type, rawExtraData)
accepted := true
if openChannelErr, ok := err.(*ssh.OpenChannelError); ok {
accepted = false
if uint32(openChannelErr.Reason) != event.RejectReason {
t.Errorf("Reject reason mismatch: got %v, want %v", uint32(openChannelErr.Reason), event.RejectReason)
}
if openChannelErr.Message != event.Message {
t.Errorf("Message mismatch: got %q, want %q", openChannelErr.Message, event.Message)
}
} else if err != nil {
return err
}
if accepted != event.Accepted {
t.Errorf("Accepted mismatch: got %v, want %v", accepted, event.Accepted)
}
if accepted {
context.clientChannels = append(context.clientChannels, clientChannel{channel, requests})
}
return nil
}
func (event newChannelReplayEntry) Wait(t *testing.T, context *clientContext) error {
select {
case newChannel, ok := <-context.channels:
if !ok {
return fmt.Errorf("Connection was closed")
}
if event.Accepted {
channel, requests, err := newChannel.Accept()
if err != nil {
return err
}
context.clientChannels = append(context.clientChannels, clientChannel{channel, requests})
} else {
if err := newChannel.Reject(ssh.RejectionReason(event.RejectReason), event.Message); err != nil {
return err
}
}
if newChannel.ChannelType() != event.Type {
t.Errorf("Type mismatch: got %v, want %v", newChannel.ChannelType(), event.Type)
}
encodedExtraData := base64.RawStdEncoding.EncodeToString(newChannel.ExtraData())
if encodedExtraData != event.ExtraData {
t.Errorf("ExtraData mismatch: got %v, want %v", encodedExtraData, event.ExtraData)
}
case <-time.After(100 * time.Millisecond):
t.Errorf("Timed out waiting for channel")
}
return nil
}
type channelDataReplayEntry struct {
ChannelID int `json:"channel_id"`
Data string `json:"data"`
}
type readWriteResult struct {
n int
err error
}
func (event channelDataReplayEntry) Execute(t *testing.T, context *clientContext) error {
data := make(chan readWriteResult)
go func() {
n, err := context.clientChannels[event.ChannelID].channel.Write([]byte(event.Data))
data <- readWriteResult{n, err}
}()
select {
case result := <-data:
if result.err != nil {
return result.err
}
if result.n != len(event.Data) {
t.Errorf("Wrote %d bytes, want %d", result.n, len(event.Data))
}
case <-time.After(100 * time.Millisecond):
t.Errorf("Timed out writing to channel")
}
return nil
}
func (event channelDataReplayEntry) Wait(t *testing.T, context *clientContext) error {
buffer := make([]byte, len(event.Data))
data := make(chan readWriteResult)
go func() {
var result readWriteResult
for result.n < len(event.Data) {
n, err := context.clientChannels[event.ChannelID].channel.Read(buffer[result.n:])
result.n += n
result.err = err
if err != nil || n == 0 {
break
}
}
data <- result
}()
select {
case result := <-data:
if result.err != nil {
return result.err
}
if result.n != len(event.Data) {
t.Errorf("Read %d bytes, want %d", result.n, len(event.Data))
}
if string(buffer[:result.n]) != event.Data {
t.Errorf("Data mismatch: got %q, want %q", string(buffer[:result.n]), event.Data)
}
case <-time.After(100 * time.Millisecond):
t.Errorf("Timed out reading from channel. Data so far: %q, want %q", string(buffer), event.Data)
}
return nil
}
type channelEOFReplayEntry struct {
ChannelID int `json:"channel_id"`
}
func (event channelEOFReplayEntry) Execute(t *testing.T, context *clientContext) error {
if err := context.clientChannels[event.ChannelID].channel.CloseWrite(); err != nil {
if err == io.EOF {
t.Errorf("Channel already closed for writing")
} else {
return err
}
}
return nil
}
func (event channelEOFReplayEntry) Wait(t *testing.T, context *clientContext) error {
buffer := make([]byte, 256)
n, err := context.clientChannels[event.ChannelID].channel.Read(buffer)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
t.Errorf("Read %q, want EOF", string(buffer[:n]))
return nil
}
type channelCloseReplayEntry struct {
ChannelID int `json:"channel_id"`
}
func (event channelCloseReplayEntry) Execute(t *testing.T, context *clientContext) error {
if err := context.clientChannels[event.ChannelID].channel.Close(); err != nil {
if err == io.EOF {
t.Errorf("Channel already closed")
} else {
return err
}
}
return nil
}
func (event channelCloseReplayEntry) Wait(t *testing.T, context *clientContext) error {
select {
case _, ok := <-context.clientChannels[event.ChannelID].requests:
if ok {
t.Errorf("Channel was not closed")
}
case <-time.After(100 * time.Millisecond):
t.Errorf("Timed out waiting for channel to close")
}
return nil
}
type connectionCloseReplayEntry struct {
}
func (event connectionCloseReplayEntry) Execute(t *testing.T, context *clientContext) error {
if err := context.conn.Close(); err != nil {
if err == io.EOF {
t.Errorf("Connection already closed")
} else {
return err
}
}
return nil
}
func (event connectionCloseReplayEntry) Wait(t *testing.T, context *clientContext) error {
result := make(chan error)
go func() {
result <- context.conn.Wait()
}()
select {
case err := <-result:
return err
case <-time.After(100 * time.Millisecond):
t.Errorf("Timed out waiting for connection to close")
}
return nil
}
type channelRequestReplayEntry struct {
ChannelID int `json:"channel_id"`
Type string `json:"type"`
WantReply bool `json:"want_reply"`
Payload string `json:"payload"`
Accepted bool `json:"accepted"`
}
func (event channelRequestReplayEntry) Execute(t *testing.T, context *clientContext) error {
rawPayload, err := base64.RawStdEncoding.DecodeString(event.Payload)
if err != nil {
return err
}
accepted, err := context.clientChannels[event.ChannelID].channel.SendRequest(event.Type, event.WantReply, rawPayload)
if err != nil {
return err
}
if accepted != event.Accepted {
t.Errorf("Accepted mismatch: got %v, want %v", accepted, event.Accepted)
}
return nil
}
func (event channelRequestReplayEntry) Wait(t *testing.T, context *clientContext) error {
select {
case request, ok := <-context.clientChannels[event.ChannelID].requests:
if !ok {
t.Errorf("Channel was closed")
return nil
}
if request.WantReply {
if err := request.Reply(event.Accepted, nil); err != nil {
return err
}
}
if request.Type != event.Type {
t.Errorf("Type mismatch: got %v, want %v", request.Type, event.Type)
}
encodedPayload := base64.RawStdEncoding.EncodeToString(request.Payload)
if encodedPayload != event.Payload {
t.Errorf("Payload mismatch: got %v, want %v", encodedPayload, event.Payload)
}
if request.WantReply != event.WantReply {
t.Errorf("WantReply mismatch: got %v, want %v", request.WantReply, event.WantReply)
}
case <-time.After(100 * time.Millisecond):
t.Errorf("Timed out waiting for request")
}
return nil
}
type channelErrorReplayEntry struct {
ChannelID int `json:"channel_id"`
Data string `json:"data"`
}
func (event channelErrorReplayEntry) Execute(t *testing.T, context *clientContext) error {
return fmt.Errorf("client can't send stderr")
}
func (event channelErrorReplayEntry) Wait(t *testing.T, context *clientContext) error {
buffer := make([]byte, len(event.Data))
data := make(chan readWriteResult)
go func() {
n, err := context.clientChannels[event.ChannelID].channel.Stderr().Read(buffer)
data <- readWriteResult{n, err}
}()
select {
case result := <-data:
if result.err != nil {
return result.err
}
if result.n != len(event.Data) {
t.Errorf("Read %d bytes, want %d", result.n, len(event.Data))
}
if string(buffer[:result.n]) != event.Data {
t.Errorf("Data mismatch: got %q, want %q", string(buffer[:result.n]), event.Data)
}
case <-time.After(100 * time.Millisecond):
t.Errorf("Timed out reading from channel. Data so far: %q, want %q", string(buffer), event.Data)
}
return nil
}
func TestReplay(t *testing.T) {
tempDir := t.TempDir()
keyFile, err := generateKey(tempDir, ecdsa_key)
if err != nil {
t.Fatal(err)
}
cfg := &config{}
cfg.Server.HostKeys = []string{keyFile}
cfg.Server.TCPIPServices = map[uint32]string{
80: "HTTP",
}
cfg.Auth.NoAuth = true
if err := cfg.setupSSHConfig(); err != nil {
t.Fatal(err)
}
listener, err := sshutils.Listen("localhost:0", cfg.sshConfig)
if err != nil {
t.Fatal(err)
}
defer listener.Close()
testFiles, err := filepath.Glob("replay_tests/*.json")
if err != nil {
t.Fatal(err)
}
for _, testFile := range testFiles {
fileName := strings.TrimSuffix(path.Base(testFile), path.Ext(testFile))
for _, jsonLogging := range []bool{false, true} {
cfg.Logging.JSON = jsonLogging
var testName string
if !jsonLogging {
testName = fmt.Sprint(fileName, "_plain")
} else {
testName = fmt.Sprint(fileName, "_json")
}
t.Run(testName, func(t *testing.T) {
logBuffer := setupLogBuffer(t, cfg)
testCaseBytes, err := os.ReadFile(testFile)
if err != nil {
t.Fatal(err)
}
var testCase replayTest
if err := json.Unmarshal(testCaseBytes, &testCase); err != nil {
t.Fatal(err)
}
serverResult := make(chan error)
go func() {
conn, err := listener.Accept()
if err != nil {
serverResult <- err
return
}
handleConnection(conn, cfg)
serverResult <- nil
}()
conn, err := net.Dial("tcp", listener.Addr().String())
if err != nil {
t.Fatal(err)
}
sshConn, newChannels, requests, err := ssh.NewClientConn(conn, listener.Addr().String(), &ssh.ClientConfig{
User: testCase.User,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
conn.Close()
t.Fatal(err)
}
defer sshConn.Close()
context := clientContext{
conn: sshConn,
requests: requests,
clientChannels: []clientChannel{},
}
for _, event := range testCase.Events {
var err error
if event.Source == client {
//t.Logf("> %v %v", event.Type, event.Entry)
err = event.Entry.Execute(t, &context)
time.Sleep(10 * time.Millisecond)
} else {
//t.Logf("< %v %#v", event.Type, event.Entry)
err = event.Entry.Wait(t, &context)
}
if err != nil {
t.Fatalf("%v: %v", event, err)
}
}
if err := <-serverResult; err != nil {
t.Fatal(err)
}
for _, channel := range context.clientChannels {
for request := range channel.requests {
t.Errorf("unexpected request: %#v", request)
}
}
for newChannel := range newChannels {
t.Errorf("unexpected new channel: %#v", newChannel)
}
for request := range requests {
t.Errorf("unexpected request: %#v", request)
}
logs := strings.TrimSpace(logBuffer.String())
logLines := strings.Split(logs, "\n")
if !jsonLogging {
if len(logLines) != len(testCase.PlainLogs) {
t.Errorf("Logs mismatch: got %d lines, want %d. Logs:\n%s", len(logLines), len(testCase.PlainLogs), logs)
}
for i, logLine := range logLines {
if i >= len(testCase.PlainLogs) {
break
}
expectedLogLine := strings.ReplaceAll(testCase.PlainLogs[i], "SOURCE", conn.LocalAddr().String())
if logLine != expectedLogLine {
t.Errorf("Log mismatch at line %d: got \n%q, want \n%q", i, logLine, expectedLogLine)
}
}
} else {
if len(logLines) != len(testCase.JSONLogs) {
t.Errorf("Logs mismatch: got %d lines, want %d. Logs:\n%s", len(logLines), len(testCase.JSONLogs), logs)
}
for i, logLine := range logLines {
if i >= len(testCase.JSONLogs) {
break
}
parsedLogLine := map[string]interface{}{}
if err := json.Unmarshal([]byte(logLine), &parsedLogLine); err != nil {
t.Fatal(err)
}
expectedLogLine := testCase.JSONLogs[i]
expectedLogLine["source"] = conn.LocalAddr().String()
if !reflect.DeepEqual(parsedLogLine, expectedLogLine) {
t.Errorf("Log mismatch at line %d: got \n%#v, want \n%#v", i, parsedLogLine, expectedLogLine)
}
}
}
})
}
}
}