forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgsql.go
495 lines (404 loc) · 11.2 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
package pgsql
import (
"errors"
"expvar"
"strings"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/packetbeat/procs"
"github.com/elastic/beats/packetbeat/protos"
"github.com/elastic/beats/packetbeat/protos/tcp"
"github.com/elastic/beats/packetbeat/publish"
)
type pgsqlPlugin struct {
// config
ports []int
maxStoreRows int
maxRowLength int
sendRequest bool
sendResponse bool
transactions *common.Cache
transactionTimeout time.Duration
results publish.Transactions
// function pointer for mocking
handlePgsql func(pgsql *pgsqlPlugin, m *pgsqlMessage, tcp *common.TCPTuple,
dir uint8, raw_msg []byte)
}
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 {
tuple common.TCPTuple
src common.Endpoint
dst common.Endpoint
responseTime int32
ts time.Time
query string
method string
bytesOut uint64
bytesIn uint64
notes []string
pgsql common.MapStr
requestRaw string
responseRaw string
}
type pgsqlStream struct {
data []byte
parseOffset int
parseState int
seenSSLRequest bool
expectSSLResponse bool
message *pgsqlMessage
}
const (
pgsqlStartState = iota
pgsqlGetDataState
pgsqlExtendedQueryState
)
const (
sslRequest = iota
startupMessage
cancelRequest
)
var (
errInvalidLength = errors.New("invalid length")
)
var (
debugf = logp.MakeDebug("pgsql")
detailedf = logp.MakeDebug("pgsqldetailed")
)
var (
unmatchedResponses = expvar.NewInt("pgsql.unmatched_responses")
)
func init() {
protos.Register("pgsql", New)
}
func New(
testMode bool,
results publish.Transactions,
cfg *common.Config,
) (protos.Plugin, error) {
p := &pgsqlPlugin{}
config := defaultConfig
if !testMode {
if err := cfg.Unpack(&config); err != nil {
return nil, err
}
}
if err := p.init(results, &config); err != nil {
return nil, err
}
return p, nil
}
func (pgsql *pgsqlPlugin) init(results publish.Transactions, config *pgsqlConfig) error {
pgsql.setFromConfig(config)
pgsql.transactions = common.NewCache(
pgsql.transactionTimeout,
protos.DefaultTransactionHashSize)
pgsql.transactions.StartJanitor(pgsql.transactionTimeout)
pgsql.handlePgsql = handlePgsql
pgsql.results = results
return nil
}
func (pgsql *pgsqlPlugin) setFromConfig(config *pgsqlConfig) {
pgsql.ports = config.Ports
pgsql.maxRowLength = config.MaxRowLength
pgsql.maxStoreRows = config.MaxRows
pgsql.sendRequest = config.SendRequest
pgsql.sendResponse = config.SendResponse
pgsql.transactionTimeout = config.TransactionTimeout
}
func (pgsql *pgsqlPlugin) getTransaction(k common.HashableTCPTuple) []*pgsqlTransaction {
v := pgsql.transactions.Get(k)
if v != nil {
return v.([]*pgsqlTransaction)
}
return nil
}
func (pgsql *pgsqlPlugin) GetPorts() []int {
return pgsql.ports
}
func (stream *pgsqlStream) prepareForNewMessage() {
stream.data = stream.data[stream.message.end:]
stream.parseState = pgsqlStartState
stream.parseOffset = 0
stream.message = nil
}
// 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
}
type pgsqlPrivateData struct {
data [2]*pgsqlStream
}
func (pgsql *pgsqlPlugin) ConnectionTimeout() time.Duration {
return pgsql.transactionTimeout
}
func (pgsql *pgsqlPlugin) 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{
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.TCPMaxDataInStream {
debugf("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
debugf("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
}
return len(msg.rows) > 0
}
// Called when there's a drop packet
func (pgsql *pgsqlPlugin) 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) {
debugf("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 *pgsqlPlugin) ReceivedFin(tcptuple *common.TCPTuple, dir uint8,
private protos.ProtocolData) protos.ProtocolData {
return private
}
var handlePgsql = func(pgsql *pgsqlPlugin, 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 *pgsqlPlugin) 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)
transList := pgsql.getTransaction(tuple.Hashable())
if transList == nil {
transList = []*pgsqlTransaction{}
}
for _, query := range queries {
trans := &pgsqlTransaction{tuple: tuple}
trans.ts = msg.ts
trans.src = common.Endpoint{
IP: msg.tcpTuple.SrcIP.String(),
Port: msg.tcpTuple.SrcPort,
Proc: string(msg.cmdlineTuple.Src),
}
trans.dst = common.Endpoint{
IP: msg.tcpTuple.DstIP.String(),
Port: msg.tcpTuple.DstPort,
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.requestRaw = query
transList = append(transList, trans)
}
pgsql.transactions.Put(tuple.Hashable(), transList)
}
func (pgsql *pgsqlPlugin) receivedPgsqlResponse(msg *pgsqlMessage) {
tuple := msg.tcpTuple
transList := pgsql.getTransaction(tuple.Hashable())
if transList == nil || len(transList) == 0 {
debugf("Response from unknown transaction. Ignoring.")
unmatchedResponses.Add(1)
return
}
// extract the first transaction from the array
trans := pgsql.removeTransaction(transList, tuple, 0)
// check if the request was received
if trans.pgsql == nil {
debugf("Response from unknown transaction. Ignoring.")
unmatchedResponses.Add(1)
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.responseRaw = common.DumpInCSVFormat(msg.fields, msg.rows)
trans.notes = append(trans.notes, msg.notes...)
pgsql.publishTransaction(trans)
debugf("Postgres transaction completed: %s\n%s", trans.pgsql, trans.responseRaw)
}
func (pgsql *pgsqlPlugin) 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.sendRequest {
event["request"] = t.requestRaw
}
if pgsql.sendResponse {
event["response"] = t.responseRaw
}
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.PublishTransaction(event)
}
func (pgsql *pgsqlPlugin) removeTransaction(transList []*pgsqlTransaction,
tuple common.TCPTuple, index int) *pgsqlTransaction {
trans := transList[index]
transList = append(transList[:index], transList[index+1:]...)
if len(transList) == 0 {
pgsql.transactions.Delete(trans.tuple.Hashable())
} else {
pgsql.transactions.Put(tuple.Hashable(), transList)
}
return trans
}