From 3e72dfe421fc5166bdc135a23e7ff1520b373697 Mon Sep 17 00:00:00 2001 From: eduardofontes Date: Tue, 19 May 2020 23:39:24 -0300 Subject: [PATCH] NIFI-8109 - Update MongoDBControllerService to externalize username and password Signed-off-by: Pierre Villard This closes #4759. --- .../nifi/mongodb/MongoDBClientService.java | 17 +++++++++++++++ .../mongodb/MongoDBControllerService.java | 21 +++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-client-service-api/src/main/java/org/apache/nifi/mongodb/MongoDBClientService.java b/nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-client-service-api/src/main/java/org/apache/nifi/mongodb/MongoDBClientService.java index 76cf543865..0c7a16b404 100644 --- a/nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-client-service-api/src/main/java/org/apache/nifi/mongodb/MongoDBClientService.java +++ b/nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-client-service-api/src/main/java/org/apache/nifi/mongodb/MongoDBClientService.java @@ -44,6 +44,23 @@ public interface MongoDBClientService extends ControllerService { .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) .addValidator(StandardValidators.URI_VALIDATOR) .build(); + PropertyDescriptor DB_USER = new PropertyDescriptor.Builder() + .name("Database User") + .displayName("Database User") + .description("Database user name") + .required(false) + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + PropertyDescriptor DB_PASSWORD = new PropertyDescriptor.Builder() + .name("Password") + .displayName("Password") + .description("The password for the database user") + .required(false) + .sensitive(true) + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); PropertyDescriptor SSL_CONTEXT_SERVICE = new PropertyDescriptor.Builder() .name("ssl-context-service") .displayName("SSL Context Service") diff --git a/nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java b/nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java index 69076742d0..08ff58d7d8 100644 --- a/nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java +++ b/nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java @@ -24,6 +24,9 @@ import com.mongodb.WriteConcern; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.List; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import javax.net.ssl.SSLContext; import org.apache.nifi.annotation.documentation.CapabilityDescription; import org.apache.nifi.annotation.documentation.Tags; @@ -45,7 +48,7 @@ public class MongoDBControllerService extends AbstractControllerService implemen @OnEnabled public void onEnabled(final ConfigurationContext context) { - this.uri = context.getProperty(URI).evaluateAttributeExpressions().getValue(); + this.uri = getURI(context); this.createClient(context); } @@ -53,6 +56,8 @@ public class MongoDBControllerService extends AbstractControllerService implemen static { descriptors.add(URI); + descriptors.add(DB_USER); + descriptors.add(DB_PASSWORD); descriptors.add(SSL_CONTEXT_SERVICE); descriptors.add(CLIENT_AUTH); } @@ -105,7 +110,19 @@ public class MongoDBControllerService extends AbstractControllerService implemen } protected String getURI(final ConfigurationContext context) { - return context.getProperty(URI).evaluateAttributeExpressions().getValue(); + final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue(); + final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue(); + final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue(); + if (!uri.contains("@") && user != null && passw != null) { + try { + return uri.replaceFirst("://", "://" + URLEncoder.encode(user, StandardCharsets.UTF_8.toString()) + ":" + URLEncoder.encode(passw, StandardCharsets.UTF_8.toString()) + "@"); + } catch (final UnsupportedEncodingException e) { + getLogger().warn("Failed to URL encode username and/or password. Using original URI."); + return uri; + } + } else { + return uri; + } } @Override