Search in sources :

Example 6 with SSLSocketChannel

use of org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel in project nifi by apache.

the class EndpointConnectionPool method establishSiteToSiteConnection.

private CommunicationsSession establishSiteToSiteConnection(final String hostname, final int port) throws IOException {
    final boolean siteToSiteSecure = siteInfoProvider.isSecure();
    CommunicationsSession commsSession = null;
    try {
        if (siteToSiteSecure) {
            if (sslContext == null) {
                throw new IOException("Unable to communicate with " + hostname + ":" + port + " because it requires Secure Site-to-Site communications, but this instance is not configured for secure communications");
            }
            final SSLSocketChannel socketChannel = new SSLSocketChannel(sslContext, hostname, port, localAddress, true);
            socketChannel.connect();
            commsSession = new SSLSocketChannelCommunicationsSession(socketChannel);
            try {
                commsSession.setUserDn(socketChannel.getDn());
            } catch (final CertificateException ex) {
                throw new IOException(ex);
            }
        } else {
            final SocketChannel socketChannel = SocketChannel.open();
            if (localAddress != null) {
                final SocketAddress localSocketAddress = new InetSocketAddress(localAddress, 0);
                socketChannel.socket().bind(localSocketAddress);
            }
            socketChannel.socket().connect(new InetSocketAddress(hostname, port), commsTimeout);
            socketChannel.socket().setSoTimeout(commsTimeout);
            commsSession = new SocketChannelCommunicationsSession(socketChannel);
        }
        commsSession.getOutput().getOutputStream().write(CommunicationsSession.MAGIC_BYTES);
    } catch (final IOException ioe) {
        if (commsSession != null) {
            commsSession.close();
        }
        throw ioe;
    }
    return commsSession;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SSLSocketChannel(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel) SSLSocketChannelCommunicationsSession(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelCommunicationsSession) SSLSocketChannel(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel) InetSocketAddress(java.net.InetSocketAddress) CertificateException(java.security.cert.CertificateException) CommunicationsSession(org.apache.nifi.remote.protocol.CommunicationsSession) SocketChannelCommunicationsSession(org.apache.nifi.remote.io.socket.SocketChannelCommunicationsSession) SSLSocketChannelCommunicationsSession(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelCommunicationsSession) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SocketChannelCommunicationsSession(org.apache.nifi.remote.io.socket.SocketChannelCommunicationsSession) SSLSocketChannelCommunicationsSession(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelCommunicationsSession)

Example 7 with SSLSocketChannel

use of org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel in project nifi by apache.

the class AbstractCacheServer method start.

@Override
public void start() throws IOException {
    serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.configureBlocking(true);
    serverSocketChannel.bind(new InetSocketAddress(port));
    final Runnable runnable = new Runnable() {

        @Override
        public void run() {
            while (true) {
                final SocketChannel socketChannel;
                try {
                    socketChannel = serverSocketChannel.accept();
                    logger.debug("Connected to {}", new Object[] { socketChannel });
                } catch (final IOException e) {
                    if (!stopped) {
                        logger.error("{} unable to accept connection from remote peer due to {}", this, e.toString());
                        if (logger.isDebugEnabled()) {
                            logger.error("", e);
                        }
                    }
                    return;
                }
                final Runnable processInputRunnable = new Runnable() {

                    @Override
                    public void run() {
                        final InputStream rawInputStream;
                        final OutputStream rawOutputStream;
                        final String peer = socketChannel.socket().getInetAddress().getHostName();
                        try {
                            if (sslContext == null) {
                                rawInputStream = new SocketChannelInputStream(socketChannel);
                                rawOutputStream = new SocketChannelOutputStream(socketChannel);
                            } else {
                                final SSLSocketChannel sslSocketChannel = new SSLSocketChannel(sslContext, socketChannel, false);
                                sslSocketChannel.connect();
                                rawInputStream = new SSLSocketChannelInputStream(sslSocketChannel);
                                rawOutputStream = new SSLSocketChannelOutputStream(sslSocketChannel);
                            }
                        } catch (IOException e) {
                            logger.error("Cannot create input and/or output streams for {}", new Object[] { identifier }, e);
                            if (logger.isDebugEnabled()) {
                                logger.error("", e);
                            }
                            try {
                                socketChannel.close();
                            } catch (IOException swallow) {
                            }
                            return;
                        }
                        try (final InputStream in = new BufferedInputStream(rawInputStream);
                            final OutputStream out = new BufferedOutputStream(rawOutputStream)) {
                            final VersionNegotiator versionNegotiator = getVersionNegotiator();
                            ProtocolHandshake.receiveHandshake(in, out, versionNegotiator);
                            boolean continueComms = true;
                            while (continueComms) {
                                continueComms = listen(in, out, versionNegotiator.getVersion());
                            }
                            // client has issued 'close'
                            logger.debug("Client issued close on {}", new Object[] { socketChannel });
                        } catch (final SocketTimeoutException e) {
                            logger.debug("30 sec timeout reached", e);
                        } catch (final IOException | HandshakeException e) {
                            if (!stopped) {
                                logger.error("{} unable to communicate with remote peer {} due to {}", new Object[] { this, peer, e.toString() });
                                if (logger.isDebugEnabled()) {
                                    logger.error("", e);
                                }
                            }
                        } finally {
                            processInputThreads.remove(Thread.currentThread());
                        }
                    }
                };
                final Thread processInputThread = new Thread(processInputRunnable);
                processInputThread.setName("Distributed Cache Server Communications Thread: " + identifier);
                processInputThread.setDaemon(true);
                processInputThread.start();
                processInputThreads.add(processInputThread);
            }
        }
    };
    final Thread thread = new Thread(runnable);
    thread.setDaemon(true);
    thread.setName("Distributed Cache Server: " + identifier);
    thread.start();
}
Also used : SSLSocketChannel(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SSLSocketChannelOutputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelOutputStream) SocketChannelOutputStream(org.apache.nifi.remote.io.socket.SocketChannelOutputStream) SSLSocketChannelOutputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelOutputStream) SSLSocketChannelInputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelInputStream) InetSocketAddress(java.net.InetSocketAddress) BufferedInputStream(java.io.BufferedInputStream) SocketChannelInputStream(org.apache.nifi.remote.io.socket.SocketChannelInputStream) SSLSocketChannelInputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) SocketChannelOutputStream(org.apache.nifi.remote.io.socket.SocketChannelOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) SSLSocketChannelOutputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelOutputStream) SocketChannelInputStream(org.apache.nifi.remote.io.socket.SocketChannelInputStream) SSLSocketChannelInputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelInputStream) IOException(java.io.IOException) SSLSocketChannel(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel) StandardVersionNegotiator(org.apache.nifi.remote.StandardVersionNegotiator) VersionNegotiator(org.apache.nifi.remote.VersionNegotiator) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) HandshakeException(org.apache.nifi.distributed.cache.protocol.exception.HandshakeException)

Aggregations

SSLSocketChannel (org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel)7 SocketChannel (java.nio.channels.SocketChannel)6 IOException (java.io.IOException)5 ServerSocketChannel (java.nio.channels.ServerSocketChannel)4 InetSocketAddress (java.net.InetSocketAddress)3 SocketTimeoutException (java.net.SocketTimeoutException)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 SocketAddress (java.net.SocketAddress)2 ByteBuffer (java.nio.ByteBuffer)2 SSLEngine (javax.net.ssl.SSLEngine)2 SocketChannelCommunicationsSession (org.apache.nifi.remote.io.socket.SocketChannelCommunicationsSession)2 SSLSocketChannelCommunicationsSession (org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelCommunicationsSession)2 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 InetAddress (java.net.InetAddress)1