forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.go
366 lines (306 loc) · 9.05 KB
/
mongodb.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
package mongodb
import (
"fmt"
"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 Mongodb struct {
// config
Ports []int
Send_request bool
Send_response bool
Max_docs int
Max_doc_length int
transactionsMap map[common.HashableTcpTuple]*MongodbTransaction
results chan common.MapStr
}
func (mongodb *Mongodb) InitDefaults() {
mongodb.Send_request = false
mongodb.Send_response = false
mongodb.Max_docs = 10
mongodb.Max_doc_length = 5000
}
func (mongodb *Mongodb) setFromConfig(config config.Mongodb) error {
mongodb.Ports = config.Ports
if config.Send_request != nil {
mongodb.Send_request = *config.Send_request
}
if config.Send_response != nil {
mongodb.Send_response = *config.Send_response
}
if config.Max_docs != nil {
mongodb.Max_docs = *config.Max_docs
}
if config.Max_doc_length != nil {
mongodb.Max_doc_length = *config.Max_doc_length
}
return nil
}
func (mongodb *Mongodb) GetPorts() []int {
return mongodb.Ports
}
func (mongodb *Mongodb) Init(test_mode bool, results chan common.MapStr) error {
logp.Debug("mongodb", "Init a MongoDB protocol parser")
mongodb.InitDefaults()
if !test_mode {
err := mongodb.setFromConfig(config.ConfigSingleton.Protocols.Mongodb)
if err != nil {
return err
}
}
mongodb.transactionsMap = make(map[common.HashableTcpTuple]*MongodbTransaction, TransactionsHashSize)
mongodb.results = results
return nil
}
func (mongodb *Mongodb) Parse(pkt *protos.Packet, tcptuple *common.TcpTuple, dir uint8,
private protos.ProtocolData) protos.ProtocolData {
logp.Debug("mongodb", "Parse method triggered")
defer logp.Recover("ParseMongodb exception")
// Either fetch or initialize current data struct for this parser
priv := mongodbPrivateData{}
if private != nil {
var ok bool
priv, ok = private.(mongodbPrivateData)
if !ok {
priv = mongodbPrivateData{}
}
}
if priv.Data[dir] == nil {
priv.Data[dir] = &MongodbStream{
tcptuple: tcptuple,
data: pkt.Payload,
message: &MongodbMessage{Ts: pkt.Ts},
}
} else {
// concatenate bytes
priv.Data[dir].data = append(priv.Data[dir].data, pkt.Payload...)
if len(priv.Data[dir].data) > tcp.TCP_MAX_DATA_IN_STREAM {
logp.Debug("mongodb", "Stream data too large, dropping TCP stream")
priv.Data[dir] = nil
return priv
}
}
stream := priv.Data[dir]
for len(stream.data) > 0 {
if stream.message == nil {
stream.message = &MongodbMessage{Ts: pkt.Ts}
}
ok, complete := mongodbMessageParser(priv.Data[dir])
if !ok {
// drop this tcp stream. Will retry parsing with the next
// segment in it
priv.Data[dir] = nil
logp.Debug("mongodb", "Ignore Mongodb message. Drop tcp stream. Try parsing with the next segment")
return priv
}
if complete {
logp.Debug("mongodb", "MongoDB message complete")
// all ok, go to next level
mongodb.handleMongodb(stream.message, tcptuple, dir)
// and reset message
stream.PrepareForNewMessage()
} else {
// wait for more data
logp.Debug("mongodb", "MongoDB wait for more data before parsing message")
break
}
}
return priv
}
func (mongodb *Mongodb) handleMongodb(m *MongodbMessage, tcptuple *common.TcpTuple,
dir uint8) {
m.TcpTuple = *tcptuple
m.Direction = dir
m.CmdlineTuple = procs.ProcWatcher.FindProcessesTuple(tcptuple.IpPort())
if m.IsResponse {
logp.Debug("mongodb", "MongoDB response message")
mongodb.receivedMongodbResponse(m)
} else {
logp.Debug("mongodb", "MongoDB request message")
mongodb.receivedMongodbRequest(m)
}
}
func (mongodb *Mongodb) receivedMongodbRequest(msg *MongodbMessage) {
// Add it to the HT
tuple := msg.TcpTuple
trans := mongodb.transactionsMap[tuple.Hashable()]
if trans != nil {
if trans.Mongodb != nil {
logp.Warn("Two requests without a Response. Dropping old request")
}
} else {
logp.Debug("mongodb", "Initialize new transaction from request")
trans = &MongodbTransaction{Type: "mongodb", tuple: tuple}
mongodb.transactionsMap[tuple.Hashable()] = trans
}
trans.Mongodb = common.MapStr{}
trans.event = msg.event
trans.method = msg.method
trans.cmdline = msg.CmdlineTuple
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.params = msg.params
trans.resource = msg.resource
trans.BytesIn = msg.messageLength
if trans.timer != nil {
trans.timer.Stop()
}
trans.timer = time.AfterFunc(TransactionTimeout, func() { mongodb.expireTransaction(trans) })
}
func (mongodb *Mongodb) expireTransaction(trans *MongodbTransaction) {
logp.Debug("mongodb", "Expire transaction")
// remove from map
delete(mongodb.transactionsMap, trans.tuple.Hashable())
}
func (mongodb *Mongodb) receivedMongodbResponse(msg *MongodbMessage) {
tuple := msg.TcpTuple
trans := mongodb.transactionsMap[tuple.Hashable()]
if trans == nil {
logp.Warn("Response from unknown transaction. Ignoring.")
return
}
// check if the request was received
if trans.Mongodb == nil {
logp.Warn("Response from unknown transaction. Ignoring.")
return
}
// Merge request and response events attributes
for k, v := range msg.event {
trans.event[k] = v
}
trans.error = msg.error
trans.documents = msg.documents
trans.ResponseTime = int32(msg.Ts.Sub(trans.ts).Nanoseconds() / 1e6) // resp_time in milliseconds
trans.BytesOut = msg.messageLength
mongodb.publishTransaction(trans)
logp.Debug("mongodb", "Mongodb transaction completed: %s", trans.Mongodb)
// remove from map
delete(mongodb.transactionsMap, trans.tuple.Hashable())
if trans.timer != nil {
trans.timer.Stop()
}
}
func (mongodb *Mongodb) GapInStream(tcptuple *common.TcpTuple, dir uint8,
nbytes int, private protos.ProtocolData) (priv protos.ProtocolData, drop bool) {
// TODO
return private, true
}
func (mongodb *Mongodb) ReceivedFin(tcptuple *common.TcpTuple, dir uint8,
private protos.ProtocolData) protos.ProtocolData {
// TODO
return private
}
func copy_map_without_key(d map[string]interface{}, key string) map[string]interface{} {
res := map[string]interface{}{}
for k, v := range d {
if k != key {
res[k] = v
}
}
return res
}
func reconstructQuery(t *MongodbTransaction, full bool) (query string) {
query = t.resource + "." + t.method + "("
if len(t.params) > 0 {
var err error
var params string
if !full {
// remove the actual data.
// TODO: review if we need to add other commands here
if t.method == "insert" {
params, err = doc2str(copy_map_without_key(t.params, "documents"))
} else if t.method == "update" {
params, err = doc2str(copy_map_without_key(t.params, "updates"))
} else if t.method == "findandmodify" {
params, err = doc2str(copy_map_without_key(t.params, "update"))
}
} else {
params, err = doc2str(t.params)
}
if err != nil {
logp.Debug("mongodb", "Error marshaling params: %v", err)
} else {
query += params
}
}
query += ")"
skip, _ := t.event["numberToSkip"].(int)
if skip > 0 {
query += fmt.Sprintf(".skip(%d)", skip)
}
limit, _ := t.event["numberToReturn"].(int)
if limit > 0 && limit < 0x7fffffff {
query += fmt.Sprintf(".limit(%d)", limit)
}
return
}
func (mongodb *Mongodb) publishTransaction(t *MongodbTransaction) {
if mongodb.results == nil {
logp.Debug("mongodb", "Try to publish transaction with null results")
return
}
event := common.MapStr{}
event["type"] = "mongodb"
if t.error == "" {
event["status"] = common.OK_STATUS
} else {
t.event["error"] = t.error
event["status"] = common.ERROR_STATUS
}
event["mongodb"] = t.event
event["method"] = t.method
event["resource"] = t.resource
event["query"] = reconstructQuery(t, false)
event["responsetime"] = t.ResponseTime
event["bytes_in"] = uint64(t.BytesIn)
event["bytes_out"] = uint64(t.BytesOut)
event["timestamp"] = common.Time(t.ts)
event["src"] = &t.Src
event["dst"] = &t.Dst
if mongodb.Send_request {
event["request"] = reconstructQuery(t, true)
}
if mongodb.Send_response {
if len(t.documents) > 0 {
// response field needs to be a string
docs := make([]string, 0, len(t.documents))
for i, doc := range t.documents {
if mongodb.Max_docs > 0 && i >= mongodb.Max_docs {
docs = append(docs, "[...]")
break
}
str, err := doc2str(doc)
if err != nil {
logp.Warn("Failed to JSON marshal document from Mongo: %v (error: %v)", doc, err)
} else {
if mongodb.Max_doc_length > 0 && len(str) > mongodb.Max_doc_length {
str = str[:mongodb.Max_doc_length] + " ..."
}
docs = append(docs, str)
}
}
event["response"] = strings.Join(docs, "\n")
}
}
mongodb.results <- event
}