Skip to content

Commit d7b19c8

Browse files
committed
Ensure command event for hello with speculativeAuthenticate is elided
JAVA-4195
1 parent 26bf644 commit d7b19c8

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnection.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public class InternalStreamConnection implements InternalConnection {
9090
"copydbsaslstart",
9191
"copydb"));
9292

93+
private static final Set<String> SECURITY_SENSITIVE_HELLO_COMMANDS = new HashSet<String>(asList(
94+
"hello",
95+
"ismaster",
96+
"isMaster"));
97+
9398
private static final Logger LOGGER = Loggers.getLogger("connection");
9499

95100
private final ClusterConnectionMode clusterConnectionMode;
@@ -787,8 +792,8 @@ public void onResult(final ByteBuf result, final Throwable t) {
787792

788793
private CommandEventSender createCommandEventSender(final CommandMessage message, final ByteBufferBsonOutput bsonOutput) {
789794
if (opened() && (commandListener != null || COMMAND_PROTOCOL_LOGGER.isDebugEnabled())) {
790-
return new LoggingCommandEventSender(SECURITY_SENSITIVE_COMMANDS, description, commandListener, message, bsonOutput,
791-
COMMAND_PROTOCOL_LOGGER);
795+
return new LoggingCommandEventSender(SECURITY_SENSITIVE_COMMANDS, SECURITY_SENSITIVE_HELLO_COMMANDS, description,
796+
commandListener, message, bsonOutput, COMMAND_PROTOCOL_LOGGER);
792797
} else {
793798
return new NoOpCommandEventSender();
794799
}

driver-core/src/main/com/mongodb/internal/connection/LoggingCommandEventSender.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,28 @@
3939
class LoggingCommandEventSender implements CommandEventSender {
4040
private static final int MAX_COMMAND_DOCUMENT_LENGTH_TO_LOG = 1000;
4141

42-
private final Set<String> securitySensitiveCommands;
4342
private final ConnectionDescription description;
4443
private final CommandListener commandListener;
4544
private final Logger logger;
4645
private final long startTimeNanos;
4746
private final CommandMessage message;
4847
private final String commandName;
4948
private volatile BsonDocument commandDocument;
49+
private final boolean redactionRequired;
5050

51-
LoggingCommandEventSender(final Set<String> securitySensitiveCommands, final ConnectionDescription description,
51+
LoggingCommandEventSender(final Set<String> securitySensitiveCommands, final Set<String> securitySensitiveHelloCommands,
52+
final ConnectionDescription description,
5253
final CommandListener commandListener, final CommandMessage message,
5354
final ByteBufferBsonOutput bsonOutput, final Logger logger) {
54-
this.securitySensitiveCommands = securitySensitiveCommands;
5555
this.description = description;
5656
this.commandListener = commandListener;
5757
this.logger = logger;
5858
this.startTimeNanos = System.nanoTime();
5959
this.message = message;
6060
this.commandDocument = message.getCommandDocument(bsonOutput);
6161
this.commandName = commandDocument.getFirstKey();
62+
this.redactionRequired = securitySensitiveCommands.contains(commandName)
63+
|| (securitySensitiveHelloCommands.contains(commandName) && commandDocument.containsKey("speculativeAuthenticate"));
6264
}
6365

6466
@Override
@@ -71,7 +73,7 @@ public void sendStartedEvent() {
7173
}
7274

7375
if (eventRequired()) {
74-
BsonDocument commandDocumentForEvent = (securitySensitiveCommands.contains(commandName))
76+
BsonDocument commandDocumentForEvent = redactionRequired
7577
? new BsonDocument() : commandDocument;
7678

7779
sendCommandStartedEvent(message, message.getNamespace().getDatabaseName(),
@@ -105,7 +107,7 @@ private String getTruncatedJsonCommand() {
105107
@Override
106108
public void sendFailedEvent(final Throwable t) {
107109
Throwable commandEventException = t;
108-
if (t instanceof MongoCommandException && (securitySensitiveCommands.contains(commandName))) {
110+
if (t instanceof MongoCommandException && redactionRequired) {
109111
commandEventException = new MongoCommandException(new BsonDocument(), description.getServerAddress());
110112
}
111113
long elapsedTimeNanos = System.nanoTime() - startTimeNanos;
@@ -136,7 +138,7 @@ public void sendSucceededEvent(final ResponseBuffers responseBuffers) {
136138
}
137139

138140
if (eventRequired()) {
139-
BsonDocument responseDocumentForEvent = (securitySensitiveCommands.contains(commandName))
141+
BsonDocument responseDocumentForEvent = redactionRequired
140142
? new BsonDocument()
141143
: responseBuffers.getResponseDocument(message.getId(), new RawBsonDocumentCodec());
142144
sendCommandSucceededEvent(message, commandName, responseDocumentForEvent, description,

driver-core/src/test/unit/com/mongodb/internal/connection/InternalStreamConnectionSpecification.groovy

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,10 @@ class InternalStreamConnectionSpecification extends Specification {
674674
new BsonDocument('updateUser', new BsonInt32(1)),
675675
new BsonDocument('copydbgetnonce', new BsonInt32(1)),
676676
new BsonDocument('copydbsaslstart', new BsonInt32(1)),
677-
new BsonDocument('copydb', new BsonInt32(1))
677+
new BsonDocument('copydb', new BsonInt32(1)),
678+
new BsonDocument('hello', new BsonInt32(1)).append('speculativeAuthenticate', new BsonDocument()),
679+
new BsonDocument('ismaster', new BsonInt32(1)).append('speculativeAuthenticate', new BsonDocument()),
680+
new BsonDocument('isMaster', new BsonInt32(1)).append('speculativeAuthenticate', new BsonDocument())
678681
]
679682
}
680683

@@ -707,7 +710,10 @@ class InternalStreamConnectionSpecification extends Specification {
707710
new BsonDocument('updateUser', new BsonInt32(1)),
708711
new BsonDocument('copydbgetnonce', new BsonInt32(1)),
709712
new BsonDocument('copydbsaslstart', new BsonInt32(1)),
710-
new BsonDocument('copydb', new BsonInt32(1))
713+
new BsonDocument('copydb', new BsonInt32(1)),
714+
new BsonDocument('hello', new BsonInt32(1)).append('speculativeAuthenticate', new BsonDocument()),
715+
new BsonDocument('ismaster', new BsonInt32(1)).append('speculativeAuthenticate', new BsonDocument()),
716+
new BsonDocument('isMaster', new BsonInt32(1)).append('speculativeAuthenticate', new BsonDocument())
711717
]
712718
}
713719

@@ -894,7 +900,10 @@ class InternalStreamConnectionSpecification extends Specification {
894900
new BsonDocument('updateUser', new BsonInt32(1)),
895901
new BsonDocument('copydbgetnonce', new BsonInt32(1)),
896902
new BsonDocument('copydbsaslstart', new BsonInt32(1)),
897-
new BsonDocument('copydb', new BsonInt32(1))
903+
new BsonDocument('copydb', new BsonInt32(1)),
904+
new BsonDocument('hello', new BsonInt32(1)).append('speculativeAuthenticate', new BsonDocument()),
905+
new BsonDocument('ismaster', new BsonInt32(1)).append('speculativeAuthenticate', new BsonDocument()),
906+
new BsonDocument('isMaster', new BsonInt32(1)).append('speculativeAuthenticate', new BsonDocument())
898907
]
899908
}
900909

driver-core/src/test/unit/com/mongodb/internal/connection/LoggingCommandEventSenderSpecification.groovy

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class LoggingCommandEventSenderSpecification extends Specification {
5555
def logger = Stub(Logger) {
5656
isDebugEnabled() >> debugLoggingEnabled
5757
}
58-
def sender = new LoggingCommandEventSender([] as Set, connectionDescription, commandListener, message, bsonOutput, logger)
58+
def sender = new LoggingCommandEventSender([] as Set, [] as Set, connectionDescription, commandListener, message, bsonOutput,
59+
logger)
5960

6061
when:
6162
sender.sendStartedEvent()
@@ -94,7 +95,8 @@ class LoggingCommandEventSenderSpecification extends Specification {
9495
def logger = Mock(Logger) {
9596
isDebugEnabled() >> true
9697
}
97-
def sender = new LoggingCommandEventSender([] as Set, connectionDescription, commandListener, message, bsonOutput, logger)
98+
def sender = new LoggingCommandEventSender([] as Set, [] as Set, connectionDescription, commandListener, message, bsonOutput,
99+
logger)
98100
when:
99101
sender.sendStartedEvent()
100102
sender.sendSucceededEventForOneWayCommand()
@@ -140,7 +142,7 @@ class LoggingCommandEventSenderSpecification extends Specification {
140142
def logger = Mock(Logger) {
141143
isDebugEnabled() >> true
142144
}
143-
def sender = new LoggingCommandEventSender([] as Set, connectionDescription, null, message, bsonOutput, logger)
145+
def sender = new LoggingCommandEventSender([] as Set, [] as Set, connectionDescription, null, message, bsonOutput, logger)
144146

145147
when:
146148
sender.sendStartedEvent()

driver-reactive-streams/src/test/functional/com/mongodb/reactivestreams/client/unified/CommandMonitoringTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@
3030
import java.net.URISyntaxException;
3131
import java.util.Collection;
3232

33-
import static org.junit.Assume.assumeFalse;
34-
3533
public class CommandMonitoringTest extends UnifiedTest {
3634
public CommandMonitoringTest(@SuppressWarnings("unused") final String fileDescription,
3735
@SuppressWarnings("unused") final String testDescription,
3836
final String schemaVersion, @Nullable final BsonArray runOnRequirements, final BsonArray entities,
3937
final BsonArray initialData, final BsonDocument definition) {
4038
super(schemaVersion, runOnRequirements, entities, initialData, definition);
41-
assumeFalse(testDescription.equals("hello with speculative authenticate"));
4239
}
4340

4441
@Override

driver-sync/src/test/functional/com/mongodb/client/unified/CommandMonitoringTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import java.net.URISyntaxException;
2929
import java.util.Collection;
3030

31-
import static org.junit.Assume.assumeFalse;
32-
3331
public class CommandMonitoringTest extends UnifiedTest {
3432

3533

@@ -39,7 +37,6 @@ public CommandMonitoringTest(@SuppressWarnings("unused") final String fileDescri
3937
@Nullable final BsonArray runOnRequirements, final BsonArray entities, final BsonArray initialData,
4038
final BsonDocument definition) {
4139
super(schemaVersion, runOnRequirements, entities, initialData, definition);
42-
assumeFalse(testDescription.equals("hello with speculative authenticate"));
4340
}
4441

4542
@Override

0 commit comments

Comments
 (0)