Search in sources :

Example 76 with SSLContextService

use of org.apache.nifi.ssl.SSLContextService in project nifi by apache.

the class ListenBeats method createDispatcher.

@Override
protected ChannelDispatcher createDispatcher(final ProcessContext context, final BlockingQueue<BeatsEvent> events) throws IOException {
    final EventFactory<BeatsEvent> eventFactory = new BeatsEventFactory();
    final ChannelHandlerFactory<BeatsEvent, AsyncChannelDispatcher> handlerFactory = new BeatsSocketChannelHandlerFactory<>();
    final int maxConnections = context.getProperty(MAX_CONNECTIONS).asInteger();
    final int bufferSize = context.getProperty(RECV_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
    final Charset charSet = Charset.forName(context.getProperty(CHARSET).getValue());
    // initialize the buffer pool based on max number of connections and the buffer size
    final BlockingQueue<ByteBuffer> bufferPool = createBufferPool(maxConnections, bufferSize);
    // if an SSLContextService was provided then create an SSLContext to pass down to the dispatcher
    SSLContext sslContext = null;
    final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    if (sslContextService != null) {
        sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.REQUIRED);
    }
    // if we decide to support SSL then get the context and pass it in here
    return new SocketChannelDispatcher<>(eventFactory, handlerFactory, bufferPool, events, getLogger(), maxConnections, sslContext, charSet);
}
Also used : BeatsEventFactory(org.apache.nifi.processors.beats.event.BeatsEventFactory) BeatsEvent(org.apache.nifi.processors.beats.event.BeatsEvent) Charset(java.nio.charset.Charset) SSLContext(javax.net.ssl.SSLContext) ByteBuffer(java.nio.ByteBuffer) AsyncChannelDispatcher(org.apache.nifi.processor.util.listen.dispatcher.AsyncChannelDispatcher) BeatsSocketChannelHandlerFactory(org.apache.nifi.processors.beats.handler.BeatsSocketChannelHandlerFactory) SSLContextService(org.apache.nifi.ssl.SSLContextService) RestrictedSSLContextService(org.apache.nifi.ssl.RestrictedSSLContextService) SocketChannelDispatcher(org.apache.nifi.processor.util.listen.dispatcher.SocketChannelDispatcher)

Example 77 with SSLContextService

use of org.apache.nifi.ssl.SSLContextService 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 78 with SSLContextService

use of org.apache.nifi.ssl.SSLContextService in project nifi by apache.

the class AbstractMongoDBControllerService method createClient.

protected final void createClient(ConfigurationContext context) throws IOException {
    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 ProviderCreationException(String.format("Unrecognized client auth '%s'. Possible values are [%s]", rawClientAuth, StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
            }
        }
        sslContext = sslService.createSSLContext(clientAuth);
    } else {
        sslContext = null;
    }
    try {
        if (sslContext == null) {
            mongoClient = new MongoClient(new MongoClientURI(getURI(context)));
        } 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;
    }
}
Also used : MongoClient(com.mongodb.MongoClient) ProviderCreationException(org.apache.nifi.authentication.exception.ProviderCreationException) SSLContextService(org.apache.nifi.ssl.SSLContextService) MongoClientURI(com.mongodb.MongoClientURI) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) ProviderCreationException(org.apache.nifi.authentication.exception.ProviderCreationException)

Example 79 with SSLContextService

use of org.apache.nifi.ssl.SSLContextService in project nifi by apache.

the class DistributedSetCacheClientService method createCommsSession.

public CommsSession createCommsSession(final ConfigurationContext context) throws IOException {
    final String hostname = context.getProperty(HOSTNAME).getValue();
    final int port = context.getProperty(PORT).asInteger();
    final int timeoutMillis = context.getProperty(COMMUNICATIONS_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue();
    final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    final CommsSession commsSession;
    if (sslContextService == null) {
        commsSession = new StandardCommsSession(hostname, port, timeoutMillis);
    } else {
        commsSession = new SSLCommsSession(sslContextService.createSSLContext(ClientAuth.REQUIRED), hostname, port, timeoutMillis);
    }
    commsSession.setTimeout(timeoutMillis, TimeUnit.MILLISECONDS);
    return commsSession;
}
Also used : SSLContextService(org.apache.nifi.ssl.SSLContextService)

Example 80 with SSLContextService

use of org.apache.nifi.ssl.SSLContextService in project nifi by apache.

the class DistributedSetCacheServer method createCacheServer.

@Override
protected CacheServer createCacheServer(final ConfigurationContext context) {
    final int port = context.getProperty(PORT).asInteger();
    final String persistencePath = context.getProperty(PERSISTENCE_PATH).getValue();
    final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    final int maxSize = context.getProperty(MAX_CACHE_ENTRIES).asInteger();
    final String evictionPolicyName = context.getProperty(EVICTION_POLICY).getValue();
    final SSLContext sslContext;
    if (sslContextService == null) {
        sslContext = null;
    } else {
        sslContext = sslContextService.createSSLContext(ClientAuth.REQUIRED);
    }
    final EvictionPolicy evictionPolicy;
    switch(evictionPolicyName) {
        case EVICTION_STRATEGY_FIFO:
            evictionPolicy = EvictionPolicy.FIFO;
            break;
        case EVICTION_STRATEGY_LFU:
            evictionPolicy = EvictionPolicy.LFU;
            break;
        case EVICTION_STRATEGY_LRU:
            evictionPolicy = EvictionPolicy.LRU;
            break;
        default:
            throw new IllegalArgumentException("Illegal Eviction Policy: " + evictionPolicyName);
    }
    try {
        final File persistenceDir = persistencePath == null ? null : new File(persistencePath);
        return new SetCacheServer(getIdentifier(), sslContext, port, maxSize, evictionPolicy, persistenceDir);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SSLContextService(org.apache.nifi.ssl.SSLContextService) SSLContext(javax.net.ssl.SSLContext) File(java.io.File)

Aggregations

SSLContextService (org.apache.nifi.ssl.SSLContextService)84 SSLContext (javax.net.ssl.SSLContext)29 Test (org.junit.Test)23 StandardSSLContextService (org.apache.nifi.ssl.StandardSSLContextService)22 RestrictedSSLContextService (org.apache.nifi.ssl.RestrictedSSLContextService)18 ArrayList (java.util.ArrayList)12 StandardRestrictedSSLContextService (org.apache.nifi.ssl.StandardRestrictedSSLContextService)12 IOException (java.io.IOException)11 ValidationResult (org.apache.nifi.components.ValidationResult)10 ComponentLog (org.apache.nifi.logging.ComponentLog)9 OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)7 InetSocketAddress (java.net.InetSocketAddress)6 ProcessException (org.apache.nifi.processor.exception.ProcessException)6 Charset (java.nio.charset.Charset)5 InitializationException (org.apache.nifi.reporting.InitializationException)5 MalformedURLException (java.net.MalformedURLException)4 URI (java.net.URI)4 ByteBuffer (java.nio.ByteBuffer)4 ProviderCreationException (org.apache.nifi.authentication.exception.ProviderCreationException)4 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)4