mirror of https://github.com/apache/nifi.git
NIFI-8109 - Update MongoDBControllerService to externalize username and password
Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com> This closes #4759.
This commit is contained in:
parent
40f193098b
commit
3e72dfe421
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue