Search in sources :

Example 6 with MongoCredential

use of com.mongodb.MongoCredential in project mongo-java-driver by mongodb.

the class ConnectionStringTest method testValidAuth.

private void testValidAuth() {
    ConnectionString connectionString = null;
    try {
        connectionString = new ConnectionString(input);
    } catch (Throwable t) {
        if (description.contains("without password")) {
            // We don't allow null passwords without setting the authentication mechanism.
            return;
        } else {
            assertTrue(String.format("Connection string '%s' should not have throw an exception: %s", input, t.toString()), false);
        }
    }
    assertString("auth.db", getAuthDB(connectionString));
    assertString("auth.username", connectionString.getUsername());
    // Passwords for certain auth mechanisms are ignored.
    String password = null;
    if (connectionString.getPassword() != null) {
        password = new String(connectionString.getPassword());
    }
    List<MongoCredential> credentials = connectionString.getCredentialList();
    if (credentials.size() > 0) {
        AuthenticationMechanism mechanism = credentials.get(0).getAuthenticationMechanism();
        if (mechanism == null) {
            assertString("auth.password", password);
        } else {
            switch(mechanism) {
                case PLAIN:
                case MONGODB_CR:
                case SCRAM_SHA_1:
                    assertString("auth.password", password);
                    break;
                default:
            }
        }
    } else {
        assertString("auth.password", password);
    }
}
Also used : MongoCredential(com.mongodb.MongoCredential) AuthenticationMechanism(com.mongodb.AuthenticationMechanism) ConnectionString(com.mongodb.ConnectionString) ConnectionString(com.mongodb.ConnectionString)

Example 7 with MongoCredential

use of com.mongodb.MongoCredential in project mongo-java-driver by mongodb.

the class PlainAuthenticator method createSaslClient.

@Override
protected SaslClient createSaslClient(final ServerAddress serverAddress) {
    final MongoCredential credential = getCredential();
    isTrue("mechanism is PLAIN", credential.getAuthenticationMechanism() == PLAIN);
    try {
        return Sasl.createSaslClient(new String[] { PLAIN.getMechanismName() }, credential.getUserName(), DEFAULT_PROTOCOL, serverAddress.getHost(), null, new CallbackHandler() {

            @Override
            public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (final Callback callback : callbacks) {
                    if (callback instanceof PasswordCallback) {
                        ((PasswordCallback) callback).setPassword(credential.getPassword());
                    } else if (callback instanceof NameCallback) {
                        ((NameCallback) callback).setName(credential.getUserName());
                    }
                }
            }
        });
    } catch (SaslException e) {
        throw new MongoSecurityException(credential, "Exception initializing SASL client", e);
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) MongoSecurityException(com.mongodb.MongoSecurityException) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) MongoCredential(com.mongodb.MongoCredential) PasswordCallback(javax.security.auth.callback.PasswordCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) SaslException(javax.security.sasl.SaslException)

Example 8 with MongoCredential

use of com.mongodb.MongoCredential in project mongo-java-driver by mongodb.

the class GSSAPIAuthenticator method createSaslClient.

@Override
protected SaslClient createSaslClient(final ServerAddress serverAddress) {
    MongoCredential credential = getCredential();
    try {
        Map<String, Object> saslClientProperties = getCredential().getMechanismProperty(JAVA_SASL_CLIENT_PROPERTIES_KEY, null);
        if (saslClientProperties == null) {
            saslClientProperties = new HashMap<String, Object>();
            saslClientProperties.put(Sasl.MAX_BUFFER, "0");
            saslClientProperties.put(Sasl.CREDENTIALS, getGSSCredential(credential.getUserName()));
        }
        SaslClient saslClient = Sasl.createSaslClient(new String[] { GSSAPI.getMechanismName() }, credential.getUserName(), credential.getMechanismProperty(SERVICE_NAME_KEY, SERVICE_NAME_DEFAULT_VALUE), getHostName(serverAddress), saslClientProperties, null);
        if (saslClient == null) {
            throw new MongoSecurityException(credential, String.format("No platform support for %s mechanism", GSSAPI));
        }
        return saslClient;
    } catch (SaslException e) {
        throw new MongoSecurityException(credential, "Exception initializing SASL client", e);
    } catch (GSSException e) {
        throw new MongoSecurityException(credential, "Exception initializing GSSAPI credentials", e);
    } catch (UnknownHostException e) {
        throw new MongoSecurityException(credential, "Unable to canonicalize host name + " + serverAddress);
    }
}
Also used : MongoSecurityException(com.mongodb.MongoSecurityException) GSSException(org.ietf.jgss.GSSException) UnknownHostException(java.net.UnknownHostException) MongoCredential(com.mongodb.MongoCredential) SaslException(javax.security.sasl.SaslException) SaslClient(javax.security.sasl.SaslClient)

Example 9 with MongoCredential

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

the class MongoClientFactoryTests method uriCanBeCustomized.

@Test
public void uriCanBeCustomized() {
    MongoProperties properties = new MongoProperties();
    properties.setUri("mongodb://user:secret@mongo1.example.com:12345," + "mongo2.example.com:23456/test");
    MongoClient client = createMongoClient(properties);
    List<ServerAddress> allAddresses = extractServerAddresses(client);
    assertThat(allAddresses).hasSize(2);
    assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
    assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
    List<MongoCredential> credentialsList = client.getCredentialsList();
    assertThat(credentialsList).hasSize(1);
    assertMongoCredential(credentialsList.get(0), "user", "secret", "test");
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) Test(org.junit.Test)

Example 10 with MongoCredential

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

the class ReactiveMongoClientFactoryTests method uriCanBeCustomized.

@Test
public void uriCanBeCustomized() throws UnknownHostException {
    MongoProperties properties = new MongoProperties();
    properties.setUri("mongodb://user:secret@mongo1.example.com:12345," + "mongo2.example.com:23456/test");
    MongoClient client = createMongoClient(properties);
    List<ServerAddress> allAddresses = extractServerAddresses(client);
    assertThat(allAddresses).hasSize(2);
    assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
    assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
    List<MongoCredential> credentialsList = extractMongoCredentials(client);
    assertThat(credentialsList).hasSize(1);
    assertMongoCredential(credentialsList.get(0), "user", "secret", "test");
}
Also used : MongoClient(com.mongodb.reactivestreams.client.MongoClient) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) Test(org.junit.Test)

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