Skip to content

Commit 364920e

Browse files
steve-thousandmp911de
authored andcommitted
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.
1 parent 0495dd8 commit 364920e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,9 +29,10 @@
2929

3030
/**
3131
* Parse a {@link String} to a Collection of {@link MongoCredential}.
32-
*
32+
*
3333
* @author Christoph Strobl
3434
* @author Oliver Gierke
35+
* @author Stephen Tyler Conrad
3536
* @since 1.7
3637
*/
3738
public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
@@ -156,7 +157,7 @@ private static String extractDB(String text) {
156157
private static Properties extractOptions(String text) {
157158

158159
int optionsSeperationIndex = text.lastIndexOf(OPTIONS_DELIMINATOR);
159-
int dbSeperationIndex = text.lastIndexOf(OPTIONS_DELIMINATOR);
160+
int dbSeperationIndex = text.lastIndexOf(DATABASE_DELIMINATOR);
160161

161162
if (optionsSeperationIndex == -1 || dbSeperationIndex > optionsSeperationIndex) {
162163
return new Properties();

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2017 the original author or authors.
2+
^ * Copyright 2015-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,8 +30,9 @@
3030

3131
/**
3232
* Unit tests for {@link MongoCredentialPropertyEditor}.
33-
*
33+
*
3434
* @author Christoph Strobl
35+
* @author Stephen Tyler Conrad
3536
*/
3637
public class MongoCredentialPropertyEditorUnitTests {
3738

@@ -56,6 +57,13 @@ public class MongoCredentialPropertyEditorUnitTests {
5657
static final String USER_3_AUTH_STRING_WITH_X509_AUTH_MECHANISM = "'" + USER_3_NAME + "@" + USER_3_DB
5758
+ "?uri.authMechanism=MONGODB-X509'";
5859

60+
static final String USER_5_NAME = "lyanna";
61+
static final String USER_5_PWD = "random?password";
62+
static final String USER_5_DB = "mormont";
63+
64+
static final String USER_5_AUTH_STRING = USER_5_NAME + ":" + USER_5_PWD + "@" + USER_5_DB;
65+
static final String USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN";
66+
5967
static final MongoCredential USER_1_CREDENTIALS = MongoCredential.createCredential(USER_1_NAME, USER_1_DB,
6068
USER_1_PWD.toCharArray());
6169
static final MongoCredential USER_1_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_1_NAME,
@@ -68,6 +76,11 @@ public class MongoCredentialPropertyEditorUnitTests {
6876

6977
static final MongoCredential USER_3_CREDENTIALS_X509_AUTH = MongoCredential.createMongoX509Credential(USER_3_NAME);
7078

79+
static final MongoCredential USER_5_CREDENTIALS = MongoCredential.createCredential(USER_5_NAME, USER_5_DB,
80+
USER_5_PWD.toCharArray());
81+
static final MongoCredential USER_5_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_5_NAME, USER_5_DB,
82+
USER_5_PWD.toCharArray());
83+
7184
MongoCredentialPropertyEditor editor;
7285

7386
@Before
@@ -202,4 +215,22 @@ public void shouldThrowExceptionWhenDbIsEmptyForMongodbCR() {
202215

203216
editor.getValue();
204217
}
218+
219+
@Test //DATAMONGO-2016
220+
@SuppressWarnings("unchecked")
221+
public void passwordWithQuestionMarkShouldNotBeInterpretedAsOptionString() {
222+
223+
editor.setAsText(USER_5_AUTH_STRING);
224+
225+
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_5_CREDENTIALS));
226+
}
227+
228+
@Test //DATAMONGO-2016
229+
@SuppressWarnings("unchecked")
230+
public void passwordWithQuestionMarkShouldNotBreakParsingOfOptionString() {
231+
232+
editor.setAsText(USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM);
233+
234+
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_5_CREDENTIALS_PLAIN_AUTH));
235+
}
205236
}

0 commit comments

Comments
 (0)