Skip to content

Commit f253f90

Browse files
committed
Ensure redacted commands are logged with elision
JAVA-4195
1 parent d7b19c8 commit f253f90

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ class LoggingCommandEventSender implements CommandEventSender {
6666
@Override
6767
public void sendStartedEvent() {
6868
if (loggingRequired()) {
69+
String commandString = redactionRequired ? String.format("{\"%s\": ...", commandName) : getTruncatedJsonCommand();
6970
logger.debug(
7071
format("Sending command '%s' with request id %d to database %s on connection [%s] to server %s",
71-
getTruncatedJsonCommand(), message.getId(),
72+
commandString, message.getId(),
7273
message.getNamespace().getDatabaseName(), description.getConnectionId(), description.getServerAddress()));
7374
}
7475

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,32 @@ class LoggingCommandEventSenderSpecification extends Specification {
155155
'to server 127.0.0.1:27017'
156156
}
157157
}
158+
159+
def 'should log redacted command with ellipses'() {
160+
given:
161+
def connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()))
162+
def namespace = new MongoNamespace('test.driver')
163+
def messageSettings = MessageSettings.builder().maxWireVersion(THREE_DOT_SIX_WIRE_VERSION).build()
164+
def commandDocument = new BsonDocument('createUser', new BsonString('private'))
165+
def message = new CommandMessage(namespace, commandDocument, new NoOpFieldNameValidator(), ReadPreference.primary(),
166+
messageSettings, null)
167+
def bsonOutput = new ByteBufferBsonOutput(new SimpleBufferProvider())
168+
message.encode(bsonOutput, NoOpSessionContext.INSTANCE)
169+
def logger = Mock(Logger) {
170+
isDebugEnabled() >> true
171+
}
172+
def sender = new LoggingCommandEventSender(['createUser'] as Set, [] as Set, connectionDescription, null, message, bsonOutput,
173+
logger)
174+
175+
when:
176+
sender.sendStartedEvent()
177+
178+
then:
179+
1 * logger.debug {
180+
it == "Sending command \'{\"createUser\": ...\' " +
181+
"with request id ${message.getId()} to database test " +
182+
"on connection [connectionId{localValue:${connectionDescription.connectionId.localValue}}] " +
183+
'to server 127.0.0.1:27017'
184+
}
185+
}
158186
}

0 commit comments

Comments
 (0)