Skip to content

Commit e27e3db

Browse files
committed
JAVA-2564: Don't use implicit sessions when more than one user is authenticated
1 parent 752eb26 commit e27e3db

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

driver/src/main/com/mongodb/Mongo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ ClientSession getClientSession(final ClientSession clientSessionFromOperation) {
857857
}
858858

859859
ClientSession createClientSession(final ClientSessionOptions options) {
860-
if (cluster.getDescription().getLogicalSessionTimeoutMinutes() != null) {
860+
if (cluster.getDescription().getLogicalSessionTimeoutMinutes() != null && credentialsList.size() < 2) {
861861
return new ClientSessionImpl(this, options);
862862
} else {
863863
return null;

driver/src/test/functional/com/mongodb/MongoClientSessionSpecification.groovy

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ import spock.lang.IgnoreIf
3030

3131
import java.util.concurrent.TimeUnit
3232

33+
import static com.mongodb.ClusterFixture.isAuthenticated
3334
import static com.mongodb.ClusterFixture.isStandalone
3435
import static com.mongodb.ClusterFixture.serverVersionAtLeast
3536
import static com.mongodb.Fixture.getDefaultDatabaseName
3637
import static com.mongodb.Fixture.getMongoClientURI
38+
import static com.mongodb.MongoCredential.createCredential
3739

3840
class MongoClientSessionSpecification extends FunctionalSpecification {
3941

@@ -235,7 +237,7 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
235237
def commandListener = new TestCommandListener()
236238
def optionsBuilder = MongoClientOptions.builder()
237239
.addCommandListener(commandListener)
238-
def client = new MongoClient(getMongoClientURI(optionsBuilder))
240+
def client = new MongoClient(Fixture.getMongoClientURI(optionsBuilder))
239241
// TODO: Remove this once SPEC-944 is resolved
240242
client.getDatabase('admin').runCommand(new BsonDocument('ping', new BsonInt32(1)))
241243

@@ -307,4 +309,33 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
307309
where:
308310
readConcern << [ReadConcern.DEFAULT, ReadConcern.LOCAL, ReadConcern.MAJORITY]
309311
}
312+
313+
@IgnoreIf({ !serverVersionAtLeast(3, 5) || !isAuthenticated() })
314+
@SuppressWarnings('deprecation')
315+
def 'should not use a default session when there is more than one authenticated user'() {
316+
given:
317+
def sessionTestUserName = 'sessionTestUser'
318+
def sessionTestPassword = 'sessionTestPassword'
319+
Fixture.getMongoClient().getDB('admin').addUser(sessionTestUserName, sessionTestPassword.toCharArray())
320+
321+
def commandListener = new TestCommandListener()
322+
def optionsBuilder = MongoClientOptions.builder()
323+
.addCommandListener(commandListener)
324+
def mongoClientURI = getMongoClientURI(optionsBuilder)
325+
def credentials = [mongoClientURI.getCredentials(),
326+
createCredential(sessionTestUserName, 'admin', sessionTestPassword.toCharArray())]
327+
def client = new MongoClient(mongoClientURI.getHosts().collect { new ServerAddress(it) },
328+
credentials, mongoClientURI.getOptions())
329+
330+
when:
331+
client.getDatabase('admin').runCommand(new BsonDocument('ping', new BsonInt32(1)))
332+
333+
then:
334+
def pingCommandStartedEvent = commandListener.events.get(0)
335+
!(pingCommandStartedEvent as CommandStartedEvent).command.containsKey('lsid')
336+
337+
cleanup:
338+
Fixture.getMongoClient().getDB('admin').removeUser(sessionTestUserName)
339+
client?.close()
340+
}
310341
}

0 commit comments

Comments
 (0)