com.mongodb.AuthenticationMechanism Java Examples

The following examples show how to use com.mongodb.AuthenticationMechanism. 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: MongoAuthenticationPanel.java    From nosql4idea with Apache License 2.0 6 votes vote down vote up
@Override
public void load(AuthenticationSettings settings) {
    usernameField.setText(settings.getUsername());
    passwordField.setText(settings.getPassword());
    MongoExtraSettings mongoExtraSettings = new MongoExtraSettings(settings.getExtras());
    authenticationDatabaseField.setText(mongoExtraSettings.getAuthenticationDatabase());
    sslConnectionField.setSelected(mongoExtraSettings.isSsl());
    AuthenticationMechanism authentificationMethod = mongoExtraSettings.getAuthenticationMechanism();
    if (AuthenticationMechanism.MONGODB_CR.equals(authentificationMethod)) {
        mongoCRAuthRadioButton.setSelected(true);
    } else if (AuthenticationMechanism.SCRAM_SHA_1.equals(authentificationMethod)) {
        scramSHA1AuthRadioButton.setSelected(true);
    } else {
        defaultAuthMethodRadioButton.setSelected(true);
    }
}
 
Example #3
Source File: CredentialListParserTest.java    From vertx-mongo-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuth_SCRAM_SHA_256() {
  JsonObject config = new JsonObject();
  String username = TestUtils.randomAlphaString(8);
  String password = TestUtils.randomAlphaString(20);
  String authSource = TestUtils.randomAlphaString(10);
  config.put("username", username);
  config.put("password", password);
  config.put("authSource", authSource);
  config.put("authMechanism", "SCRAM-SHA-256");

  List<MongoCredential> credentials = new CredentialListParser(config).credentials();
  assertEquals(1, credentials.size());
  MongoCredential credential = credentials.get(0);
  assertEquals(username, credential.getUserName());
  assertArrayEquals(password.toCharArray(), credential.getPassword());
  assertEquals(authSource, credential.getSource());

  assertEquals(AuthenticationMechanism.SCRAM_SHA_256, credential.getAuthenticationMechanism());
}
 
Example #4
Source File: CredentialListParserTest.java    From vertx-mongo-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuth_SCRAM_SHA_1() {
  JsonObject config = new JsonObject();
  String username = TestUtils.randomAlphaString(8);
  String password = TestUtils.randomAlphaString(20);
  String authSource = TestUtils.randomAlphaString(10);
  config.put("username", username);
  config.put("password", password);
  config.put("authSource", authSource);
  config.put("authMechanism", "SCRAM-SHA-1");

  List<MongoCredential> credentials = new CredentialListParser(config).credentials();
  assertEquals(1, credentials.size());
  MongoCredential credential = credentials.get(0);
  assertEquals(username, credential.getUserName());
  assertArrayEquals(password.toCharArray(), credential.getPassword());
  assertEquals(authSource, credential.getSource());

  assertEquals(AuthenticationMechanism.SCRAM_SHA_1, credential.getAuthenticationMechanism());
}
 
Example #5
Source File: MongoSchemaFactory.java    From calcite with Apache License 2.0 6 votes vote down vote up
private MongoCredential createCredential(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:
    return MongoCredential.createPlainCredential(username, authDatabase,
        password.toCharArray());
  case SCRAM_SHA_1:
    return MongoCredential.createScramSha1Credential(username, authDatabase,
        password.toCharArray());
  case SCRAM_SHA_256:
    return MongoCredential.createScramSha256Credential(username, authDatabase,
        password.toCharArray());
  case GSSAPI:
    return MongoCredential.createGSSAPICredential(username);
  case MONGODB_X509:
    return MongoCredential.createMongoX509Credential(username);
  }
  throw new IllegalArgumentException("Unsupported authentication mechanism "
      + authMechanismName);
}
 
Example #6
Source File: CredentialListParserTest.java    From vertx-mongo-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuth_MONGODB_X509() {
  JsonObject config = new JsonObject();
  String username = TestUtils.randomAlphaString(8);
  String authSource = TestUtils.randomAlphaString(10);
  config.put("username", username);
  config.put("authSource", authSource);
  config.put("authMechanism", "MONGODB-X509");

  List<MongoCredential> credentials = new CredentialListParser(config).credentials();
  assertEquals(1, credentials.size());
  MongoCredential credential = credentials.get(0);
  assertEquals(username, credential.getUserName());
  assertNotEquals(authSource, credential.getSource()); // It should ignore the source we pass in

  assertEquals(AuthenticationMechanism.MONGODB_X509, credential.getAuthenticationMechanism());
}
 
