Skip to content

Commit

Permalink
Allow MongoDB datasources without username/password (#3149)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharat87 committed Feb 23, 2021
1 parent 17dd534 commit 72df6fb
Showing 1 changed file with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,17 @@ public static String buildClientURI(DatasourceConfiguration datasourceConfigurat

DBAuth authentication = (DBAuth) datasourceConfiguration.getAuthentication();
if (authentication != null) {
builder
.append(urlEncode(authentication.getUsername()))
.append(':')
.append(urlEncode(authentication.getPassword()))
.append('@');
final boolean hasUsername = StringUtils.hasText(authentication.getUsername());
final boolean hasPassword = StringUtils.hasText(authentication.getPassword());
if (hasUsername) {
builder.append(urlEncode(authentication.getUsername()));
}
if (hasPassword) {
builder.append(':').append(urlEncode(authentication.getPassword()));
}
if (hasUsername || hasPassword) {
builder.append('@');
}
}

for (Endpoint endpoint : endpoints) {
Expand Down Expand Up @@ -313,25 +319,11 @@ public Set<String> validateDatasource(DatasourceConfiguration datasourceConfigur
}

DBAuth authentication = (DBAuth) datasourceConfiguration.getAuthentication();
if (authentication == null) {
invalids.add("Missing authentication details.");

} else {
if (authentication != null) {
DBAuth.Type authType = authentication.getAuthType();

if (authType != null && VALID_AUTH_TYPES.contains(authType)) {

if (StringUtils.isEmpty(authentication.getUsername())) {
invalids.add("Missing username for authentication. Needed because authType is " + authType + ".");
}

if (StringUtils.isEmpty(authentication.getPassword())) {
invalids.add("Missing password for authentication. Needed because authType is " + authType + ".");
}

} else {
if (authType == null || !VALID_AUTH_TYPES.contains(authType)) {
invalids.add("Invalid authType. Must be one of " + VALID_AUTH_TYPES_STR);

}

if (StringUtils.isEmpty(authentication.getDatabaseName())) {
Expand Down

0 comments on commit 72df6fb

Please sign in to comment.