changeset 1051:ef65dfd56fdb

PgSqlLoginFilter - node def, configure(), instance()
author Devel 1
date Wed, 15 Apr 2020 01:57:37 +0200
parents a7a719af3302
children 9b341057540b
files stress-tester/src/main/java/com/passus/st/client/http/filter/HttpAbstractLoginFilter.java stress-tester/src/main/java/com/passus/st/client/pgsql/filter/PgSqlLoginFilter.java stress-tester/src/main/java/com/passus/st/filter/CredentialsProviderNodeDefinition.java
diffstat 3 files changed, 101 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpAbstractLoginFilter.java	Wed Apr 15 01:20:10 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpAbstractLoginFilter.java	Wed Apr 15 01:57:37 2020 +0200
@@ -1,12 +1,8 @@
 package com.passus.st.client.http.filter;
 
-import com.passus.commons.plugin.PluginInfo;
 import com.passus.config.Configuration;
 import com.passus.config.ConfigurationContext;
-import com.passus.config.annotations.NodeDefinitionCreate;
-import com.passus.config.schema.KeyNameVaryListNodeDefinition;
 import com.passus.config.schema.MapNodeDefinition;
-import com.passus.config.schema.NodeDefinition;
 import com.passus.config.schema.NodeDefinitionCreator;
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpResponse;
@@ -14,13 +10,11 @@
 import com.passus.st.client.FlowContext;
 import com.passus.st.client.credentials.Credentials;
 import com.passus.st.client.credentials.CredentialsProvider;
-import com.passus.st.client.credentials.CredentialsProviderFactory;
-import com.passus.st.client.credentials.MultiCredentialsProviderTransformer;
+import com.passus.st.filter.CredentialsProviderNodeDefinition;
 import com.passus.st.filter.FlowFilterFactory;
 import com.passus.st.filter.MessagePredicate;
 import com.passus.st.filter.MessagePredicateNodeDefinition;
 
-import java.util.Collection;
 
 import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
 import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
@@ -116,47 +110,12 @@
 
     public static class GenericNodeDefinitionCreator implements NodeDefinitionCreator {
 
-        private static KeyNameVaryListNodeDefinition providersNodeDef;
-
-        private static MessagePredicateNodeDefinition messagePredicateNodeDefinition = new MessagePredicateNodeDefinition();
-
-        private static NodeDefinition providersNodeDef() {
-            if (providersNodeDef == null) {
-                providersNodeDef = new KeyNameVaryListNodeDefinition()
-                        .setNodeTransformer(new MultiCredentialsProviderTransformer());
-
-                CredentialsProviderFactory factory = CredentialsProviderFactory.getInstance();
-                Collection<PluginInfo> plugins = factory.getAll();
-                for (PluginInfo plugin : plugins) {
-                    final Class<?> pluginClass = plugin.getPluginClass();
-                    NodeDefinitionCreate nodeDefCreate = pluginClass.getAnnotation(NodeDefinitionCreate.class);
-                    NodeDefinition nodeDef;
-                    if (nodeDefCreate == null) {
-                        nodeDef = mapDef();
-                    } else {
-                        try {
-                            NodeDefinitionCreator creator = nodeDefCreate.value().newInstance();
-                            nodeDef = creator.create();
-                            if (!(nodeDef instanceof MapNodeDefinition)) {
-                                throw new RuntimeException("NodeDefinitionCreator '" + creator.getClass() + "' "
-                                        + "should return '" + MapNodeDefinition.class + "'.");
-                            }
-                        } catch (Exception ex) {
-                            throw new RuntimeException(ex.getMessage(), ex);
-                        }
-                    }
-
-                    providersNodeDef.add(plugin.getElementName(), (MapNodeDefinition) nodeDef);
-                }
-            }
-
-            return providersNodeDef;
-        }
+        private static final MessagePredicateNodeDefinition messagePredicateNodeDefinition = new MessagePredicateNodeDefinition();
 
         public final MapNodeDefinition create(boolean applyIfRequired, boolean providerRequired) {
             MapNodeDefinition mapDef = mapDef().setCanBeEmpty(true);
             mapDef.add(tupleDef("applyIf", messagePredicateNodeDefinition).setRequired(applyIfRequired));
-            mapDef.add(tupleDef("provider", providersNodeDef()).setRequired(providerRequired));
+            mapDef.add(tupleDef("provider", CredentialsProviderNodeDefinition.providersNodeDef()).setRequired(providerRequired));
             return mapDef;
         }
 
--- a/stress-tester/src/main/java/com/passus/st/client/pgsql/filter/PgSqlLoginFilter.java	Wed Apr 15 01:20:10 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/pgsql/filter/PgSqlLoginFilter.java	Wed Apr 15 01:57:37 2020 +0200
@@ -1,5 +1,14 @@
 package com.passus.st.client.pgsql.filter;
 
+import com.passus.commons.annotations.Plugin;
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.annotations.NodeDefinitionCreate;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
+import com.passus.config.schema.MapNodeDefinition;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
 import com.passus.net.pgsql.PgSqlAuthRequestMessage;
 import com.passus.net.pgsql.PgSqlAuthRequestMessageMD5;
 import com.passus.net.pgsql.PgSqlMessage;
@@ -7,6 +16,8 @@
 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 static com.passus.st.utils.HashUtils.HEX_L;
 import static com.passus.st.utils.HashUtils.getMD5;
 import java.nio.charset.StandardCharsets;
@@ -18,15 +29,26 @@
  *
  * @author mikolaj.podbielski
  */
+@NodeDefinitionCreate(PgSqlLoginFilter.PgSqlLoginFilterNodeDefinitionCreator.class)
+@Plugin(name = PgSqlLoginFilter.TYPE, category = PluginConstants.CATEGORY_FLOW_FILTER)
 public class PgSqlLoginFilter extends PgSqlFilter {
 
     private static final Logger LOGGER = LogManager.getLogger(PgSqlLoginFilter.class);
 
-    protected CredentialsProvider credentialsProvider; // init, clone
+    public static final String TYPE = "pgSqlLogin";
+
+    protected CredentialsProvider credentialsProvider;
 
     @Override
     public PgSqlLoginFilter instanceForWorker(int index) {
-        return new PgSqlLoginFilter();
+        PgSqlLoginFilter worker = new PgSqlLoginFilter();
+        worker.credentialsProvider = credentialsProvider;
+        return worker;
+    }
+
+    @Override
+    public void configure(Configuration config, ConfigurationContext context) {
+        credentialsProvider = config.get("provider", null);
     }
 
     @Override
@@ -45,7 +67,7 @@
             PgSqlAuthRequestMessage authReq = context.getParamValue("authReq");
             PgSqlAuthRequestMessage.AuthType authType = authReq.getAuthType();
 
-            Credentials credentials = getCredentials(context);
+            Credentials credentials = credentialsProvider.getCredentials(context);
             String password = credentials.getPassword();
             String user = credentials.getUser();
 
@@ -68,16 +90,19 @@
         return DUNNO;
     }
 
-    protected Credentials getCredentials(FlowContext context) {
-        return credentialsProvider == null ? null : credentialsProvider.getCredentials(context);
-    }
-
     public static String hashMD5(String user, String password, byte[] salt) {
         byte[] hash = hashMD5(user.getBytes(StandardCharsets.UTF_8), password.getBytes(StandardCharsets.UTF_8), salt);
         return new String(hash, StandardCharsets.US_ASCII);
     }
 
-    //MD5(MD5(password + user) + salt)
+    /**
+     * Computes MD5(MD5(password + user) + salt).
+     *
+     * @param user
+     * @param password
+     * @param salt
+     * @return
+     */
     public static byte[] hashMD5(byte[] user, byte[] password, byte[] salt) {
         MessageDigest md5 = getMD5();
         byte[] hexes = new byte[35];
@@ -108,4 +133,14 @@
         }
     }
 
