Example usage for com.mongodb MongoCredential MONGODB_CR_MECHANISM

List of usage examples for com.mongodb MongoCredential MONGODB_CR_MECHANISM

Introduction

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

Prototype

String MONGODB_CR_MECHANISM

To view the source code for com.mongodb MongoCredential MONGODB_CR_MECHANISM.

Click Source Link

Document

The MongoDB Challenge Response mechanism.

Usage

From source file:me.konglong.momei.mongodb.config.MongoCredentialPropertyEditor.java

License:Apache License

@Override
public void setAsText(String text) throws IllegalArgumentException {

    if (!StringUtils.hasText(text)) {
        return;//from  w  w w.j av  a2 s. c  om
    }

    List<MongoCredential> credentials = new ArrayList<MongoCredential>();

    for (String credentialString : extractCredentialsString(text)) {

        String[] userNameAndPassword = extractUserNameAndPassword(credentialString);
        String database = extractDB(credentialString);
        Properties options = extractOptions(credentialString);

        if (!options.isEmpty()) {

            if (options.containsKey(AUTH_MECHANISM_KEY)) {

                String authMechanism = options.getProperty(AUTH_MECHANISM_KEY);

                if (MongoCredential.GSSAPI_MECHANISM.equals(authMechanism)) {

                    verifyUserNamePresent(userNameAndPassword);
                    credentials.add(MongoCredential.createGSSAPICredential(userNameAndPassword[0]));
                } else if (MongoCredential.MONGODB_CR_MECHANISM.equals(authMechanism)) {

                    verifyUsernameAndPasswordPresent(userNameAndPassword);
                    verifyDatabasePresent(database);
                    credentials.add(MongoCredential.createMongoCRCredential(userNameAndPassword[0], database,
                            userNameAndPassword[1].toCharArray()));
                } else if (MongoCredential.MONGODB_X509_MECHANISM.equals(authMechanism)) {

                    verifyUserNamePresent(userNameAndPassword);
                    credentials.add(MongoCredential.createMongoX509Credential(userNameAndPassword[0]));
                } else if (MongoCredential.PLAIN_MECHANISM.equals(authMechanism)) {

                    verifyUsernameAndPasswordPresent(userNameAndPassword);
                    verifyDatabasePresent(database);
                    credentials.add(MongoCredential.createPlainCredential(userNameAndPassword[0], database,
                            userNameAndPassword[1].toCharArray()));
                } else if (MongoCredential.SCRAM_SHA_1_MECHANISM.equals(authMechanism)) {

                    verifyUsernameAndPasswordPresent(userNameAndPassword);
                    verifyDatabasePresent(database);
                    credentials.add(MongoCredential.createScramSha1Credential(userNameAndPassword[0], database,
                            userNameAndPassword[1].toCharArray()));
                } else {
                    throw new IllegalArgumentException(String.format(
                            "Cannot create MongoCredentials for unknown auth mechanism '%s'!", authMechanism));
                }
            }
        } else {

            verifyUsernameAndPasswordPresent(userNameAndPassword);
            verifyDatabasePresent(database);
            credentials.add(MongoCredential.createCredential(userNameAndPassword[0], database,
                    userNameAndPassword[1].toCharArray()));
        }
    }

    setValue(credentials);
}

From source file:org.pentaho.mongo.MongoUtils.java

License:Open Source License

/**
 * Return a list of custom "lastErrorModes" (if any) defined in the replica
 * set configuration object on the server. These can be used as the "w"
 * setting for the write concern in addition to the standard "w" values of
 * <number> or "majority"./*from   ww  w .  j a  va2  s.c om*/
 * 
 * @param hostsPorts the hosts to use
 * @param singlePort the default port to use if no ports are given in the
 *          hostsPorts spec
 * @param username the username for authentication
 * @param password the password for authentication
 * @param vars the environment variables to use
 * @param log for logging
 * @return a list of the names of any custom "lastErrorModes"
 * @throws KettleException if a problem occurs
 */
public static List<String> getLastErrorModes(String hostsPorts, String singlePort, MongoCredential cred,
        VariableSpace vars, LogChannelInterface log) throws KettleException {

    List<String> customLastErrorModes = new ArrayList<String>();

    MongoClient mongo = null;
    try {
        if (cred != null && cred.getMechanism().equals(MongoCredential.MONGODB_CR_MECHANISM)) {
            // need to make a new credential that specifies the local database
            cred = MongoCredential.createMongoCRCredential(cred.getUserName(), LOCAL_DB, cred.getPassword());
        }
        mongo = initConnection(hostsPorts, singlePort, cred, false, null, null, null, null, null, false, null,
                vars, log);

        DB local = mongo.getDB(LOCAL_DB);
        if (local != null) {

            DBCollection replset = local.getCollection(REPL_SET_COLLECTION);
            if (replset != null) {
                DBObject config = replset.findOne();

                extractLastErrorModes(config, customLastErrorModes);
            }
        }
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }

    return customLastErrorModes;
}

From source file:org.pentaho.mongo.MongoUtils.java

License:Open Source License

protected static BasicDBList getRepSetMemberRecords(String hostsPorts, String singlePort, MongoCredential cred,
        VariableSpace vars, LogChannelInterface log) throws KettleException {

    MongoClient mongo = null;/*from   w  w w.  j  a v  a  2s  .  com*/
    BasicDBList setMembers = null;
    try {
        if (cred != null && cred.getMechanism().equals(MongoCredential.MONGODB_CR_MECHANISM)) {
            // need to make a new credential that specifies the local database
            cred = MongoCredential.createMongoCRCredential(cred.getUserName(), LOCAL_DB, cred.getPassword());
        }
        mongo = initConnection(hostsPorts, singlePort, cred, false, null, null, null, null, null, false, null,
                vars, log);

        DB local = mongo.getDB(LOCAL_DB);
        if (local != null) {

            DBCollection replset = local.getCollection(REPL_SET_COLLECTION);
            if (replset != null) {
                DBObject config = replset.findOne();

                if (config != null) {
                    Object members = config.get(REPL_SET_MEMBERS);

                    if (members instanceof BasicDBList) {
                        if (((BasicDBList) members).size() == 0) {
                            // log that there are no replica set members defined
                            if (log != null) {
                                log.logBasic(BaseMessages.getString(PKG,
                                        "MongoUtils.Message.Warning.NoReplicaSetMembersDefined")); //$NON-NLS-1$
                            }
                        } else {
                            setMembers = (BasicDBList) members;
                        }

                    } else {
                        // log that there are no replica set members defined
                        if (log != null) {
                            log.logBasic(BaseMessages.getString(PKG,
                                    "MongoUtils.Message.Warning.NoReplicaSetMembersDefined")); //$NON-NLS-1$
                        }
                    }
                } else {
                    // log that there are no replica set members defined
                    if (log != null) {
                        log.logBasic(BaseMessages.getString(PKG,
                                "MongoUtils.Message.Warning.NoReplicaSetMembersDefined")); //$NON-NLS-1$
                    }
                }
            } else {
                // log that the replica set collection is not available
                if (log != null) {
                    log.logBasic(BaseMessages.getString(PKG,
                            "MongoUtils.Message.Warning.ReplicaSetCollectionUnavailable")); //$NON-NLS-1$
                }
            }
        } else {
            // log that the local database is not available!!
            if (log != null) {
                log.logBasic(BaseMessages.getString(PKG, "MongoUtils.Message.Warning.LocalDBNotAvailable")); //$NON-NLS-1$
            }
        }
    } catch (Exception ex) {
        throw new KettleException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }

    return setMembers;
}