-
Notifications
You must be signed in to change notification settings - Fork 3k
NIFI-7037 Split off username and password fields for GetMongo processor #4231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f88dcdc
a7edf6a
546450f
ae4d4d7
1db5d20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,9 +20,12 @@ | |
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.ObjectWriter; | ||
| import com.mongodb.MongoClient; | ||
| import com.mongodb.MongoClientURI; | ||
| import com.mongodb.client.FindIterable; | ||
| import com.mongodb.client.MongoCollection; | ||
| import com.mongodb.client.MongoCursor; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.nifi.annotation.behavior.InputRequirement; | ||
| import org.apache.nifi.annotation.behavior.InputRequirement.Requirement; | ||
| import org.apache.nifi.annotation.behavior.WritesAttribute; | ||
|
|
@@ -36,14 +39,18 @@ | |
| import org.apache.nifi.context.PropertyContext; | ||
| import org.apache.nifi.flowfile.FlowFile; | ||
| import org.apache.nifi.logging.ComponentLog; | ||
| import org.apache.nifi.mongodb.MongoDBClientService; | ||
| import org.apache.nifi.processor.ProcessContext; | ||
| import org.apache.nifi.processor.ProcessSession; | ||
| import org.apache.nifi.processor.Relationship; | ||
| import org.apache.nifi.processor.exception.ProcessException; | ||
| import org.apache.nifi.processor.util.StandardValidators; | ||
| import org.apache.nifi.security.util.SslContextFactory; | ||
| import org.apache.nifi.ssl.SSLContextService; | ||
| import org.bson.Document; | ||
| import org.bson.json.JsonWriterSettings; | ||
|
|
||
| import javax.net.ssl.SSLContext; | ||
| import java.io.IOException; | ||
| import java.nio.charset.Charset; | ||
| import java.util.ArrayList; | ||
|
|
@@ -85,6 +92,22 @@ public class GetMongo extends AbstractMongoQueryProcessor { | |
| .allowableValues(YES_PP, NO_PP) | ||
| .addValidator(Validator.VALID) | ||
| .build(); | ||
| static final PropertyDescriptor USER_NAME = new PropertyDescriptor.Builder() | ||
| .name("User Name") | ||
| .displayName("username") | ||
| .description("User for mongodb authentication") | ||
| .required(false) | ||
| .addValidator(Validator.VALID) | ||
| .sensitive(false) | ||
| .build(); | ||
|
|
||
| static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() | ||
| .name("Password") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| .description("The Password for the user") | ||
| .required(false) | ||
| .addValidator(Validator.VALID) | ||
| .sensitive(true) | ||
| .build(); | ||
|
|
||
| private final static Set<Relationship> relationships; | ||
| private final static List<PropertyDescriptor> propertyDescriptors; | ||
|
|
@@ -107,6 +130,8 @@ public class GetMongo extends AbstractMongoQueryProcessor { | |
| _propertyDescriptors.add(SSL_CONTEXT_SERVICE); | ||
| _propertyDescriptors.add(CLIENT_AUTH); | ||
| _propertyDescriptors.add(SEND_EMPTY_RESULTS); | ||
| _propertyDescriptors.add(USER_NAME); | ||
| _propertyDescriptors.add(PASSWORD); | ||
| propertyDescriptors = Collections.unmodifiableList(_propertyDescriptors); | ||
|
|
||
| final Set<Relationship> _relationships = new HashSet<>(); | ||
|
|
@@ -253,7 +278,13 @@ public void onTrigger(final ProcessContext context, final ProcessSession session | |
| }); | ||
|
|
||
| outgoingFlowFile = session.putAllAttributes(outgoingFlowFile, attributes); | ||
| session.getProvenanceReporter().receive(outgoingFlowFile, getURI(context)); | ||
| String uriPass = ""; | ||
| if (context.getProperty(USER_NAME).getValue() != null) { | ||
| uriPass = "mongodb://" + context.getProperty(USER_NAME).getValue() + ":" + context.getProperty(PASSWORD).getValue() + "@" + getURI(context).substring(10); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe the URI is validated anywhere to ensure it starts with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, there is a note in the MongoDB docs regarding usernames and passwords that contain special characters:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add in the validation for the starting 10 characters for the URI. I did see the MongoDB doc, for percent encoding which would be better: current implementation is goes the first way. But if you think It is better for processor to handle, I will look into it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not something I would expect the user to do manually. It should be done automatically by the processor. |
||
| } else { | ||
| uriPass = getURI(context); | ||
| } | ||
| session.getProvenanceReporter().receive(outgoingFlowFile, uriPass); | ||
| session.transfer(outgoingFlowFile, REL_SUCCESS); | ||
| sent++; | ||
| } | ||
|
|
@@ -271,4 +302,59 @@ public void onTrigger(final ProcessContext context, final ProcessSession session | |
| } | ||
|
|
||
| } | ||
|
|
||
| @OnScheduled | ||
| public void createClient(ProcessContext context) throws IOException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of this method appears to duplicate
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is true I could have done the changes in AbstractMongoProcessor, but it would end up affecting the components of PutMongo. Also Since request was for just getMongo I did it this way.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think all 6 of the MongoDB processors need this change if one does, so it should be made in a consistent manner. It appears the Jira is incomplete as it only indicates the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alopresto We started moving toward using a controller service to manage the client connection I think ~3 releases ago. The best place for these changes might be there, so we can start moving toward deprecating per-processor connection management altogether. |
||
| if (context.getProperty(CLIENT_SERVICE).isSet()) { | ||
| clientService = context.getProperty(CLIENT_SERVICE).asControllerService(MongoDBClientService.class); | ||
| return; | ||
| } | ||
|
|
||
| if (mongoClient != null) { | ||
| closeClient(); | ||
| } | ||
|
|
||
| getLogger().info("Creating MongoClient"); | ||
|
|
||
| // Set up the client for secure (SSL/TLS communications) if configured to do so | ||
| final SSLContextService sslService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class); | ||
| final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue(); | ||
| final SSLContext sslContext; | ||
|
|
||
| if (sslService != null) { | ||
| final SSLContextService.ClientAuth clientAuth; | ||
| if (StringUtils.isBlank(rawClientAuth)) { | ||
| clientAuth = SSLContextService.ClientAuth.REQUIRED; | ||
| } else { | ||
| try { | ||
| clientAuth = SSLContextService.ClientAuth.valueOf(rawClientAuth); | ||
| } catch (final IllegalArgumentException iae) { | ||
| throw new IllegalStateException(String.format("Unrecognized client auth '%s'. Possible values are [%s]", | ||
| rawClientAuth, StringUtils.join(SslContextFactory.ClientAuth.values(), ", "))); | ||
| } | ||
| } | ||
| sslContext = sslService.createSSLContext(clientAuth); | ||
| } else { | ||
| sslContext = null; | ||
| } | ||
|
|
||
| try { | ||
| String uriPass = ""; | ||
| if (sslContext == null) { | ||
| if (context.getProperty(USER_NAME).getValue() != null) { | ||
| uriPass = "mongodb://" + context.getProperty(USER_NAME).getValue() + ":" + context.getProperty(PASSWORD).getValue() + "@" + getURI(context).substring(10); | ||
|
|
||
| } else { | ||
| uriPass = getURI(context); | ||
| } | ||
| mongoClient = new MongoClient(new MongoClientURI(uriPass)); | ||
| } else { | ||
| mongoClient = new MongoClient(new MongoClientURI(getURI(context), getClientOptions(sslContext))); | ||
| } | ||
| } catch (Exception e) { | ||
| getLogger().error("Failed to schedule {} due to {}", new Object[] { this.getClass().getName(), e }, e); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
nameanddisplayNamevalues are switched.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for pointing it out. I will add it