From ac46f6c34df61cb15b935b90140262542365dab5 Mon Sep 17 00:00:00 2001 From: Alexander Katanov Date: Mon, 21 Jan 2019 16:29:36 +0200 Subject: [PATCH 1/3] Fixes #181 --- .../gelf/intern/sender/GelfTCPSSLSender.java | 3 + ...lfTCPSSLSenderConnectIntegrationTests.java | 116 ++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/test/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSenderConnectIntegrationTests.java diff --git a/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java index 42121e4fe..65fc9659b 100644 --- a/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java +++ b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java @@ -53,6 +53,9 @@ public GelfTCPSSLSender(String host, int port, int connectTimeoutMs, int readTim @Override protected boolean connect() throws IOException { + if (isConnected()) { + return false; + } this.sslEngine = sslContext.createSSLEngine(); this.sslEngine.setUseClientMode(true); diff --git a/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSenderConnectIntegrationTests.java b/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSenderConnectIntegrationTests.java new file mode 100644 index 000000000..fd2209b2d --- /dev/null +++ b/src/test/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSenderConnectIntegrationTests.java @@ -0,0 +1,116 @@ +package biz.paluch.logging.gelf.intern.sender; + +import biz.paluch.logging.gelf.intern.ErrorReporter; +import biz.paluch.logging.gelf.intern.GelfMessage; +import biz.paluch.logging.gelf.netty.NettyLocalServer; +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import java.io.File; +import java.io.FileInputStream; +import java.security.KeyStore; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +public class GelfTCPSSLSenderConnectIntegrationTests { + + private static NettyLocalServer server = new NettyLocalServer(NioServerSocketChannel.class); + private static SSLContext sslContext; + + @BeforeAll + public static void setupClass() throws Exception { + + File file = new File("work/keystore.jks"); + assumeTrue(file.exists()); + + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + keyStore.load(new FileInputStream(file), "changeit".toCharArray()); + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + kmf.init(keyStore, "changeit".toCharArray()); + + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + tmf.init(keyStore); + + final SslContext sslContext = SslContextBuilder.forServer(kmf).build(); + + GelfTCPSSLSenderConnectIntegrationTests.sslContext = SSLContext.getInstance("TLSv1.2"); + GelfTCPSSLSenderConnectIntegrationTests.sslContext.init(new KeyManager[0], tmf.getTrustManagers(), null); + + server.run(new ChannelInitializer() { + @Override + protected void initChannel(Channel ch) throws Exception { + + ch.pipeline().addLast(sslContext.newHandler(ch.alloc())); + ch.pipeline().addLast(server.getHandler()); + } + }); + } + + @Test + public void shouldSendTCPMessagesViaSsl() throws Exception { + + final GelfTCPSSLSender tcpsslSender = new GelfTCPSSLSender("localhost", server.getPort(), 1000, 1000, 1, true, + new ErrorReporter() { + @Override + public void reportError(String message, Exception e) { + System.out.println(Thread.currentThread() + " " + message); + if (e != null) { + e.printStackTrace(); + } + } + }, sslContext); + + Thread thread1 = new Thread(new Runnable() { + @Override + public void run() { + GelfMessage gelfMessage = new GelfMessage("short1", "long1", 1, "info"); + gelfMessage.setHost("host"); + tcpsslSender.sendMessage(gelfMessage); + } + }); + + Thread thread2 = new Thread(new Runnable() { + @Override + public void run() { + GelfMessage gelfMessage = new GelfMessage("short2", "long2", 1, "info"); + gelfMessage.setHost("host"); + tcpsslSender.sendMessage(gelfMessage); + } + }); + + thread2.start(); + thread1.start(); + + thread1.join(); + thread2.join(); + + for (int i = 0; i < 100; i++) { + if (!server.getJsonValues().isEmpty()) { + continue; + + } + Thread.sleep(100); + } + + assertThat(server.getJsonValues()).isNotEmpty(); + assertThat(server.getJsonValues()).hasSize(2); + + tcpsslSender.close(); + } + + @AfterAll + public static void afterClass() throws Exception { + server.close(); + } +} From f9a99a1753716982ed337e5491ee68582362ec7e Mon Sep 17 00:00:00 2001 From: Alexander Katanov Date: Tue, 22 Jan 2019 10:02:01 +0200 Subject: [PATCH 2/3] Fixes #181 Move initialization of sslEngine after connect happened --- .../logging/gelf/intern/sender/GelfTCPSSLSender.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java index 65fc9659b..1401dbb14 100644 --- a/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java +++ b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java @@ -57,11 +57,11 @@ protected boolean connect() throws IOException { return false; } - this.sslEngine = sslContext.createSSLEngine(); - this.sslEngine.setUseClientMode(true); - this.sslSession = sslEngine.getSession(); - if (super.connect()) { + this.sslEngine = sslContext.createSSLEngine(); + this.sslEngine.setUseClientMode(true); + this.sslSession = sslEngine.getSession(); + // Begin handshake sslEngine.beginHandshake(); doHandshake(channel(), sslEngine, ByteBuffer.allocate(sslSession.getPacketBufferSize()), From bd125b0b84de3691a0d2b6fb94bed2fabe79efe7 Mon Sep 17 00:00:00 2001 From: Alexander Katanov Date: Tue, 22 Jan 2019 10:24:04 +0200 Subject: [PATCH 3/3] Fixes #181 remove redundant check --- .../paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java index 1401dbb14..84e705ae0 100644 --- a/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java +++ b/src/main/java/biz/paluch/logging/gelf/intern/sender/GelfTCPSSLSender.java @@ -53,10 +53,6 @@ public GelfTCPSSLSender(String host, int port, int connectTimeoutMs, int readTim @Override protected boolean connect() throws IOException { - if (isConnected()) { - return false; - } - if (super.connect()) { this.sslEngine = sslContext.createSSLEngine(); this.sslEngine.setUseClientMode(true);