changeset 1236:d526a06722f7

SocketConnection - SSL trust all + bugfixes
author Devel 2
date Tue, 30 Jun 2020 09:04:20 +0200
parents 5dc2ff622d6c
children e8d302936240
files stress-tester/src/main/java/com/passus/st/client/FlowProcessor.java stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapper.java stress-tester/src/main/java/com/passus/st/emitter/SessionMapper.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/main/java/com/passus/st/emitter/socket/SocketEmitter.java stress-tester/src/main/java/com/passus/st/utils/SSLUtils.java
diffstat 7 files changed, 73 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/FlowProcessor.java	Mon Jun 29 21:45:45 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/FlowProcessor.java	Tue Jun 30 09:04:20 2020 +0200
@@ -13,6 +13,7 @@
 import com.passus.st.filter.FlowFilterChain;
 import org.apache.logging.log4j.Logger;
 
+import static com.passus.commons.utils.StringUtils.escapeAsciiControlChars;
 import static com.passus.st.client.FlowContext.*;
 import static com.passus.st.client.FlowError.*;
 import static com.passus.st.client.FlowUtils.*;
@@ -428,6 +429,17 @@
                 FlowHandlerDataDecoder decoder = client.getResponseDecoder(flowContext);
                 int res = decoder.decode(data, flowContext);
 
+                if (logger.isDebugEnabled()) {
+                    debug(logger, flowContext, "Decoded " + res + "B. ");
+                }
+
+                if (res > 0) {
+                    data.skipBytes(res);
+                    if (logger.isDebugEnabled()) {
+                        debug(logger, flowContext, "Left " + data.readableBytes() + "B. ");
+                    }
+                }
+
                 long now = System.currentTimeMillis();
                 if (flowContext.receivedStartTimestamp() == -1) {
                     flowContext.receivedStartTimestamp(now);
@@ -440,13 +452,14 @@
                         }
                     }
 
+                    String decoderError = decoder.getLastError();
                     if (logger.isDebugEnabled()) {
-                        debug(logger, flowContext, "Decoder error. " + decoder.getLastError());
+                        debug(logger, flowContext, "Decoder error. " + decoderError + ". " + escapeAsciiControlChars(data.toString(0, 100)));
                     }
 
                     responseReceived0(flowContext, null, true);
                     decoder.clear(flowContext);
