forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomms_test.go
880 lines (695 loc) · 24.8 KB
/
comms_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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/*
Velociraptor - Dig Deeper
Copyright (C) 2019-2024 Rapid7 Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package http_comms
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"www.velocidex.com/golang/velociraptor/config"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
crypto_test "www.velocidex.com/golang/velociraptor/crypto/testing"
"www.velocidex.com/golang/velociraptor/executor"
"www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/velociraptor/services/writeback"
"www.velocidex.com/golang/velociraptor/utils"
"www.velocidex.com/golang/velociraptor/vtesting"
)
var (
counter int
)
type FakeClock struct {
*utils.MockClock
events *[]string
}
func (self *FakeClock) After(d time.Duration) <-chan time.Time {
*self.events = append(*self.events, fmt.Sprintf("%d sleep: %v\n", counter, d))
counter++
return time.After(0)
}
func (self *FakeClock) Sleep(d time.Duration) {
<-self.After(d)
}
type Response struct {
data string
status int
location string
}
type FakeServer struct {
events []string
resp_idx int
responses []*Response
server *httptest.Server
URL string
}
func (self *FakeServer) Close() {
self.server.Close()
}
func (self *FakeServer) Log(fmtstring string, args ...interface{}) {
msg := fmt.Sprintf("%d ", counter)
counter++
msg += fmt.Sprintf(fmtstring, args...)
self.events = append(self.events, msg)
}
func NewFakeServer() *FakeServer {
self := &FakeServer{}
self.server = httptest.NewServer(http.HandlerFunc(
func(rw http.ResponseWriter, req *http.Request) {
self.Log("request: %v", req.URL.String())
// Break out of a runaway condition.
if len(self.events) > 100 {
fmt.Printf("Something went horribly wrong")
utils.Debug(self.events)
os.Exit(-1)
}
encrypted := &bytes.Buffer{}
io.Copy(encrypted, req.Body)
if self.resp_idx >= len(self.responses) {
http.Error(rw, "OK", 200)
self.Log("response: default OK 200")
self.resp_idx = 0
return
}
response := self.responses[self.resp_idx]
if response.location != "" {
rw.Header()["Location"] = []string{response.location}
}
if response.status == 200 {
self.Log("response: %v 200", response.data)
rw.Write([]byte(response.data))
} else {
self.Log("response: %v %d", response.data, response.status)
http.Error(rw, "OK", response.status)
}
self.resp_idx++
}))
self.URL = self.server.URL + "/"
return self
}
type CommsTestSuite struct {
suite.Suite
frontend1, frontend2 *FakeServer
config_obj *config_proto.Config
empty_response []byte
writeback_path string
}
func (self *CommsTestSuite) SetupTest() {
counter = 0
self.frontend1 = NewFakeServer()
self.frontend2 = NewFakeServer()
config_obj, err := new(config.Loader).WithFileLoader(
"../http_comms/test_data/server.config.yaml").
WithRequiredClient().WithWriteback().LoadAndValidate()
require.NoError(self.T(), err)
require.NoError(self.T(), config.ValidateClientConfig(config_obj))
self.config_obj = config_obj
self.config_obj.Client.LocalBuffer.DiskSize = 0
cm := &crypto_test.NullCryptoManager{}
self.empty_response, _ = cm.EncryptMessageList(
&crypto_proto.MessageList{},
self.config_obj.Client.Nonce,
"C.1234")
// Disable randomness for the test.
mu.Lock()
Rand = func(int) int { return 0 }
mu.Unlock()
// Initialize the writeback
self.writeback_path = getTempFile(self.T())
self.config_obj.Client.WritebackLinux = self.writeback_path
self.config_obj.Client.WritebackWindows = self.writeback_path
self.config_obj.Client.WritebackDarwin = self.writeback_path
writeback_service := writeback.GetWritebackService()
writeback_service.LoadWriteback(self.config_obj)
}
func (self *CommsTestSuite) TearDownTest() {
self.frontend1.Close()
self.frontend2.Close()
os.Remove(self.writeback_path)
}
// Check that unexpected closing of the executor calls the abort
// function.
func (self *CommsTestSuite) TestAbort() {
var mu sync.Mutex
func_called := false
on_error := func() {
mu.Lock()
func_called = true
mu.Unlock()
}
urls := []string{self.frontend1.URL}
// Not a real executor but we can emulate closing channels.
exec := executor.NewClientExecutorForTests(self.config_obj)
wg := &sync.WaitGroup{}
defer wg.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
crypto_manager := &crypto_test.NullCryptoManager{}
communicator, err := NewHTTPCommunicator(ctx, self.config_obj, crypto_manager,
exec, urls, on_error, utils.RealClock{})
assert.NoError(self.T(), err)
// Start a communicator feeding data to the executor.
wg.Add(1)
go func() {
defer wg.Done()
communicator.Run(ctx, wg)
}()
// Emulate the case of the executor exiting early - this
// should never happen in practice but might happen due to a
// bug or panic(). If this is allowed to go unchecked we might
// get into a state where the client does not have a working
// executor and can not be reached. The only sensible thing to
// do in this case is to abort which happens via the provided
// on_error callback.
close(exec.Outbound)
vtesting.WaitUntil(2*time.Second, self.T(), func() bool {
mu.Lock()
defer mu.Unlock()
return func_called
})
assert.Equal(self.T(), func_called, true)
}
func (self *CommsTestSuite) TestEnrollment() {
urls := []string{self.frontend1.URL}
mock_clock := utils.NewMockClock(time.Unix(100, 0))
cancel := utils.MockTime(mock_clock)
defer cancel()
clock := &FakeClock{
MockClock: mock_clock,
events: &self.frontend1.events}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
crypto_manager := &crypto_test.NullCryptoManager{}
communicator, err := NewHTTPCommunicator(ctx, self.config_obj, crypto_manager,
executor.NewTestExecutor(), urls, nil, clock)
assert.NoError(self.T(), err)
self.frontend1.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: "", status: 406},
{data: "", status: 406},
{data: "", status: 406},
}
communicator.receiver.sendMessageList(
context.Background(), nil, !URGENT,
crypto_proto.PackedMessageList_ZCOMPRESSION)
json.Dump(self.frontend1.events)
checkResponses(self.T(), self.frontend1.events, []string{
// First request looks for server.pem but fails on frontend1
"0 request: /server.pem",
"1 response: -----BEG",
"2 request: /reader",
// A 406 should not trigger resend - we just schedule
// a new CSR to go in the next poll.
"3 response: 406",
})
}
func (self *CommsTestSuite) TestServerError() {
urls := []string{self.frontend1.URL}
mock_clock := utils.NewMockClock(time.Unix(100, 0))
cancel := utils.MockTime(mock_clock)
defer cancel()
clock := &FakeClock{
MockClock: mock_clock,
events: &self.frontend1.events}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
crypto_manager := &crypto_test.NullCryptoManager{}
communicator, err := NewHTTPCommunicator(ctx, self.config_obj, crypto_manager,
executor.NewTestExecutor(), urls, nil, clock)
assert.NoError(self.T(), err)
self.frontend1.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: "", status: 500},
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
}
communicator.receiver.sendMessageList(
context.Background(), nil, !URGENT,
crypto_proto.PackedMessageList_ZCOMPRESSION)
checkResponses(self.T(), self.frontend1.events, []string{
// First request looks for server.pem
"request: /server.pem",
"response: -----BEGIN CERTIFICATE-----",
// We will fail the next request with a 500
"request: /reader",
"response: 500",
// Client will sleep to back off and try to rekey
"sleep: 10",
"sleep: 10",
"request: /server.pem",
"response: -----BEGIN CERTIFICATE-----",
// This one worked and should be successful.
"request: /reader",
"response: \n\vx\x01\x01\x00\x00\xff\xff\x00\x00\x00\x01 200",
})
}
// Client configured with two frontends. Frontend1 is down returning
// 500, Frontend2 is down too.
func (self *CommsTestSuite) TestMultiFrontends() {
urls := []string{self.frontend1.URL, self.frontend2.URL}
mock_clock := utils.NewMockClock(time.Unix(100, 0))
cancel := utils.MockTime(mock_clock)
defer cancel()
clock := &FakeClock{
MockClock: mock_clock,
events: &self.frontend1.events}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
crypto_manager := &crypto_test.NullCryptoManager{}
communicator, err := NewHTTPCommunicator(ctx, self.config_obj, crypto_manager,
executor.NewTestExecutor(), urls, nil, clock)
assert.NoError(self.T(), err)
// Frontend1 will return 500 all the time.
self.frontend1.responses = []*Response{
{data: "", status: 500},
{data: "", status: 500},
{data: "", status: 500},
}
// Frontend2 errors at first but then comes back online
self.frontend2.responses = []*Response{
{data: "", status: 500},
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
}
communicator.receiver.sendMessageList(context.Background(), nil,
!URGENT, crypto_proto.PackedMessageList_ZCOMPRESSION)
// Message ordering is important
checkResponses(self.T(), self.frontend1.events, []string{
// First request looks for server.pem but fails on frontend1
"0 request: /server.pem",
"1 response: 500",
// After trying frontend2 immediately, we back off all frontends
"4 sleep: 10",
// We try frontend1 again but again it fails
"5 request: /server.pem",
"6 response: 500",
})
checkResponses(self.T(), self.frontend2.events, []string{
// First request on FE 2 fails.
"2 request: /server.pem",
"3 response: 500",
// Second round - try FE 2 again - works this time.
"7 request: /server.pem",
"8 response: -----BEGIN CERTIFICATE-----",
// This one worked and should be successful.
"9 request: /reader",
"10 response: \n\vx\x01\x01\x00\x00\xff\xff\x00\x00\x00\x01 200",
})
}
// Client configured with two frontends. Both keep failing.
func (self *CommsTestSuite) TestMultiFrontendsAllIsBorked() {
urls := []string{self.frontend1.URL, self.frontend2.URL}
mock_clock := utils.NewMockClock(time.Unix(100, 0))
cancel := utils.MockTime(mock_clock)
defer cancel()
clock := &FakeClock{
MockClock: mock_clock,
events: &self.frontend1.events}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
crypto_manager := &crypto_test.NullCryptoManager{}
communicator, err := NewHTTPCommunicator(ctx, self.config_obj, crypto_manager,
executor.NewTestExecutor(), urls, nil, clock)
assert.NoError(self.T(), err)
// Frontend1 will return 500 all the time.
self.frontend1.responses = []*Response{
{data: "", status: 500},
{data: "", status: 500},
{data: "", status: 500},
}
// Frontend2 errors at first but then comes back online
self.frontend2.responses = []*Response{
{data: "", status: 500},
{data: "", status: 500},
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
}
communicator.receiver.sendMessageList(context.Background(), nil,
!URGENT, crypto_proto.PackedMessageList_ZCOMPRESSION)
//utils.Debug(self.frontend1.events)
//utils.Debug(self.frontend2.events)
checkResponses(self.T(), self.frontend1.events, []string{
// First request looks for server.pem but fails on frontend1
"0 request: /server.pem",
"1 response: 500",
// Must sleep now as both FE are down.
"4 sleep: 10",
// Try FE1 again
"5 request: /server.pem",
"6 response: 500",
// Must sleep again
"9 sleep: 10",
"10 request: /server.pem",
"11 response: 500",
})
checkResponses(self.T(), self.frontend2.events, []string{
// Immediately switch to FE2 but it is down as well.
"2 request: /server.pem",
"3 response: 500",
// Immediately switch to FE2 - no sleep
"7 request: /server.pem",
"8 response: 500",
"12 request: /server.pem",
"13 response: -----BEGIN CERTIFICATE-----\n",
// This one worked and should be successful.
"14 request: /reader",
"15 response: \n\vx\x01\x01\x00\x00\xff\xff\x00\x00\x00\x01 200",
})
}
// With 2 FE configured if FE 1 fails intermittantly (perhaps due to
// load), client should back off and try FE1 one more time before
// switching to FE2
func (self *CommsTestSuite) TestMultiFrontendsIntermittantFailure() {
urls := []string{self.frontend1.URL, self.frontend2.URL}
mock_clock := utils.NewMockClock(time.Unix(100, 0))
cancel := utils.MockTime(mock_clock)
defer cancel()
clock := &FakeClock{
MockClock: mock_clock,
events: &self.frontend1.events}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
crypto_manager := &crypto_test.NullCryptoManager{}
communicator, err := NewHTTPCommunicator(ctx, self.config_obj, crypto_manager,
executor.NewTestExecutor(), urls, nil, clock)
assert.NoError(self.T(), err)
// FE1 is not completely off just loaded - so initial
// server.pem would work but processing is not possible.
self.frontend1.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
// Emit a single failure.
{data: "", status: 500},
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
}
self.frontend2.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
}
communicator.receiver.sendMessageList(context.Background(), nil,
!URGENT, crypto_proto.PackedMessageList_ZCOMPRESSION)
//utils.Debug(self.frontend1.events)
//utils.Debug(self.frontend2.events)
// Message ordering is important
checkResponses(self.T(), self.frontend1.events, []string{
// First request looks for server.pem is ok.
"0 request: /server.pem",
"1 response: -----BEGIN CERTIFICATE-",
// Now client tries to connect for real.
"2 request: /reader",
"3 response: 500",
"4 sleep: 10",
})
checkResponses(self.T(), self.frontend2.events, []string{
"5 request: /server.pem",
"6 response: -----BEGIN CERTIFICAT",
// This time we get through.
"7 request: /reader",
"8 response: \n\vx\x01\x01\x00\x00\xff\xff\x00\x00\x00\x01 200",
})
}
// With 2 FE configured if FE 1 fails we switch to FE2 and when that
// fails we switch back to FE1.
func (self *CommsTestSuite) TestMultiFrontendsHeavyFailure() {
urls := []string{self.frontend1.URL, self.frontend2.URL}
mock_clock := utils.NewMockClock(time.Unix(100, 0))
cancel := utils.MockTime(mock_clock)
defer cancel()
clock := &FakeClock{
MockClock: mock_clock,
events: &self.frontend1.events}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
crypto_manager := &crypto_test.NullCryptoManager{}
communicator, err := NewHTTPCommunicator(ctx, self.config_obj, crypto_manager,
executor.NewTestExecutor(), urls, nil, clock)
assert.NoError(self.T(), err)
// FE1 is not completely off just loaded - so initial
// server.pem would work but processing is not possible.
self.frontend1.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
// Emit a single failure.
{data: "", status: 500},
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
}
self.frontend2.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: "", status: 500},
}
communicator.receiver.sendMessageList(context.Background(), nil,
!URGENT, crypto_proto.PackedMessageList_ZCOMPRESSION)
//utils.Debug(self.frontend1.events)
//utils.Debug(self.frontend2.events)
// Message ordering is important
checkResponses(self.T(), self.frontend1.events, []string{
// First request looks for server.pem is ok.
"0 request: /server.pem",
"1 response: -----BEGIN CERTIFICATE-",
// Now client tries to connect for real - failed and
// switch immediately to FE2
"2 request: /reader",
"3 response: 500",
"4 sleep: 10",
"9 sleep: 10",
"10 sleep: 10",
// Sleep after switching back from FE2
"11 request: /server.pem",
"12 response: -----BEGIN CERTIFICATE-",
"13 request: /reader",
"14 response: \n\vx\x01\x01\x00\x00\xff\xff\x00\x00\x00\x01 200",
})
checkResponses(self.T(), self.frontend2.events, []string{
// Rekey FE2
"5 request: /server.pem",
// Still failing will now switch to FE2
"6 response: -----BEGIN CERTIFICAT",
// ERROR - switch back but this time we sleep.
"7 request: /reader",
"8 response: 500",
})
}
// With 2 FE configured. FE1 redirects to FE2
func (self *CommsTestSuite) TestMultiFrontendRedirect() {
// FE2 is not known to the client in advance.
urls := []string{self.frontend1.URL}
mock_clock := utils.NewMockClock(time.Unix(100, 0))
cancel := utils.MockTime(mock_clock)
defer cancel()
clock := &FakeClock{
MockClock: mock_clock,
events: &self.frontend1.events}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
crypto_manager := &crypto_test.NullCryptoManager{}
communicator, err := NewHTTPCommunicator(ctx, self.config_obj, crypto_manager,
executor.NewTestExecutor(), urls, nil, clock)
assert.NoError(self.T(), err)
// FE1 is not completely off just loaded - so initial
// server.pem would work but processing is not possible.
self.frontend1.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
// Send the client to FE2
{data: "", status: 301, location: self.frontend2.URL},
}
self.frontend2.responses = []*Response{
// Client will rekey to FE2 and this request will work.
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
{data: string(self.empty_response), status: 200},
}
// Request 2 packets.
communicator.receiver.sendMessageList(context.Background(), nil,
!URGENT, crypto_proto.PackedMessageList_ZCOMPRESSION)
communicator.receiver.sendMessageList(context.Background(), nil,
!URGENT, crypto_proto.PackedMessageList_ZCOMPRESSION)
//utils.Debug(self.frontend1.events)
//utils.Debug(self.frontend2.events)
// Message ordering is important
checkResponses(self.T(), self.frontend1.events, []string{
// First request looks for server.pem is ok.
"0 request: /server.pem",
"1 response: -----BEGIN CERTIFICATE-",
// Now client tries to connect for real.
"2 request: /reader",
"3 response: 301",
})
checkResponses(self.T(), self.frontend2.events, []string{
// Rekey this FE
"4 request: /server.pem",
"5 response: -----BEGIN CERTIFIC",
// This FE is up.
"6 request: /reader?r=1",
"7 response: \n\vx\x01\x01\x00\x00\xff\xff\x00\x00\x00\x01 200",
// Next request goes straight to this FE and includes
// the r=1 parameter to avoid another redirect.
"8 request: /reader?r=1",
"9 response: \n\vx\x01\x01\x00\x00\xff\xff\x00\x00\x00\x01 200",
})
}
// Check that redirects do not cause un-neccesary sleeps.
func (self *CommsTestSuite) TestMultiFrontendRedirectWithErrors() {
mock_clock := utils.NewMockClock(time.Unix(100, 0))
cancel := utils.MockTime(mock_clock)
defer cancel()
urls := []string{self.frontend1.URL}
clock := &FakeClock{
MockClock: mock_clock,
events: &self.frontend1.events}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
crypto_manager := &crypto_test.NullCryptoManager{}
communicator, err := NewHTTPCommunicator(ctx, self.config_obj, crypto_manager,
executor.NewTestExecutor(), urls, nil, clock)
assert.NoError(self.T(), err)
self.frontend1.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
// Send the client to FE2
{data: "", status: 301, location: self.frontend2.URL},
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: "", status: 500},
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
}
self.frontend2.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
{data: "", status: 500},
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
}
communicator.receiver.sendMessageList(context.Background(), nil,
!URGENT, crypto_proto.PackedMessageList_ZCOMPRESSION)
communicator.receiver.sendMessageList(context.Background(), nil,
!URGENT, crypto_proto.PackedMessageList_ZCOMPRESSION)
//utils.Debug(self.frontend1.events)
//utils.Debug(self.frontend2.events)
// Message ordering is important
checkResponses(self.T(), self.frontend1.events, []string{
// First request looks for server.pem is ok.
"0 request: /server.pem",
"1 response: -----BEGIN CERTIFICATE-",
// FE1 -> FE2 redirection
"2 request: /reader",
"3 response: 301",
// Immediately switch to FE1 (no sleep)
"10 sleep: 10",
"11 request: /server.pem",
"12 response: -----BEGIN CERTIFICATE-",
// Try to connect to FE1 but there is an error. NOTE
// r=1 is now removed.
"13 request: /reader",
"14 response: 500",
// Now must sleep since we tried all endpoints and
// they all failed.
"15 sleep: 10",
"16 sleep: 10",
})
checkResponses(self.T(), self.frontend2.events, []string{
// Rekey FE2
"4 request: /server.pem",
"5 response: -----BEGIN CERTIFIC",
// This FE is up.
"6 request: /reader?r=1",
"7 response: \n\vx\x01\x01\x00\x00\xff\xff\x00\x00\x00\x01 200",
// Next request goes straight to this FE and includes
// the r=1 parameter to avoid another redirect. FE2 is
// down now.
"8 request: /reader?r=1",
"9 response: 500",
// After sleep switch to FE2 and succeed.
"17 request: /server.pem",
"18 response: -----BEGIN CERTIFIC",
"19 request: /reader",
"20 response: \n\vx\x01\x01\x00\x00\xff\xff\x00\x00\x00\x01 200",
})
}
// Frontends redirecting to each other.
func (self *CommsTestSuite) TestMultiRedirects() {
urls := []string{self.frontend1.URL}
mock_clock := utils.NewMockClock(time.Unix(100, 0))
cancel := utils.MockTime(mock_clock)
defer cancel()
clock := &FakeClock{
MockClock: mock_clock,
events: &self.frontend1.events}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
crypto_manager := &crypto_test.NullCryptoManager{}
communicator, err := NewHTTPCommunicator(ctx, self.config_obj, crypto_manager,
executor.NewTestExecutor(), urls, nil, clock)
assert.NoError(self.T(), err)
self.frontend1.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
// Send the client to FE2
{data: "", status: 301, location: self.frontend2.URL},
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
}
self.frontend2.responses = []*Response{
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: "", status: 301, location: self.frontend1.URL},
{data: self.config_obj.Frontend.Certificate, status: 200},
{data: string(self.empty_response), status: 200},
}
communicator.receiver.sendMessageList(context.Background(), nil,
!URGENT, crypto_proto.PackedMessageList_ZCOMPRESSION)
//utils.Debug(self.frontend1.events)
//utils.Debug(self.frontend2.events)
// Multiple redirections should not add duplicates to the url list.
assert.Equal(self.T(), communicator.receiver.connector.(*HTTPConnector).urls,
[]string{self.frontend1.URL, self.frontend2.URL})
// Message ordering is important
checkResponses(self.T(), self.frontend1.events, []string{
// First request looks for server.pem is ok.
"0 request: /server.pem",
"1 response: -----BEGIN CERTIFICATE-",
// FE1 -> FE2 redirection
"2 request: /reader",
"3 response: 301",
"8 sleep: 10",
// Immediately switch to FE1 (no sleep)
"9 request: /server.pem",
"10 response: -----BEGIN CERTIFICATE-",
"11 request: /reader?r=1",
"12 response: \n\vx\x01\x01\x00\x00\xff\xff\x00\x00\x00\x01 200",
})
checkResponses(self.T(), self.frontend2.events, []string{
// Rekey FE2
"4 request: /server.pem",
"5 response: -----BEGIN CERTIFIC",
// This FE is up.
"6 request: /reader?r=1",
"7 response: 301",
})
}
func checkResponses(t *testing.T, expected []string, seen []string) {
for idx, seen_line := range seen {
assert.Contains(t, expected[idx], seen_line)
}
assert.Equal(t, len(expected), len(seen))
}
func TestHTTPComms(t *testing.T) {
suite.Run(t, new(CommsTestSuite))
}