Java Code Examples for com.mongodb.MongoCredential#createMongoCRCredential()

The following examples show how to use com.mongodb.MongoCredential#createMongoCRCredential() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoSchemaFactory.java    From Quicksql with MIT License 6 votes vote down vote up
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("dbName");
  final String password = (String) map.get("password");

  switch (authenticationMechanism) {
  case PLAIN:
    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);
}
 
Example 2
Source File: MongoConfiguration.java    From mongolastic with MIT License 6 votes vote down vote up
private MongoCredential findMongoCredential(String user, String database, char[] pwd, String mechanism) {
    MongoCredential credential = null;
    switch (mechanism) {
        case "scram-sha-1":
            credential = MongoCredential.createScramSha1Credential(user, database, pwd);
            break;
        case "x509":
            credential = MongoCredential.createMongoX509Credential(user);
            break;
        case "cr":
            credential = MongoCredential.createMongoCRCredential(user, database, pwd);
            break;
        case "plain":
            credential = MongoCredential.createPlainCredential(user, database, pwd);
            break;
        case "gssapi":
            credential = MongoCredential.createGSSAPICredential(user);
            break;
        default:
            credential = MongoCredential.createCredential(user, database, pwd);
            break;
    }
    return credential;
}
 
Example 3
Source File: MongoClientWrapper.java    From mongowp with Apache License 2.0 6 votes vote down vote up
private MongoCredential toMongoCredential(MongoAuthenticationConfiguration authConfiguration) {
  switch (authConfiguration.getMechanism()) {
    case cr:
      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");
  }
}
 
Example 4
Source File: MongoConfig.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private MongoCredential createCredential(Map<String, String> properties) throws DataServiceFault {
    MongoCredential credential = null;
    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;
    }
}
 
Example 5
Source File: MongoDBClientSupport.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
private MongoClient baseClient(MongoClientOptions connectionOptions) {
    if (usesAuthentication) {
        MongoCredential credential = MongoCredential.createMongoCRCredential(username, authenticationDatabase, password.toCharArray());
        return new MongoClient(address, ImmutableList.of(credential), connectionOptions);
    } else {
        return new MongoClient(address, connectionOptions);
    }
}
 
Example 6
Source File: MongoReader.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Init void.
 *
 * @param partition the partition
 */
public void init(Partition partition) {
    try {

        List<ServerAddress> addressList = new ArrayList<>();

        for (String s : (List<String>) ((DeepPartition) partition).splitWrapper().getReplicas()) {
            addressList.add(new ServerAddress(s));
        }

        //Credentials
        List<MongoCredential> mongoCredentials = new ArrayList<>();

        if (mongoDeepJobConfig.getUsername() != null && mongoDeepJobConfig.getPassword() != null) {
            MongoCredential credential = MongoCredential.createMongoCRCredential(mongoDeepJobConfig.getUsername(),
                    mongoDeepJobConfig.getDatabase(),
                    mongoDeepJobConfig.getPassword().toCharArray());
            mongoCredentials.add(credential);

        }

        mongoClient = new MongoClient(addressList, mongoCredentials);
        mongoClient.setReadPreference(ReadPreference.valueOf(mongoDeepJobConfig.getReadPreference()));
        db = mongoClient.getDB(mongoDeepJobConfig.getDatabase());
        collection = db.getCollection(mongoDeepJobConfig.getCollection());

        dbCursor = collection.find(generateFilterQuery((MongoPartition) partition),
                mongoDeepJobConfig.getDBFields());

    } catch (UnknownHostException e) {
        throw new DeepExtractorInitializationException(e);
    }
}
 
Example 7
Source File: MongoConnector.java    From carina with Apache License 2.0 5 votes vote down vote up
/**
 * Creates client for DB specified in properties.
 * 
 * @return MongoDB client
 * @throws NumberFormatException java.lang.NumberFormatException
 * @throws UnknownHostException java.net.UnknownHostException
 */
public static MongoClient createClient() throws NumberFormatException, UnknownHostException {
    if (!clients.containsKey(database)) {
        validateConfig(database);
        MongoCredential credential = MongoCredential.createMongoCRCredential(user, database, password.toCharArray());
        clients.put(database, new MongoClient(new ServerAddress(host, Integer.valueOf(port)), Arrays.asList(credential)));
    }
    return clients.get(database);
}
 
Example 8
Source File: MongoConnector.java    From carina with Apache License 2.0 5 votes vote down vote up
/**
 * Creates client for DB specified by parameter.
 * 
 * @param database DB
 * @return MongoDB client
 * @throws NumberFormatException java.lang.NumberFormatException
 * @throws UnknownHostException java.net.UnknownHostException
 */
public static MongoClient createClient(String database) throws NumberFormatException, UnknownHostException {
    if (!clients.containsKey(database)) {
        validateConfig(database);
        MongoCredential credential = MongoCredential.createMongoCRCredential(user, database, password.toCharArray());
        clients.put(database, new MongoClient(new ServerAddress(host, Integer.valueOf(port)), Arrays.asList(credential)));
    }
    return clients.get(database);
}