Example #7
Source File: CredentialListParserTest.java    From vertx-mongo-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuth_GSSAPI() {
  JsonObject config = new JsonObject();
  String username = TestUtils.randomAlphaString(8);
  String authSource = TestUtils.randomAlphaString(10);
  config.put("username", username);
  config.put("authSource", authSource);
  config.put("authMechanism", "GSSAPI");

  List<MongoCredential> credentials = new CredentialListParser(config).credentials();
  assertEquals(1, credentials.size());
  MongoCredential credential = credentials.get(0);
  assertEquals(username, credential.getUserName());
  assertNotEquals(authSource, credential.getSource()); // It should ignore the source we pass in

  assertEquals(AuthenticationMechanism.GSSAPI, credential.getAuthenticationMechanism());
}
 
Example #8
Source File: CredentialListParserTest.java    From vertx-mongo-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuth_GSSAPI_WithServiceName() {
  JsonObject config = new JsonObject();
  String username = TestUtils.randomAlphaString(8);
  String authSource = TestUtils.randomAlphaString(10);
  String serviceName = TestUtils.randomAlphaString(11);
  config.put("username", username);
  config.put("authSource", authSource);
  config.put("authMechanism", "GSSAPI");
  config.put("gssapiServiceName", serviceName);

  List<MongoCredential> credentials = new CredentialListParser(config).credentials();
  assertEquals(1, credentials.size());
  MongoCredential credential = credentials.get(0);
  assertEquals(username, credential.getUserName());
  assertNotEquals(authSource, credential.getSource()); // It should ignore the source we pass in

  assertEquals(AuthenticationMechanism.GSSAPI, credential.getAuthenticationMechanism());
  assertEquals(serviceName, credential.getMechanismProperty("SERVICE_NAME", null));
}
 
Example #9
Source File: CredentialListParserTest.java    From vertx-mongo-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuth_PLAIN() {
  JsonObject config = new JsonObject();
  String username = TestUtils.randomAlphaString(8);
  String password = TestUtils.randomAlphaString(20);
  String authSource = TestUtils.randomAlphaString(10);
  config.put("username", username);
  config.put("password", password);
  config.put("authSource", authSource);
  config.put("authMechanism", "PLAIN");

  List<MongoCredential> credentials = new CredentialListParser(config).credentials();
  assertEquals(1, credentials.size());
  MongoCredential credential = credentials.get(0);
  assertEquals(username, credential.getUserName());
  assertArrayEquals(password.toCharArray(), credential.getPassword());
  assertEquals(authSource, credential.getSource());

  assertEquals(AuthenticationMechanism.PLAIN, credential.getAuthenticationMechanism());
}
 
Example #10
Source File: MongoClientURIBuilderTest.java    From nosql4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void withSpecificAuthentication() throws Exception {
    assertEquals(
            "mongodb://toto:pass@localhost:27018/?authMechanism=MONGODB-CR&authSource=userdb",
            MongoClientURIBuilder.builder()
                    .setServerAddresses("localhost:27018")
                    .setCredential("toto", "pass", "userdb")
                    .setAuthenticationMecanism(AuthenticationMechanism.MONGODB_CR)
                    .build());
}
 
Example #11
Source File: ServerConfigurationPanelTest.java    From nosql4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void loadMongoConfiguration() throws Exception {
    ServerConfiguration configuration = new ServerConfiguration();
    configuration.setLabel("Localhost");
    configuration.setDatabaseVendor(DatabaseVendor.MONGO);
    configuration.setServerUrl("localhost:25");

    AuthenticationSettings authenticationSettings = new AuthenticationSettings();
    authenticationSettings.setUsername("john");
    authenticationSettings.setPassword("johnpassword");
    MongoExtraSettings mongoExtraSettings = new MongoExtraSettings();
    mongoExtraSettings.setAuthenticationDatabase("admin");
    mongoExtraSettings.setAuthenticationMechanism(AuthenticationMechanism.SCRAM_SHA_1);
    mongoExtraSettings.setSsl(true);

    authenticationSettings.setExtras(mongoExtraSettings.get());

    configuration.setAuthenticationSettings(authenticationSettings);
    configuration.setUserDatabase("mydatabase");

    configurationPanel.loadConfigurationData(configuration);

    frameFixture.textBox("labelField").requireText("Localhost");
    frameFixture.textBox("serverUrlField").requireText("localhost:25");
    frameFixture.textBox("usernameField").requireText("john");
    frameFixture.textBox("passwordField").requireText("johnpassword");
    frameFixture.textBox("authenticationDatabaseField").requireText("admin");
    frameFixture.checkBox("sslConnectionField").requireSelected();
    frameFixture.radioButton("scramSHA1AuthField").requireSelected();
}
 