-                    throw new Exception("Decoder error. " + decoder.getLastError());
+                    throw new Exception("Decoder error. " + decoderError);
                 } else if (decoder.state() == DataDecoder.STATE_FINISHED) {
                     if (collectMetric) {
                         synchronized (metric) {
--- a/stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapper.java	Mon Jun 29 21:45:45 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/RuleBasedSessionMapper.java	Tue Jun 30 09:04:20 2020 +0200
@@ -88,7 +88,7 @@
             if (params == null) {
                 for (Rule rule : rules) {
                     if (rule.match(sessionInfo)) {
-                        params = new ConnectionParams(rule.getRemoteAddress(), rule.getBindAddress());
+                        params = new ConnectionParams(rule.getRemoteAddress(), rule.getBindAddress(), rule.getParameters());
                         map.put(sessionInfo, params);
                         break;
                     }
--- a/stress-tester/src/main/java/com/passus/st/emitter/SessionMapper.java	Mon Jun 29 21:45:45 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/SessionMapper.java	Tue Jun 30 09:04:20 2020 +0200
@@ -4,7 +4,7 @@
 import com.passus.net.Ip4Address;
 import com.passus.net.SocketAddress;
 
-import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @author Mirosław Hawrot
@@ -25,7 +25,7 @@
 
         private final SocketAddress bindAddress;
 
-        private final HashMap<String, Object> parameters;
+        private final Map<String, Object> parameters;
 
         public ConnectionParams() {
             this(null);
@@ -39,7 +39,7 @@
             this(remoteAddress, bindAddress, null);
         }
 
-        public ConnectionParams(SocketAddress remoteAddress, SocketAddress bindAddress, HashMap<String, Object> parameters) {
+        public ConnectionParams(SocketAddress remoteAddress, SocketAddress bindAddress, Map<String, Object> parameters) {
             Assert.notNull(bindAddress, "bindAddress");
             this.remoteAddress = remoteAddress;
             this.bindAddress = bindAddress;
@@ -78,6 +78,13 @@
                 sb.append(bindAddress);
             }
 
+            if (parameters != null && !parameters.isEmpty()) {
+                sb.append("; ");
+                parameters.forEach((k, v) -> {
+                    sb.append(k).append("=").append(v).append(" ");
+                });
+            }
+
             return sb.toString();
         }
 
--- a/stress-tester/src/main/java/com/passus/st/emitter/socket/Connection.java	Mon Jun 29 21:45:45 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/socket/Connection.java	Tue Jun 30 09:04:20 2020 +0200
@@ -100,7 +100,7 @@
     protected abstract void write(byte[] data, int offset, int length);
 
     private void doRead() {
-        buffer.clear();
+        //buffer.clear();
 
         try {
             int read;
--- a/stress-tester/src/main/java/com/passus/st/emitter/socket/SocketConnection.java	Mon Jun 29 21:45:45 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/socket/SocketConnection.java	Tue Jun 30 09:04:20 2020 +0200
@@ -10,16 +10,21 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSocket;
 import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.TrustManager;
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.Socket;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
 
 import static com.passus.net.utils.AddressUtils.jdkSocketToSocketAddress;
 import static com.passus.net.utils.AddressUtils.socketAddressToJdkSocket;
+import static com.passus.st.utils.SSLUtils.TRUST_ALL_CERTIFICATES;
 
 public class SocketConnection extends Connection {
 
@@ -57,6 +62,12 @@
         return connectionPending;
     }
 
+    private SSLSocketFactory sslSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
+        final SSLContext context = SSLContext.getInstance("SSL");
+        context.init(null, new TrustManager[]{TRUST_ALL_CERTIFICATES}, null);
+        return context.getSocketFactory();
+    }
+
     @Override
     public void connect() {
         SessionMapper.ConnectionParams connParams = getConnParams(sessionInfo, handler);
@@ -71,7 +82,7 @@
         try {
             SocketAddress bindAddress = connParams.getBindAddress();
             if (connParams.isSsl()) {
-                SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
+                SSLSocketFactory factory = sslSocketFactory();
                 socket = factory.createSocket();
             } else {
                 socket = new Socket();
@@ -79,12 +90,10 @@
 
             if (bindAddress != null) {
                 socket.bind(socketAddressToJdkSocket(bindAddress));
-
-
             }
 
             socketParameters.apply(socket);
-        } catch (IOException ex) {
+        } catch (Exception ex) {
             doCatchException(channelContext, ex);
             return;
         }
@@ -147,20 +156,20 @@
             LOGGER.debug("Creating SSL layer.");
         }
 
-        SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
-        SSLSocket newSocket;
+        try {
+            final SSLSocketFactory socketFactory = sslSocketFactory();
+            SSLSocket newSocket;
 
-        try {
             newSocket = (SSLSocket) socketFactory.createSocket(socket,
                     remoteAddress.getIp().getAddressString(), remoteAddress.getPort(), true);
             newSocket.setUseClientMode(true);
             newSocket.startHandshake();
-        } catch (IOException ex) {
+
+            socket = newSocket;
+        } catch (Exception ex) {
             doCatchException(channelContext, ex);
             return;
         }
-
-        socket = newSocket;
     }
 
     @Override
@@ -215,4 +224,6 @@
 
         return readed;
     }
+
+
 }
--- a/stress-tester/src/main/java/com/passus/st/emitter/socket/SocketEmitter.java	Mon Jun 29 21:45:45 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/socket/SocketEmitter.java	Tue Jun 30 09:04:20 2020 +0200
@@ -21,7 +21,6 @@
 import java.util.concurrent.ConcurrentHashMap;
 
 import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
-import static com.passus.st.config.CommonNodeDefs.INT_GREATER_THAN_ZERO_DEF;
 
 @NodeDefinitionCreate(SocketEmitter.SocketEmitterNodeDefCreator.class)
 @Plugin(name = SocketEmitter.TYPE, category = PluginConstants.CATEGORY_EMITTER)
@@ -189,7 +188,9 @@
                 public void onConnectionClosed(ChannelContext channelContext) {
                     if (collectMetrics) {
                         Connection connection = connections.get(channelContext.getSessionInfo());
-                        deferredMetrics.update(connection.metric);
+                        if (connection != null) {
+                            deferredMetrics.update(connection.metric);
+                        }
                     }
 
                     connections.remove(channelContext.getSessionInfo());
--- a/stress-tester/src/main/java/com/passus/st/utils/SSLUtils.java	Mon Jun 29 21:45:45 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/utils/SSLUtils.java	Tue Jun 30 09:04:20 2020 +0200
@@ -2,14 +2,36 @@
 
 import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.TrustManagerFactory;
+import javax.net.ssl.X509TrustManager;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.security.GeneralSecurityException;
 import java.security.KeyStore;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
 
 public class SSLUtils {
 
+    public static final X509TrustManager TRUST_ALL_CERTIFICATES = new X509TrustManager() {
+
+        @Override
+        public void checkClientTrusted(final X509Certificate[] chain, final String authType)
+                throws CertificateException {
+        }
+
+        @Override
+        public void checkServerTrusted(final X509Certificate[] chain, final String authType)
+                throws CertificateException {
+        }
+
+        @Override
+        public X509Certificate[] getAcceptedIssuers() {
+            return new X509Certificate[0];
+        }
+
+    };
+
     private SSLUtils() {
     }