Search in sources :

Example 1 with MongoCredential

use of com.mongodb.MongoCredential in project cas by apereo.

the class MongoDbCloudConfigBootstrapConfiguration method mongo.

@Override
public Mongo mongo() throws Exception {
    final MongoCredential credential = MongoCredential.createCredential(mongoClientUri().getUsername(), getDatabaseName(), mongoClientUri().getPassword());
    final String hostUri = mongoClientUri().getHosts().get(0);
    final String[] host = hostUri.split(":");
    return new MongoClient(new ServerAddress(host[0], host.length > 1 ? Integer.parseInt(host[1]) : DEFAULT_PORT), Collections.singletonList(credential), mongoClientOptions());
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress)

Example 2 with MongoCredential

use of com.mongodb.MongoCredential in project spring-cloud-connectors by spring-cloud.

the class MongoServiceConnectorCreatorTest method cloudMongoCreationWithMultipleHostsByUri.

@Test
public void cloudMongoCreationWithMultipleHostsByUri() throws Exception {
    String uri = String.format("%s://%s:%s@%s:%s/%s", MONGODB_SCHEME, TEST_USERNAME, TEST_PASSWORD, StringUtils.arrayToDelimitedString(TEST_HOSTS, ","), TEST_PORT, TEST_DB);
    MongoServiceInfo serviceInfo = new MongoServiceInfo("id", uri);
    MongoDbFactory mongoDbFactory = testCreator.create(serviceInfo, null);
    assertNotNull(mongoDbFactory);
    MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
    assertNotNull(mongo);
    List<ServerAddress> addresses = extractServerAddresses(mongo);
    assertEquals(3, addresses.size());
    MongoCredential credentials = mongo.getCredentialsList().get(0);
    assertEquals(TEST_USERNAME, credentials.getUserName());
    assertNotNull(credentials.getPassword());
    // Don't do connector.getDatabase().getName() as that will try to initiate the connection
    assertEquals(TEST_DB, ReflectionTestUtils.getField(mongoDbFactory, "databaseName"));
    ServerAddress address1 = addresses.get(0);
    assertEquals(TEST_HOST, address1.getHost());
    assertEquals(TEST_PORT_DEFAULT, address1.getPort());
    ServerAddress address2 = addresses.get(1);
    assertEquals(TEST_HOST_1, address2.getHost());
    assertEquals(TEST_PORT_DEFAULT, address2.getPort());
    ServerAddress address3 = addresses.get(2);
    assertEquals(TEST_HOST_2, address3.getHost());
    assertEquals(TEST_PORT, address3.getPort());
}
Also used : MongoClient(com.mongodb.MongoClient) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) Test(org.junit.Test)

Example 3 with MongoCredential

use of com.mongodb.MongoCredential in project spring-boot by spring-projects.

the class ReactiveMongoClientFactory method createNetworkMongoClient.

private MongoClient createNetworkMongoClient(MongoClientSettings settings) {
    if (hasCustomAddress() || hasCustomCredentials()) {
        if (this.properties.getUri() != null) {
            throw new IllegalStateException("Invalid mongo configuration, " + "either uri or host/port/credentials must be specified");
        }
        Builder builder = builder(settings);
        if (hasCustomCredentials()) {
            List<MongoCredential> credentials = new ArrayList<>();
            String database = this.properties.getAuthenticationDatabase() == null ? this.properties.getMongoClientDatabase() : this.properties.getAuthenticationDatabase();
            credentials.add(MongoCredential.createCredential(this.properties.getUsername(), database, this.properties.getPassword()));
            builder.credentialList(credentials);
        }
        String host = this.properties.getHost() == null ? "localhost" : this.properties.getHost();
        int port = this.properties.getPort() != null ? this.properties.getPort() : MongoProperties.DEFAULT_PORT;
        ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(host, port))).build();
        builder.clusterSettings(clusterSettings);
        return MongoClients.create(builder.build());
    }
    ConnectionString connectionString = new ConnectionString(this.properties.determineUri());
    return MongoClients.create(createBuilder(settings, connectionString).build());
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) MongoCredential(com.mongodb.MongoCredential) Builder(com.mongodb.async.client.MongoClientSettings.Builder) ArrayList(java.util.ArrayList) ServerAddress(com.mongodb.ServerAddress) ConnectionString(com.mongodb.ConnectionString) ConnectionString(com.mongodb.ConnectionString)

Example 4 with MongoCredential

use of com.mongodb.MongoCredential in project spring-boot by spring-projects.

the class MongoClientFactory method createNetworkMongoClient.

private MongoClient createNetworkMongoClient(MongoClientOptions options) {
    if (hasCustomAddress() || hasCustomCredentials()) {
        if (this.properties.getUri() != null) {
            throw new IllegalStateException("Invalid mongo configuration, " + "either uri or host/port/credentials must be specified");
        }
        if (options == null) {
            options = MongoClientOptions.builder().build();
        }
        List<MongoCredential> credentials = new ArrayList<>();
        if (hasCustomCredentials()) {
            String database = this.properties.getAuthenticationDatabase() == null ? this.properties.getMongoClientDatabase() : this.properties.getAuthenticationDatabase();
            credentials.add(MongoCredential.createCredential(this.properties.getUsername(), database, this.properties.getPassword()));
        }
        String host = this.properties.getHost() == null ? "localhost" : this.properties.getHost();
        int port = this.properties.getPort() != null ? this.properties.getPort() : MongoProperties.DEFAULT_PORT;
        return new MongoClient(Collections.singletonList(new ServerAddress(host, port)), credentials, options);
    }
    // The options and credentials are in the URI
    return new MongoClient(new MongoClientURI(this.properties.determineUri(), builder(options)));
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) MongoClientURI(com.mongodb.MongoClientURI) ArrayList(java.util.ArrayList) ServerAddress(com.mongodb.ServerAddress)

Example 5 with MongoCredential

use of com.mongodb.MongoCredential in project qi4j-sdk by Qi4j.

the class MongoMapEntityStoreMixin method activateService.

@Override
public void activateService() throws Exception {
    loadConfiguration();
    // Create Mongo driver and open the database
    if (username.isEmpty()) {
        mongo = new MongoClient(serverAddresses);
    } else {
        MongoCredential credential = MongoCredential.createMongoCRCredential(username, databaseName, password);
        mongo = new MongoClient(serverAddresses, Arrays.asList(credential));
    }
    db = mongo.getDB(databaseName);
    // Create index if needed
    db.requestStart();
    DBCollection entities = db.getCollection(collectionName);
    if (entities.getIndexInfo().isEmpty()) {
        entities.createIndex(new BasicDBObject(IDENTITY_COLUMN, 1));
    }
    db.requestDone();
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MongoCredential(com.mongodb.MongoCredential)

Aggregations

MongoCredential (com.mongodb.MongoCredential)13 ServerAddress (com.mongodb.ServerAddress)9 MongoClient (com.mongodb.MongoClient)8 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 ConnectionString (com.mongodb.ConnectionString)2 MongoClientURI (com.mongodb.MongoClientURI)2 MongoSecurityException (com.mongodb.MongoSecurityException)2 SaslException (javax.security.sasl.SaslException)2 MongoServiceInfo (org.springframework.cloud.service.common.MongoServiceInfo)2 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)2 AuthenticationMechanism (com.mongodb.AuthenticationMechanism)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBCollection (com.mongodb.DBCollection)1 Builder (com.mongodb.async.client.MongoClientSettings.Builder)1 ClusterSettings (com.mongodb.connection.ClusterSettings)1 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Callback (javax.security.auth.callback.Callback)1