Example usage for com.mongodb MongoCredential createGSSAPICredential

List of usage examples for com.mongodb MongoCredential createGSSAPICredential

Introduction

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

Prototype

public static MongoCredential createGSSAPICredential(final String userName) 

Source Link

Document

Creates a MongoCredential instance for the GSSAPI SASL mechanism.

Usage

From source file:GSSAPICredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    // Set this property to avoid the default behavior where the program prompts on the command line for username/password
    //        Security.setProperty("auth.login.defaultCallbackHandler", "DefaultSecurityCallbackHandler");

    String server = args[0];/*from ww w .jav a2 s .c o  m*/
    String user = args[1];
    String databaseName = args[2];

    System.out.println("javax.security.auth.useSubjectCredsOnly: "
            + System.getProperty("javax.security.auth.useSubjectCredsOnly"));
    System.out.println("java.security.krb5.realm: " + System.getProperty("java.security.krb5.realm"));
    System.out.println("java.security.krb5.kdc: " + System.getProperty("java.security.krb5.kdc"));
    System.out.println(
            "auth.login.defaultCallbackHandler: " + Security.getProperty("auth.login.defaultCallbackHandler"));
    System.out.println("login.configuration.provider: " + Security.getProperty("login.configuration.provider"));
    System.out.println(
            "java.security.auth.login.config: " + Security.getProperty("java.security.auth.login.config"));
    System.out.println("login.config.url.1: " + Security.getProperty("login.config.url.1"));
    System.out.println("login.config.url.2: " + Security.getProperty("login.config.url.2"));
    System.out.println("login.config.url.3: " + Security.getProperty("login.config.url.3"));

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

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            Arrays.asList(MongoCredential.createGSSAPICredential(user)),
            new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
    DB testDB = mongoClient.getDB(databaseName);

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

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. jav  a 2  s  .co 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.github.maasdi.mongo.wrapper.KerberosMongoClientWrapper.java

License:Apache License

@Override
protected MongoCredential getCredential(MongoDbMeta meta, VariableSpace vars) {
    return MongoCredential.createGSSAPICredential(vars.environmentSubstitute(meta.getAuthenticationUser()));
}

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

License:Open Source License

public static MongoCredential credentialFromJson(ObjectNode node) {
    String userName = null;/*from   w  w w  . java 2s  .c o 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.GSSAPICredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    // Set this property to avoid the default behavior where the program prompts on the command line for username/password
    //        Security.setProperty("auth.login.defaultCallbackHandler", "example.DefaultSecurityCallbackHandler");

    String server = args[0];//from   w w  w  . ja v a  2s.c o m
    String user = args[1];
    String databaseName = args[2];

    System.out.println("javax.security.auth.useSubjectCredsOnly: "
            + System.getProperty("javax.security.auth.useSubjectCredsOnly"));
    System.out.println("java.security.krb5.realm: " + System.getProperty("java.security.krb5.realm"));
    System.out.println("java.security.krb5.kdc: " + System.getProperty("java.security.krb5.kdc"));
    System.out.println(
            "auth.login.defaultCallbackHandler: " + Security.getProperty("auth.login.defaultCallbackHandler"));
    System.out.println("login.configuration.provider: " + Security.getProperty("login.configuration.provider"));
    System.out.println(
            "java.security.auth.login.config: " + Security.getProperty("java.security.auth.login.config"));
    System.out.println("login.config.url.1: " + Security.getProperty("login.config.url.1"));
    System.out.println("login.config.url.2: " + Security.getProperty("login.config.url.2"));
    System.out.println("login.config.url.3: " + Security.getProperty("login.config.url.3"));

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

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            asList(MongoCredential.createGSSAPICredential(user).withMechanismProperty("SERVICE_NAME",
                    "mongodb")),
            new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
    DB testDB = mongoClient.getDB(databaseName);

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

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;// w ww . j  av a  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://w w  w  .j  a v  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.pentaho.mongo.MongoUtils.java

License:Open Source License

/**
 * Create a credentials object//from   ww  w.  j  a va 2s  .co  m
 * 
 * @param username the username to use
 * @param password the password to use (normal authentication only)
 * @param dbName the database to authenticate against (normal authentication
 *          only)
 * @param kerberos true if Kerberos authentication is to be used
 * @return a configured MongoCredential object
 */
public static MongoCredential createCredentials(String username, String password, String dbName,
        boolean kerberos) {
    MongoCredential cred = null;

    if (kerberos) {
        if (!Const.isEmpty(username)) {
            cred = MongoCredential.createGSSAPICredential(username);
        }
    } else {
        // standard authentication
        if (!Const.isEmpty(username) || !Const.isEmpty(password)) {
            cred = MongoCredential.createMongoCRCredential(username, dbName, password.toCharArray());
        }
    }

    return cred;
}

From source file:org.pentaho.mongo.wrapper.KerberosMongoClientWrapper.java

License:Open Source License

@Override
public List<MongoCredential> getCredentialList() {
    List<MongoCredential> credList = new ArrayList<MongoCredential>();
    credList.add(MongoCredential.createGSSAPICredential(props.get(MongoProp.USERNAME)));
    return credList;
}

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 w  w .ja va 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;
    }
}