Commit 80111c80 authored by Andy Wilkinson's avatar Andy Wilkinson

Consider custom authentication database when creating MongoClient

Previously, MongoProperties did not consider the configuration of a
custom authentication database when creating a MongoClient. This
commit updates MongoProperties to use the authentication database
when it is configured, falling back to the normal database when it is
not configured.

Closes gh-2562
parent 1e3a0577
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -171,8 +171,10 @@ public class MongoProperties { ...@@ -171,8 +171,10 @@ public class MongoProperties {
} }
List<MongoCredential> credentials = null; List<MongoCredential> credentials = null;
if (hasCustomCredentials()) { if (hasCustomCredentials()) {
String database = this.authenticationDatabase == null ? getMongoClientDatabase()
: this.authenticationDatabase;
credentials = Arrays.asList(MongoCredential.createMongoCRCredential( credentials = Arrays.asList(MongoCredential.createMongoCRCredential(
this.username, getMongoClientDatabase(), this.password)); this.username, database, this.password));
} }
String host = this.host == null ? "localhost" : this.host; String host = this.host == null ? "localhost" : this.host;
int port = this.port == null ? DEFAULT_PORT : this.port; int port = this.port == null ? DEFAULT_PORT : this.port;
......
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -79,7 +79,28 @@ public class MongoPropertiesTests { ...@@ -79,7 +79,28 @@ public class MongoPropertiesTests {
properties.setUsername("user"); properties.setUsername("user");
properties.setPassword("secret".toCharArray()); properties.setPassword("secret".toCharArray());
MongoClient client = properties.createMongoClient(null); MongoClient client = properties.createMongoClient(null);
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret"); assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
"test");
}
@Test
public void databaseCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setDatabase("foo");
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoClient client = properties.createMongoClient(null);
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret", "foo");
}
@Test
public void authenticationDatabaseCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setAuthenticationDatabase("foo");
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoClient client = properties.createMongoClient(null);
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret", "foo");
} }
@Test @Test
...@@ -94,7 +115,7 @@ public class MongoPropertiesTests { ...@@ -94,7 +115,7 @@ public class MongoPropertiesTests {
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456); assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
List<MongoCredential> credentialsList = client.getCredentialsList(); List<MongoCredential> credentialsList = client.getCredentialsList();
assertEquals(1, credentialsList.size()); assertEquals(1, credentialsList.size());
assertMongoCredential(credentialsList.get(0), "user", "secret"); assertMongoCredential(credentialsList.get(0), "user", "secret", "test");
} }
private void assertServerAddress(ServerAddress serverAddress, String expectedHost, private void assertServerAddress(ServerAddress serverAddress, String expectedHost,
...@@ -104,9 +125,10 @@ public class MongoPropertiesTests { ...@@ -104,9 +125,10 @@ public class MongoPropertiesTests {
} }
private void assertMongoCredential(MongoCredential credentials, private void assertMongoCredential(MongoCredential credentials,
String expectedUsername, String expectedPassword) { String expectedUsername, String expectedPassword, String expectedSource) {
assertThat(credentials.getUserName(), equalTo(expectedUsername)); assertThat(credentials.getUserName(), equalTo(expectedUsername));
assertThat(credentials.getPassword(), equalTo(expectedPassword.toCharArray())); assertThat(credentials.getPassword(), equalTo(expectedPassword.toCharArray()));
assertThat(credentials.getSource(), equalTo(expectedSource));
} }
@Configuration @Configuration
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment