Example usage for com.mongodb MongoCredential createScramSha1Credential

List of usage examples for com.mongodb MongoCredential createScramSha1Credential

Introduction

In this page you can find the example usage for com.mongodb MongoCredential createScramSha1Credential.

Prototype

public static MongoCredential createScramSha1Credential(final String userName, final String source,
        final char[] password) 

Source Link

Document

Creates a MongoCredential instance for the SCRAM-SHA-1 SASL mechanism.

Usage

From source file:org.opencb.commons.datastore.mongodb.MongoDataStoreManager.java

License:Apache License

private MongoDataStore create(String database, MongoDBConfiguration mongoDBConfiguration) {
    MongoDataStore mongoDataStore = null;
    MongoClient mc = null;//from   w  w  w  .j  a v  a2 s . c om
    logger.debug(
            "MongoDataStoreManager: creating a MongoDataStore object for database: '" + database + "' ...");
    long t0 = System.currentTimeMillis();
    if (database != null && !database.trim().equals("")) {
        // read DB configuration for that SPECIES.VERSION, by default
        // PRIMARY_DB is selected
        //            String dbPrefix = applicationProperties.getProperty(speciesVersionPrefix + ".DB", "PRIMARY_DB");
        // We create the MongoClientOptions
        MongoClientOptions mongoClientOptions;
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder()
                .connectionsPerHost(
                        mongoDBConfiguration.getInt(CONNECTIONS_PER_HOST, CONNECTIONS_PER_HOST_DEFAULT))
                .connectTimeout(mongoDBConfiguration.getInt(CONNECT_TIMEOUT, CONNECT_TIMEOUT_DEFAULT))
                .readPreference(ReadPreference.valueOf(
                        mongoDBConfiguration.getString(READ_PREFERENCE, READ_PREFERENCE_DEFAULT.getValue())));

        if (mongoDBConfiguration.getString(REPLICA_SET) != null
                && !mongoDBConfiguration.getString(REPLICA_SET).isEmpty()) {
            logger.debug("Setting replicaSet to " + mongoDBConfiguration.getString(REPLICA_SET));
            builder = builder.requiredReplicaSetName(mongoDBConfiguration.getString(REPLICA_SET));
        }

        if (mongoDBConfiguration.getBoolean(SSL_ENABLED)) {
            logger.debug("SSL connections enabled for " + database);
            builder = builder.sslEnabled(mongoDBConfiguration.getBoolean(SSL_ENABLED));
        }

        mongoClientOptions = builder.build();

        assert (dataStoreServerAddresses != null);

        // We create the MongoCredential object
        String user = mongoDBConfiguration.getString(USERNAME, "");
        String pass = mongoDBConfiguration.getString(PASSWORD, "");
        MongoCredential mongoCredential = null;
        if ((user != null && !user.equals("")) || (pass != null && !pass.equals(""))) {
            //                final DB authenticationDatabase;
            if (mongoDBConfiguration.get(AUTHENTICATION_DATABASE) != null
                    && !mongoDBConfiguration.getString(AUTHENTICATION_DATABASE).isEmpty()) {
                //                        authenticationDatabase = mc.getDB(mongoDBConfiguration.getString("authenticationDatabase"));
                mongoCredential = MongoCredential.createScramSha1Credential(user,
                        mongoDBConfiguration.getString(AUTHENTICATION_DATABASE), pass.toCharArray());
            } else {
                //                        authenticationDatabase = db;
                mongoCredential = MongoCredential.createScramSha1Credential(user, "", pass.toCharArray());
            }
            //                    authenticationDatabase.authenticate(user, pass.toCharArray());
        }

        mc = newMongoClient(mongoClientOptions, mongoCredential);

        //                mc.setReadPreference(ReadPreference.secondary(new BasicDBObject("dc", "PG")));
        //                mc.setReadPreference(ReadPreference.primary());
        //                System.out.println("Replica Status: "+mc.getReplicaSetStatus());
        logger.debug(mongoDBConfiguration.toString());
        MongoDatabase db = mc.getDatabase(database);
        //                db.setReadPreference(ReadPreference.secondary(new BasicDBObject("dc", "PG")));
        //                db.setReadPreference(ReadPreference.primary());

        long t1 = System.currentTimeMillis();
        logger.debug("MongoDataStoreManager: MongoDataStore object for database: '" + database + "' created in "
                + (t0 - t1) + "ms");
        mongoDataStore = new MongoDataStore(mc, db, mongoDBConfiguration);
    } else {
        logger.debug("MongoDB database is null or empty");
    }
    return mongoDataStore;
}

From source file:org.perfcake.message.sender.MongoDBSender.java

License:Apache License

@Override
public void doInit(final Properties messageAttributes) throws PerfCakeException {
    final String t = getTarget(messageAttributes);
    final ServerAddress a;

    if (t.contains(":")) {
        final String[] addr = t.split(":", 2);
        final String host = addr[0];
        final int port = Integer.valueOf(addr[1]);
        a = new ServerAddress(host, port);
    } else {//from   www. j ava 2 s.  co m
        a = new ServerAddress(t);
    }

    if (dbUsername != null) {
        final MongoCredential c = MongoCredential.createScramSha1Credential(dbUsername, dbName,
                dbPassword.toCharArray());
        mongoClient = new MongoClient(a, Collections.singletonList(c),
                MongoClientOptions.builder().connectionsPerHost(connectionPoolSize).build());
    } else {
        mongoClient = new MongoClient(a,
                MongoClientOptions.builder().connectionsPerHost(connectionPoolSize).build());
    }

    db = mongoClient.getDatabase(dbName);
}

From source file:org.wso2.carbon.dataservices.core.description.config.MongoConfig.java

License:Open Source License

private MongoCredential createCredential(Map<String, String> properties) throws DataServiceFault {
    MongoCredential credential = null;// w  ww  .j  a  v  a 2  s .c  o m
    String authenticationType = properties.get(DBConstants.MongoDB.AUTHENTICATION_TYPE);
    String username = properties.get(DBConstants.MongoDB.USERNAME);
    String password = properties.get(DBConstants.MongoDB.PASSWORD);
    String database = properties.get(DBConstants.MongoDB.DATABASE);
    if (authenticationType != null) {
        switch (authenticationType) {
        case DBConstants.MongoDB.MongoAuthenticationTypes.PLAIN:
            credential = MongoCredential.createPlainCredential(username, database, password.toCharArray());
            break;
        case DBConstants.MongoDB.MongoAuthenticationTypes.SCRAM_SHA_1:
            credential = MongoCredential.createScramSha1Credential(username, database, password.toCharArray());
            break;
        case DBConstants.MongoDB.MongoAuthenticationTypes.MONGODB_CR:
            credential = MongoCredential.createMongoCRCredential(username, database, password.toCharArray());
            break;
        case DBConstants.MongoDB.MongoAuthenticationTypes.GSSAPI:
            credential = MongoCredential.createGSSAPICredential(username);
            break;
        case DBConstants.MongoDB.MongoAuthenticationTypes.MONGODB_X509:
            credential = MongoCredential.createMongoX509Credential(username);
            break;
        default:
            throw new DataServiceFault("Invalid Authentication type. ");
        }
        return credential;
    } else {
        return null;
    }
}

From source file:org.wso2.carbon.datasource.reader.mongo.MongoDataSourceReaderUtil.java

License:Open Source License

private static MongoCredential createCredentials(MongoDataSourceConfiguration fileConfig) {
    MongoCredential credential;//ww  w  .ja v  a  2 s . co  m
    switch (fileConfig.getAuthenticationMethodEnum()) {
    case SCRAM_SHA_1:
        credential = MongoCredential.createScramSha1Credential(fileConfig.getUsername(),
                fileConfig.getDatabase(), fileConfig.getPassword().toCharArray());
        break;
    case MONGODB_CR:
        credential = MongoCredential.createMongoCRCredential(fileConfig.getUsername(), fileConfig.getDatabase(),
                fileConfig.getPassword().toCharArray());
    case LDAP_PLAIN:
        credential = MongoCredential.createPlainCredential(fileConfig.getUsername(), fileConfig.getAuthSource(),
                fileConfig.getPassword().toCharArray());
    case X_509:
        credential = MongoCredential.createMongoX509Credential(fileConfig.getUsername());
    case GSSAPI:
        credential = MongoCredential.createGSSAPICredential(fileConfig.getUsername());
    case DEFAULT:
    default:
        credential = MongoCredential.createCredential(fileConfig.getUsername(), fileConfig.getDatabase(),
                fileConfig.getPassword().toCharArray());
    }
    return credential;
}