Example #12
Source File: ServerConfigurationPanelTest.java    From nosql4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void createMongoConfiguration() throws Exception {
    frameFixture.textBox("labelField").setText("Localhost");

    frameFixture.label("databaseVendorLabel").requireText("MongoDB");
    frameFixture.label("databaseTipsLabel").requireText("format: host:port. If replicat set: host:port1,host:port2,...");

    frameFixture.textBox("serverUrlField").setText("localhost:25");
    frameFixture.textBox("usernameField").setText("john");
    frameFixture.textBox("passwordField").setText("johnpassword");

    frameFixture.textBox("userDatabaseField").setText("mydatabase");

    frameFixture.textBox("authenticationDatabaseField").setText("admin");
    frameFixture.radioButton("defaultAuthMethod").requireSelected();
    frameFixture.radioButton("mongoCRAuthField").click();

    frameFixture.checkBox("sslConnectionField").check();
    frameFixture.checkBox("autoConnectField").check();
    ServerConfiguration configuration = new ServerConfiguration();

    configurationPanel.applyConfigurationData(configuration);

    assertEquals("Localhost", configuration.getLabel());
    assertEquals(DatabaseVendor.MONGO, configuration.getDatabaseVendor());
    assertEquals("localhost:25", configuration.getServerUrl());
    AuthenticationSettings authenticationSettings = configuration.getAuthenticationSettings();
    assertEquals("john", authenticationSettings.getUsername());
    assertEquals("johnpassword", authenticationSettings.getPassword());

    MongoExtraSettings mongoExtraSettings = new MongoExtraSettings(authenticationSettings.getExtras());

    assertEquals("admin", mongoExtraSettings.getAuthenticationDatabase());
    assertEquals(AuthenticationMechanism.MONGODB_CR, mongoExtraSettings.getAuthenticationMechanism());
    assertEquals("mydatabase", configuration.getUserDatabase());
    assertTrue(configuration.isConnectOnIdeStartup());
}
 
Example #13
Source File: MongoAuthenticationPanel.java    From nosql4idea with Apache License 2.0 5 votes vote down vote up
private AuthenticationMechanism getAuthenticationMechanism() {
    if (mongoCRAuthRadioButton.isSelected()) {
        return AuthenticationMechanism.MONGODB_CR;
    } else if (scramSHA1AuthRadioButton.isSelected()) {
        return AuthenticationMechanism.SCRAM_SHA_1;
    }
    return null;
}
 
Example #14
Source File: CredentialListParserTest.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuth_MONGODB_X509_without_username() {
  JsonObject config = new JsonObject();
  String authSource = TestUtils.randomAlphaString(10);
  config.put("authSource", authSource);
  config.put("authMechanism", "MONGODB-X509");

  List<MongoCredential> credentials = new CredentialListParser(config).credentials();
  assertEquals(1, credentials.size());
  MongoCredential credential = credentials.get(0);
  assertNull(credential.getUserName());
  assertNotEquals(authSource, credential.getSource()); // It should ignore the source we pass in

  assertEquals(AuthenticationMechanism.MONGODB_X509, credential.getAuthenticationMechanism());
}
 
Example #15
Source File: CredentialListParser.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
private AuthenticationMechanism getAuthenticationMechanism(String authMechanism) {
  AuthenticationMechanism mechanism;
  try {
    mechanism = AuthenticationMechanism.fromMechanismName(authMechanism);
  } catch (IllegalArgumentException e) {
    throw new IllegalArgumentException("Invalid authMechanism '" + authMechanism + "'");
  }
  return mechanism;
}
 
