-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
1035 lines (901 loc) · 26.4 KB
/
api.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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package api
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/kubeshark/gopacket"
corev1 "k8s.io/api/core/v1"
)
type Protocol struct {
Name string `json:"name"`
Version string `json:"version"`
Abbreviation string `json:"abbr"`
LongName string `json:"longName"`
Macro string `json:"macro"`
BackgroundColor string `json:"backgroundColor"`
ForegroundColor string `json:"foregroundColor"`
FontSize int8 `json:"fontSize"`
ReferenceLink string `json:"referenceLink"`
Ports []string `json:"ports"`
Layer4 string `json:"layer4"`
Layer3 string `json:"layer3"`
Priority uint8 `json:"priority"`
}
type ResolutionMechanism string
const (
ResolutionMechanismNone ResolutionMechanism = "none"
ResolutionMechanismIp ResolutionMechanism = "ip"
ResolutionMechanismIpAndPort ResolutionMechanism = "ip-and-port"
ResolutionMechanismDns ResolutionMechanism = "dns"
ResolutionMechanismHttpHeader ResolutionMechanism = "http-header"
ResolutionMechanismCgroupID ResolutionMechanism = "cgroup-id"
ResolutionMechanismContainerID ResolutionMechanism = "container-id"
ResolutionMechanismSyscall ResolutionMechanism = "syscall"
ResolutionMechanismSidecarProxy ResolutionMechanism = "sidecar-proxy"
)
type Resolution struct {
IP string `json:"ip"`
Port string `json:"port"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Pod *corev1.Pod `json:"pod"`
EndpointSlice *corev1.Endpoints `json:"endpointSlice"`
Service *corev1.Service `json:"service"`
CgroupID uint `json:"cgroupId"`
ContainerID string `json:"containerId"`
SocketID uint `json:"socketId"`
ProcessID int `json:"processId"`
ParentProcessID int `json:"parentProcessId"`
HostProcessID int `json:"hostProcessId"`
HostParentProcessID int `json:"hostParentProcessId"`
ProcessName string `json:"processName"`
ProcessPath string `json:"processPath"`
ResolutionMechanism ResolutionMechanism `json:"resolutionMechanism"`
sync.Mutex
}
func (resolution *Resolution) GetIP() string {
resolution.Lock()
defer resolution.Unlock()
return resolution.IP
}
func (resolution *Resolution) GetPort() string {
resolution.Lock()
defer resolution.Unlock()
return resolution.Port
}
func (resolution *Resolution) GetName() string {
resolution.Lock()
defer resolution.Unlock()
return resolution.Name
}
func (resolution *Resolution) GetNamespace() string {
resolution.Lock()
defer resolution.Unlock()
return resolution.Namespace
}
func (resolution *Resolution) GetPod() *corev1.Pod {
resolution.Lock()
defer resolution.Unlock()
return resolution.Pod
}
func (resolution *Resolution) GetEndpointSlice() *corev1.Endpoints {
resolution.Lock()
defer resolution.Unlock()
return resolution.EndpointSlice
}
func (resolution *Resolution) GetService() *corev1.Service {
resolution.Lock()
defer resolution.Unlock()
return resolution.Service
}
func (resolution *Resolution) GetCgroupID() uint {
resolution.Lock()
defer resolution.Unlock()
return resolution.CgroupID
}
func (resolution *Resolution) GetContainerID() string {
resolution.Lock()
defer resolution.Unlock()
return resolution.ContainerID
}
func (resolution *Resolution) GetSocketID() uint {
resolution.Lock()
defer resolution.Unlock()
return resolution.SocketID
}
func (resolution *Resolution) GetProcessID() int {
resolution.Lock()
defer resolution.Unlock()
return resolution.ProcessID
}
func (resolution *Resolution) GetParentProcessID() int {
resolution.Lock()
defer resolution.Unlock()
return resolution.ParentProcessID
}
func (resolution *Resolution) GetHostProcessID() int {
resolution.Lock()
defer resolution.Unlock()
return resolution.HostProcessID
}
func (resolution *Resolution) GetHostParentProcessID() int {
resolution.Lock()
defer resolution.Unlock()
return resolution.HostParentProcessID
}
func (resolution *Resolution) GetProcessName() string {
resolution.Lock()
defer resolution.Unlock()
return resolution.ProcessName
}
func (resolution *Resolution) GetProcessPath() string {
resolution.Lock()
defer resolution.Unlock()
return resolution.ProcessPath
}
func (resolution *Resolution) GetResolutionMechanism() ResolutionMechanism {
resolution.Lock()
defer resolution.Unlock()
return resolution.ResolutionMechanism
}
func (resolution *Resolution) SetIP(IP string) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.IP = IP
return resolution
}
func (resolution *Resolution) SetPort(Port string) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.Port = Port
return resolution
}
func (resolution *Resolution) SetName(Name string) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.Name = Name
return resolution
}
func (resolution *Resolution) SetNamespace(Namespace string) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.Namespace = Namespace
return resolution
}
func (resolution *Resolution) SetPod(Pod *corev1.Pod) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.Pod = Pod
return resolution
}
func (resolution *Resolution) SetEndpointSlice(EndpointSlice *corev1.Endpoints) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.EndpointSlice = EndpointSlice
return resolution
}
func (resolution *Resolution) SetService(Service *corev1.Service) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.Service = Service
return resolution
}
func (resolution *Resolution) SetCgroupID(CgroupID uint) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.CgroupID = CgroupID
return resolution
}
func (resolution *Resolution) SetContainerID(ContainerID string) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.ContainerID = ContainerID
return resolution
}
func (resolution *Resolution) SetSocketID(SocketID uint) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.SocketID = SocketID
return resolution
}
func (resolution *Resolution) SetProcessID(ProcessID int) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.ProcessID = ProcessID
return resolution
}
func (resolution *Resolution) SetParentProcessID(ParentProcessID int) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.ParentProcessID = ParentProcessID
return resolution
}
func (resolution *Resolution) SetHostProcessID(HostProcessID int) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.HostProcessID = HostProcessID
return resolution
}
func (resolution *Resolution) SetHostParentProcessID(HostParentProcessID int) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.HostParentProcessID = HostParentProcessID
return resolution
}
func (resolution *Resolution) SetProcessName(ProcessName string) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.ProcessName = ProcessName
return resolution
}
func (resolution *Resolution) SetProcessPath(ProcessPath string) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.ProcessPath = ProcessPath
return resolution
}
func (resolution *Resolution) SetResolutionMechanism(ResolutionMechanism ResolutionMechanism) *Resolution {
resolution.Lock()
defer resolution.Unlock()
resolution.ResolutionMechanism = ResolutionMechanism
return resolution
}
func (r *Resolution) New() *Resolution {
r.Lock()
newR := &Resolution{
IP: r.IP,
Port: r.Port,
Name: r.Name,
Namespace: r.Namespace,
Pod: r.Pod,
EndpointSlice: r.EndpointSlice,
Service: r.Service,
ResolutionMechanism: r.ResolutionMechanism,
}
r.Unlock()
return newR
}
type ObjectMeta struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}
type SpecSummary struct {
NodeName string `json:"nodeName"`
}
type StatusSummary struct {
HostIP string `json:"hostIp"`
}
type PodSummary struct {
Metadata *ObjectMeta `json:"metadata"`
Spec *SpecSummary `json:"spec"`
Status *StatusSummary `json:"status"`
}
type Object struct {
Metadata *ObjectMeta `json:"metadata"`
}
type ResolutionSummary struct {
IP string `json:"ip"`
Port string `json:"port"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Pod *PodSummary `json:"pod"`
EndpointSlice *Object `json:"endpointSlice"`
Service *Object `json:"service"`
CgroupID uint `json:"cgroupId"`
ContainerID string `json:"containerId"`
SocketID uint `json:"socketId"`
ProcessID int `json:"processId"`
ParentProcessID int `json:"parentProcessId"`
HostProcessID int `json:"hostProcessId"`
HostParentProcessID int `json:"hostParentProcessId"`
ProcessName string `json:"processName"`
ResolutionMechanism ResolutionMechanism `json:"resolutionMechanism"`
}
type Extension struct {
Protocol *Protocol
Path string
Dissector Dissector
}
type VLAN struct {
ID uint16 `json:"id"`
Dot1Q bool `json:"dot1q"`
}
type Proxy struct {
Name string `json:"name"`
Pid string `json:"pid"`
}
type Capture struct {
Backend string `json:"backend"`
Source string `json:"source"`
Proxy *Proxy `json:"proxy"`
VLAN *VLAN `json:"vlan"`
}
type ConnectionInfo struct {
ClientIP string
ClientPort string
ClientCgroupID uint64
ServerIP string
ServerPort string
ServerCgroupID uint64
IsKubeProbe bool
ContainerId string
sync.Mutex
}
func (connectioninfo *ConnectionInfo) GetClientIP() string {
connectioninfo.Lock()
defer connectioninfo.Unlock()
return connectioninfo.ClientIP
}
func (connectioninfo *ConnectionInfo) GetClientPort() string {
connectioninfo.Lock()
defer connectioninfo.Unlock()
return connectioninfo.ClientPort
}
func (connectioninfo *ConnectionInfo) GetClientCgroupID() uint64 {
connectioninfo.Lock()
defer connectioninfo.Unlock()
return connectioninfo.ClientCgroupID
}
func (connectioninfo *ConnectionInfo) GetServerIP() string {
connectioninfo.Lock()
defer connectioninfo.Unlock()
return connectioninfo.ServerIP
}
func (connectioninfo *ConnectionInfo) GetServerPort() string {
connectioninfo.Lock()
defer connectioninfo.Unlock()
return connectioninfo.ServerPort
}
func (connectioninfo *ConnectionInfo) GetServerCgroupID() uint64 {
connectioninfo.Lock()
defer connectioninfo.Unlock()
return connectioninfo.ServerCgroupID
}
func (connectioninfo *ConnectionInfo) GetIsKubeProbe() bool {
connectioninfo.Lock()
defer connectioninfo.Unlock()
return connectioninfo.IsKubeProbe
}
func (connectioninfo *ConnectionInfo) GetContainerId() string {
connectioninfo.Lock()
defer connectioninfo.Unlock()
return connectioninfo.ContainerId
}
func (connectioninfo *ConnectionInfo) SetClientIP(ClientIP string) *ConnectionInfo {
connectioninfo.Lock()
defer connectioninfo.Unlock()
connectioninfo.ClientIP = ClientIP
return connectioninfo
}
func (connectioninfo *ConnectionInfo) SetClientPort(ClientPort string) *ConnectionInfo {
connectioninfo.Lock()
defer connectioninfo.Unlock()
connectioninfo.ClientPort = ClientPort
return connectioninfo
}
func (connectioninfo *ConnectionInfo) SetClientCgroupID(ClientCgroupID uint64) *ConnectionInfo {
connectioninfo.Lock()
defer connectioninfo.Unlock()
connectioninfo.ClientCgroupID = ClientCgroupID
return connectioninfo
}
func (connectioninfo *ConnectionInfo) SetServerIP(ServerIP string) *ConnectionInfo {
connectioninfo.Lock()
defer connectioninfo.Unlock()
connectioninfo.ServerIP = ServerIP
return connectioninfo
}
func (connectioninfo *ConnectionInfo) SetServerPort(ServerPort string) *ConnectionInfo {
connectioninfo.Lock()
defer connectioninfo.Unlock()
connectioninfo.ServerPort = ServerPort
return connectioninfo
}
func (connectioninfo *ConnectionInfo) SetServerCgroupID(ServerCgroupID uint64) *ConnectionInfo {
connectioninfo.Lock()
defer connectioninfo.Unlock()
connectioninfo.ServerCgroupID = ServerCgroupID
return connectioninfo
}
func (connectioninfo *ConnectionInfo) SetIsKubeProbe(IsKubeProbe bool) *ConnectionInfo {
connectioninfo.Lock()
defer connectioninfo.Unlock()
connectioninfo.IsKubeProbe = IsKubeProbe
return connectioninfo
}
func (connectioninfo *ConnectionInfo) SetContainerId(ContainerId string) *ConnectionInfo {
connectioninfo.Lock()
defer connectioninfo.Unlock()
connectioninfo.ContainerId = ContainerId
return connectioninfo
}
type TcpID struct {
SrcIP string
DstIP string
SrcPort string
DstPort string
SrcCgroupID uint64
DstCgroupID uint64
sync.Mutex
}
func (tcpid *TcpID) GetSrcIP() string {
tcpid.Lock()
defer tcpid.Unlock()
return tcpid.SrcIP
}
func (tcpid *TcpID) GetDstIP() string {
tcpid.Lock()
defer tcpid.Unlock()
return tcpid.DstIP
}
func (tcpid *TcpID) GetSrcPort() string {
tcpid.Lock()
defer tcpid.Unlock()
return tcpid.SrcPort
}
func (tcpid *TcpID) GetDstPort() string {
tcpid.Lock()
defer tcpid.Unlock()
return tcpid.DstPort
}
func (tcpid *TcpID) GetSrcCgroupID() uint64 {
tcpid.Lock()
defer tcpid.Unlock()
return tcpid.SrcCgroupID
}
func (tcpid *TcpID) GetDstCgroupID() uint64 {
tcpid.Lock()
defer tcpid.Unlock()
return tcpid.DstCgroupID
}
func (tcpid *TcpID) SetSrcIP(SrcIP string) *TcpID {
tcpid.Lock()
defer tcpid.Unlock()
tcpid.SrcIP = SrcIP
return tcpid
}
func (tcpid *TcpID) SetDstIP(DstIP string) *TcpID {
tcpid.Lock()
defer tcpid.Unlock()
tcpid.DstIP = DstIP
return tcpid
}
func (tcpid *TcpID) SetSrcPort(SrcPort string) *TcpID {
tcpid.Lock()
defer tcpid.Unlock()
tcpid.SrcPort = SrcPort
return tcpid
}
func (tcpid *TcpID) SetDstPort(DstPort string) *TcpID {
tcpid.Lock()
defer tcpid.Unlock()
tcpid.DstPort = DstPort
return tcpid
}
func (tcpid *TcpID) SetSrcCgroupID(SrcCgroupID uint64) *TcpID {
tcpid.Lock()
defer tcpid.Unlock()
tcpid.SrcCgroupID = SrcCgroupID
return tcpid
}
func (tcpid *TcpID) SetDstCgroupID(DstCgroupID uint64) *TcpID {
tcpid.Lock()
defer tcpid.Unlock()
tcpid.DstCgroupID = DstCgroupID
return tcpid
}
func (tcpid *TcpID) NewConnectionInfo() *ConnectionInfo {
tcpid.Lock()
connectioninfo := &ConnectionInfo{
ClientIP: tcpid.SrcIP,
ClientPort: tcpid.SrcPort,
ClientCgroupID: tcpid.SrcCgroupID,
ServerIP: tcpid.DstIP,
ServerPort: tcpid.DstPort,
ServerCgroupID: tcpid.DstCgroupID,
}
tcpid.Unlock()
return connectioninfo
}
func (tcpid *TcpID) NewConnectionInfoFlipped() *ConnectionInfo {
tcpid.Lock()
connectioninfo := &ConnectionInfo{
ClientIP: tcpid.DstIP,
ClientPort: tcpid.DstPort,
ClientCgroupID: tcpid.DstCgroupID,
ServerIP: tcpid.SrcIP,
ServerPort: tcpid.SrcPort,
ServerCgroupID: tcpid.SrcCgroupID,
}
tcpid.Unlock()
return connectioninfo
}
type CounterPair struct {
Request uint
Response uint
sync.Mutex
}
type GenericMessage struct {
IsRequest bool
CaptureTime time.Time
CaptureSize int
Payload interface{}
IsKubeProbe bool
}
type RequestResponsePair struct {
Request *GenericMessage
Response *GenericMessage
}
// {Stream}-{Index} uniquely identifies an item
// `Protocol` is modified in later stages of data propagation. Therefore, it's not a pointer.
type OutputChannelItem struct {
Index int64
Stream string
Protocol Protocol
Timestamp int64
ConnectionInfo *ConnectionInfo
Pair *RequestResponsePair
Data *GenericMessage
Tls bool
Error *Error
Capture *Capture
Checksums []string
}
type ReadProgress struct {
readBytes int
lastCheckpoint int
sync.Mutex
}
func (p *ReadProgress) Feed(n int) {
p.Lock()
p.readBytes += n
p.Unlock()
}
func (p *ReadProgress) Current() (n int) {
p.Lock()
current := p.readBytes - p.lastCheckpoint
p.lastCheckpoint = p.readBytes
p.Unlock()
return current
}
func (p *ReadProgress) Reset() {
p.Lock()
p.readBytes = 0
p.lastCheckpoint = 0
p.Unlock()
}
type Dissector interface {
Register(*Extension)
Dissect(b *bufio.Reader, reader TcpReader) (err error)
Analyze(item *OutputChannelItem, resolvedSource *Resolution, resolvedDestination *Resolution) *Entry
Summarize(entry *Entry) *BaseEntry
Represent(request interface{}, response interface{}, event *Event, data interface{}) (representation *Representation)
Macros() map[string]string
NewResponseRequestMatcher() RequestResponseMatcher
Typed(data []byte, requestRef string, responseRef string, eventRef string, dataRef string) (request interface{}, response interface{}, event *Event, dataValue interface{}, err error)
}
type RequestResponseMatcher interface {
GetMap() *sync.Map
SetMaxTry(value int)
GetLastRequest() *GenericMessage
GetLastResponse() *GenericMessage
}
type Emitting struct {
AppStats *AppStats
Stream TcpStream
OutputChannel chan *OutputChannelItem
}
type Emitter interface {
Emit(item *OutputChannelItem)
}
func (e *Emitting) Emit(item *OutputChannelItem) {
e.AppStats.IncMatchedPairs()
item.Stream = e.Stream.GetPcapId()
item.Index = e.Stream.GetIndex()
item.Tls = e.Stream.GetTls()
e.Stream.IncrementItemCount()
e.OutputChannel <- item
}
type Node struct {
IP string `json:"ip"`
Name string `json:"name"`
}
type ErrorType int
const (
DissectionError ErrorType = iota
ConnectionError
TimeoutError
PairNotFoundError
)
type Error struct {
Type ErrorType `json:"type"`
Message string `json:"msg"`
}
func (e *ErrorType) MarshalJSON() ([]byte, error) {
var val string
switch *e {
case DissectionError:
val = "dissection"
case ConnectionError:
val = "connection"
case TimeoutError:
val = "timeout"
case PairNotFoundError:
val = "pair-not-found"
default:
return []byte{}, errors.New("the error type is unknown")
}
return json.Marshal(val)
}
func (e *ErrorType) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
switch s {
case "dissection":
*e = DissectionError
case "connection":
*e = ConnectionError
case "timeout":
*e = TimeoutError
case "pair-not-found":
*e = PairNotFoundError
default:
return errors.New("the error type is unknown")
}
return nil
}
type Event struct {
Source string `json:"source"`
Type string `json:"type"`
Data interface{} `json:"data"`
}
// {Worker}/{Stream}-{Index} uniquely identifies an item
type Entry struct {
Id string `json:"id"`
Index int64 `json:"index"`
Stream string `json:"stream"`
Worker string `json:"worker"`
Node *Node `json:"node"`
Protocol Protocol `json:"protocol"`
Tls bool `json:"tls"`
Source *Resolution `json:"src"`
Destination *Resolution `json:"dst"`
Timestamp int64 `json:"timestamp"`
StartTime time.Time `json:"startTime"`
Request interface{} `json:"request"`
Response interface{} `json:"response"`
RequestRef string `json:"requestRef"`
ResponseRef string `json:"responseRef"`
RequestSize int `json:"requestSize"`
ResponseSize int `json:"responseSize"`
ElapsedTime int64 `json:"elapsedTime"`
Passed bool `json:"passed"`
Failed bool `json:"failed"`
Error *Error `json:"error"`
EntryFile string `json:"entryFile"`
Record string `json:"record"`
Event *Event `json:"event"`
EventRef string `json:"eventRef"`
Base *BaseEntry `json:"base"`
Capture *Capture `json:"capture"`
Checksums []string `json:"checksums"`
Duplicate string `json:"duplicate"`
Data interface{} `json:"data"`
DataRef string `json:"dataRef"`
Size int `json:"size"`
}
func (e *Entry) BuildId() {
e.Id = fmt.Sprintf("%s/%s-%d", e.Worker, e.Stream, e.Index)
}
func (e *Entry) BuildFilenames() {
e.EntryFile = GetEntryFile(e.Stream, e.Index)
}
func (e *Entry) SourceSummary() *ResolutionSummary {
if e.Source == nil {
return &ResolutionSummary{}
}
s := &ResolutionSummary{
IP: e.Source.IP,
Port: e.Source.Port,
Name: e.Source.Name,
Namespace: e.Source.Namespace,
CgroupID: e.Source.CgroupID,
ContainerID: e.Source.ContainerID,
SocketID: e.Source.SocketID,
ProcessID: e.Source.ProcessID,
ParentProcessID: e.Source.ParentProcessID,
HostProcessID: e.Source.HostProcessID,
HostParentProcessID: e.Source.HostParentProcessID,
ProcessName: e.Source.ProcessName,
ResolutionMechanism: e.Source.ResolutionMechanism,
}
if e.Source.Pod != nil {
s.Pod = &PodSummary{
Metadata: &ObjectMeta{
Name: e.Source.Pod.ObjectMeta.Name,
Namespace: e.Source.Pod.ObjectMeta.Namespace,
},
Spec: &SpecSummary{
NodeName: e.Source.Pod.Spec.NodeName,
},
Status: &StatusSummary{
HostIP: e.Source.Pod.Status.HostIP,
},
}
}
if e.Source.EndpointSlice != nil {
s.EndpointSlice = &Object{
Metadata: &ObjectMeta{
Name: e.Source.EndpointSlice.ObjectMeta.Name,
Namespace: e.Source.EndpointSlice.ObjectMeta.Namespace,
},
}
}
if e.Source.Service != nil {
s.Service = &Object{
Metadata: &ObjectMeta{
Name: e.Source.Service.ObjectMeta.Name,
Namespace: e.Source.Service.ObjectMeta.Namespace,
},
}
}
return s
}
func (e *Entry) DestinationSummary() *ResolutionSummary {
if e.Destination == nil {
return &ResolutionSummary{}
}
s := &ResolutionSummary{
IP: e.Destination.IP,
Port: e.Destination.Port,
Name: e.Destination.Name,
Namespace: e.Destination.Namespace,
CgroupID: e.Destination.CgroupID,
ContainerID: e.Destination.ContainerID,
SocketID: e.Destination.SocketID,
ProcessID: e.Destination.ProcessID,
ParentProcessID: e.Destination.ParentProcessID,
HostProcessID: e.Destination.HostProcessID,
HostParentProcessID: e.Source.HostParentProcessID,
ProcessName: e.Destination.ProcessName,
ResolutionMechanism: e.Destination.ResolutionMechanism,
}
if e.Destination.Pod != nil {
s.Pod = &PodSummary{
Metadata: &ObjectMeta{
Name: e.Destination.Pod.ObjectMeta.Name,
Namespace: e.Destination.Pod.ObjectMeta.Namespace,
},
Spec: &SpecSummary{
NodeName: e.Destination.Pod.Spec.NodeName,
},
Status: &StatusSummary{
HostIP: e.Destination.Pod.Status.HostIP,
},
}
}
if e.Destination.EndpointSlice != nil {
s.EndpointSlice = &Object{
Metadata: &ObjectMeta{
Name: e.Destination.EndpointSlice.ObjectMeta.Name,
Namespace: e.Destination.EndpointSlice.ObjectMeta.Namespace,
},
}
}
if e.Destination.Service != nil {
s.Service = &Object{
Metadata: &ObjectMeta{
Name: e.Destination.Service.ObjectMeta.Name,
Namespace: e.Destination.Service.ObjectMeta.Namespace,
},
}
}
return s
}
type Representation struct {
Request []*SectionData `json:"request"`
Response []*SectionData `json:"response"`
Event []*SectionData `json:"event"`
Data []*SectionData `json:"data"`
}
type EntryWrapper struct {
Protocol Protocol `json:"protocol"`
Representation *Representation `json:"representation"`
Data *Entry `json:"data"`
Base *BaseEntry `json:"base"`
}
// {Worker}/{Id} uniquely identifies an item
type BaseEntry struct {
Id string `json:"id"`
Stream string `json:"stream"`
Worker string `json:"worker"`
Protocol Protocol `json:"proto,omitempty"`
Tls bool `json:"tls"`
Summary string `json:"summary,omitempty"`
SummaryQuery string `json:"summaryQuery,omitempty"`
Status int `json:"status"`
StatusQuery string `json:"statusQuery"`
Method string `json:"method,omitempty"`
MethodQuery string `json:"methodQuery,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Source *ResolutionSummary `json:"src"`
Destination *ResolutionSummary `json:"dst"`
RequestSize int `json:"requestSize"`
ResponseSize int `json:"responseSize"`
ElapsedTime int64 `json:"elapsedTime"`
Passed bool `json:"passed"`
Failed bool `json:"failed"`
Error *Error `json:"error"`
Record string `json:"record"`
Event bool `json:"event"`
Capture *Capture `json:"capture"`
Checksums []string `json:"checksums"`
Duplicate string `json:"duplicate"`
Size int `json:"size"`
}
const (
TABLE string = "table"
BODY string = "body"
)
type SectionData struct {
Type string `json:"type"`
Title string `json:"title"`
TableData []*TableData `json:"tableData"`
Encoding string `json:"encoding,omitempty"`
MimeType string `json:"mimeType,omitempty"`
Body string `json:"body"`
Selector string `json:"selector,omitempty"`
}
type TableData struct {
Name string `json:"name"`
Value interface{} `json:"value"`
Selector string `json:"selector"`
}
type TcpReaderDataMsg interface {
GetBytes() []byte
GetTimestamp() time.Time
GetCaptureInfo() gopacket.CaptureInfo
}
type TcpReader interface {
Read(p []byte) (int, error)
GetReqResMatcher() RequestResponseMatcher
GetIsClient() bool