changeset 1040:bff9be223040

ChannelContext - secure connection support, SocketConnection.doMakeSSL implemetation
author Devel 2
date Tue, 14 Apr 2020 09:43:23 +0200
parents 58860e741b9e
children 7c0305805185
files stress-tester/src/main/java/com/passus/st/emitter/ChannelContext.java stress-tester/src/main/java/com/passus/st/emitter/StatelessTasks.java stress-tester/src/main/java/com/passus/st/emitter/Task.java stress-tester/src/main/java/com/passus/st/emitter/socket/AbstractChannelContext.java stress-tester/src/main/java/com/passus/st/emitter/socket/Connection.java stress-tester/src/main/java/com/passus/st/emitter/socket/SocketConnection.java stress-tester/src/test/java/com/passus/st/source/NcEventDestinationTest.java
diffstat 7 files changed, 65 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/emitter/ChannelContext.java	Fri Apr 10 12:47:48 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/ChannelContext.java	Tue Apr 14 09:43:23 2020 +0200
@@ -19,6 +19,14 @@
 
     boolean isConnectionPending();
 
+    default boolean isSecure() {
+        return false;
+    }
+
+    default void makeSecure() throws IOException {
+        throw new IOException("Secure connection not supported");
+    }
+
     void write(byte[] data, int offset, int length) throws IOException;
 
     void write(ByteBuff data) throws IOException;
--- a/stress-tester/src/main/java/com/passus/st/emitter/StatelessTasks.java	Fri Apr 10 12:47:48 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/StatelessTasks.java	Tue Apr 14 09:43:23 2020 +0200
@@ -3,6 +3,7 @@
 public class StatelessTasks {
 
     public static final Task CONNECT_TASK = new Task(Task.CONNECT);
+    public static final Task MAKE_SSL_TASK = new Task(Task.MAKE_SSL);
     public static final Task READ_TASK = new Task(Task.READ);
     public static final Task FLUSH_TASK = new Task(Task.FLUSH);
     public static final Task CLOSE_TASK = new Task(Task.CLOSE);
--- a/stress-tester/src/main/java/com/passus/st/emitter/Task.java	Fri Apr 10 12:47:48 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/Task.java	Tue Apr 14 09:43:23 2020 +0200
@@ -3,9 +3,10 @@
 public class Task {
 
     public static final int CONNECT = 1;
-    public static final int READ = 2;
-    public static final int FLUSH = 3;
-    public static final int CLOSE = 4;
+    public static final int MAKE_SSL = 2;
+    public static final int READ = 3;
+    public static final int FLUSH = 4;
+    public static final int CLOSE = 5;
 
     public final int code;
 
--- a/stress-tester/src/main/java/com/passus/st/emitter/socket/AbstractChannelContext.java	Fri Apr 10 12:47:48 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/socket/AbstractChannelContext.java	Tue Apr 14 09:43:23 2020 +0200
@@ -10,8 +10,7 @@
 import java.util.LinkedList;
 import java.util.Queue;
 
-import static com.passus.st.emitter.StatelessTasks.CLOSE_TASK;
-import static com.passus.st.emitter.StatelessTasks.FLUSH_TASK;
+import static com.passus.st.emitter.StatelessTasks.*;
 
 public abstract class AbstractChannelContext implements ChannelContext {
 
@@ -23,6 +22,8 @@
 
     private FlowContext flowContext;
 
+    private boolean secure;
+
     public AbstractChannelContext(Connection connection) {
         this.connection = connection;
         this.dataQueue = new LinkedList<>();
@@ -58,6 +59,16 @@
     }
 
     @Override
+    public boolean isSecure() {
+        return secure;
+    }
+
+    @Override
+    public void makeSecure() throws IOException {
+        connection.tasks.add(MAKE_SSL_TASK);
+    }
+
+    @Override
     public void write(byte[] data, int offset, int length) throws IOException {
         byte[] out = new byte[length];
         System.arraycopy(data, offset, out, 0, length);
--- a/stress-tester/src/main/java/com/passus/st/emitter/socket/Connection.java	Fri Apr 10 12:47:48 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/socket/Connection.java	Tue Apr 14 09:43:23 2020 +0200
@@ -64,6 +64,8 @@
 
     public abstract void close();
 
+    protected abstract void doMakeSSL();
+
     private void doWrite() {
         Queue<byte[]> queue = channelContext.dataQueue();
         if (!queue.isEmpty()) {
@@ -161,6 +163,9 @@
                         case Task.CONNECT:
                             connect();
                             break;
+                        case Task.MAKE_SSL:
+                            doMakeSSL();
+                            break;
                         case Task.READ:
                             doRead();
                             break;
--- a/stress-tester/src/main/java/com/passus/st/emitter/socket/SocketConnection.java	Fri Apr 10 12:47:48 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/socket/SocketConnection.java	Tue Apr 14 09:43:23 2020 +0200
@@ -8,6 +8,8 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
@@ -73,8 +75,6 @@
             if (bindAddress != null && ANY_SOCKET.equals(bindAddress)) {
                 socket.bind(socketAddressToJdkSocket(bindAddress));
             }
-
-
         } catch (IOException ex) {
             doCatchException(channelContext, ex);
             return;
@@ -117,6 +117,33 @@
     }
 
     @Override
+    protected void doMakeSSL() {
+        if (socket instanceof SSLSocket) {
+            LOGGER.debug("doMakeSSL called but socket is SSLSocket already.");
+            return;
+        }
+
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug("Creating SSL layer.");
+        }
+
+        SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
+        SSLSocket newSocket;
+
+        try {
+            newSocket = (SSLSocket) socketFactory.createSocket(socket,
+                    remoteAddress.getIp().getAddressString(), remoteAddress.getPort(), true);
+            newSocket.setUseClientMode(true);
+            newSocket.startHandshake();
+        } catch (IOException ex) {
+            doCatchException(channelContext, ex);
+            return;
+        }
+
+        socket = newSocket;
+    }
+
+    @Override
     public void close() {
         closeQuietly(socket);
         closeQuietly(in);
--- a/stress-tester/src/test/java/com/passus/st/source/NcEventDestinationTest.java	Fri Apr 10 12:47:48 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/source/NcEventDestinationTest.java	Tue Apr 14 09:43:23 2020 +0200
@@ -1,27 +1,23 @@
 package com.passus.st.source;
 
-import static com.passus.commons.utils.ResourceUtils.createTmpFile;
 import com.passus.st.client.Event;
 import com.passus.st.client.SessionPayloadEvent;
 import com.passus.st.client.SessionStatusEvent;
 import com.passus.st.emitter.SessionInfo;
-import com.passus.st.reader.nc.NcDataBlock;
-import com.passus.st.reader.nc.NcSegmentBlock;
-import com.passus.st.reader.nc.NcSessionInfoBlock;
-import com.passus.st.reader.nc.NcSessionPayloadBlock;
-import com.passus.st.reader.nc.NcSessionStatusBlock;
+import com.passus.st.reader.nc.*;
 import com.passus.st.utils.EventUtils;
 import com.passus.st.utils.NcDataBlockReaderUtils;
+import org.testng.annotations.Test;
+
 import java.io.File;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
+
+import static com.passus.commons.utils.ResourceUtils.createTmpFile;
 import static org.testng.AssertJUnit.assertEquals;
-import org.testng.annotations.Test;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class NcEventDestinationTest {