@@ -50,21 +50,25 @@ public class BinaryConnection_CommandEventTests : IDisposable
50
50
51
51
public static IEnumerable < object [ ] > GetPotentiallyRedactedCommandTestCases ( )
52
52
{
53
- var potentiallyRedactedCommands = new [ ]
53
+ return new object [ ] [ ]
54
54
{
55
- "authenticate" ,
56
- "saslStart" ,
57
- "saslContinue" ,
58
- "getnonce" ,
59
- "createUser" ,
60
- "updateUser" ,
61
- "copydbgetnonce" ,
62
- "copydbsaslstart" ,
63
- "copydb" ,
64
- "isMaster" ,
55
+ // string commandJson, bool shouldBeRedacted
56
+ new object [ ] { "{ xyz : 1 }" , false } ,
57
+ new object [ ] { "{ authenticate : 1 }" , true } ,
58
+ new object [ ] { "{ saslStart : 1 }" , true } ,
59
+ new object [ ] { "{ saslContinue : 1 }" , true } ,
60
+ new object [ ] { "{ getnonce : 1 }" , true } ,
61
+ new object [ ] { "{ createUser : 1 }" , true } ,
62
+ new object [ ] { "{ updateUser : 1 }" , true } ,
63
+ new object [ ] { "{ copydbgetnonce : 1 }" , true } ,
64
+ new object [ ] { "{ copydbsaslstart : 1 }" , true } ,
65
+ new object [ ] { "{ copydb : 1 }" , true } ,
66
+ new object [ ] { "{ authenticate : 1 }" , true } ,
67
+ new object [ ] { "{ isMaster : 1 }" , false } ,
68
+ new object [ ] { "{ isMaster : 1, speculativeAuthenticate : { } }" , true } ,
65
69
} ;
66
- return potentiallyRedactedCommands . Select ( c => new object [ ] { c } ) ;
67
70
}
71
+
68
72
public BinaryConnection_CommandEventTests ( )
69
73
{
70
74
_capturedEvents = new EventCapturer ( )
@@ -144,13 +148,10 @@ public void Should_process_a_command()
144
148
145
149
[ Theory ]
146
150
[ MemberData ( nameof ( GetPotentiallyRedactedCommandTestCases ) ) ]
147
- public void Should_process_a_redacted_command ( string commandName )
151
+ public void Should_process_a_redacted_command ( string commandJson , bool shouldBeRedacted )
148
152
{
149
- var command = BsonDocument . Parse ( $ "{{ { commandName } : 1, extra: true }}") ;
150
- command = ModifyMessageToTriggerConditionToRedact ( commandName , command ) ;
151
-
153
+ var command = BsonDocument . Parse ( commandJson ) ;
152
154
var reply = BsonDocument . Parse ( "{ ok: 1, extra: true }" ) ;
153
- reply = ModifyMessageToTriggerConditionToRedact ( commandName , reply ) ;
154
155
155
156
var requestMessage = MessageHelper . BuildCommand (
156
157
command ,
@@ -163,17 +164,11 @@ public void Should_process_a_redacted_command(string commandName)
163
164
responseTo : requestMessage . RequestId ) ;
164
165
ReceiveMessages ( replyMessage ) ;
165
166
166
-
167
- var commandStartedCommandShouldBeRedacted = CommandEventHelperReflector . ShouldRedactMessage ( commandName , command ) ;
168
- var commandSucceededCommandShouldBeRedacted = CommandEventHelperReflector . ShouldRedactMessage ( commandName , replyMessage . Documents [ 0 ] ) ;
169
- var commandStartedCommandExpectedElementCount = commandStartedCommandShouldBeRedacted ? 0 : command . ElementCount ;
170
- var commandSucceededCommandExpectedElementCount = commandSucceededCommandShouldBeRedacted ? 0 : reply . ElementCount ;
171
-
172
167
var commandStartedEvent = ( CommandStartedEvent ) _capturedEvents . Next ( ) ;
173
168
var commandSucceededEvent = ( CommandSucceededEvent ) _capturedEvents . Next ( ) ;
174
169
175
170
commandStartedEvent . CommandName . Should ( ) . Be ( command . GetElement ( 0 ) . Name ) ;
176
- commandStartedEvent . Command . ElementCount . Should ( ) . Be ( commandStartedCommandExpectedElementCount ) ;
171
+ commandStartedEvent . Command . Should ( ) . Be ( shouldBeRedacted ? new BsonDocument ( ) : command ) ;
177
172
commandStartedEvent . ConnectionId . Should ( ) . Be ( _subject . ConnectionId ) ;
178
173
commandStartedEvent . DatabaseNamespace . Should ( ) . Be ( MessageHelper . DefaultDatabaseNamespace ) ;
179
174
commandStartedEvent . OperationId . Should ( ) . Be ( EventContext . OperationId ) ;
@@ -183,7 +178,7 @@ public void Should_process_a_redacted_command(string commandName)
183
178
commandSucceededEvent . ConnectionId . Should ( ) . Be ( commandStartedEvent . ConnectionId ) ;
184
179
commandSucceededEvent . Duration . Should ( ) . BeGreaterThan ( TimeSpan . Zero ) ;
185
180
commandSucceededEvent . OperationId . Should ( ) . Be ( commandStartedEvent . OperationId ) ;
186
- commandSucceededEvent . Reply . ElementCount . Should ( ) . Be ( commandSucceededCommandExpectedElementCount ) ;
181
+ commandSucceededEvent . Reply . Should ( ) . Be ( shouldBeRedacted ? new BsonDocument ( ) : reply ) ;
187
182
commandSucceededEvent . RequestId . Should ( ) . Be ( commandStartedEvent . RequestId ) ;
188
183
}
189
184
@@ -224,13 +219,10 @@ public void Should_process_a_failed_command()
224
219
225
220
[ Theory ]
226
221
[ MemberData ( nameof ( GetPotentiallyRedactedCommandTestCases ) ) ]
227
- public void Should_process_a_redacted_failed_command ( string commandName )
222
+ public void Should_process_a_redacted_failed_command ( string commandJson , bool shouldBeRedacted )
228
223
{
229
- var command = BsonDocument . Parse ( $ "{{ { commandName } : 1, extra: true }}") ;
230
- command = ModifyMessageToTriggerConditionToRedact ( commandName , command ) ;
231
-
224
+ var command = BsonDocument . Parse ( commandJson ) ;
232
225
var reply = BsonDocument . Parse ( "{ ok: 0, extra: true }" ) ;
233
- reply = ModifyMessageToTriggerConditionToRedact ( commandName , reply ) ;
234
226
235
227
var requestMessage = MessageHelper . BuildCommand (
236
228
command ,
@@ -243,16 +235,11 @@ public void Should_process_a_redacted_failed_command(string commandName)
243
235
responseTo : requestMessage . RequestId ) ;
244
236
ReceiveMessages ( replyMessage ) ;
245
237
246
- var commandStartedCommandShouldBeRedacted = CommandEventHelperReflector . ShouldRedactMessage ( commandName , command ) ;
247
- var commandSucceededCommandShouldBeRedacted = CommandEventHelperReflector . ShouldRedactMessage ( commandName , replyMessage . Documents [ 0 ] ) ;
248
- var commandStartedCommandExpectedElementCount = commandStartedCommandShouldBeRedacted ? 0 : command . ElementCount ;
249
- var commandSucceededCommandExpectedElementCount = commandSucceededCommandShouldBeRedacted ? 0 : reply . ElementCount ;
250
-
251
238
var commandStartedEvent = ( CommandStartedEvent ) _capturedEvents . Next ( ) ;
252
239
var commandFailedEvent = ( CommandFailedEvent ) _capturedEvents . Next ( ) ;
253
240
254
241
commandStartedEvent . CommandName . Should ( ) . Be ( command . GetElement ( 0 ) . Name ) ;
255
- commandStartedEvent . Command . ElementCount . Should ( ) . Be ( commandStartedCommandExpectedElementCount ) ;
242
+ commandStartedEvent . Command . Should ( ) . Be ( shouldBeRedacted ? new BsonDocument ( ) : command ) ;
256
243
commandStartedEvent . ConnectionId . Should ( ) . Be ( _subject . ConnectionId ) ;
257
244
commandStartedEvent . DatabaseNamespace . Should ( ) . Be ( MessageHelper . DefaultDatabaseNamespace ) ;
258
245
commandStartedEvent . OperationId . Should ( ) . Be ( EventContext . OperationId ) ;
@@ -265,7 +252,7 @@ public void Should_process_a_redacted_failed_command(string commandName)
265
252
commandFailedEvent . RequestId . Should ( ) . Be ( commandStartedEvent . RequestId ) ;
266
253
commandFailedEvent . Failure . Should ( ) . BeOfType < MongoCommandException > ( ) ;
267
254
var exception = ( MongoCommandException ) commandFailedEvent . Failure ;
268
- exception . Result . ElementCount . Should ( ) . Be ( commandSucceededCommandExpectedElementCount ) ;
255
+ exception . Result . Should ( ) . Be ( shouldBeRedacted ? new BsonDocument ( ) : reply ) ;
269
256
}
270
257
271
258
[ Fact ]
@@ -982,24 +969,5 @@ private void ReceiveMessages(params ReplyMessage<BsonDocument>[] messages)
982
969
_subject . ReceiveMessageAsync ( message . ResponseTo , encoderSelector , _messageEncoderSettings , CancellationToken . None ) . Wait ( ) ;
983
970
}
984
971
}
985
-
986
- private static BsonDocument ModifyMessageToTriggerConditionToRedact ( string commandName , BsonDocument command )
987
- {
988
- switch ( commandName )
989
- {
990
- case "isMaster" :
991
- command . Add ( "speculativeAuthenticate" , new BsonDocument ( "db" , "authSource" ) ) ;
992
- break ;
993
- }
994
-
995
- return command ;
996
- }
997
- }
998
-
999
- internal static class CommandEventHelperReflector
1000
- {
1001
- public static bool ShouldRedactMessage ( string commandName , BsonDocument command ) =>
1002
- ( bool ) Reflector . InvokeStatic ( typeof ( CommandEventHelper ) , nameof ( ShouldRedactMessage ) , commandName , command ) ;
1003
-
1004
972
}
1005
973
}
0 commit comments