DATAMONGO-2051 - Add support for SCRAM-SHA-256 authentication mechanism to MongoCredentialPropertyEditor.

Original pull request: #598.
Related pull request: #597.
This commit is contained in:
Christoph Strobl
2018-08-13 09:29:21 +02:00
committed by Mark Paluch
parent 03246f04b8
commit e4da45baed
2 changed files with 21 additions and 1 deletions

View File

@@ -100,6 +100,12 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
verifyDatabasePresent(database);
credentials.add(MongoCredential.createScramSha1Credential(userNameAndPassword[0], database,
userNameAndPassword[1].toCharArray()));
} else if (MongoCredential.SCRAM_SHA_256_MECHANISM.equals(authMechanism)) {
verifyUsernameAndPasswordPresent(userNameAndPassword);
verifyDatabasePresent(database);
credentials.add(MongoCredential.createScramSha256Credential(userNameAndPassword[0], database,
userNameAndPassword[1].toCharArray()));
} else {
throw new IllegalArgumentException(
String.format("Cannot create MongoCredentials for unknown auth mechanism '%s'!", authMechanism));

View File

@@ -73,6 +73,9 @@ public class MongoCredentialPropertyEditorUnitTests {
static final String USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN";
static final String USER_5_AUTH_STRING_WITH_QUERY_ARGS = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN&foo=&bar";
static final String SCRAM_SHA_256_AUTH_STRING = USER_1_NAME + ":" + USER_1_PWD + "@" + USER_1_DB
+ "?uri.authMechanism=SCRAM-SHA-256";
static final MongoCredential USER_1_CREDENTIALS = MongoCredential.createCredential(USER_1_NAME, USER_1_DB,
USER_1_PWD.toCharArray());
static final MongoCredential USER_1_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_1_NAME,
@@ -93,6 +96,9 @@ public class MongoCredentialPropertyEditorUnitTests {
static final MongoCredential USER_5_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_5_NAME,
USER_5_DB, USER_5_PWD.toCharArray());
static final MongoCredential SCRAM_SHA_256_CREDENTIALS = MongoCredential.createScramSha256Credential(USER_1_NAME,
USER_1_DB, USER_1_PWD.toCharArray());
MongoCredentialPropertyEditor editor;
static {
@@ -223,7 +229,7 @@ public class MongoCredentialPropertyEditorUnitTests {
editor.setAsText("tyrion?uri.authMechanism=MONGODB-X509");
assertThat(getValue()). contains(MongoCredential.createMongoX509Credential("tyrion"));
assertThat(getValue()).contains(MongoCredential.createMongoX509Credential("tyrion"));
}
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1257
@@ -269,6 +275,14 @@ public class MongoCredentialPropertyEditorUnitTests {
assertThat(getValue()).contains(USER_5_CREDENTIALS_PLAIN_AUTH);
}
@Test // DATAMONGO-2051
public void shouldReturnScramSha256Credentials() {
editor.setAsText(SCRAM_SHA_256_AUTH_STRING);
assertThat(getValue()).contains(SCRAM_SHA_256_CREDENTIALS);
}
@Test(expected = IllegalArgumentException.class) // DATAMONGO-2016
@SuppressWarnings("unchecked")
public void failsGracefullyOnEmptyQueryArgument() {