Search in sources :

Example 1 with PlainTextSaslNettyClient

use of org.corfudb.security.sasl.plaintext.PlainTextSaslNettyClient in project CorfuDB by CorfuDB.

the class SaslUtils method enableSaslPlainText.

public static PlainTextSaslNettyClient enableSaslPlainText(String usernameFile, String passwordFile) {
    if (usernameFile == null) {
        throw new RuntimeException("Invalid username file");
    }
    if (passwordFile == null) {
        throw new RuntimeException("Invalid password file");
    }
    String username = null;
    try {
        username = (new String(Files.readAllBytes(Paths.get(usernameFile)))).trim();
    } catch (Exception e) {
        throw new RuntimeException("Error reading the username file: " + e.getClass().getSimpleName(), e);
    }
    String password = null;
    try {
        password = (new String(Files.readAllBytes(Paths.get(passwordFile)))).trim();
    } catch (Exception e) {
        throw new RuntimeException("Error reading the password file: " + e.getClass().getSimpleName(), e);
    }
    PlainTextSaslNettyClient saslNettyClient = null;
    try {
        saslNettyClient = new PlainTextSaslNettyClient(username, password);
    } catch (SaslException se) {
        throw new RuntimeException("Could not create a SASL Plain Text Netty client" + se.getClass().getSimpleName(), se);
    }
    return saslNettyClient;
}
Also used : SaslException(javax.security.sasl.SaslException) SaslException(javax.security.sasl.SaslException) PlainTextSaslNettyClient(org.corfudb.security.sasl.plaintext.PlainTextSaslNettyClient)

Example 2 with PlainTextSaslNettyClient

use of org.corfudb.security.sasl.plaintext.PlainTextSaslNettyClient in project CorfuDB by CorfuDB.

the class NettyClientRouter method start.

public void start(long c) {
    shutdown = false;
    if (workerGroup == null || workerGroup.isShutdown() || !channel.isOpen()) {
        workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2, new ThreadFactory() {

            final AtomicInteger threadNum = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("worker-" + threadNum.getAndIncrement());
                t.setDaemon(true);
                return t;
            }
        });
        ee = new DefaultEventExecutorGroup(Runtime.getRuntime().availableProcessors() * 2, new ThreadFactory() {

            final AtomicInteger threadNum = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName(this.getClass().getName() + "event-" + threadNum.getAndIncrement());
                t.setDaemon(true);
                return t;
            }
        });
        Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.option(ChannelOption.SO_REUSEADDR, true);
        b.option(ChannelOption.TCP_NODELAY, true);
        NettyClientRouter router = this;
        b.handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                if (tlsEnabled) {
                    ch.pipeline().addLast("ssl", sslContext.newHandler(ch.alloc()));
                }
                ch.pipeline().addLast(new LengthFieldPrepender(4));
                ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                if (saslPlainTextEnabled) {
                    PlainTextSaslNettyClient saslNettyClient = SaslUtils.enableSaslPlainText(saslPlainTextUsernameFile, saslPlainTextPasswordFile);
                    ch.pipeline().addLast("sasl/plain-text", saslNettyClient);
                }
                ch.pipeline().addLast(ee, new NettyCorfuMessageDecoder());
                ch.pipeline().addLast(ee, new NettyCorfuMessageEncoder());
                ch.pipeline().addLast(ee, router);
            }
        });
        try {
            connectChannel(b, c);
        } catch (Exception e) {
            try {
                // shutdown EventLoopGroup
                workerGroup.shutdownGracefully().sync();
            } catch (InterruptedException ie) {
            }
            throw new NetworkException(e.getClass().getSimpleName() + " connecting to endpoint failed", host + ":" + port, e);
        }
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) NettyCorfuMessageEncoder(org.corfudb.protocols.wireprotocol.NettyCorfuMessageEncoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) NoSuchElementException(java.util.NoSuchElementException) WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException) NetworkException(org.corfudb.runtime.exceptions.NetworkException) PlainTextSaslNettyClient(org.corfudb.security.sasl.plaintext.PlainTextSaslNettyClient) NettyCorfuMessageDecoder(org.corfudb.protocols.wireprotocol.NettyCorfuMessageDecoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NetworkException(org.corfudb.runtime.exceptions.NetworkException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

PlainTextSaslNettyClient (org.corfudb.security.sasl.plaintext.PlainTextSaslNettyClient)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)1 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)1 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)1 NoSuchElementException (java.util.NoSuchElementException)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SaslException (javax.security.sasl.SaslException)1 NettyCorfuMessageDecoder (org.corfudb.protocols.wireprotocol.NettyCorfuMessageDecoder)1 NettyCorfuMessageEncoder (org.corfudb.protocols.wireprotocol.NettyCorfuMessageEncoder)1 NetworkException (org.corfudb.runtime.exceptions.NetworkException)1 WrongEpochException (org.corfudb.runtime.exceptions.WrongEpochException)1