Example #16
Source File: EclipseLinkConfigurationTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testMongoCredentials() {
    // GIVEN
    final Map<String, Object> properties = new HashMap<>();
    when(descriptor.getProperties()).thenReturn(properties);

    properties.put("eclipselink.nosql.property.mongo.db", "foo");
    properties.put("eclipselink.nosql.property.user", "user");
    properties.put("eclipselink.nosql.property.password", "pass");

    final ConfigurationFactory factory = new ConfigurationFactoryImpl();

    // WHEN
    final Configuration configuration = factory.createConfiguration(descriptor);

    // THEN
    assertThat(configuration, notNullValue());

    final List<MongoCredential> credentials = configuration.getCredentials();
    assertThat(credentials, notNullValue());
    assertThat(credentials.size(), equalTo(1));

    final MongoCredential mongoCredential = credentials.get(0);
    assertThat(mongoCredential, notNullValue());
    assertThat(mongoCredential.getUserName(), equalTo("user"));
    assertThat(mongoCredential.getPassword(), equalTo("pass".toCharArray()));
    assertThat(mongoCredential.getSource(), equalTo("admin"));
    assertThat(mongoCredential.getAuthenticationMechanism(), equalTo(AuthenticationMechanism.PLAIN));
}
 
Example #17
Source File: HibernateOgmConfigurationTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testMongoCredentialsWithAuthenticationDatabaseSet() {
    // GIVEN
    final Map<String, Object> properties = new HashMap<>();
    when(descriptor.getProperties()).thenReturn(properties);

    properties.put("hibernate.ogm.datastore.database", "foo");
    properties.put("hibernate.ogm.datastore.username", "user");
    properties.put("hibernate.ogm.datastore.password", "pass");
    properties.put("hibernate.ogm.mongodb.authentication_database", "auth");

    final ConfigurationFactory factory = new ConfigurationFactoryImpl();

    // WHEN
    final Configuration configuration = factory.createConfiguration(descriptor);

    // THEN
    assertThat(configuration, notNullValue());

    final List<MongoCredential> credentials = configuration.getCredentials();
    assertThat(credentials, notNullValue());
    assertThat(credentials.size(), equalTo(1));

    final MongoCredential mongoCredential = credentials.get(0);
    assertThat(mongoCredential, notNullValue());
    assertThat(mongoCredential.getUserName(), equalTo("user"));
    assertThat(mongoCredential.getPassword(), equalTo("pass".toCharArray()));
    assertThat(mongoCredential.getSource(), equalTo("auth"));
    assertThat(mongoCredential.getAuthenticationMechanism(), equalTo(AuthenticationMechanism.PLAIN));
}
 
Example #18
Source File: HibernateOgmConfigurationTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testMongoCredentialsWithoutSpecifyingAuthenticationDatabase() {
    // GIVEN
    final Map<String, Object> properties = new HashMap<>();
    when(descriptor.getProperties()).thenReturn(properties);

    properties.put("hibernate.ogm.datastore.database", "foo");
    properties.put("hibernate.ogm.datastore.username", "user");
    properties.put("hibernate.ogm.datastore.password", "pass");

    final ConfigurationFactory factory = new ConfigurationFactoryImpl();

    // WHEN
    final Configuration configuration = factory.createConfiguration(descriptor);

    // THEN
    assertThat(configuration, notNullValue());

    final List<MongoCredential> credentials = configuration.getCredentials();
    assertThat(credentials, notNullValue());
    assertThat(credentials.size(), equalTo(1));

    final MongoCredential mongoCredential = credentials.get(0);
    assertThat(mongoCredential, notNullValue());
    assertThat(mongoCredential.getUserName(), equalTo("user"));
    assertThat(mongoCredential.getPassword(), equalTo("pass".toCharArray()));
    assertThat(mongoCredential.getSource(), equalTo("admin"));
    assertThat(mongoCredential.getAuthenticationMechanism(), equalTo(AuthenticationMechanism.PLAIN));
}
 
Example #19
Source File: DataNucleusConfigurationTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testMongoCredentials() {
    // GIVEN
    final Map<String, Object> properties = new HashMap<>();
    when(descriptor.getProperties()).thenReturn(properties);

    properties.put("datanucleus.ConnectionURL", "mongodb:/foo");
    properties.put("datanucleus.ConnectionUserName", "user");
    properties.put("datanucleus.ConnectionPassword", "pass");

    final ConfigurationFactory factory = new ConfigurationFactoryImpl();

    // WHEN
    final Configuration configuration = factory.createConfiguration(descriptor);

    // THEN
    assertThat(configuration, notNullValue());

    final List<MongoCredential> credentials = configuration.getCredentials();
    assertThat(credentials, notNullValue());
    assertThat(credentials.size(), equalTo(1));

    final MongoCredential mongoCredential = credentials.get(0);
    assertThat(mongoCredential, notNullValue());
    assertThat(mongoCredential.getUserName(), equalTo("user"));
    assertThat(mongoCredential.getPassword(), equalTo("pass".toCharArray()));
    assertThat(mongoCredential.getSource(), equalTo("admin"));
    assertThat(mongoCredential.getAuthenticationMechanism(), equalTo(AuthenticationMechanism.PLAIN));
}
 
