Skip to content

Commit 01e34ce

Browse files
authored
PYTHON-2769 Test redaction of replies to security-sensitive commands (mongodb#676)
Resync command monitoring and unified test format tests. Redact entire hello response when the command started contained speculativeAuthenticate. Make OP_REPLY cursor.cursor_id always be an Int64.
1 parent 9a4c64f commit 01e34ce

File tree

63 files changed

+1662
-501
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1662
-501
lines changed

pymongo/message.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
_dict_to_bson,
3434
_make_c_string)
3535
from bson.codec_options import DEFAULT_CODEC_OPTIONS
36+
from bson.int64 import Int64
3637
from bson.raw_bson import (_inflate_bson, DEFAULT_RAW_BSON_OPTIONS,
3738
RawBSONDocument)
3839
from bson.son import SON
@@ -1503,7 +1504,7 @@ class _OpReply(object):
15031504

15041505
def __init__(self, flags, cursor_id, number_returned, documents):
15051506
self.flags = flags
1506-
self.cursor_id = cursor_id
1507+
self.cursor_id = Int64(cursor_id)
15071508
self.number_returned = number_returned
15081509
self.documents = documents
15091510

pymongo/monitoring.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,8 @@ def publish_command_start(self, command, database_name,
13501350

13511351
def publish_command_success(self, duration, reply, command_name,
13521352
request_id, connection_id, op_id=None,
1353-
service_id=None):
1353+
service_id=None,
1354+
speculative_hello=False):
13541355
"""Publish a CommandSucceededEvent to all command listeners.
13551356
13561357
:Parameters:
@@ -1362,9 +1363,14 @@ def publish_command_success(self, duration, reply, command_name,
13621363
command was sent to.
13631364
- `op_id`: The (optional) operation id for this operation.
13641365
- `service_id`: The service_id this command was sent to, or ``None``.
1366+
- `speculative_hello`: Was the command sent with speculative auth?
13651367
"""
13661368
if op_id is None:
13671369
op_id = request_id
1370+
if speculative_hello:
1371+
# Redact entire response when the command started contained
1372+
# speculativeAuthenticate.
1373+
reply = {}
13681374
event = CommandSucceededEvent(
13691375
duration, reply, command_name, request_id, connection_id, op_id,
13701376
service_id)

pymongo/network.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
ProtocolError,
3232
_OperationCancelled)
3333
from pymongo.message import _UNPACK_REPLY, _OpMsg
34+
from pymongo.monitoring import _is_speculative_authenticate
3435
from pymongo.socket_checker import _errno_from_exception
3536

3637

@@ -82,6 +83,7 @@ def command(sock_info, dbname, spec, slave_ok, is_mongos,
8283
name = next(iter(spec))
8384
ns = dbname + '.$cmd'
8485
flags = 4 if slave_ok else 0
86+
speculative_hello = False
8587

8688
# Publish the original command document, perhaps with lsid and $clusterTime.
8789
orig = spec
@@ -98,6 +100,7 @@ def command(sock_info, dbname, spec, slave_ok, is_mongos,
98100
publish = listeners is not None and listeners.enabled_for_commands
99101
if publish:
100102
start = datetime.datetime.now()
103+
speculative_hello = _is_speculative_authenticate(name, spec)
101104

102105
if compression_ctx and name.lower() in _NO_COMPRESSION:
103106
compression_ctx = None
@@ -170,7 +173,8 @@ def command(sock_info, dbname, spec, slave_ok, is_mongos,
170173
duration = (datetime.datetime.now() - start) + encoding_duration
171174
listeners.publish_command_success(
172175
duration, response_doc, name, request_id, address,
173-
service_id=sock_info.service_id)
176+
service_id=sock_info.service_id,
177+
speculative_hello=speculative_hello)
174178

175179
if client and client._encrypter and reply:
176180
decrypted = client._encrypter.decrypt(reply.raw_command_response())

test/command_monitoring/legacy/bulkWrite.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@
8686
"$set": {
8787
"x": 333
8888
}
89-
},
90-
"upsert": false,
91-
"multi": false
89+
}
9290
}
9391
],
9492
"ordered": true

test/command_monitoring/legacy/insertMany.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
"x": 22
3333
}
3434
],
35-
"options": {
36-
"ordered": true
37-
}
35+
"ordered": true
3836
},
3937
"command_name": "insert",
4038
"database_name": "command-monitoring-tests"
@@ -75,9 +73,7 @@
7573
"x": 11
7674
}
7775
],
78-
"options": {
79-
"ordered": true
80-
}
76+
"ordered": true
8177
},
8278
"command_name": "insert",
8379
"database_name": "command-monitoring-tests"
@@ -128,9 +124,7 @@
128124
"x": 22
129125
}
130126
],
131-
"options": {
132-
"ordered": false
133-
}
127+
"ordered": false
134128
},
135129
"command_name": "insert",
136130
"database_name": "command-monitoring-tests"

test/command_monitoring/legacy/updateMany.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@
5151
"x": 1
5252
}
5353
},
54-
"multi": true,
55-
"upsert": false
54+
"multi": true
5655
}
5756
]
5857
},
@@ -106,8 +105,7 @@
106105
"x": 1
107106
}
108107
},
109-
"multi": true,
110-
"upsert": false
108+
"multi": true
111109
}
112110
]
113111
},

test/command_monitoring/legacy/updateOne.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@
5050
"$inc": {
5151
"x": 1
5252
}
53-
},
54-
"multi": false,
55-
"upsert": false
53+
}
5654
}
5755
]
5856
},
@@ -103,7 +101,6 @@
103101
"x": 1
104102
}
105103
},
106-
"multi": false,
107104
"upsert": true
108105
}
109106
]
@@ -163,9 +160,7 @@
163160
"$nothing": {
164161
"x": 1
165162
}
166-
},
167-
"multi": false,
168-
"upsert": false
163+
}
169164
}
170165
]
171166
},

0 commit comments

Comments
 (0)