Skip to content

Commit 93880a6

Browse files
authored
Merge pull request #132 from eclipse/mongo_authenticate
Mongo authenticate
2 parents 308177b + 06672a3 commit 93880a6

File tree

4 files changed

+238
-2
lines changed

4 files changed

+238
-2
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2019 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
16+
package org.jnosql.diana.mongodb.document;
17+
18+
import com.mongodb.AuthenticationMechanism;
19+
import com.mongodb.MongoCredential;
20+
import org.jnosql.diana.api.Configurations;
21+
import org.jnosql.diana.api.JNoSQLException;
22+
import org.jnosql.diana.api.Settings;
23+
24+
import java.util.Arrays;
25+
import java.util.Optional;
26+
import java.util.function.Supplier;
27+
28+
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_MECHANISM;
29+
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_SOURCE;
30+
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.PASSWORD;
31+
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.USER;
32+
33+
final class MongoAuthentication {
34+
35+
private MongoAuthentication() {
36+
}
37+
38+
static Optional<MongoCredential> of(Settings settings) {
39+
40+
41+
Optional<String> user = settings.get(Arrays.asList(USER.get(),
42+
Configurations.USER.get()))
43+
.map(Object::toString);
44+
45+
Optional<char[]> password = settings.get(Arrays.asList(PASSWORD.get(),
46+
Configurations.PASSWORD.get()))
47+
.map(Object::toString).map(String::toCharArray);
48+
49+
Optional<String> source = settings.get(AUTHENTICATION_SOURCE.get())
50+
.map(Object::toString);
51+
52+
AuthenticationMechanism mechanism = settings.get(AUTHENTICATION_MECHANISM.get())
53+
.map(Object::toString)
54+
.map(AuthenticationMechanism::fromMechanismName)
55+
.orElse(AuthenticationMechanism.PLAIN);
56+
57+
if (!user.isPresent()) {
58+
return Optional.empty();
59+
}
60+
61+
switch (mechanism) {
62+
case PLAIN:
63+
return Optional.of(MongoCredential.createPlainCredential(user.orElseThrow(missingExceptionUser()),
64+
source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword())));
65+
case GSSAPI:
66+
return Optional.of(MongoCredential.createGSSAPICredential(user.orElseThrow(missingExceptionUser())));
67+
case SCRAM_SHA_1:
68+
return Optional.of(MongoCredential.createScramSha1Credential(user.orElseThrow(missingExceptionUser()),
69+
source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword())));
70+
case MONGODB_X509:
71+
return Optional.of(MongoCredential.createMongoX509Credential(user.orElseThrow(missingExceptionUser())));
72+
case SCRAM_SHA_256:
73+
return Optional.of(MongoCredential.createScramSha256Credential(user.orElseThrow(missingExceptionUser()),
74+
source.orElseThrow(missingExceptionSource()), password.orElseThrow(missingExceptionPassword())));
75+
default:
76+
throw new JNoSQLException("There is not support to the type: " + mechanism);
77+
}
78+
79+
}
80+
81+
82+
private static Supplier<JNoSQLException> missingExceptionUser() {
83+
return missingException("user");
84+
}
85+
86+
private static Supplier<JNoSQLException> missingExceptionPassword() {
87+
return missingException("password");
88+
}
89+
90+
private static Supplier<JNoSQLException> missingExceptionSource() {
91+
return missingException("source");
92+
}
93+
94+
95+
private static Supplier<JNoSQLException> missingException(String parameter) {
96+
return () -> new JNoSQLException("There is a missing parameter in mongoDb authentication: " + parameter);
97+
}
98+
99+
100+
}

mongodb-driver/src/main/java/org/jnosql/diana/mongodb/document/MongoDBDocumentConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.jnosql.diana.mongodb.document;
1717

1818
import com.mongodb.MongoClient;
19+
import com.mongodb.MongoCredential;
1920
import com.mongodb.ServerAddress;
2021
import com.mongodb.async.client.MongoClientSettings;
2122
import com.mongodb.async.client.MongoClients;
@@ -31,6 +32,7 @@
3132
import java.util.List;
3233
import java.util.Map;
3334
import java.util.Objects;
35+
import java.util.Optional;
3436
import java.util.stream.Collectors;
3537

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

110-
return new MongoDBDocumentCollectionManagerFactory(new MongoClient(servers));
112+
Optional<MongoCredential> credential = MongoAuthentication.of(settings);
113+
MongoClient mongoClient = credential.map(c -> new MongoClient(servers, c, null))
114+
.orElseGet(() -> new MongoClient(servers));
115+
116+
return new MongoDBDocumentCollectionManagerFactory(mongoClient);
111117
}
112118

