DATAMONGO-2016 - Fix username/password extraction in MongoCredentialPropertyEditor.

MongoCredentialPropertyEditor inspects now the connection URI for the appropriate delimiter tokens. Previously, inspection used the char questionmark for username/password delimiter inspection.

Original pull request: #578.
This commit is contained in:
Stephen Tyler Conrad
2018-06-30 21:54:36 -05:00
committed by Mark Paluch
parent 5a0171203d
commit fb8084c9f7
2 changed files with 33 additions and 1 deletions

View File

@@ -35,6 +35,7 @@ import com.mongodb.MongoCredential;
*
* @author Christoph Strobl
* @author Oliver Gierke
* @author Stephen Tyler Conrad
* @since 1.7
*/
public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
@@ -164,7 +165,7 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
private static Properties extractOptions(String text) {
int optionsSeparationIndex = text.lastIndexOf(OPTIONS_DELIMITER);
int dbSeparationIndex = text.lastIndexOf(OPTIONS_DELIMITER);
int dbSeparationIndex = text.lastIndexOf(DATABASE_DELIMITER);
if (optionsSeparationIndex == -1 || dbSeparationIndex > optionsSeparationIndex) {
return new Properties();

View File

@@ -34,6 +34,7 @@ import com.mongodb.MongoCredential;
* Unit tests for {@link MongoCredentialPropertyEditor}.
*
* @author Christoph Strobl
* @author Stephen Tyler Conrad
*/
public class MongoCredentialPropertyEditorUnitTests {
@@ -54,6 +55,10 @@ public class MongoCredentialPropertyEditorUnitTests {
static final String USER_4_ENCODED_PWD;
static final String USER_4_DB = "targaryen";
static final String USER_5_NAME = "lyanna";
static final String USER_5_PWD = "random?password";
static final String USER_5_DB = "mormont";
static final String USER_1_AUTH_STRING = USER_1_NAME + ":" + USER_1_PWD + "@" + USER_1_DB;
static final String USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM = USER_1_AUTH_STRING + "?uri.authMechanism=PLAIN";
@@ -66,6 +71,9 @@ public class MongoCredentialPropertyEditorUnitTests {
static final String USER_4_AUTH_STRING;
static final String USER_5_AUTH_STRING = USER_5_NAME + ":" + USER_5_PWD + "@" + USER_5_DB;
static final String USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN";
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,
@@ -81,6 +89,11 @@ public class MongoCredentialPropertyEditorUnitTests {
static final MongoCredential USER_4_CREDENTIALS = MongoCredential.createCredential(USER_4_PLAIN_NAME, USER_4_DB,
USER_4_PLAIN_PWD.toCharArray());
static final MongoCredential USER_5_CREDENTIALS = MongoCredential.createCredential(USER_5_NAME, USER_5_DB,
USER_5_PWD.toCharArray());
static final MongoCredential USER_5_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_5_NAME, USER_5_DB,
USER_5_PWD.toCharArray());
MongoCredentialPropertyEditor editor;
static {
@@ -239,4 +252,22 @@ public class MongoCredentialPropertyEditorUnitTests {
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_4_CREDENTIALS));
}
@Test //DATAMONGO-2016
@SuppressWarnings("unchecked")
public void passwordWithQuestionMarkShouldNotBeInterpretedAsOptionString() {
editor.setAsText(USER_5_AUTH_STRING);
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_5_CREDENTIALS));
}
@Test //DATAMONGO-2016
@SuppressWarnings("unchecked")
public void passwordWithQuestionMarkShouldNotBreakParsingOfOptionString() {
editor.setAsText(USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM);
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_5_CREDENTIALS_PLAIN_AUTH));
}
}