Skip to content

Commit

Permalink
Merge pull request #132 from eclipse/mongo_authenticate
Browse files Browse the repository at this point in the history
Mongo authenticate
  • Loading branch information
otaviojava authored May 3, 2019
2 parents 308177b + 06672a3 commit 93880a6
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2019 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/

package org.jnosql.diana.mongodb.document;

import com.mongodb.AuthenticationMechanism;
import com.mongodb.MongoCredential;
import org.jnosql.diana.api.Configurations;
import org.jnosql.diana.api.JNoSQLException;
import org.jnosql.diana.api.Settings;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Supplier;

import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_MECHANISM;
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_SOURCE;
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.PASSWORD;
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.USER;

final class MongoAuthentication {

private MongoAuthentication() {
}

static Optional<MongoCredential> of(Settings settings) {


Optional<String> user = settings.get(Arrays.asList(USER.get(),
Configurations.USER.get()))
.map(Object::toString);

Optional<char[]> password = settings.get(Arrays.asList(PASSWORD.get(),
Configurations.PASSWORD.get()))
.map(Object::toString).map(String::toCharArray);

Optional<String> source = settings.get(AUTHENTICATION_SOURCE.get())
.map(Object::toString);

AuthenticationMechanism mechanism = settings.get(AUTHENTICATION_MECHANISM.get())
.map(Object::toString)
.map(AuthenticationMechanism::fromMechanismName)
.orElse(AuthenticationMechanism.PLAIN);

if (!user.isPresent()) {
return Optional.empty();
}

switch (mechanism) {
case PLAIN:
return Optional.of(MongoCredential.createPlainCredential(user.orElseThrow(missingExceptionUser()),
source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword())));
case GSSAPI:
return Optional.of(MongoCredential.createGSSAPICredential(user.orElseThrow(missingExceptionUser())));
case SCRAM_SHA_1:
return Optional.of(MongoCredential.createScramSha1Credential(user.orElseThrow(missingExceptionUser()),
source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword())));
case MONGODB_X509:
return Optional.of(MongoCredential.createMongoX509Credential(user.orElseThrow(missingExceptionUser())));
case SCRAM_SHA_256:
return Optional.of(MongoCredential.createScramSha256Credential(user.orElseThrow(missingExceptionUser()),
source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword())));
default:
throw new JNoSQLException("There is not support to the type: " + mechanism);
}

}


private static Supplier<JNoSQLException> missingExceptionUser() {
return missingException("user");
}

private static Supplier<JNoSQLException> missingExceptionPassword() {
return missingException("password");
}

private static Supplier<JNoSQLException> missingExceptionSource() {
return missingException("source");
}


private static Supplier<JNoSQLException> missingException(String parameter) {
return () -> new JNoSQLException("There is a missing parameter in mongoDb authentication: " + parameter);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.jnosql.diana.mongodb.document;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.async.client.MongoClients;
Expand All @@ -31,6 +32,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -107,7 +109,11 @@ public MongoDBDocumentCollectionManagerFactory get(Settings settings) throws Nul
return new MongoDBDocumentCollectionManagerFactory(new MongoClient());
}

return new MongoDBDocumentCollectionManagerFactory(new MongoClient(servers));
Optional<MongoCredential> credential = MongoAuthentication.of(settings);
MongoClient mongoClient = credential.map(c -> new MongoClient(servers, c, null))
.orElseGet(() -> new MongoClient(servers));

return new MongoDBDocumentCollectionManagerFactory(mongoClient);
}

public MongoDBDocumentCollectionManagerFactory get(String pathFileConfig) throws NullPointerException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import java.util.function.Supplier;

