forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgsql.go
965 lines (776 loc) · 23.1 KB
/
pgsql.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
package pgsql
import (
"strings"
"time"
"github.com/elastic/libbeat/common"
"github.com/elastic/libbeat/logp"
"github.com/elastic/packetbeat/config"
"github.com/elastic/packetbeat/procs"
"github.com/elastic/packetbeat/protos"
"github.com/elastic/packetbeat/protos/tcp"
)
type PgsqlMessage struct {
start int
end int
isSSLResponse bool
isSSLRequest bool
toExport bool
Ts time.Time
IsRequest bool
Query string
Size uint64
Fields []string
FieldsFormat []byte
Rows [][]string
NumberOfRows int
NumberOfFields int
IsOK bool
IsError bool
ErrorInfo string
ErrorCode string
ErrorSeverity string
Notes []string
Direction uint8
TcpTuple common.TcpTuple
CmdlineTuple *common.CmdlineTuple
}
type PgsqlTransaction struct {
Type string
tuple common.TcpTuple
Src common.Endpoint
Dst common.Endpoint
ResponseTime int32
Ts int64
JsTs time.Time
ts time.Time
Query string
Method string
BytesOut uint64
BytesIn uint64
Notes []string
Pgsql common.MapStr
Request_raw string
Response_raw string
timer *time.Timer
}
type PgsqlStream struct {
tcptuple *common.TcpTuple
data []byte
parseOffset int
parseState int
seenSSLRequest bool
expectSSLResponse bool
message *PgsqlMessage
}
const (
TransactionsHashSize = 2 ^ 16
TransactionTimeout = 10 * 1e9
)
const (
PgsqlStartState = iota
PgsqlGetDataState
)
const (
SSLRequest = iota
StartupMessage
CancelRequest
)
type Pgsql struct {
// config
Ports []int
maxStoreRows int
maxRowLength int
Send_request bool
Send_response bool
transactionsMap map[common.HashableTcpTuple][]*PgsqlTransaction
results chan common.MapStr
// function pointer for mocking
handlePgsql func(pgsql *Pgsql, m *PgsqlMessage, tcp *common.TcpTuple,
dir uint8, raw_msg []byte)
}
func (pgsql *Pgsql) InitDefaults() {
pgsql.maxRowLength = 1024
pgsql.maxStoreRows = 10
pgsql.Send_request = false
pgsql.Send_response = false
}
func (pgsql *Pgsql) setFromConfig(config config.Pgsql) error {
pgsql.Ports = config.Ports
if config.Max_row_length != nil {
pgsql.maxRowLength = *config.Max_row_length
}
if config.Max_rows != nil {
pgsql.maxStoreRows = *config.Max_rows
}
if config.Send_request != nil {
pgsql.Send_request = *config.Send_request
}
if config.Send_response != nil {
pgsql.Send_response = *config.Send_response
}
return nil
}
func (pgsql *Pgsql) GetPorts() []int {
return pgsql.Ports
}
func (pgsql *Pgsql) Init(test_mode bool, results chan common.MapStr) error {
pgsql.InitDefaults()
if !test_mode {
err := pgsql.setFromConfig(config.ConfigSingleton.Protocols.Pgsql)
if err != nil {
return err
}
}
pgsql.transactionsMap = make(map[common.HashableTcpTuple][]*PgsqlTransaction, TransactionsHashSize)
pgsql.handlePgsql = handlePgsql
pgsql.results = results
return nil
}
func (stream *PgsqlStream) PrepareForNewMessage() {
stream.data = stream.data[stream.message.end:]
stream.parseState = PgsqlStartState
stream.parseOffset = 0
stream.message = nil
}
// Parse a list of commands separated by semicolon from the query
func pgsqlQueryParser(query string) []string {
array := strings.Split(query, ";")
queries := []string{}
for _, q := range array {
qt := strings.TrimSpace(q)
if len(qt) > 0 {
queries = append(queries, qt)
}
}
return queries
}
// Extract the method from a SQL query
func getQueryMethod(q string) string {
index := strings.Index(q, " ")
var method string
if index > 0 {
method = strings.ToUpper(q[:index])
} else {
method = strings.ToUpper(q)
}
return method
}
func pgsqlFieldsParser(s *PgsqlStream) {
m := s.message
// read field count (int16)
field_count := int(common.Bytes_Ntohs(s.data[s.parseOffset : s.parseOffset+2]))
s.parseOffset += 2
logp.Debug("pgsqldetailed", "Row Description field count=%d", field_count)
fields := []string{}
fields_format := []byte{}
for i := 0; i < field_count; i++ {
// read field name (null terminated string)
field_name, err := common.ReadString(s.data[s.parseOffset:])
if err != nil {
logp.Err("Fail to read the column field")
}
fields = append(fields, field_name)
m.NumberOfFields += 1
s.parseOffset += len(field_name) + 1
// read Table OID (int32)
s.parseOffset += 4
// read Column Index (int16)
s.parseOffset += 2
// read Type OID (int32)
s.parseOffset += 4
// read column length (int16)
s.parseOffset += 2
// read type modifier (int32)
s.parseOffset += 4
// read format (int16)
format := common.Bytes_Ntohs(s.data[s.parseOffset : s.parseOffset+2])
fields_format = append(fields_format, byte(format))
s.parseOffset += 2
logp.Debug("pgsqldetailed", "Field name=%s, format=%d", field_name, format)
}
m.Fields = fields
m.FieldsFormat = fields_format
if m.NumberOfFields != field_count {
logp.Err("Missing fields from RowDescription. Expected %d. Received %d", field_count, m.NumberOfFields)
}
}
func (pgsql *Pgsql) pgsqlRowsParser(s *PgsqlStream) {
m := s.message
// read field count (int16)
field_count := int(common.Bytes_Ntohs(s.data[s.parseOffset : s.parseOffset+2]))
s.parseOffset += 2
logp.Debug("pgsqldetailed", "DataRow field count=%d", field_count)
row := []string{}
var row_len int
for i := 0; i < field_count; i++ {
// read column length (int32)
column_length := int32(common.Bytes_Ntohl(s.data[s.parseOffset : s.parseOffset+4]))
s.parseOffset += 4
// read column value (byten)
column_value := []byte{}
if m.FieldsFormat[i] == 0 {
// field value in text format
if column_length > 0 {
column_value = s.data[s.parseOffset : s.parseOffset+int(column_length)]
} else if column_length == -1 {
column_value = nil
}
}
if row_len < pgsql.maxRowLength {
if row_len+len(column_value) > pgsql.maxRowLength {
column_value = column_value[:pgsql.maxRowLength-row_len]
}
row = append(row, string(column_value))
row_len += len(column_value)
}
if column_length > 0 {
s.parseOffset += int(column_length)
}
logp.Debug("pgsqldetailed", "Value %s, length=%d", string(column_value), column_length)
}
m.NumberOfRows += 1
if len(m.Rows) < pgsql.maxStoreRows {
m.Rows = append(m.Rows, row)
}
}
func pgsqlErrorParser(s *PgsqlStream) {
m := s.message
for len(s.data[s.parseOffset:]) > 0 {
// read field type(byte1)
field_type := s.data[s.parseOffset]
s.parseOffset += 1
if field_type == 0 {
break
}
// read field value(string)
field_value, err := common.ReadString(s.data[s.parseOffset:])
if err != nil {
logp.Err("Fail to read the column field")
}
s.parseOffset += len(field_value) + 1
if field_type == 'M' {
m.ErrorInfo = field_value
} else if field_type == 'C' {
m.ErrorCode = field_value
} else if field_type == 'S' {
m.ErrorSeverity = field_value
}
}
logp.Debug("pgsqldetailed", "%s %s %s", m.ErrorSeverity, m.ErrorCode, m.ErrorInfo)
}
func isSpecialPgsqlCommand(data []byte) (bool, int) {
if len(data) < 8 {
// 8 bytes required
return false, 0
}
// read length
length := int(common.Bytes_Ntohl(data[0:4]))
// read command identifier
code := int(common.Bytes_Ntohl(data[4:8]))
if length == 16 && code == 80877102 {
// Cancel Request
logp.Debug("pgsqldetailed", "Cancel Request, length=%d", length)
return true, CancelRequest
} else if length == 8 && code == 80877103 {
// SSL Request
logp.Debug("pgsqldetailed", "SSL Request, length=%d", length)
return true, SSLRequest
} else if code == 196608 {
// Startup Message
logp.Debug("pgsqldetailed", "Startup Message, length=%d", length)
return true, StartupMessage
}
return false, 0
}
func (pgsql *Pgsql) pgsqlMessageParser(s *PgsqlStream) (bool, bool) {
m := s.message
for s.parseOffset < len(s.data) {
switch s.parseState {
case PgsqlStartState:
if len(s.data[s.parseOffset:]) < 5 {
logp.Warn("Postgresql Message too short. %X (length=%d). Wait for more.", s.data[s.parseOffset:], len(s.data[s.parseOffset:]))
return true, false
}
is_special, command := isSpecialPgsqlCommand(s.data[s.parseOffset:])
if is_special {
// In case of Commands: StartupMessage, SSLRequest, CancelRequest that don't have
// their type in the first byte
// read length
length := int(common.Bytes_Ntohl(s.data[s.parseOffset : s.parseOffset+4]))
// ignore command
if len(s.data[s.parseOffset:]) >= length {
if command == SSLRequest {
// if SSLRequest is received, expect for one byte reply (S or N)
m.start = s.parseOffset
s.parseOffset += length
m.end = s.parseOffset
m.isSSLRequest = true
m.Size = uint64(m.end - m.start)
return true, true
}
s.parseOffset += length
} else {
// wait for more
logp.Debug("pgsqldetailed", "Wait for more data 1")
return true, false
}
} else {
// In case of Commands that have their type in the first byte
// read type
typ := byte(s.data[s.parseOffset])
if s.expectSSLResponse {
// SSLRequest was received in the other stream
if typ == 'N' || typ == 'S' {
// one byte reply to SSLRequest
logp.Debug("pgsqldetailed", "Reply for SSLRequest %c", typ)
m.start = s.parseOffset
s.parseOffset += 1
m.end = s.parseOffset
m.isSSLResponse = true
m.Size = uint64(m.end - m.start)
return true, true
}
}
// read length
length := int(common.Bytes_Ntohl(s.data[s.parseOffset+1 : s.parseOffset+5]))
if length < 4 {
// length should include the size of itself (int32)
logp.Debug("pgsqldetailed", "Invalid pgsql command length.")
return false, false
}
logp.Debug("pgsqldetailed", "Pgsql type %c, length=%d", typ, length)
if typ == 'Q' {
// SimpleQuery
m.start = s.parseOffset
m.IsRequest = true
if len(s.data[s.parseOffset:]) >= length+1 {
s.parseOffset += 1 //type
s.parseOffset += length
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
m.Query = string(s.data[m.start+5 : m.end-1]) //without string termination
m.toExport = true
logp.Debug("pgsqldetailed", "Simple Query: %s", m.Query)
return true, true
} else {
// wait for more
logp.Debug("pgsqldetailed", "Wait for more data 2")
return true, false
}
} else if typ == 'T' {
// RowDescription
m.start = s.parseOffset
m.IsRequest = false
m.IsOK = true
m.toExport = true
if len(s.data[s.parseOffset:]) >= length+1 {
s.parseOffset += 1 //type
s.parseOffset += 4 //length
pgsqlFieldsParser(s)
logp.Debug("pgsqldetailed", "Fields: %s", m.Fields)
s.parseState = PgsqlGetDataState
} else {
// wait for more
logp.Debug("pgsqldetailed", "Wait for more data 3")
return true, false
}
} else if typ == 'I' {
// EmptyQueryResponse, appears as a response for empty queries
// substitutes CommandComplete
logp.Debug("pgsqldetailed", "EmptyQueryResponse")
m.start = s.parseOffset
m.IsOK = true
m.IsRequest = false
m.toExport = true
s.parseOffset += 5 // type + length
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
return true, true
} else if typ == 'E' {
// ErrorResponse
logp.Debug("pgsqldetailed", "ErrorResponse")
m.start = s.parseOffset
m.IsRequest = false
m.IsError = true
m.toExport = true
if len(s.data[s.parseOffset:]) >= length+1 {
s.parseOffset += 1 //type
s.parseOffset += 4 //length
pgsqlErrorParser(s)
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
return true, true
} else {
// wait for more
logp.Debug("pgsqldetailed", "Wait for more data 4")
return true, false
}
} else if typ == 'C' {
// CommandComplete -> Successful response
m.start = s.parseOffset
m.IsRequest = false
m.IsOK = true
m.toExport = true
if len(s.data[s.parseOffset:]) >= length+1 {
s.parseOffset += 1 //type
name := string(s.data[s.parseOffset+4 : s.parseOffset+length-1]) //without \0
logp.Debug("pgsqldetailed", "CommandComplete length=%d, tag=%s", length, name)
s.parseOffset += length
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
return true, true
} else {
// wait for more
logp.Debug("pgsqldetailed", "Wait for more data 5")
return true, false
}
} else if typ == 'Z' {
// ReadyForQuery -> backend ready for a new query cycle
if len(s.data[s.parseOffset:]) >= length+1 {
m.start = s.parseOffset
s.parseOffset += 1 // type
s.parseOffset += length
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
return true, true
} else {
// wait for more
logp.Debug("pgsqldetailed", "Wait for more 5b")
return true, false
}
} else {
// TODO: add info from NoticeResponse in case there are warning messages for a query
// ignore command
if len(s.data[s.parseOffset:]) >= length+1 {
s.parseOffset += 1 //type
s.parseOffset += length
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
// ok and complete, but ignore
m.toExport = false
return true, true
} else {
// wait for more
logp.Debug("pgsqldetailed", "Wait for more data 6")
return true, false
}
}
}
break
case PgsqlGetDataState:
// The response to queries that return row sets contains:
// RowDescription
// zero or more DataRow
// CommandComplete
// ReadyForQuery
if len(s.data[s.parseOffset:]) < 5 {
logp.Warn("Postgresql Message too short (length=%d). Wait for more.", len(s.data[s.parseOffset:]))
return true, false
}
// read type
typ := byte(s.data[s.parseOffset])
// read message length
length := int(common.Bytes_Ntohl(s.data[s.parseOffset+1 : s.parseOffset+5]))
if typ == 'D' {
// DataRow
if len(s.data[s.parseOffset:]) >= length+1 {
// skip type
s.parseOffset += 1
// skip length size
s.parseOffset += 4
pgsql.pgsqlRowsParser(s)
} else {
// wait for more
logp.Debug("pgsqldetailed", "Wait for more data 7")
return true, false
}
} else if typ == 'C' {
// CommandComplete
if len(s.data[s.parseOffset:]) >= length+1 {
// skip type
s.parseOffset += 1
name := string(s.data[s.parseOffset+4 : s.parseOffset+length-1]) //without \0
logp.Debug("pgsqldetailed", "CommandComplete length=%d, tag=%s", length, name)
s.parseOffset += length
m.end = s.parseOffset
m.Size = uint64(m.end - m.start)
s.parseState = PgsqlStartState
logp.Debug("pgsqldetailed", "Rows: %s", m.Rows)
return true, true
} else {
// wait for more
logp.Debug("pgsqldetailed", "Wait for more data 8")
return true, false
}
} else {
// shouldn't happen
logp.Debug("pgsqldetailed", "Skip command of type %c", typ)
s.parseState = PgsqlStartState
}
break
}
}
return true, false
}
type pgsqlPrivateData struct {
Data [2]*PgsqlStream
}
func (pgsql *Pgsql) Parse(pkt *protos.Packet, tcptuple *common.TcpTuple,
dir uint8, private protos.ProtocolData) protos.ProtocolData {
defer logp.Recover("ParsePgsql exception")
priv := pgsqlPrivateData{}
if private != nil {
var ok bool
priv, ok = private.(pgsqlPrivateData)
if !ok {
priv = pgsqlPrivateData{}
}
}
if priv.Data[dir] == nil {
priv.Data[dir] = &PgsqlStream{
tcptuple: tcptuple,
data: pkt.Payload,
message: &PgsqlMessage{Ts: pkt.Ts},
}
logp.Debug("pgsqldetailed", "New stream created")
} else {
// concatenate bytes
priv.Data[dir].data = append(priv.Data[dir].data, pkt.Payload...)
logp.Debug("pgsqldetailed", "Len data: %d cap data: %d", len(priv.Data[dir].data), cap(priv.Data[dir].data))
if len(priv.Data[dir].data) > tcp.TCP_MAX_DATA_IN_STREAM {
logp.Debug("pgsql", "Stream data too large, dropping TCP stream")
priv.Data[dir] = nil
return priv
}
}
stream := priv.Data[dir]
if priv.Data[1-dir] != nil && priv.Data[1-dir].seenSSLRequest {
stream.expectSSLResponse = true
}
for len(stream.data) > 0 {
if stream.message == nil {
stream.message = &PgsqlMessage{Ts: pkt.Ts}
}
ok, complete := pgsql.pgsqlMessageParser(priv.Data[dir])
//logp.Debug("pgsqldetailed", "MessageParser returned ok=%v complete=%v", ok, complete)
if !ok {
// drop this tcp stream. Will retry parsing with the next
// segment in it
priv.Data[dir] = nil
logp.Debug("pgsql", "Ignore Postgresql message. Drop tcp stream. Try parsing with the next segment")
return priv
}
if complete {
// all ok, ship it
msg := stream.data[stream.message.start:stream.message.end]
if stream.message.isSSLRequest {
// SSL request
stream.seenSSLRequest = true
} else if stream.message.isSSLResponse {
// SSL request answered
stream.expectSSLResponse = false
priv.Data[1-dir].seenSSLRequest = false
} else {
if stream.message.toExport {
pgsql.handlePgsql(pgsql, stream.message, tcptuple, dir, msg)
}
}
// and reset message
stream.PrepareForNewMessage()
} else {
// wait for more data
break
}
}
return priv
}
func messageHasEnoughData(msg *PgsqlMessage) bool {
if msg == nil {
return false
}
if msg.isSSLRequest || msg.isSSLResponse {
return false
}
if msg.IsRequest {
return len(msg.Query) > 0
} else {
return len(msg.Rows) > 0
}
}
// Called when there's a drop packet
func (pgsql *Pgsql) GapInStream(tcptuple *common.TcpTuple, dir uint8,
nbytes int, private protos.ProtocolData) (priv protos.ProtocolData, drop bool) {
defer logp.Recover("GapInPgsqlStream exception")
if private == nil {
return private, false
}
pgsqlData, ok := private.(pgsqlPrivateData)
if !ok {
return private, false
}
if pgsqlData.Data[dir] == nil {
return pgsqlData, false
}
// If enough data was received, send it to the
// next layer but mark it as incomplete.
stream := pgsqlData.Data[dir]
if messageHasEnoughData(stream.message) {
logp.Debug("pgsql", "Message not complete, but sending to the next layer")
m := stream.message
m.toExport = true
m.end = stream.parseOffset
if m.IsRequest {
m.Notes = append(m.Notes, "Packet loss while capturing the request")
} else {
m.Notes = append(m.Notes, "Packet loss while capturing the response")
}
msg := stream.data[stream.message.start:stream.message.end]
pgsql.handlePgsql(pgsql, stream.message, tcptuple, dir, msg)
// and reset message
stream.PrepareForNewMessage()
}
return pgsqlData, true
}
func (pgsql *Pgsql) ReceivedFin(tcptuple *common.TcpTuple, dir uint8,
private protos.ProtocolData) protos.ProtocolData {
// TODO
return private
}
var handlePgsql = func(pgsql *Pgsql, m *PgsqlMessage, tcptuple *common.TcpTuple,
dir uint8, raw_msg []byte) {
m.TcpTuple = *tcptuple
m.Direction = dir
m.CmdlineTuple = procs.ProcWatcher.FindProcessesTuple(tcptuple.IpPort())
if m.IsRequest {
pgsql.receivedPgsqlRequest(m)
} else {
pgsql.receivedPgsqlResponse(m)
}
}
func (pgsql *Pgsql) receivedPgsqlRequest(msg *PgsqlMessage) {
tuple := msg.TcpTuple
// parse the query, as it might contain a list of pgsql command
// separated by ';'
queries := pgsqlQueryParser(msg.Query)
logp.Debug("pgsqldetailed", "Queries (%d) :%s", len(queries), queries)
if pgsql.transactionsMap[tuple.Hashable()] == nil {
pgsql.transactionsMap[tuple.Hashable()] = []*PgsqlTransaction{}
}
for _, query := range queries {
trans := &PgsqlTransaction{Type: "pgsql", tuple: tuple}
trans.ts = msg.Ts
trans.Ts = int64(trans.ts.UnixNano() / 1000) // transactions have microseconds resolution
trans.JsTs = msg.Ts
trans.Src = common.Endpoint{
Ip: msg.TcpTuple.Src_ip.String(),
Port: msg.TcpTuple.Src_port,
Proc: string(msg.CmdlineTuple.Src),
}
trans.Dst = common.Endpoint{
Ip: msg.TcpTuple.Dst_ip.String(),
Port: msg.TcpTuple.Dst_port,
Proc: string(msg.CmdlineTuple.Dst),
}
if msg.Direction == tcp.TcpDirectionReverse {
trans.Src, trans.Dst = trans.Dst, trans.Src
}
trans.Pgsql = common.MapStr{}
trans.Query = query
trans.Method = getQueryMethod(query)
trans.BytesIn = msg.Size
trans.Notes = msg.Notes
trans.Request_raw = query
if trans.timer != nil {
trans.timer.Stop()
}
trans.timer = time.AfterFunc(TransactionTimeout, func() { pgsql.expireTransaction(trans) })
pgsql.transactionsMap[tuple.Hashable()] = append(pgsql.transactionsMap[tuple.Hashable()], trans)
}
}
func (pgsql *Pgsql) receivedPgsqlResponse(msg *PgsqlMessage) {
tuple := msg.TcpTuple
trans_list := pgsql.transactionsMap[tuple.Hashable()]
if trans_list == nil || len(trans_list) == 0 {
logp.Warn("Response from unknown transaction. Ignoring.")
return
}
// extract the first transaction from the array
trans := pgsql.removeTransaction(tuple, 0)
// check if the request was received
if trans.Pgsql == nil {
logp.Warn("Response from unknown transaction. Ignoring.")
return
}
trans.Pgsql.Update(common.MapStr{
"iserror": msg.IsError,
"num_rows": msg.NumberOfRows,
"num_fields": msg.NumberOfFields,
"error_code": msg.ErrorCode,
"error_message": msg.ErrorInfo,
"error_severity": msg.ErrorSeverity,
})
trans.BytesOut = msg.Size
trans.ResponseTime = int32(msg.Ts.Sub(trans.ts).Nanoseconds() / 1e6) // resp_time in milliseconds
trans.Response_raw = common.DumpInCSVFormat(msg.Fields, msg.Rows)
trans.Notes = append(trans.Notes, msg.Notes...)
pgsql.publishTransaction(trans)
logp.Debug("pgsql", "Postgres transaction completed: %s\n%s", trans.Pgsql, trans.Response_raw)
if trans.timer != nil {
trans.timer.Stop()
}
}
func (pgsql *Pgsql) publishTransaction(t *PgsqlTransaction) {
if pgsql.results == nil {
return
}
event := common.MapStr{}
event["type"] = "pgsql"
if t.Pgsql["iserror"].(bool) {
event["status"] = common.ERROR_STATUS
} else {
event["status"] = common.OK_STATUS
}
event["responsetime"] = t.ResponseTime
if pgsql.Send_request {
event["request"] = t.Request_raw
}
if pgsql.Send_response {
event["response"] = t.Response_raw
}
event["query"] = t.Query
event["method"] = t.Method
event["bytes_out"] = t.BytesOut
event["bytes_in"] = t.BytesIn
event["pgsql"] = t.Pgsql
event["timestamp"] = common.Time(t.ts)
event["src"] = &t.Src
event["dst"] = &t.Dst
if len(t.Notes) > 0 {
event["notes"] = t.Notes
}
pgsql.results <- event
}
func (pgsql *Pgsql) expireTransaction(trans *PgsqlTransaction) {
// TODO: Here we need to PUBLISH an incomplete/timeout transaction
// remove from map
for i, t := range pgsql.transactionsMap[trans.tuple.Hashable()] {
if t == trans {
pgsql.removeTransaction(trans.tuple, i)
break
}
}
if len(pgsql.transactionsMap[trans.tuple.Hashable()]) == 0 {
delete(pgsql.transactionsMap, trans.tuple.Hashable())
}
}
func (pgsql *Pgsql) removeTransaction(tuple common.TcpTuple, index int) *PgsqlTransaction {
trans_list := pgsql.transactionsMap[tuple.Hashable()]
trans := trans_list[index]
trans_list = append(trans_list[:index], trans_list[index+1:]...)
if len(trans_list) == 0 {
delete(pgsql.transactionsMap, trans.tuple.Hashable())
} else {
pgsql.transactionsMap[tuple.Hashable()] = trans_list
}
return trans
}