Example #20
Source File: KunderaConfigurationTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testMongoCredentials() {
    // GIVEN
    final Map<String, Object> properties = new HashMap<>();
    when(descriptor.getProperties()).thenReturn(properties);

    properties.put("kundera.keyspace", "foo");
    properties.put("kundera.username", "user");
    properties.put("kundera.password", "pass");

    final ConfigurationFactory factory = new ConfigurationFactoryImpl();

    // WHEN
    final Configuration configuration = factory.createConfiguration(descriptor);

    // THEN
    assertThat(configuration, notNullValue());

    final List<MongoCredential> credentials = configuration.getCredentials();
    assertThat(credentials, notNullValue());
    assertThat(credentials.size(), equalTo(1));

    final MongoCredential mongoCredential = credentials.get(0);
    assertThat(mongoCredential, notNullValue());
    assertThat(mongoCredential.getUserName(), equalTo("user"));
    assertThat(mongoCredential.getPassword(), equalTo("pass".toCharArray()));
    assertThat(mongoCredential.getSource(), equalTo("admin"));
    assertThat(mongoCredential.getAuthenticationMechanism(), equalTo(AuthenticationMechanism.PLAIN));
}
 
Example #21
Source File: MongoClients.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private AuthenticationMechanism getAuthenticationMechanism(String authMechanism) {
    AuthenticationMechanism mechanism;
    try {
        mechanism = AuthenticationMechanism.fromMechanismName(authMechanism.toUpperCase());
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Invalid authMechanism '" + authMechanism + "'");
    }
    return mechanism;
}
 
Example #22
Source File: CredentialListParser.java    From vertx-mongo-client with Apache License 2.0 4 votes vote down vote up
public CredentialListParser(JsonObject config) {
  String username = config.getString("username");
  // AuthMechanism
  AuthenticationMechanism mechanism = null;
  String authMechanism = config.getString("authMechanism");
  if (authMechanism != null) {
    mechanism = getAuthenticationMechanism(authMechanism);
  }
  credentials = new ArrayList<>();
  if (username == null) {
    if (mechanism == MONGODB_X509) {
      credentials.add(MongoCredential.createMongoX509Credential());
    }
  } else {
    String passwd = config.getString("password");
    char[] password = (passwd == null) ? null : passwd.toCharArray();
    // See https://github.com/vert-x3/vertx-mongo-client/issues/46 - 'admin' as default is a security
    // concern, use  the 'db_name' if none is set.
    String authSource = config.getString("authSource",
      config.getString("db_name", MongoClientImpl.DEFAULT_DB_NAME));

    // MongoCredential
    String gssapiServiceName = config.getString("gssapiServiceName");
    MongoCredential credential;
    if (mechanism == GSSAPI) {
      credential = MongoCredential.createGSSAPICredential(username);
      credential = getMongoCredential(gssapiServiceName, credential);
    } else if (mechanism == PLAIN) {
      credential = MongoCredential.createPlainCredential(username, authSource, password);
    } else if (mechanism == MONGODB_X509) {
      credential = MongoCredential.createMongoX509Credential(username);
    } else if (mechanism == SCRAM_SHA_1) {
      credential = MongoCredential.createScramSha1Credential(username, authSource, password);
    } else if (mechanism == SCRAM_SHA_256) {
      credential = MongoCredential.createScramSha256Credential(username, authSource, password);
    } else if (mechanism == null) {
      credential = MongoCredential.createCredential(username, authSource, password);
    } else {
      throw new IllegalArgumentException("Unsupported authentication mechanism " + mechanism);
    }

    credentials.add(credential);
  }
}
 
