-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-2858 Use OP_MSG to authenticate if server supports OP_MSG #843
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
Changes from 12 commits
3f4372c
7d9dbed
6c20dd5
feb7f40
61d2b19
88e4100
0745764
69a5e34
29579d2
9f0a2f0
b354d6b
18b126e
fcd048e
8ca1f78
1b5792c
66cc32d
4f253de
2c6315c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
from bson.objectid import ObjectId | ||
|
||
import unittest | ||
from copy import deepcopy | ||
|
||
|
||
def test_hello_with_option(self, protocol, **kwargs): | ||
|
@@ -44,13 +45,13 @@ def respond(r): | |
|
||
# We need a special dict because MongoClient uses "server_api" and all | ||
# of the commands use "apiVersion". | ||
k_map = {("apiVersion", "1"):("server_api", ServerApi( | ||
ServerApiVersion.V1))} | ||
k_map = {("apiVersion", "1"): ("server_api", ServerApi( | ||
ServerApiVersion.V1))} | ||
client = MongoClient("mongodb://"+primary.address_string, | ||
appname='my app', # For _check_handshake_data() | ||
**dict([k_map.get((k, v), (k, v)) for k, v | ||
in kwargs.items()])) | ||
|
||
self.addCleanup(client.close) | ||
|
||
# We have an autoresponder luckily, so no need for `go()`. | ||
|
@@ -84,76 +85,78 @@ def test_client_handshake_data(self): | |
self.addCleanup(server.stop) | ||
|
||
hosts = [server.address_string for server in (primary, secondary)] | ||
primary_response = OpReply('ismaster', True, | ||
setName='rs', hosts=hosts, | ||
minWireVersion=2, maxWireVersion=6) | ||
error_response = OpReply( | ||
primary_response = {"setName": 'rs', "hosts": hosts, | ||
"minWireVersion": 2, "maxWireVersion": 13} | ||
error_response = OpMsgReply( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is already passing so do we need to change it at all? |
||
0, errmsg='Cache Reader No keys found for HMAC ...', code=211) | ||
|
||
secondary_response = OpReply('ismaster', False, | ||
setName='rs', hosts=hosts, | ||
secondary=True, | ||
minWireVersion=2, maxWireVersion=6) | ||
secondary_response = {"setName": 'rs', "hosts": hosts, | ||
"minWireVersion": 2, "maxWireVersion": 13} | ||
|
||
client = MongoClient(primary.uri, | ||
replicaSet='rs', | ||
appname='my app', | ||
heartbeatFrequencyMS=500) # Speed up the test. | ||
|
||
self.addCleanup(client.close) | ||
|
||
# New monitoring sockets send data during handshake. | ||
heartbeat = primary.receives('ismaster') | ||
heartbeat = primary.receives(Command('ismaster')) | ||
_check_handshake_data(heartbeat) | ||
heartbeat.ok(primary_response) | ||
heartbeat.ok(**primary_response) | ||
|
||
heartbeat = secondary.receives('ismaster') | ||
heartbeat = secondary.receives(Command('ismaster')) | ||
_check_handshake_data(heartbeat) | ||
heartbeat.ok(secondary_response) | ||
heartbeat.ok(**secondary_response) | ||
|
||
# Subsequent heartbeats have no client data. | ||
primary.receives('ismaster', 1, client=absent).ok(error_response) | ||
secondary.receives('ismaster', 1, client=absent).ok(error_response) | ||
primary.receives(OpMsg('hello', 1, client=absent)).ok(error_response) | ||
secondary.receives(OpMsg('hello', 1, client=absent)).ok( | ||
error_response) | ||
|
||
# The heartbeat retry (on a new connection) does have client data. | ||
heartbeat = primary.receives('ismaster') | ||
heartbeat = primary.receives(Command('ismaster')) | ||
_check_handshake_data(heartbeat) | ||
heartbeat.ok(primary_response) | ||
heartbeat.reply(primary_response) | ||
|
||
heartbeat = secondary.receives('ismaster') | ||
heartbeat = secondary.receives(Command('ismaster')) | ||
_check_handshake_data(heartbeat) | ||
heartbeat.ok(secondary_response) | ||
heartbeat.reply(secondary_response) | ||
|
||
# Still no client data. | ||
primary.receives('ismaster', 1, client=absent).ok(primary_response) | ||
secondary.receives('ismaster', 1, client=absent).ok(secondary_response) | ||
primary.receives(OpMsg('hello', 1, client=absent)).reply(**primary_response) | ||
secondary.receives(OpMsg('hello', 1, client=absent)).reply(**secondary_response) | ||
|
||
primary.receives(OpMsg('hello', 1, client=absent)).hangup() | ||
secondary.receives(OpMsg('hello')).hangup() | ||
# After a disconnect, next ismaster has client data again. | ||
primary.receives('ismaster', 1, client=absent).hangup() | ||
heartbeat = primary.receives('ismaster') | ||
_check_handshake_data(heartbeat) | ||
heartbeat.ok(primary_response) | ||
|
||
secondary.autoresponds('ismaster', secondary_response) | ||
hb_port = deepcopy(heartbeat.client_port) | ||
heartbeat.reply(primary_response) | ||
|
||
# Start a command, so the client opens an application socket. | ||
future = go(client.db.command, 'whatever') | ||
|
||
future = go(client.db.command, "whatever") | ||
handshook = None | ||
for request in primary: | ||
if request.matches(Command('ismaster')): | ||
if request.client_port == heartbeat.client_port: | ||
# This is the monitor again, keep going. | ||
request.ok(primary_response) | ||
if handshook is None: | ||
handshook = True | ||
else: | ||
# Handshaking a new application socket. | ||
_check_handshake_data(request) | ||
request.ok(primary_response) | ||
else: | ||
handshook = False | ||
# Handshaking a new application socket. | ||
_check_handshake_data(request) | ||
request.reply(**primary_response) | ||
elif request.matches(OpMsg("whatever")): | ||
# Command succeeds. | ||
request.assert_matches(OpMsg('whatever')) | ||
request.ok() | ||
assert future() | ||
request.reply(OpMsgReply(**primary_response)) | ||
# If handshook is false it means it tried to handshake | ||
# multiple times. If it's None it means it never tried. | ||
assert future() and handshook | ||
return | ||
else: | ||
request.reply(isWritablePrimary=True, | ||
**primary_response) | ||
|
||
def test_client_handshake_saslSupportedMechs(self): | ||
server = MockupDB() | ||
|
@@ -217,5 +220,42 @@ def test_handshake_not_either(self): | |
with self.assertRaisesRegex(AssertionError, "does not match"): | ||
test_hello_with_option(self, OpMsg) | ||
|
||
def test_handshake_max_wire(self): | ||
server = MockupDB() | ||
primary_response = {"hello": 1, "ok": 1, | ||
"minWireVersion": 0, "maxWireVersion": 6} | ||
self.found_auth_msg = False | ||
|
||
def responder(request): | ||
if request.matches(OpMsg, saslStart=1): | ||
self.found_auth_msg = True | ||
# Immediately closes the connection with | ||
request.reply(OpMsgReply(**primary_response, | ||
**{'payload': | ||
b'r=wPleNM8S5p8gMaffMDF7Py4ru9bnmmoqb0' | ||
b'1WNPsil6o=pAvr6B1garhlwc6MKNQ93ZfFky' | ||
b'tXdF9r,' | ||
b's=4dcxugMJq2P4hQaDbGXZR8uR3ei' | ||
b'PHrSmh4uhkg==,i=15000', | ||
"saslSupportedMechs": [ | ||
"SCRAM-SHA-1"]})) | ||
else: | ||
return request.reply(OpMsgReply(**primary_response)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it responds to an OP_QUERY ismaster handshake with an OP_MSG response. Let's change it to use OP_REPLY. Eg: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
server.autoresponds(responder) | ||
self.addCleanup(server.stop) | ||
server.run() | ||
client = MongoClient(server.uri, | ||
username='username', | ||
password='password', | ||
appname='my app', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. appname is not needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
) | ||
self.addCleanup(client.close) | ||
self.assertRaises(OperationFailure, client.db.collection.find_one, | ||
{"a": 1}) | ||
assert self.found_auth_msg, """Could not find authentication command | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use self.assertTrue with regular string (not triple quotes). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
with correct protocol""" | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change isn't needed. We already set op_msg_enabled to true when
max_wire_version >= 6)
elsewhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, yeah. You're correct. This was due to me confusing DRIVERS-1916 with saying that OP_MSG necessitates "hello" not the other way around. Reverted.