113119
public MongoDBDocumentCollectionManagerFactory get(String pathFileConfig) throws NullPointerException {

mongodb-driver/src/main/java/org/jnosql/diana/mongodb/document/MongoDBDocumentConfigurations.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
import java.util.function.Supplier;
1919

2020
public enum MongoDBDocumentConfigurations implements Supplier<String> {
21-
HOST("mongodb.host"), USER("mongodb.user"), PASSWORD("mongodb.password");
21+
HOST("mongodb.host"), USER("mongodb.user"),
22+
PASSWORD("mongodb.password"),
23+
AUTHENTICATION_SOURCE("mongodb.authentication.source"),
24+
AUTHENTICATION_MECHANISM("mongodb.authentication.mechanism");
2225

2326
private final String configuration;
2427

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2019 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.jnosql.diana.mongodb.document;
16+
17+
import com.mongodb.AuthenticationMechanism;
18+
import com.mongodb.MongoCredential;
19+
import org.jnosql.diana.api.JNoSQLException;
20+
import org.jnosql.diana.api.Settings;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.Arrays;
24+
25+
import static com.mongodb.AuthenticationMechanism.GSSAPI;
26+
import static com.mongodb.AuthenticationMechanism.PLAIN;
27+
import static com.mongodb.AuthenticationMechanism.SCRAM_SHA_1;
28+
import static com.mongodb.AuthenticationMechanism.SCRAM_SHA_256;
29+
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_MECHANISM;
30+
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.AUTHENTICATION_SOURCE;
31+
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.PASSWORD;
32+
import static org.jnosql.diana.mongodb.document.MongoDBDocumentConfigurations.USER;
33+
import static org.junit.jupiter.api.Assertions.assertEquals;
34+
import static org.junit.jupiter.api.Assertions.assertThrows;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
36+
37+
class MongoAuthenticationTest {
38+
39+
@Test
40+
public void shouldReturnErrorWhenTheNumberParameterIsInvalid() {
41+
Settings settings = Settings.builder().put(USER.get(), "value")
42+
.build();
43+
44+
assertThrows(JNoSQLException.class, () -> MongoAuthentication.of(settings));
45+
46+
}
47+
48+
@Test
49+
public void shouldReturnOneAuthentication() {
50+
Settings settings = Settings.builder()
51+
.put(AUTHENTICATION_SOURCE.get(), "database")
52+
.put(PASSWORD.get(), "password")
53+
.put(USER.get(), "user")
54+
.build();
55+
56+
MongoCredential credential = MongoAuthentication.of(settings).get();
57+
assertEquals("database", credential.getSource());
58+
assertTrue(Arrays.equals("password".toCharArray(), credential.getPassword()));
59+
assertEquals("user", credential.getUserName());
60+
assertEquals(PLAIN.getMechanismName(), credential.getMechanism());
61+
62+
}
63+
64+
@Test
65+
public void shouldReturnOneAuthenticationWithGSSAPI() {
66+
Settings settings = Settings.builder()
67+
.put(AUTHENTICATION_SOURCE.get(), "database")
68+
.put(PASSWORD.get(), "password")
69+
.put(USER.get(), "user")
70+
.put(AUTHENTICATION_MECHANISM.get(), "GSSAPI")
71+
.build();
72+
73+
MongoCredential credential = MongoAuthentication.of(settings).get();
74+
assertEquals("$external", credential.getSource());
75+
assertEquals("user", credential.getUserName());
76+
assertEquals(GSSAPI.getMechanismName(), credential.getMechanism());
77+
78+
}
79+
80+
@Test
81+
public void shouldReturnOneAuthenticationWithMongoX509() {
82+
Settings settings = Settings.builder()
83+
.put(AUTHENTICATION_SOURCE.get(), "database")
84+
.put(PASSWORD.get(), "password")
85+
.put(USER.get(), "user")
86+
.put(AUTHENTICATION_MECHANISM.get(), "MONGODB-X509")
87+
.build();
88+
89+
MongoCredential credential = MongoAuthentication.of(settings).get();
90+
assertEquals("$external", credential.getSource());
91+
assertEquals("user", credential.getUserName());
92+
assertEquals(AuthenticationMechanism.MONGODB_X509.getMechanismName(), credential.getMechanism());
93+
}
94+
95+
@Test
96+
public void shouldReturnOneAuthenticationWithSCRAMSHA1() {
97+
Settings settings = Settings.builder()
98+
.put(AUTHENTICATION_SOURCE.get(), "database")
99+
.put(PASSWORD.get(), "password")
100+
.put(USER.get(), "user")
101+
.put(AUTHENTICATION_MECHANISM.get(), "SCRAM-SHA-1")
102+
.build();
103+
104+
MongoCredential credential = MongoAuthentication.of(settings).get();
105+
assertEquals("database", credential.getSource());
106+
assertTrue(Arrays.equals("password".toCharArray(), credential.getPassword()));
107+
assertEquals("user", credential.getUserName());
108+
assertEquals(SCRAM_SHA_1.getMechanismName(), credential.getMechanism());
109+
}
110+
111+
@Test
112+
public void shouldReturnOneAuthenticationWithSCRAMSHA256() {
113+
Settings settings = Settings.builder()
114+
.put(AUTHENTICATION_SOURCE.get(), "database")
115+
.put(PASSWORD.get(), "password")
116+
.put(USER.get(), "user")
117+
.put(AUTHENTICATION_MECHANISM.get(), "SCRAM-SHA-256")
118+
.build();
119+
120+
MongoCredential credential = MongoAuthentication.of(settings).get();
121+
assertEquals("database", credential.getSource());
122+
assertTrue(Arrays.equals("password".toCharArray(), credential.getPassword()));
123+
assertEquals("user", credential.getUserName());
124+
assertEquals(SCRAM_SHA_256.getMechanismName(), credential.getMechanism());
125+
}
126+
127+
}

0 commit comments

Comments
 (0)