changeset 1082:b09fd168aeab

MySqlLoginFilter
author Devel 2
date Mon, 04 May 2020 14:47:48 +0200
parents dd0dad00725c
children 6473e8d9693f
files stress-tester/src/main/java/com/passus/st/client/mysql/filter/MySqlFilter.java stress-tester/src/main/java/com/passus/st/client/mysql/filter/MySqlLoginFilter.java
diffstat 2 files changed, 120 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/mysql/filter/MySqlFilter.java	Mon May 04 14:47:48 2020 +0200
@@ -0,0 +1,33 @@
+package com.passus.st.client.mysql.filter;
+
+import com.passus.net.mysql.MySqlPacket;
+import com.passus.st.client.FlowContext;
+import com.passus.st.filter.FlowFilter;
+
+public abstract class MySqlFilter implements FlowFilter {
+
+    @Override
+    public int filterInbound(Object req, Object resp, FlowContext context) {
+        if (resp instanceof MySqlPacket) {
+            return filterInbound((MySqlPacket) req, (MySqlPacket) resp, context);
+        }
+        return DUNNO;
+    }
+
+    public int filterInbound(MySqlPacket req, MySqlPacket resp, FlowContext context) {
+        return DUNNO;
+    }
+
+    @Override
+    public int filterOutbound(Object req, Object resp, FlowContext context) {
+        if (req instanceof MySqlPacket || resp instanceof MySqlPacket) {
+            return filterOutbound((MySqlPacket) req, (MySqlPacket) resp, context);
+        }
+        return DUNNO;
+    }
+
+    public int filterOutbound(MySqlPacket req, MySqlPacket resp, FlowContext context) {
+        return DUNNO;
+    }
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/mysql/filter/MySqlLoginFilter.java	Mon May 04 14:47:48 2020 +0200
@@ -0,0 +1,87 @@
+package com.passus.st.client.mysql.filter;
+
+import com.passus.commons.annotations.Plugin;
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.annotations.NodeDefinitionCreate;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+import com.passus.net.mysql.MySqlLoginRequest;
+import com.passus.net.mysql.MySqlPacket;
+import com.passus.net.mysql.MySqlPacketTypes;
+import com.passus.net.mysql.MySqlServerGreetingResponse;
+import com.passus.st.client.FlowContext;
+import com.passus.st.client.credentials.Credentials;
+import com.passus.st.client.credentials.CredentialsProvider;
+import com.passus.st.filter.CredentialsProviderNodeDefinition;
+import com.passus.st.plugin.PluginConstants;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
+import static com.passus.net.mysql.MySqlUtils.passScramble411;
+
+@NodeDefinitionCreate(MySqlLoginFilter.MySqlLoginFilterNodeDefinitionCreator.class)
+@Plugin(name = MySqlLoginFilter.TYPE, category = PluginConstants.CATEGORY_FLOW_FILTER)
+public class MySqlLoginFilter extends MySqlFilter {
+
+    private static final Logger LOGGER = LogManager.getLogger(MySqlLoginFilter.class);
+
+    public static final String TYPE = "mySqlLogin";
+
+    protected CredentialsProvider credentialsProvider;
+
+    @Override
+    public MySqlLoginFilter instanceForWorker(int index) {
+        MySqlLoginFilter worker = new MySqlLoginFilter();
+        worker.credentialsProvider = credentialsProvider;
+        return worker;
+    }
+
+    @Override
+    public void configure(Configuration config, ConfigurationContext context) {
+        credentialsProvider = (CredentialsProvider) config.get("provider");
+    }
+
+    @Override
+    public int filterInbound(MySqlPacket req, MySqlPacket resp, FlowContext context) {
+        if (resp.getPacketType() == MySqlPacketTypes.GREETINGS) {
+            context.setParam("mysql.greetings", resp);
+        }
+
+        return DUNNO;
+    }
+
+    @Override
+    public int filterOutbound(MySqlPacket req, MySqlPacket resp, FlowContext context) {
+        if (req.getPacketType() == MySqlPacketTypes.LOGIN_REQUEST) {
+            MySqlServerGreetingResponse greetings = context.getParamValue("mysql.greetings");
+            if (greetings != null) {
+                MySqlLoginRequest loginReq = (MySqlLoginRequest) req;
+                Credentials credentials = credentialsProvider.getCredentials(context);
+                if (credentials != null) {
+                    byte[] passScramble = passScramble411(
+                            credentials.getPassword().getBytes(),
+                            greetings.getAuthSalt().getBytes()
+                    );
+                    loginReq.setUsername(credentials.getUser());
+                    loginReq.setPassword(passScramble);
+                }
+            }
+        }
+
+        return DUNNO;
+    }
+
+    public static class MySqlLoginFilterNodeDefinitionCreator implements NodeDefinitionCreator {
+
+        @Override
+        public NodeDefinition create() {
+            return mapDef(
+                    tupleDef("provider", CredentialsProviderNodeDefinition.providersNodeDef()).setRequired(true)
+            );
+        }
+    }
+
+}