Search in sources :

Example 71 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class AbstractCassandraProcessor method connectToCassandra.

protected void connectToCassandra(ProcessContext context) {
    if (cluster.get() == null) {
        ComponentLog log = getLogger();
        final String contactPointList = context.getProperty(CONTACT_POINTS).evaluateAttributeExpressions().getValue();
        final String consistencyLevel = context.getProperty(CONSISTENCY_LEVEL).getValue();
        List<InetSocketAddress> contactPoints = getContactPoints(contactPointList);
        // Set up the client for secure (SSL/TLS communications) if configured to do so
        final SSLContextService sslService = context.getProperty(PROP_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 ProviderCreationException(String.format("Unrecognized client auth '%s'. Possible values are [%s]", rawClientAuth, StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
                }
            }
            sslContext = sslService.createSSLContext(clientAuth);
        } else {
            sslContext = null;
        }
        final String username, password;
        PropertyValue usernameProperty = context.getProperty(USERNAME).evaluateAttributeExpressions();
        PropertyValue passwordProperty = context.getProperty(PASSWORD).evaluateAttributeExpressions();
        if (usernameProperty != null && passwordProperty != null) {
            username = usernameProperty.getValue();
            password = passwordProperty.getValue();
        } else {
            username = null;
            password = null;
        }
        // Create the cluster and connect to it
        Cluster newCluster = createCluster(contactPoints, sslContext, username, password);
        PropertyValue keyspaceProperty = context.getProperty(KEYSPACE).evaluateAttributeExpressions();
        final Session newSession;
        if (keyspaceProperty != null) {
            newSession = newCluster.connect(keyspaceProperty.getValue());
        } else {
            newSession = newCluster.connect();
        }
        newCluster.getConfiguration().getQueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel));
        Metadata metadata = newCluster.getMetadata();
        log.info("Connected to Cassandra cluster: {}", new Object[] { metadata.getClusterName() });
        cluster.set(newCluster);
        cassandraSession.set(newSession);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Metadata(com.datastax.driver.core.Metadata) PropertyValue(org.apache.nifi.components.PropertyValue) Cluster(com.datastax.driver.core.Cluster) SSLContext(javax.net.ssl.SSLContext) ComponentLog(org.apache.nifi.logging.ComponentLog) ProviderCreationException(org.apache.nifi.authentication.exception.ProviderCreationException) SSLContextService(org.apache.nifi.ssl.SSLContextService) Session(com.datastax.driver.core.Session)

Example 72 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class HttpNotificationService method getSslSocketFactory.

private static SSLSocketFactory getSslSocketFactory(NotificationInitializationContext context) throws Exception {
    final String protocol = context.getProperty(SSL_ALGORITHM).getValue();
    try {
        final PropertyValue keyPasswdProp = context.getProperty(PROP_KEY_PASSWORD);
        final char[] keyPassword = keyPasswdProp.isSet() ? keyPasswdProp.getValue().toCharArray() : null;
        final SSLContext sslContext;
        final String truststoreFile = context.getProperty(PROP_TRUSTSTORE).getValue();
        final String keystoreFile = context.getProperty(PROP_KEYSTORE).getValue();
        if (keystoreFile == null) {
            // If keystore not specified, create SSL Context based only on trust store.
            sslContext = SslContextFactory.createTrustSslContext(context.getProperty(PROP_TRUSTSTORE).getValue(), context.getProperty(PROP_TRUSTSTORE_PASSWORD).getValue().toCharArray(), context.getProperty(PROP_TRUSTSTORE_TYPE).getValue(), protocol);
        } else if (truststoreFile == null) {
            // If truststore not specified, create SSL Context based only on key store.
            sslContext = SslContextFactory.createSslContext(context.getProperty(PROP_KEYSTORE).getValue(), context.getProperty(PROP_KEYSTORE_PASSWORD).getValue().toCharArray(), keyPassword, context.getProperty(PROP_KEYSTORE_TYPE).getValue(), protocol);
        } else {
            sslContext = SslContextFactory.createSslContext(context.getProperty(PROP_KEYSTORE).getValue(), context.getProperty(PROP_KEYSTORE_PASSWORD).getValue().toCharArray(), keyPassword, context.getProperty(PROP_KEYSTORE_TYPE).getValue(), context.getProperty(PROP_TRUSTSTORE).getValue(), context.getProperty(PROP_TRUSTSTORE_PASSWORD).getValue().toCharArray(), context.getProperty(PROP_TRUSTSTORE_TYPE).getValue(), SslContextFactory.ClientAuth.REQUIRED, protocol);
        }
        return sslContext.getSocketFactory();
    } catch (final Exception e) {
        throw new ProcessException(e);
    }
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) PropertyValue(org.apache.nifi.components.PropertyValue) SSLContext(javax.net.ssl.SSLContext) NotificationFailedException(org.apache.nifi.bootstrap.notification.NotificationFailedException) ProcessException(org.apache.nifi.processor.exception.ProcessException)

