Skip to content

Commit 10f4894

Browse files
author
Ahmad Shah
committed
Update mongodb client script to retrieve credentials from URI and throw warning when they mismatch the properties
1 parent 7cbcd86 commit 10f4894

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

ycsb-mongodb/mongodb/src/main/java/com/yahoo/ycsb/db/MongoDbClient.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,31 @@ public void init() {
403403
Properties props = getProperties();
404404
String urls = props.getProperty("mongodb.url", "mongodb://localhost:27017");
405405

406-
database = props.getProperty("mongodb.database", "ycsb");
407406
/* Credentials */
407+
database = props.getProperty("mongodb.database", "ycsb");
408+
409+
// Retrieve username and password from properties, set to empty string if they are undefined
408410
String username = props.getProperty("mongodb.username", "");
409411
String password = props.getProperty("mongodb.password", "");
410412

413+
// If the URI contains an @, that means a username and password are specified here as well, so this will parse them out
414+
if (urls.contains("@")) {
415+
String uriCredentials = urls.substring(urls.indexOf("//") + 2, urls.indexOf("@"));
416+
String[] uriCredentialsList = uriCredentials.split(":");
417+
String uriUsername = uriCredentialsList[0];
418+
String uriPassword = uriCredentialsList[1];
419+
420+
// If both the URI and properties have credentials defined, check that they are equivalent
421+
// If they are not, update credentials to those in the URI and log a warning
422+
if (props.keySet().contains("mongodb.username") && props.keySet().contains("mongodb.password")){
423+
if (!uriUsername.equals(username) || !uriPassword.equals(password)){
424+
System.out.println("WARNING: Username/Password provided in the properties does not match what is present in the URI, defaulting to the URI");
425+
}
426+
}
427+
username = uriUsername;
428+
password = uriPassword;
429+
}
430+
411431
// Set insert batchsize, default 1 - to be YCSB-original equivalent
412432
final String batchSizeString = props.getProperty("batchsize", "1");
413433
BATCHSIZE = Integer.parseInt(batchSizeString);

0 commit comments

Comments
 (0)