Example #23
Source File: MongoConsoleRunner.java    From nosql4idea with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
protected Process createProcess() throws ExecutionException {

    NoSqlConfiguration noSqlConfiguration = NoSqlConfiguration.getInstance(getProject());
    String shellPath = noSqlConfiguration.getShellPath(DatabaseVendor.MONGO);
    final GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setExePath(shellPath);

    commandLine.addParameter(MongoUtils.buildMongoUrl(serverConfiguration, database));

    String shellWorkingDir = serverConfiguration.getShellWorkingDir();
    if (StringUtils.isNotBlank(shellWorkingDir)) {
        commandLine.withWorkDirectory(shellWorkingDir);
    }

    AuthenticationSettings authenticationSettings = serverConfiguration.getAuthenticationSettings();

    String username = authenticationSettings.getUsername();
    if (StringUtils.isNotBlank(username)) {
        commandLine.addParameter("--username");
        commandLine.addParameter(username);
    }

    String password = authenticationSettings.getPassword();
    if (StringUtils.isNotBlank(password)) {
        commandLine.addParameter("--password");
        commandLine.addParameter(password);
    }

    MongoExtraSettings mongoExtraSettings = new MongoExtraSettings(authenticationSettings.getExtras());
    String authenticationDatabase = mongoExtraSettings.getAuthenticationDatabase();
    if (StringUtils.isNotBlank(authenticationDatabase)) {
        commandLine.addParameter("--authenticationDatabase");
        commandLine.addParameter(authenticationDatabase);
    }

    AuthenticationMechanism authenticationMecanism = mongoExtraSettings.getAuthenticationMechanism();
    if (authenticationMecanism != null) {
        commandLine.addParameter("--authenticationMecanism");
        commandLine.addParameter(authenticationMecanism.getMechanismName());
    }

    String shellArgumentsLine = serverConfiguration.getShellArgumentsLine();
    if (StringUtils.isNotBlank(shellArgumentsLine)) {
        commandLine.addParameters(shellArgumentsLine.split(" "));
    }

    return commandLine.createProcess();
}
 
Example #24
Source File: MongoExtraSettings.java    From nosql4idea with Apache License 2.0 4 votes vote down vote up
public AuthenticationMechanism getAuthenticationMechanism() {
    String authMecanism = extras.get(AUTH_MECHANISM);
    return authMecanism == null ? null : AuthenticationMechanism.valueOf(authMecanism);
}
 
Example #25
Source File: MongoExtraSettings.java    From nosql4idea with Apache License 2.0 4 votes vote down vote up
public void setAuthenticationMechanism(AuthenticationMechanism authenticationMechanism) {
    if (authenticationMechanism != null) {
        extras.put(AUTH_MECHANISM, authenticationMechanism.name());
    }
}
 
Example #26
Source File: MongoClientURIBuilder.java    From nosql4idea with Apache License 2.0 4 votes vote down vote up
public MongoClientURIBuilder setAuthenticationMecanism(@NotNull AuthenticationMechanism authenticationMecanism) {
    this.authenticationMecanism = authenticationMecanism;
    return this;
}
 
Example #27
Source File: MongoClients.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private MongoCredential createMongoCredential(MongoClientConfig config) {
    String username = config.credentials.username.orElse(null);
    if (username == null) {
        return null;
    }

    char[] password = config.credentials.password.map(String::toCharArray).orElse(null);
    // get the authsource, or the database from the config, or 'admin' as it is the default auth source in mongo
    // and null is not allowed
    String authSource = config.credentials.authSource.orElse(config.database.orElse("admin"));
    // AuthMechanism
    AuthenticationMechanism mechanism = null;
    Optional<String> maybeMechanism = config.credentials.authMechanism;
    if (maybeMechanism.isPresent()) {
        mechanism = getAuthenticationMechanism(maybeMechanism.get());
    }

    // Create the MongoCredential instance.
    MongoCredential credential;
    if (mechanism == GSSAPI) {
        credential = MongoCredential.createGSSAPICredential(username);
    } else if (mechanism == PLAIN) {
        credential = MongoCredential.createPlainCredential(username, authSource, password);
    } else if (mechanism == MONGODB_X509) {
        credential = MongoCredential.createMongoX509Credential(username);
    } else if (mechanism == SCRAM_SHA_1) {
        credential = MongoCredential.createScramSha1Credential(username, authSource, password);
    } else if (mechanism == null) {
        credential = MongoCredential.createCredential(username, authSource, password);
    } else {
        throw new IllegalArgumentException("Unsupported authentication mechanism " + mechanism);
    }

    //add the properties
    if (!config.credentials.authMechanismProperties.isEmpty()) {
        for (Map.Entry<String, String> entry : config.credentials.authMechanismProperties.entrySet()) {
            credential = credential.withMechanismProperty(entry.getKey(), entry.getValue());
        }
    }

    return credential;
}