Example 73 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class StandardSSLContextService method createSSLContext.

@Override
public SSLContext createSSLContext(final ClientAuth clientAuth) throws ProcessException {
    final String protocol = getSslAlgorithm();
    try {
        final PropertyValue keyPasswdProp = configContext.getProperty(KEY_PASSWORD);
        final char[] keyPassword = keyPasswdProp.isSet() ? keyPasswdProp.getValue().toCharArray() : null;
        final String keystoreFile = configContext.getProperty(KEYSTORE).getValue();
        if (keystoreFile == null) {
            // If keystore not specified, create SSL Context based only on trust store.
            return SslContextFactory.createTrustSslContext(configContext.getProperty(TRUSTSTORE).getValue(), configContext.getProperty(TRUSTSTORE_PASSWORD).getValue().toCharArray(), configContext.getProperty(TRUSTSTORE_TYPE).getValue(), protocol);
        }
        final String truststoreFile = configContext.getProperty(TRUSTSTORE).getValue();
        if (truststoreFile == null) {
            // If truststore not specified, create SSL Context based only on key store.
            return SslContextFactory.createSslContext(configContext.getProperty(KEYSTORE).getValue(), configContext.getProperty(KEYSTORE_PASSWORD).getValue().toCharArray(), keyPassword, configContext.getProperty(KEYSTORE_TYPE).getValue(), protocol);
        }
        return SslContextFactory.createSslContext(configContext.getProperty(KEYSTORE).getValue(), configContext.getProperty(KEYSTORE_PASSWORD).getValue().toCharArray(), keyPassword, configContext.getProperty(KEYSTORE_TYPE).getValue(), configContext.getProperty(TRUSTSTORE).getValue(), configContext.getProperty(TRUSTSTORE_PASSWORD).getValue().toCharArray(), configContext.getProperty(TRUSTSTORE_TYPE).getValue(), org.apache.nifi.security.util.SslContextFactory.ClientAuth.valueOf(clientAuth.name()), protocol);
    } catch (final Exception e) {
        throw new ProcessException(e);
    }
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) PropertyValue(org.apache.nifi.components.PropertyValue) MalformedURLException(java.net.MalformedURLException) InitializationException(org.apache.nifi.reporting.InitializationException) ProcessException(org.apache.nifi.processor.exception.ProcessException)

Aggregations

PropertyValue (org.apache.nifi.components.PropertyValue)73 HashMap (java.util.HashMap)29 Test (org.junit.Test)22 StandardPropertyValue (org.apache.nifi.attribute.expression.language.StandardPropertyValue)21 ComponentLog (org.apache.nifi.logging.ComponentLog)18 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)16 IOException (java.io.IOException)15 Map (java.util.Map)13 FlowFile (org.apache.nifi.flowfile.FlowFile)11 ProcessException (org.apache.nifi.processor.exception.ProcessException)11 MockPropertyValue (org.apache.nifi.util.MockPropertyValue)11 ArrayList (java.util.ArrayList)9 SSLContext (javax.net.ssl.SSLContext)7 Relationship (org.apache.nifi.processor.Relationship)7 AuthorizerCreationException (org.apache.nifi.authorization.exception.AuthorizerCreationException)6 File (java.io.File)5 DynamicRelationship (org.apache.nifi.annotation.behavior.DynamicRelationship)5 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)5 SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5