public enum MongoDBDocumentConfigurations implements Supplier<String> {
HOST("mongodb.host"), USER("mongodb.user"), PASSWORD("mongodb.password");
HOST("mongodb.host"), USER("mongodb.user"),
PASSWORD("mongodb.password"),
AUTHENTICATION_SOURCE("mongodb.authentication.source"),
AUTHENTICATION_MECHANISM("mongodb.authentication.mechanism");

private final String configuration;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2019 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.jnosql.diana.mongodb.document;

import com.mongodb.AuthenticationMechanism;
import com.mongodb.MongoCredential;
import org.jnosql.diana.api.JNoSQLException;
import org.jnosql.diana.api.Settings;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static com.mongodb.AuthenticationMechanism.GSSAPI;
import static com.mongodb.AuthenticationMechanism.PLAIN;
import static com.mongodb.AuthenticationMechanism.SCRAM_SHA_1;
import static com.mongodb.AuthenticationMechanism.SCRAM_SHA_256;
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_MECHANISM;
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_SOURCE;
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.PASSWORD;
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.USER;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class MongoAuthenticationTest {

@Test
public void shouldReturnErrorWhenTheNumberParameterIsInvalid() {
Settings settings = Settings.builder().put(USER.get(), "value")
.build();

assertThrows(JNoSQLException.class, () -> MongoAuthentication.of(settings));

}

@Test
public void shouldReturnOneAuthentication() {
Settings settings = Settings.builder()
.put(AUTHENTICATION_SOURCE.get(), "database")
.put(PASSWORD.get(), "password")
.put(USER.get(), "user")
.build();

MongoCredential credential = MongoAuthentication.of(settings).get();
assertEquals("database", credential.getSource());
assertTrue(Arrays.equals("password".toCharArray(), credential.getPassword()));
assertEquals("user", credential.getUserName());
assertEquals(PLAIN.getMechanismName(), credential.getMechanism());

}

@Test
public void shouldReturnOneAuthenticationWithGSSAPI() {
Settings settings = Settings.builder()
.put(AUTHENTICATION_SOURCE.get(), "database")
.put(PASSWORD.get(), "password")
.put(USER.get(), "user")
.put(AUTHENTICATION_MECHANISM.get(), "GSSAPI")
.build();

MongoCredential credential = MongoAuthentication.of(settings).get();
assertEquals("$external", credential.getSource());
assertEquals("user", credential.getUserName());
assertEquals(GSSAPI.getMechanismName(), credential.getMechanism());

}

@Test
public void shouldReturnOneAuthenticationWithMongoX509() {
Settings settings = Settings.builder()
.put(AUTHENTICATION_SOURCE.get(), "database")
.put(PASSWORD.get(), "password")
.put(USER.get(), "user")
.put(AUTHENTICATION_MECHANISM.get(), "MONGODB-X509")
.build();

MongoCredential credential = MongoAuthentication.of(settings).get();
assertEquals("$external", credential.getSource());
assertEquals("user", credential.getUserName());
assertEquals(AuthenticationMechanism.MONGODB_X509.getMechanismName(), credential.getMechanism());
}

@Test
public void shouldReturnOneAuthenticationWithSCRAMSHA1() {
Settings settings = Settings.builder()
.put(AUTHENTICATION_SOURCE.get(), "database")
.put(PASSWORD.get(), "password")
.put(USER.get(), "user")
.put(AUTHENTICATION_MECHANISM.get(), "SCRAM-SHA-1")
.build();

MongoCredential credential = MongoAuthentication.of(settings).get();
assertEquals("database", credential.getSource());
assertTrue(Arrays.equals("password".toCharArray(), credential.getPassword()));
assertEquals("user", credential.getUserName());
assertEquals(SCRAM_SHA_1.getMechanismName(), credential.getMechanism());
}

@Test
public void shouldReturnOneAuthenticationWithSCRAMSHA256() {
Settings settings = Settings.builder()
.put(AUTHENTICATION_SOURCE.get(), "database")
.put(PASSWORD.get(), "password")
.put(USER.get(), "user")
.put(AUTHENTICATION_MECHANISM.get(), "SCRAM-SHA-256")
.build();

MongoCredential credential = MongoAuthentication.of(settings).get();
assertEquals("database", credential.getSource());
assertTrue(Arrays.equals("password".toCharArray(), credential.getPassword()));
assertEquals("user", credential.getUserName());
assertEquals(SCRAM_SHA_256.getMechanismName(), credential.getMechanism());
}

}

0 comments on commit 93880a6

Please sign in to comment.