Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
.DS_Store
.vagrant
target
*.iml
.idea
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ static List<ServerAddress> createServerAddressList( List<Server> seeds ) {

static List<MongoCredential> createCredentials( MongoConfiguration configuration ) {
Credentials credentialConfig = configuration.getCredentials();
final String authenticationDatabase = Optional.ofNullable(configuration.getAuthenticationDatabase())
.orElse(configuration.getDatabase());
List<MongoCredential> credentials = credentialConfig == null ?
Collections.<MongoCredential>emptyList() :
Collections.singletonList(MongoCredential.createCredential(credentialConfig.getUserName(), configuration.getDatabase(), credentialConfig
Collections.singletonList(MongoCredential.createCredential(credentialConfig.getUserName(), authenticationDatabase, credentialConfig
.getPassword().toCharArray()));

if( credentials.isEmpty() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class MongoConfiguration {
protected String database;
protected boolean enabled = true;
protected Boolean alwaysUseMBeans;
protected String authenticationDatabase;
protected Integer connectionsPerHost;
protected Integer connectTimeout;
protected Boolean cursorFinalizerEnabled;
Expand Down Expand Up @@ -95,6 +96,12 @@ public void setDatabase( String database ) {
this.database = database;
}

public String getAuthenticationDatabase() {
return authenticationDatabase;
}
public void setAuthenticationDatabase( String authenticationDatabase ) {
this.authenticationDatabase = authenticationDatabase;
}
public Boolean getAlwaysUseMBeans() {
return alwaysUseMBeans;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
*/
package com.meltmedia.dropwizard.mongo.junit;

import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import com.mongodb.MongoClient;

import java.util.Arrays;
import java.util.List;

/**
* A JUnit Rule for creating a mongo client. This rule is designed to be used as
* a ClassRule.
Expand All @@ -33,12 +38,26 @@ public class MongoRule
MongoClient mongoClient;
String host;
int port;
String user;
String password;
String authenticationDatabase;

public MongoRule( String host, int port ) {
this.host = host;
this.port = port;
}

public MongoRule( String host, int port , String user, String password ) {
this(host, port);
this.user = user;
this.password = password;
}

public MongoRule( String host, int port, String user, String password, String authenticationDatabase ) {
this(host, port, user, password);
this.authenticationDatabase = authenticationDatabase;
}

/**
* Return an instance of the mongo client when this rule is in effect.
*
Expand All @@ -58,7 +77,10 @@ public Statement apply( final Statement statement, final Description description
@Override
public void evaluate() throws Throwable {
try {
mongoClient = new MongoClient(host, port);
final MongoCredential credential = MongoCredential.createCredential(user,authenticationDatabase, password.toCharArray());
final List<MongoCredential> creds = Arrays.asList(credential);
final ServerAddress dbAddress = new ServerAddress(host, port);
mongoClient = new MongoClient(dbAddress, creds);
statement.evaluate();
} finally {
if( mongoClient != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
public class RxOplogServiceIT {
@ClassRule
public static MongoRule mongo = new MongoRule("localhost", 27017);
public static MongoRule mongo = new MongoRule("localhost", 27017, "admin", "password", "admin");

@Test
public void clientWorks() {
Expand Down
1 change: 1 addition & 0 deletions example/conf/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mongo:
- host: "localhost"
port: 27017
database: "example"
authenticationDatabase: "admin"
credentials:
userName: "example"
password:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import io.dropwizard.setup.Environment;

public class JongoBundleIT {
@ClassRule public static MongoRule mongoRule = new MongoRule("localhost", 27017);
@ClassRule public static MongoRule mongoRule = new MongoRule("localhost", 27017, "admin", "password", "admin");

Bootstrap<?> bootstrap = mock(Bootstrap.class);
Configuration configuration = mock(Configuration.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
*/
public class ObjectIdMappingIT {
@ClassRule public static MongoRule mongoRule = new MongoRule("localhost", 27017);
@ClassRule public static MongoRule mongoRule = new MongoRule("localhost", 27017, "admin", "password", "admin");

public abstract class ObjectIdOnly {
@org.jongo.marshall.jackson.oid.MongoObjectId
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<artifactId>dropwizard-mongo-parent</artifactId>
<version>0.6.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Dropwizard Mogno Parent</name>
<name>Dropwizard Mongo Parent</name>
<description>A Dropwizard Bundle for accessing Mongo.</description>
<url>http://github.com/meltmedia/dropwizard-mongo</url>
<inceptionYear>2014</inceptionYear>
Expand Down Expand Up @@ -237,7 +237,7 @@
<plugin>
<groupId>org.skife.maven</groupId>
<artifactId>really-executable-jar-maven-plugin</artifactId>
<version>1.0.5</version>
<version>1.5.0</version>
<configuration>
<programFile>${program.file}</programFile>
<flags>-Djava.net.preferIPv4Stack=true</flags>
Expand Down