Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #10878 to 7.x: [Packetbeat] [MongoDB] Report unknown opcodes once #10889

Merged
merged 1 commit into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

- Fix DHCPv4 dashboard that wouldn't load in Kibana. {issue}9850[9850]
- Fixed a crash when using af_packet capture {pull}10477[10477]
- Avoid reporting unknown MongoDB opcodes more than once. {pull}10878[10878]

*Winlogbeat*

Expand Down
13 changes: 12 additions & 1 deletion packetbeat/protos/mongodb/mongodb_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ import (
"encoding/json"
"errors"
"strings"
"sync"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"

"gopkg.in/mgo.v2/bson"
)

var (
unknownOpcodes = map[opCode]struct{}{}
mutex sync.Mutex
)

func mongodbMessageParser(s *stream) (bool, bool) {
d := newDecoder(s.data)

Expand Down Expand Up @@ -56,7 +62,12 @@ func mongodbMessageParser(s *stream) (bool, bool) {
opCode := opCode(code)

if !validOpcode(opCode) {
logp.Err("Unknown operation code: %v", opCode)
mutex.Lock()
defer mutex.Unlock()
if _, reported := unknownOpcodes[opCode]; !reported {
logp.Err("Unknown operation code: %v", opCode)
unknownOpcodes[opCode] = struct{}{}
}
return false, false
}

Expand Down
Binary file not shown.
12 changes: 12 additions & 0 deletions packetbeat/tests/system/test_0025_mongodb_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,15 @@ def test_request_after_reply(self):
o = objs[0]
assert o["type"] == "mongodb"
assert o["event.duration"] >= 0

def test_unknown_opcode_flood(self):
"""
Tests that a repeated unknown opcode is reported just once.
"""
self.render_config_template(
mongodb_ports=[9991]
)
self.run_packetbeat(pcap="mongodb_op_msg_opcode.pcap",
debug_selectors=["mongodb"])
num_msgs = self.log_contains_count('Unknown operation code: ')
assert num_msgs == 1, "Unknown opcode reported more than once: {0}".format(num_msgs)