Skip to content

Commit 79f4e6f

Browse files
committed
JAVA-2403: Don't require a username in the connection string for MONGODB-X509 authentication mechanism
For all other mechanisms throw an IllegalArgumentException if there is no username
1 parent 26d4bce commit 79f4e6f

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

driver-core/src/main/com/mongodb/ConnectionString.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
import java.util.Set;
3131
import java.util.concurrent.TimeUnit;
3232

33-
import static com.mongodb.AuthenticationMechanism.GSSAPI;
34-
import static com.mongodb.AuthenticationMechanism.MONGODB_CR;
35-
import static com.mongodb.AuthenticationMechanism.MONGODB_X509;
36-
import static com.mongodb.AuthenticationMechanism.PLAIN;
37-
import static com.mongodb.AuthenticationMechanism.SCRAM_SHA_1;
3833
import static java.lang.String.format;
3934
import static java.util.Arrays.asList;
4035
import static java.util.Collections.singletonList;
@@ -474,10 +469,6 @@ private ReadPreference createReadPreference(final Map<String, List<String>> opti
474469

475470
private MongoCredential createCredentials(final Map<String, List<String>> optionsMap, final String userName,
476471
final char[] password) {
477-
if (userName == null) {
478-
return null;
479-
}
480-
481472
AuthenticationMechanism mechanism = null;
482473
String authSource = (database == null) ? "admin" : database;
483474
String gssapiServiceName = null;
@@ -501,28 +492,38 @@ private MongoCredential createCredentials(final Map<String, List<String>> option
501492
}
502493
}
503494

504-
MongoCredential credential;
505-
if (mechanism == GSSAPI) {
506-
credential = MongoCredential.createGSSAPICredential(userName);
507-
if (gssapiServiceName != null) {
508-
credential = credential.withMechanismProperty("SERVICE_NAME", gssapiServiceName);
495+
496+
MongoCredential credential = null;
497+
if (mechanism != null) {
498+
switch (mechanism) {
499+
case GSSAPI:
500+
credential = MongoCredential.createGSSAPICredential(userName);
501+
if (gssapiServiceName != null) {
502+
credential = credential.withMechanismProperty("SERVICE_NAME", gssapiServiceName);
503+
}
504+
break;
505+
case PLAIN:
506+
credential = MongoCredential.createPlainCredential(userName, authSource, password);
507+
break;
508+
case MONGODB_CR:
509+
credential = MongoCredential.createMongoCRCredential(userName, authSource, password);
510+
break;
511+
case MONGODB_X509:
512+
credential = MongoCredential.createMongoX509Credential(userName);
513+
break;
514+
case SCRAM_SHA_1:
515+
credential = MongoCredential.createScramSha1Credential(userName, authSource, password);
516+
break;
517+
default:
518+
throw new UnsupportedOperationException(format("The connection string contains an invalid authentication mechanism'. "
519+
+ "'%s' is not a supported authentication mechanism",
520+
mechanism));
509521
}
510-
} else if (mechanism == PLAIN) {
511-
credential = MongoCredential.createPlainCredential(userName, authSource, password);
512-
} else if (mechanism == MONGODB_CR) {
513-
credential = MongoCredential.createMongoCRCredential(userName, authSource, password);
514-
} else if (mechanism == MONGODB_X509) {
515-
credential = MongoCredential.createMongoX509Credential(userName);
516-
} else if (mechanism == SCRAM_SHA_1) {
517-
credential = MongoCredential.createScramSha1Credential(userName, authSource, password);
518-
} else if (mechanism == null) {
522+
} else if (userName != null) {
519523
credential = MongoCredential.createCredential(userName, authSource, password);
520-
} else {
521-
throw new UnsupportedOperationException(format("The connection string contains an invalid authentication mechanism'. "
522-
+ "'%s' is not a supported authentication mechanism", mechanism));
523524
}
524525

525-
if (authMechanismProperties != null) {
526+
if (credential != null && authMechanismProperties != null) {
526527
for (String part : authMechanismProperties.split(",")) {
527528
String[] mechanismPropertyKeyValue = part.split(":");
528529
if (mechanismPropertyKeyValue.length != 2) {

driver-core/src/test/unit/com/mongodb/ConnectionStringSpecification.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ class ConnectionStringSpecification extends Specification {
181181
'contains tags and primary mode' | 'mongodb://localhost:27017/?readPreference=primary&readPreferenceTags=dc:ny'
182182
'contains max staleness and primary mode' | 'mongodb://localhost:27017/?readPreference=primary&maxStalenessSeconds=100'
183183
'contains non-integral max staleness' | 'mongodb://localhost:27017/?readPreference=secondary&maxStalenessSeconds=100.0'
184+
'contains GSSAPI mechanism with no user' | 'mongodb://localhost:27017/?authMechanism=GSSAPI'
185+
'contains SCRAM mechanism with no user' | 'mongodb://localhost:27017/?authMechanism=SCRAM-SHA-1'
186+
'contains MONGODB mechanism with no user' | 'mongodb://localhost:27017/?authMechanism=MONGODB-CR'
187+
'contains PLAIN mechanism with no user' | 'mongodb://localhost:27017/?authMechanism=PLAIN'
184188
}
185189

186190
def 'should have correct defaults for options'() {
@@ -228,6 +232,8 @@ class ConnectionStringSpecification extends Specification {
228232
'authMechanism=PLAIN') | asList(createPlainCredential('jeff', 'admin', '123'.toCharArray()))
229233
new ConnectionString('mongodb://jeff@localhost/?' +
230234
'authMechanism=MONGODB-X509') | asList(createMongoX509Credential('jeff'))
235+
new ConnectionString('mongodb://localhost/?' +
236+
'authMechanism=MONGODB-X509') | asList(createMongoX509Credential())
231237
new ConnectionString('mongodb://jeff@localhost/?' +
232238
'authMechanism=GSSAPI' +
233239
'&gssapiServiceName=foo') | asList(createGSSAPICredential('jeff')
@@ -253,6 +259,18 @@ class ConnectionStringSpecification extends Specification {
253259
.withMechanismProperty('SERVICE_REALM', 'AWESOME'))
254260
}
255261

262+
def 'should ignore authSource if there is no credential'() {
263+
expect:
264+
new ConnectionString('mongodb://localhost/?authSource=test').credentialList == []
265+
266+
}
267+
268+
def 'should ignore authMechanismProperties if there is no credential'() {
269+
expect:
270+
new ConnectionString('mongodb://localhost/?&authMechanismProperties=SERVICE_REALM:AWESOME').credentialList == []
271+
272+
}
273+
256274
@Unroll
257275
def 'should create immutable credential list'() {
258276
when:

0 commit comments

Comments
 (0)