+    public static class PgSqlLoginFilterNodeDefinitionCreator implements NodeDefinitionCreator {
+
+        @Override
+        public NodeDefinition create() {
+            MapNodeDefinition mapDef = mapDef();
+            mapDef.add(tupleDef("provider", CredentialsProviderNodeDefinition.providersNodeDef()).setRequired(true));
+            return mapDef;
+        }
+    }
+
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/filter/CredentialsProviderNodeDefinition.java	Wed Apr 15 01:57:37 2020 +0200
@@ -0,0 +1,55 @@
+package com.passus.st.filter;
+
+import com.passus.commons.plugin.PluginInfo;
+import com.passus.config.annotations.NodeDefinitionCreate;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
+import com.passus.config.schema.KeyNameVaryListNodeDefinition;
+import com.passus.config.schema.MapNodeDefinition;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+import com.passus.st.client.credentials.CredentialsProviderFactory;
+import com.passus.st.client.credentials.MultiCredentialsProviderTransformer;
+import java.util.Collection;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class CredentialsProviderNodeDefinition {
+
+    private static KeyNameVaryListNodeDefinition providersNodeDef;
+
+    public static NodeDefinition providersNodeDef() {
+        if (providersNodeDef == null) {
+            providersNodeDef = new KeyNameVaryListNodeDefinition()
+                    .setNodeTransformer(new MultiCredentialsProviderTransformer());
+
+            CredentialsProviderFactory factory = CredentialsProviderFactory.getInstance();
+            Collection<PluginInfo> plugins = factory.getAll();
+            for (PluginInfo plugin : plugins) {
+                final Class<?> pluginClass = plugin.getPluginClass();
+                NodeDefinitionCreate nodeDefCreate = pluginClass.getAnnotation(NodeDefinitionCreate.class);
+                NodeDefinition nodeDef;
+                if (nodeDefCreate == null) {
+                    nodeDef = mapDef();
+                } else {
+                    try {
+                        NodeDefinitionCreator creator = nodeDefCreate.value().newInstance();
+                        nodeDef = creator.create();
+                        if (!(nodeDef instanceof MapNodeDefinition)) {
+                            throw new RuntimeException("NodeDefinitionCreator '" + creator.getClass() + "' "
+                                    + "should return '" + MapNodeDefinition.class + "'.");
+                        }
+                    } catch (Exception ex) {
+                        throw new RuntimeException(ex.getMessage(), ex);
+                    }
+                }
+
+                providersNodeDef.add(plugin.getElementName(), (MapNodeDefinition) nodeDef);
+            }
+        }
+
+        return providersNodeDef;
+    }
+
+}