Skip to content

Commit 96747b2

Browse files
committed
JAVA-436: If a database is not specified in the URI but credentials are, authenticate to the admin database
1 parent 4874df2 commit 96747b2

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/main/com/mongodb/Mongo.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ public class Mongo {
9090
private static final String FULL_VERSION = "2.10.0-SNAPSHOT";
9191

9292
static int cleanerIntervalMS;
93+
94+
private static final String ADMIN_DATABASE_NAME = "admin";
95+
9396
static {
9497
cleanerIntervalMS = Integer.parseInt(System.getProperty("com.mongodb.cleanerIntervalMS", "1000"));
9598
}
@@ -328,8 +331,15 @@ public Mongo( MongoURI uri )
328331
_connector = new DBTCPConnector( this , replicaSetSeeds );
329332
}
330333

331-
if (uri.getDatabase() != null && uri.getUsername() != null) {
332-
DB db = new DBApiLayer(this, uri.getDatabase() , _connector, uri.getUsername(), uri.getPassword());
334+
if (uri.getUsername() != null) {
335+
String databaseName;
336+
if (uri.getDatabase() != null) {
337+
databaseName = uri.getDatabase();
338+
} else {
339+
databaseName = ADMIN_DATABASE_NAME;
340+
}
341+
342+
DB db = new DBApiLayer(this, databaseName, _connector, uri.getUsername(), uri.getPassword());
333343
_dbs.put(db.getName(), db);
334344
}
335345

@@ -380,7 +390,7 @@ public List<String> getDatabaseNames(){
380390
cmd.put("listDatabases", 1);
381391

382392

383-
CommandResult res = getDB( "admin" ).command(cmd, getOptions());
393+
CommandResult res = getDB(ADMIN_DATABASE_NAME).command(cmd, getOptions());
384394
res.throwOnError();
385395

386396
List l = (List)res.get("databases");
@@ -648,7 +658,7 @@ public CommandResult fsync(boolean async) {
648658
if (async) {
649659
cmd.put("async", 1);
650660
}
651-
return getDB("admin").command(cmd);
661+
return getDB(ADMIN_DATABASE_NAME).command(cmd);
652662
}
653663

654664
/**
@@ -660,7 +670,7 @@ public CommandResult fsync(boolean async) {
660670
public CommandResult fsyncAndLock() {
661671
DBObject cmd = new BasicDBObject("fsync", 1);
662672
cmd.put("lock", 1);
663-
return getDB("admin").command(cmd);
673+
return getDB(ADMIN_DATABASE_NAME).command(cmd);
664674
}
665675

666676
/**
@@ -670,7 +680,7 @@ public CommandResult fsyncAndLock() {
670680
* @throws MongoException
671681
*/
672682
public DBObject unlock() {
673-
DB db = getDB("admin");
683+
DB db = getDB(ADMIN_DATABASE_NAME);
674684
DBCollection col = db.getCollection("$cmd.sys.unlock");
675685
return col.findOne();
676686
}
@@ -681,7 +691,7 @@ public DBObject unlock() {
681691
* @throws MongoException
682692
*/
683693
public boolean isLocked() {
684-
DB db = getDB("admin");
694+
DB db = getDB(ADMIN_DATABASE_NAME);
685695
DBCollection col = db.getCollection("$cmd.sys.inprog");
686696
BasicDBObject res = (BasicDBObject) col.findOne();
687697
if (res.containsField("fsyncLock")) {

src/test/com/mongodb/JavaClientTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,35 @@ public void testAuthenticateCommand() throws UnknownHostException {
705705
}
706706
}
707707

708+
@Test
709+
public void testAuthenticateWithCredentialsInURIAndNoDatabase() throws UnknownHostException {
710+
// First add the user
711+
Mongo m = new Mongo(new MongoURI("mongodb://localhost"));
712+
DB db = m.getDB("admin");
713+
DBCollection u = db.getCollection( "system.users" );
714+
try {
715+
assertEquals( 0 , u.find().count() );
716+
717+
db.addUser( "xx" , "e".toCharArray() );
718+
assertEquals( 1 , u.find().count() );
719+
}
720+
finally {
721+
m.close();
722+
}
723+
724+
m = new Mongo(new MongoURI("mongodb://xx:e@localhost"));
725+
db = m.getDB("admin");
726+
727+
try {
728+
assertNotNull(db.getAuthenticationCredentials());
729+
assertEquals(true, db.authenticate("xx", "e".toCharArray()) );
730+
}
731+
finally {
732+
db.getCollection( "system.users" ).remove(new BasicDBObject());
733+
m.close();
734+
}
735+
}
736+
708737
@Test
709738
public void testAuthenticateWithCredentialsInURI() throws UnknownHostException {
710739
// First add the user
@@ -725,6 +754,7 @@ public void testAuthenticateWithCredentialsInURI() throws UnknownHostException {
725754
db = m.getDB(cleanupDB);
726755

727756
try {
757+
assertNotNull(db.getAuthenticationCredentials());
728758
assertEquals(true, db.authenticate("xx", "e".toCharArray()) );
729759
}
730760
finally {
@@ -753,6 +783,7 @@ public void testAuthenticateCommandWithCredentialsInURI() throws UnknownHostExce
753783
db = m.getDB(cleanupDB);
754784

755785
try {
786+
assertNotNull(db.getAuthenticationCredentials());
756787
assertTrue(db.authenticateCommand("xx", "e".toCharArray()).ok());
757788
}
758789
finally {

0 commit comments

Comments
 (0)