17
17
18
18
import java .beans .PropertyEditorSupport ;
19
19
import java .util .ArrayList ;
20
+ import java .util .Arrays ;
20
21
import java .util .List ;
21
22
import java .util .Properties ;
23
+ import java .util .regex .Matcher ;
24
+ import java .util .regex .Pattern ;
22
25
23
26
import org .springframework .util .StringUtils ;
24
27
32
35
*/
33
36
public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
34
37
38
+ private static final Pattern GROUP_PATTERN = Pattern .compile ("(\\ \\ ?')(.*?)\\ 1" );
39
+
35
40
private static final String AUTH_MECHANISM_KEY = "uri.authMechanism" ;
36
41
private static final String USERNAME_PASSWORD_DELIMINATOR = ":" ;
37
42
private static final String DATABASE_DELIMINATOR = "@" ;
@@ -51,11 +56,7 @@ public void setAsText(String text) throws IllegalArgumentException {
51
56
52
57
List <MongoCredential > credentials = new ArrayList <MongoCredential >();
53
58
54
- for (String credentialString : text .split ("," )) {
55
-
56
- if (!text .contains (USERNAME_PASSWORD_DELIMINATOR ) || !text .contains (DATABASE_DELIMINATOR )) {
57
- throw new IllegalArgumentException ("Credentials need to be in format 'username:password@database'!" );
58
- }
59
+ for (String credentialString : extractCredentialsString (text )) {
59
60
60
61
String [] userNameAndPassword = extractUserNameAndPassword (credentialString );
61
62
String database = extractDB (credentialString );
@@ -68,16 +69,29 @@ public void setAsText(String text) throws IllegalArgumentException {
68
69
String authMechanism = options .getProperty (AUTH_MECHANISM_KEY );
69
70
70
71
if (MongoCredential .GSSAPI_MECHANISM .equals (authMechanism )) {
72
+
73
+ verifyUserNamePresent (userNameAndPassword );
71
74
credentials .add (MongoCredential .createGSSAPICredential (userNameAndPassword [0 ]));
72
75
} else if (MongoCredential .MONGODB_CR_MECHANISM .equals (authMechanism )) {
76
+
77
+ verifyUsernameAndPasswordPresent (userNameAndPassword );
78
+ verifyDatabasePresent (database );
73
79
credentials .add (MongoCredential .createMongoCRCredential (userNameAndPassword [0 ], database ,
74
80
userNameAndPassword [1 ].toCharArray ()));
75
81
} else if (MongoCredential .MONGODB_X509_MECHANISM .equals (authMechanism )) {
82
+
83
+ verifyUserNamePresent (userNameAndPassword );
76
84
credentials .add (MongoCredential .createMongoX509Credential (userNameAndPassword [0 ]));
77
85
} else if (MongoCredential .PLAIN_MECHANISM .equals (authMechanism )) {
86
+
87
+ verifyUsernameAndPasswordPresent (userNameAndPassword );
88
+ verifyDatabasePresent (database );
78
89
credentials .add (MongoCredential .createPlainCredential (userNameAndPassword [0 ], database ,
79
90
userNameAndPassword [1 ].toCharArray ()));
80
91
} else if (MongoCredential .SCRAM_SHA_1_MECHANISM .equals (authMechanism )) {
92
+
93
+ verifyUsernameAndPasswordPresent (userNameAndPassword );
94
+ verifyDatabasePresent (database );
81
95
credentials .add (MongoCredential .createScramSha1Credential (userNameAndPassword [0 ], database ,
82
96
userNameAndPassword [1 ].toCharArray ()));
83
97
} else {
@@ -86,6 +100,9 @@ public void setAsText(String text) throws IllegalArgumentException {
86
100
}
87
101
}
88
102
} else {
103
+
104
+ verifyUsernameAndPasswordPresent (userNameAndPassword );
105
+ verifyDatabasePresent (database );
89
106
credentials .add (MongoCredential .createCredential (userNameAndPassword [0 ], database ,
90
107
userNameAndPassword [1 ].toCharArray ()));
91
108
}
@@ -94,17 +111,45 @@ public void setAsText(String text) throws IllegalArgumentException {
94
111
setValue (credentials );
95
112
}
96
113
114
+ private List <String > extractCredentialsString (String source ) {
115
+
116
+ Matcher matcher = GROUP_PATTERN .matcher (source );
117
+
118
+ List <String > list = new ArrayList <String >();
119
+ while (matcher .find ()) {
120
+
121
+ String value = StringUtils .trimLeadingCharacter (matcher .group (), '\'' );
122
+ list .add (StringUtils .trimTrailingCharacter (value , '\'' ));
123
+ }
124
+
125
+ if (!list .isEmpty ()) {
126
+ return list ;
127
+ }
128
+ return Arrays .asList (source .split ("," ));
129
+ }
130
+
97
131
private static String [] extractUserNameAndPassword (String text ) {
98
132
99
- int dbSeperationIndex = text .lastIndexOf (DATABASE_DELIMINATOR );
100
- String userNameAndPassword = text .substring (0 , dbSeperationIndex );
133
+ int index = text .lastIndexOf (DATABASE_DELIMINATOR );
134
+
135
+ if (index == -1 ) {
136
+ index = text .lastIndexOf (OPTIONS_DELIMINATOR );
137
+ }
138
+ if (index == -1 ) {
139
+ return new String [] {};
140
+ }
141
+ String userNameAndPassword = text .substring (0 , index );
101
142
return userNameAndPassword .split (USERNAME_PASSWORD_DELIMINATOR );
102
143
}
103
144
104
145
private static String extractDB (String text ) {
105
146
106
147
int dbSeperationIndex = text .lastIndexOf (DATABASE_DELIMINATOR );
107
148
149
+ if (dbSeperationIndex == -1 ) {
150
+ return "" ;
151
+ }
152
+
108
153
String tmp = text .substring (dbSeperationIndex + 1 );
109
154
int optionsSeperationIndex = tmp .lastIndexOf (OPTIONS_DELIMINATOR );
110
155
@@ -129,4 +174,27 @@ private static Properties extractOptions(String text) {
129
174
130
175
return properties ;
131
176
}
177
+
178
+ private void verifyUserNamePresent (String [] source ) {
179
+
180
+ if (source .length == 0 || !StringUtils .hasText (source [0 ])) {
181
+ throw new IllegalArgumentException ("Credentials need to specify username!" );
182
+ }
183
+ }
184
+
185
+ private void verifyUsernameAndPasswordPresent (String [] source ) {
186
+
187
+ verifyUserNamePresent (source );
188
+ if (source .length != 2 ) {
189
+ throw new IllegalArgumentException (
190
+ "Credentials need to specify username and password like in 'username:password@database'!" );
191
+ }
192
+ }
193
+
194
+ private void verifyDatabasePresent (String source ) {
195
+
196
+ if (!StringUtils .hasText (source )) {
197
+ throw new IllegalArgumentException ("Credentials need to specify database like in 'username:password@database'!" );
198
+ }
199
+ }
132
200
}
0 commit comments