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;
}
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);
}
}
}
Aggregations