MikeThomsen commented on a change in pull request #5301: URL: https://github.com/apache/nifi/pull/5301#discussion_r686684108
########## File path: nifi-nar-bundles/nifi-graph-bundle/nifi-neo4j-cypher-service/src/main/java/org/apache/nifi/graph/Neo4jCypherClientService.java ########## @@ -0,0 +1,299 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nifi.graph; + +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.ssl.SSLContextService; +import org.neo4j.driver.AuthTokens; +import org.neo4j.driver.Config; +import org.neo4j.driver.Driver; +import org.neo4j.driver.GraphDatabase; +import org.neo4j.driver.Record; +import org.neo4j.driver.Result; +import org.neo4j.driver.Session; +import org.neo4j.driver.internal.InternalNode; +import org.neo4j.driver.summary.ResultSummary; +import org.neo4j.driver.summary.SummaryCounters; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +@Tags({ "graph", "neo4j", "cypher" }) +@CapabilityDescription("Provides a client service for managing connections to a Neo4j 4.X or newer database. Configuration information for " + + "the Neo4j driver that corresponds to most of the settings for this service can be found here: " + + "https://neo4j.com/docs/driver-manual/current/client-applications/#driver-configuration. This service was created as a " + + "result of the break in driver compatibility between Neo4j 3.X and 4.X and might be renamed in the future if and when " + + "Neo4j should break driver compatibility between 4.X and a future release.") +public class Neo4jCypherClientService extends AbstractControllerService implements GraphClientService { + public static final PropertyDescriptor CONNECTION_URL = new PropertyDescriptor.Builder() + .name("neo4j-connection-url") + .displayName("Neo4j Connection URL") + .description("Neo4j Server URL.") + .required(true) + .defaultValue("bolt://localhost:7687") + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("neo4j-username") + .displayName("Username") + .description("Username for accessing Neo4j") + .required(true) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("neo4j-password") + .displayName("Password") + .description("Password for Neo4j user. A dummy non-blank password is required even if it disabled on the server.") + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CONNECTION_TIMEOUT = new PropertyDescriptor.Builder() + .name("neo4j-max-connection-time-out") + .displayName("Neo4j Max Connection Time Out (seconds)") + .description("The maximum time for establishing connection to the Neo4j") + .defaultValue("5 seconds") + .required(true) + .addValidator(StandardValidators.TIME_PERIOD_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .sensitive(false) + .build(); + + public static final PropertyDescriptor MAX_CONNECTION_POOL_SIZE = new PropertyDescriptor.Builder() + .name("neo4j-max-connection-pool-size") + .displayName("Neo4j Max Connection Pool Size") + .description("The maximum connection pool size for Neo4j.") + .defaultValue("100") + .required(true) + .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .sensitive(false) + .build(); + + public static final PropertyDescriptor MAX_CONNECTION_ACQUISITION_TIMEOUT = new PropertyDescriptor.Builder() + .name("neo4j-max-connection-acquisition-timeout") + .displayName("Neo4j Max Connection Acquisition Timeout") + .description("The maximum connection acquisition timeout.") + .defaultValue("60 second") + .required(true) + .addValidator(StandardValidators.TIME_PERIOD_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .sensitive(false) + .build(); + + public static final PropertyDescriptor IDLE_TIME_BEFORE_CONNECTION_TEST = new PropertyDescriptor.Builder() + .name("neo4j-idle-time-before-test") + .displayName("Neo4j Idle Time Before Connection Test") + .description("The idle time before connection test.") + .defaultValue("60 seconds") + .required(true) + .addValidator(StandardValidators.TIME_PERIOD_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .sensitive(false) + .build(); + + public static final PropertyDescriptor MAX_CONNECTION_LIFETIME = new PropertyDescriptor.Builder() + .name("neo4j-max-connection-lifetime") + .displayName("Neo4j Max Connection Lifetime") + .description("The maximum connection lifetime") + .defaultValue("3600 seconds") + .required(true) + .addValidator(StandardValidators.TIME_PERIOD_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .sensitive(false) + .build(); + + public static final PropertyDescriptor ENCRYPTION = new PropertyDescriptor.Builder() + .name("neo4j-driver-tls-encryption-enabled") + .displayName("Neo4j Driver TLS Encryption") + .description("Is the driver using TLS encryption ?") + .defaultValue("false") + .required(true) + .allowableValues("true","false") + .addValidator(StandardValidators.BOOLEAN_VALIDATOR) + .sensitive(false) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT_SERVICE = new PropertyDescriptor.Builder() + .name("SSL Context Service") + .description("The SSL Context Service used to provide client certificate information for TLS/SSL " + + "connections.") + .required(false) + .identifiesControllerService(SSLContextService.class) + .build(); + + protected Driver neo4jDriver; + protected String username; + protected String password; + protected String connectionUrl; + + private static final List<PropertyDescriptor> DESCRIPTORS; + static { + List<PropertyDescriptor> _temp = new ArrayList<>(); + _temp.add(CONNECTION_URL); + _temp.add(USERNAME); + _temp.add(PASSWORD); + _temp.add(CONNECTION_TIMEOUT); + _temp.add(MAX_CONNECTION_POOL_SIZE); + _temp.add(MAX_CONNECTION_ACQUISITION_TIMEOUT); + _temp.add(IDLE_TIME_BEFORE_CONNECTION_TEST); + _temp.add(MAX_CONNECTION_LIFETIME); + _temp.add(ENCRYPTION); + _temp.add(SSL_CONTEXT_SERVICE); + + DESCRIPTORS = Collections.unmodifiableList(_temp); + } + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return DESCRIPTORS; + } + + protected Driver getDriver(ConfigurationContext context) { + connectionUrl = context.getProperty(CONNECTION_URL).evaluateAttributeExpressions().getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + + Config.ConfigBuilder configBuilder = Config.builder(); + + configBuilder.withMaxConnectionPoolSize(context.getProperty(MAX_CONNECTION_POOL_SIZE).evaluateAttributeExpressions().asInteger()); + + configBuilder.withConnectionTimeout(context.getProperty(CONNECTION_TIMEOUT).evaluateAttributeExpressions().asTimePeriod(TimeUnit.SECONDS), TimeUnit.SECONDS); + + configBuilder.withConnectionAcquisitionTimeout(context.getProperty(MAX_CONNECTION_ACQUISITION_TIMEOUT).evaluateAttributeExpressions().asTimePeriod(TimeUnit.SECONDS), TimeUnit.SECONDS); + + configBuilder.withMaxConnectionLifetime(context.getProperty(MAX_CONNECTION_LIFETIME).evaluateAttributeExpressions().asTimePeriod(TimeUnit.SECONDS), TimeUnit.SECONDS); + + configBuilder.withConnectionLivenessCheckTimeout(context.getProperty(IDLE_TIME_BEFORE_CONNECTION_TEST).evaluateAttributeExpressions().asTimePeriod(TimeUnit.SECONDS), TimeUnit.SECONDS); + + if ( context.getProperty(ENCRYPTION).asBoolean() ) { + configBuilder.withEncryption(); + } else { + configBuilder.withoutEncryption(); + } + + final SSLContextService sslService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class); + if (sslService != null) { + if ( sslService.isTrustStoreConfigured()) { + configBuilder.withTrustStrategy(Config.TrustStrategy.trustCustomCertificateSignedBy(new File( Review comment: Interesting. I'm not sure how that made it through the first reviews a while back, but I'll try to check it out. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
