Skip to content

Commit

Permalink
BUG: mongodb output: auth with different server versions
Browse files Browse the repository at this point in the history
fixes #1439
  • Loading branch information
Sebastian Wagner committed Mar 27, 2020
1 parent c8c6d7f commit f0afacf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ CHANGELOG
#### Outputs
- `intelmq.bots.outputs.mongodb`:
- Set default port 27017.
- Use different authentication mechanisms per MongoDB server version to fix compatibility with server version >= 3.4 (#1439)

### Documentation
- Feeds:
Expand Down
2 changes: 1 addition & 1 deletion docs/Bots.md
Original file line number Diff line number Diff line change
Expand Up @@ -2584,7 +2584,7 @@ Saves events in a MongoDB either as hierarchical structure or flat with full key
pip3 install pymongo>=2.7.1
```

The bot has been tested with pymongo versions 2.7.1 and 3.4.
The bot has been tested with pymongo versions 2.7.1, 3.4 and 3.10.1 (server versions 2.6.10 and 3.6.8).

* * *

Expand Down
11 changes: 8 additions & 3 deletions intelmq/bots/outputs/mongodb/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ def init(self):
self.connect()

def connect(self):
self.logger.debug('Connecting to MongoDB server.')
self.logger.debug('Getting server info.')
server_info = pymongo.MongoClient(self.parameters.host, self.port).server_info()
server_version = server_info['version']
server_version_split = tuple(server_version.split('.'))
self.logger.debug('Connecting to MongoDB server version %s.',
server_version)
try:
if self.pymongo_35 and self.username:
if self.pymongo_35 and self.username and server_version_split >= ('3', '4'):
self.client = pymongo.MongoClient(self.parameters.host,
self.port,
username=self.username,
Expand All @@ -50,7 +55,7 @@ def connect(self):
raise ValueError('Connection to MongoDB server failed.')
else:
db = self.client[self.parameters.database]
if self.username and not self.pymongo_35:
if self.username and not self.pymongo_35 or server_version_split < ('3', '4'):
self.logger.debug('Trying to authenticate to database %s.',
self.parameters.database)
try:
Expand Down

0 comments on commit f0afacf

Please sign in to comment.