Fail fast if both uri and client attributes are set

Previously, an URI and individual client attributes can be set to
configure the mongo client. In such a scenario, the URI is ignored.

This commit changes the URI to be "null" and the creation of the client
to fail if both the uri and client attributes are set. If no client
attributes are set, the uri is used as before.

Closes gh-6739
This commit is contained in:
Stephane Nicoll 2016-10-03 11:29:20 +02:00
parent 42adeda6a3
commit 8ad477537e
3 changed files with 53 additions and 13 deletions

View File

@ -50,20 +50,22 @@ public class MongoProperties {
*/
public static final int DEFAULT_PORT = 27017;
public static final String DEFAULT_URI = "mongodb://localhost/test";
/**
* Mongo server host.
* Mongo server host. Cannot be set with uri.
*/
private String host;
/**
* Mongo server port.
* Mongo server port. Cannot be set with uri.
*/
private Integer port = null;
/**
* Mongo database URI. When set, host and port are ignored.
* Mongo database URI. Cannot be set with host, port and credentials.
*/
private String uri = "mongodb://localhost/test";
private String uri;
/**
* Database name.
@ -81,12 +83,12 @@ public class MongoProperties {
private String gridFsDatabase;
/**
* Login user of the mongo server.
* Login user of the mongo server. Cannot be set with uri.
*/
private String username;
/**
* Login password of the mongo server.
* Login password of the mongo server. Cannot be set with uri.
*/
private char[] password;
@ -156,6 +158,10 @@ public class MongoProperties {
return this.uri;
}
public String determineUri() {
return (this.uri != null ? this.uri : DEFAULT_URI);
}
public void setUri(String uri) {
this.uri = uri;
}
@ -180,7 +186,7 @@ public class MongoProperties {
if (this.database != null) {
return this.database;
}
return new MongoClientURI(this.uri).getDatabase();
return new MongoClientURI(determineUri()).getDatabase();
}
/**
@ -198,6 +204,10 @@ public class MongoProperties {
Environment environment) throws UnknownHostException {
try {
if (hasCustomAddress() || hasCustomCredentials()) {
if (this.uri != null) {
throw new IllegalStateException("Invalid mongo configuration, " +
"either uri or host/port/credentials must be specified");
}
if (options == null) {
options = MongoClientOptions.builder().build();
}
@ -215,7 +225,7 @@ public class MongoProperties {
credentials, options);
}
// The options and credentials are in the URI
return new MongoClient(new MongoClientURI(this.uri, builder(options)));
return new MongoClient(new MongoClientURI(determineUri(), builder(options)));
}
finally {
clearPassword();

View File

@ -25,7 +25,9 @@ import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.connection.Cluster;
import com.mongodb.connection.ClusterSettings;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.util.EnvironmentTestUtils;
@ -40,9 +42,13 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Phillip Webb
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
public class MongoPropertiesTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void canBindCharArrayPassword() {
// gh-1572
@ -121,6 +127,30 @@ public class MongoPropertiesTests {
assertMongoCredential(credentialsList.get(0), "user", "secret", "test");
}
@Test
public void uriCannotBeSetWithCredentials() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setUri("mongodb://127.0.0.1:1234/mydb");
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Invalid mongo configuration, " +
"either uri or host/port/credentials must be specified");
properties.createMongoClient(null, null);
}
@Test
public void uriCannotBeSetWithHostPort() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setUri("mongodb://127.0.0.1:1234/mydb");
properties.setHost("localhost");
properties.setPort(4567);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Invalid mongo configuration, " +
"either uri or host/port/credentials must be specified");
properties.createMongoClient(null, null);
}
@Test
public void allMongoClientOptionsCanBeSet() throws UnknownHostException {
MongoClientOptions.Builder builder = MongoClientOptions.builder();

View File

@ -554,12 +554,12 @@ content into your application; rather pick only the properties that you need.
spring.data.mongodb.database=test # Database name.
spring.data.mongodb.field-naming-strategy= # Fully qualified name of the FieldNamingStrategy to use.
spring.data.mongodb.grid-fs-database= # GridFS database name.
spring.data.mongodb.host=localhost # Mongo server host.
spring.data.mongodb.password= # Login password of the mongo server.
spring.data.mongodb.port=27017 # Mongo server port.
spring.data.mongodb.host=localhost # Mongo server host. Cannot be set with uri.
spring.data.mongodb.password= # Login password of the mongo server. Cannot be set with uri.
spring.data.mongodb.port=27017 # Mongo server port. Cannot be set with uri.
spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories.
spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. When set, host and port are ignored.
spring.data.mongodb.username= # Login user of the mongo server.
spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. Cannot be set with host, port and credentials.
spring.data.mongodb.username= # Login user of the mongo server. Cannot be set with uri.
# DATA REDIS
spring.data.redis.repositories.enabled=true # Enable Redis repositories.