Example usage for com.mongodb MongoCredential createMongoX509Credential

List of usage examples for com.mongodb MongoCredential createMongoX509Credential

Introduction

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

Prototype

public static MongoCredential createMongoX509Credential(final String userName) 

Source Link

Document

Creates a MongoCredential instance for the MongoDB X.509 protocol.

Usage

From source file:com.ca.apm.mongo.Collector.java

License:Open Source License

public static void setupCreds(final List<MongoCredential> mc, final Properties iprops) {
    final String type = getStringProp(DB_AUTH_PROP, iprops);
    String user;//from   w  w  w  .j  ava  2  s .  c o m
    String pw;
    if (AUTH_NONE.equalsIgnoreCase(type)) {
        // nothing to do
    } else if (AUTH_CR.equalsIgnoreCase(type)) {
        user = getStringProp(DB_USER_PROP, iprops);
        pw = getStringProp(DB_PASSWD_PROP, iprops);
        mc.add(MongoCredential.createMongoCRCredential(user, "admin", pw.toCharArray()));
    } else if (AUTH_X509.equalsIgnoreCase(type)) {
        user = getStringProp(DB_USER_PROP, iprops);
        System.out.printf("X509 cred user(%s)%n", user);
        mc.add(MongoCredential.createMongoX509Credential(user));
    } else if (AUTH_KERBEROS.equalsIgnoreCase(type)) {
        user = getStringProp(DB_USER_PROP, iprops);
        mc.add(MongoCredential.createGSSAPICredential(user));
    } else if (AUTH_SASL.equalsIgnoreCase(type)) {
        user = getStringProp(DB_USER_PROP, iprops);
        pw = getStringProp(DB_PASSWD_PROP, iprops);
        mc.add(MongoCredential.createPlainCredential(user, "$external", pw.toCharArray()));
    } else {
        throw new IllegalArgumentException(String.format("Invalid %s property", DB_AUTH_PROP));
    }
}

From source file:com.eightkdata.mongowp.client.wrapper.MongoClientWrapper.java

License:Open Source License

private MongoCredential toMongoCredential(MongoAuthenticationConfiguration authConfiguration) {
    switch (authConfiguration.getMechanism()) {
    case cr:/* w ww .j a  v a  2 s. c o m*/
        return MongoCredential.createMongoCRCredential(authConfiguration.getUser(),
                authConfiguration.getSource(), authConfiguration.getPassword().toCharArray());
    case scram_sha1:
        return MongoCredential.createScramSha1Credential(authConfiguration.getUser(),
                authConfiguration.getSource(), authConfiguration.getPassword().toCharArray());
    case negotiate:
        return MongoCredential.createCredential(authConfiguration.getUser(), authConfiguration.getSource(),
                authConfiguration.getPassword().toCharArray());
    case x509:
        return MongoCredential.createMongoX509Credential(authConfiguration.getUser());
    default:
        throw new UnsupportedOperationException(
                "Authentication mechanism " + authConfiguration.getMechanism() + " not supported");
    }
}

From source file:com.redhat.lightblue.mongo.config.MongoConfiguration.java

License:Open Source License

public static MongoCredential credentialFromJson(ObjectNode node) {
    String userName = null;/*  w w w . ja  va 2 s. co  m*/
    String password = null;
    String source = null;

    JsonNode xnode = node.get("mechanism");
    if (xnode == null) {
        throw new IllegalArgumentException("mechanism is required in credentials");
    }
    String mech = xnode.asText();
    xnode = node.get("userName");
    if (xnode != null) {
        userName = xnode.asText();
    }
    xnode = node.get("password");
    if (xnode != null) {
        password = xnode.asText();
    }
    xnode = node.get("source");
    if (xnode != null) {
        source = xnode.asText();
    }

    MongoCredential cr;
    if ("GSSAPI_MECHANISM".equals(mech)) {
        cr = MongoCredential.createGSSAPICredential(userName);
    } else if ("MONGODB_CR_MECHANISM".equals(mech)) {
        cr = MongoCredential.createMongoCRCredential(userName, source,
                password == null ? null : password.toCharArray());
    } else if ("MONGODB_X509_MECHANISM".equals(mech)) {
        cr = MongoCredential.createMongoX509Credential(userName);
    } else if ("PLAIN_MECHANISM".equals(mech)) {
        cr = MongoCredential.createPlainCredential(userName, source,
                password == null ? null : password.toCharArray());
    } else {
        throw new IllegalArgumentException("invalid mechanism:" + mech + ", must be one of "
                + "GSSAPI_MECHANISM, MONGODB_CR_MECHANISM, " + "MONGODB_X5090_MECHANISM, or PLAIN_MECHANISM");
    }
    return cr;
}

From source file:example.X509CredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    String server = args[0];//from  w w w  .j  a  v a  2  s . c  o m
    String user = "CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US";

    System.out.println("server: " + server);
    System.out.println("user: " + user);

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            Arrays.asList(MongoCredential.createMongoX509Credential(user)),
            new MongoClientOptions.Builder().socketFactory(SSLSocketFactory.getDefault()).build());
    DB testDB = mongoClient.getDB("test");

    System.out.println("Count: " + testDB.getCollection("test").count());

    System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));

}

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;/*ww  w.ja va 2 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.apache.calcite.adapter.mongodb.MongoSchemaFactory.java

License:Apache License

private MongoCredential createCredentials(Map<String, Object> map) {
    final String authMechanismName = (String) map.get("authMechanism");
    final AuthenticationMechanism authenticationMechanism = AuthenticationMechanism
            .fromMechanismName(authMechanismName);
    final String username = (String) map.get("username");
    final String authDatabase = (String) map.get("authDatabase");
    final String password = (String) map.get("password");

    switch (authenticationMechanism) {
    case PLAIN:/*from  ww  w.j  av a 2  s. c o  m*/
        return MongoCredential.createPlainCredential(username, authDatabase, password.toCharArray());
    case SCRAM_SHA_1:
        return MongoCredential.createScramSha1Credential(username, authDatabase, password.toCharArray());
    case GSSAPI:
        return MongoCredential.createGSSAPICredential(username);
    case MONGODB_CR:
        return MongoCredential.createMongoCRCredential(username, authDatabase, password.toCharArray());
    case MONGODB_X509:
        return MongoCredential.createMongoX509Credential(username);
    }
    throw new IllegalArgumentException("Unsupported authentication mechanism " + authMechanismName);
}

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;//from  w  w w . j  a  v a2 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;/*  w  w  w . j  av  a 2s  . c o 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;
}