Skip to content

Commit 941d4d8

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1260 - Prevent accidental authentication misconfiguration on SimpleMongoDbFactory.
We now reject configuration using MongoClient along with UserCredentials in SimpleMongoDbFactory. This move favors the native authentication mechanism provided via MongoCredential. <mongo:mongo-client id="mongo-client-with-credentials" credentials="jon:warg@snow?uri.authMechanism=PLAIN" /> Original pull request: #309.
1 parent 44c76d8 commit 941d4d8

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.springframework.beans.factory.DisposableBean;
2121
import org.springframework.dao.DataAccessException;
22+
import org.springframework.dao.InvalidDataAccessApiUsageException;
2223
import org.springframework.dao.support.PersistenceExceptionTranslator;
2324
import org.springframework.data.authentication.UserCredentials;
2425
import org.springframework.data.mongodb.MongoDbFactory;
@@ -103,8 +104,8 @@ public SimpleMongoDbFactory(Mongo mongo, String databaseName, UserCredentials cr
103104
*/
104105
@Deprecated
105106
public SimpleMongoDbFactory(MongoURI uri) throws MongoException, UnknownHostException {
106-
this(new Mongo(uri), uri.getDatabase(), new UserCredentials(uri.getUsername(), parseChars(uri.getPassword())),
107-
true, uri.getDatabase());
107+
this(new Mongo(uri), uri.getDatabase(), new UserCredentials(uri.getUsername(), parseChars(uri.getPassword())), true,
108+
uri.getDatabase());
108109
}
109110

110111
/**
@@ -132,6 +133,11 @@ public SimpleMongoDbFactory(MongoClient mongoClient, String databaseName) {
132133
private SimpleMongoDbFactory(Mongo mongo, String databaseName, UserCredentials credentials,
133134
boolean mongoInstanceCreated, String authenticationDatabaseName) {
134135

136+
if (mongo instanceof MongoClient && (credentials != null && !UserCredentials.NO_CREDENTIALS.equals(credentials))) {
137+
throw new InvalidDataAccessApiUsageException(
138+
"Usage of 'UserCredentials' with 'MongoClient' is no longer supported. Please use 'MongoCredential' for 'MongoClient' or just 'Mongo'.");
139+
}
140+
135141
Assert.notNull(mongo, "Mongo must not be null");
136142
Assert.hasText(databaseName, "Database name must not be empty");
137143
Assert.isTrue(databaseName.matches("[\\w-]+"),

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/SimpleMongoDbFactoryUnitTests.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222

2323
import java.net.UnknownHostException;
2424

25+
import org.junit.Rule;
2526
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
2628
import org.junit.runner.RunWith;
2729
import org.mockito.Mock;
2830
import org.mockito.runners.MockitoJUnitRunner;
31+
import org.springframework.dao.InvalidDataAccessApiUsageException;
2932
import org.springframework.data.authentication.UserCredentials;
3033
import org.springframework.data.mongodb.MongoDbFactory;
3134

@@ -43,6 +46,7 @@
4346
@RunWith(MockitoJUnitRunner.class)
4447
public class SimpleMongoDbFactoryUnitTests {
4548

49+
public @Rule ExpectedException expectedException = ExpectedException.none();
4650
@Mock Mongo mongo;
4751

4852
/**
@@ -115,6 +119,46 @@ public void shouldDefaultAuthenticationDbNameToDbNameWhenUsingMongoClient() thro
115119
assertThat(getField(factory, "authenticationDatabaseName").toString(), is("FooBar"));
116120
}
117121

122+
/**
123+
* @see DATAMONGO-1260
124+
*/
125+
@Test
126+
public void rejectsMongoClientWithUserCredentials() {
127+
128+
expectedException.expect(InvalidDataAccessApiUsageException.class);
129+
expectedException.expectMessage("use 'MongoCredential' for 'MongoClient'");
130+
131+
new SimpleMongoDbFactory(mock(MongoClient.class), "cairhienin", new UserCredentials("moiraine", "sedai"));
132+
}
133+
134+
/**
135+
* @see DATAMONGO-1260
136+
*/
137+
@Test
138+
public void rejectsMongoClientWithUserCredentialsAndAuthDb() {
139+
140+
expectedException.expect(InvalidDataAccessApiUsageException.class);
141+
expectedException.expectMessage("use 'MongoCredential' for 'MongoClient'");
142+
143+
new SimpleMongoDbFactory(mock(MongoClient.class), "malkieri", new UserCredentials("lan", "mandragoran"), "authdb");
144+
}
145+
146+
/**
147+
* @see DATAMONGO-1260
148+
*/
149+
@Test
150+
public void shouldNotRejectMongoClientWithNoCredentials() {
151+
new SimpleMongoDbFactory(mock(MongoClient.class), "andoran", UserCredentials.NO_CREDENTIALS);
152+
}
153+
154+
/**
155+
* @see DATAMONGO-1260
156+
*/
157+
@Test
158+
public void shouldNotRejectMongoClientWithEmptyUserCredentials() {
159+
new SimpleMongoDbFactory(mock(MongoClient.class), "shangtai", new UserCredentials("", ""));
160+
}
161+
118162
@SuppressWarnings("deprecation")
119163
private void rejectsDatabaseName(String databaseName) {
120164

0 commit comments

Comments
 (0)