changeset 1013:f65876d67d33

DnsFilter, DnsRecordTypeFilter + HttpMatchFilter bugfix
author Devel 2
date Tue, 24 Mar 2020 14:36:47 +0100
parents cd6132690104
children 6be220a546db
files stress-tester/src/main/java/com/passus/st/client/dns/filter/DnsFilter.java stress-tester/src/main/java/com/passus/st/client/dns/filter/DnsRecordTypeFilter.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMatchFilter.java stress-tester/src/test/java/com/passus/st/client/dns/filter/DnsRecordTypeFilterTest.java
diffstat 4 files changed, 165 insertions(+), 2 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/dns/filter/DnsFilter.java	Tue Mar 24 14:36:47 2020 +0100
@@ -0,0 +1,36 @@
+package com.passus.st.client.dns.filter;
+
+import com.passus.net.dns.DnsRecord;
+import com.passus.st.client.FlowContext;
+import com.passus.st.client.filter.FlowFilter;
+
+public abstract class DnsFilter implements FlowFilter {
+
+    @Override
+    public int filterInbound(Object req, Object resp, FlowContext context) {
+        if (req instanceof DnsRecord
+                || resp instanceof DnsRecord) {
+            return filterInbound((DnsRecord) req, (DnsRecord) resp, context);
+        }
+
+        return DUNNO;
+    }
+
+    public int filterInbound(DnsRecord req, DnsRecord resp, FlowContext context) {
+        return DUNNO;
+    }
+
+    @Override
+    public int filterOutbound(Object req, Object resp, FlowContext context) {
+        if (req instanceof DnsRecord
+                || resp instanceof DnsRecord) {
+            return filterOutbound((DnsRecord) req, (DnsRecord) resp, context);
+        }
+
+        return DUNNO;
+    }
+
+    public int filterOutbound(DnsRecord req, DnsRecord resp, FlowContext context) {
+        return DUNNO;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/dns/filter/DnsRecordTypeFilter.java	Tue Mar 24 14:36:47 2020 +0100
@@ -0,0 +1,84 @@
+package com.passus.st.client.dns.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.dns.DnsRecord;
+import com.passus.net.dns.DnsRecordType;
+import com.passus.st.client.FlowContext;
+import com.passus.st.client.filter.FlowFilter;
+import com.passus.st.plugin.PluginConstants;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
+
+@NodeDefinitionCreate(DnsRecordTypeFilter.DnsRecordTypeFilterNodeDefCreator.class)
+@Plugin(name = DnsRecordTypeFilter.TYPE, category = PluginConstants.CATEGORY_FLOW_FILTER)
+public class DnsRecordTypeFilter extends DnsFilter {
+
+    public static final String TYPE = "dns.recordType";
+
+    private Set<DnsRecordType> recordTypes = new HashSet<>();
+
+    private boolean acceptOnMatch = true;
+
+    public Set<DnsRecordType> getRecordTypes() {
+        return Collections.unmodifiableSet(recordTypes);
+    }
+
+    public void setRecordTypes(Set<DnsRecordType> recordTypes) {
+        this.recordTypes.clear();
+        this.recordTypes.addAll(recordTypes);
+    }
+
+    public boolean isAcceptOnMatch() {
+        return acceptOnMatch;
+    }
+
+    public void setAcceptOnMatch(boolean acceptOnMatch) {
+        this.acceptOnMatch = acceptOnMatch;
+    }
+
+    @Override
+    public void configure(Configuration config, ConfigurationContext context) {
+        recordTypes.clear();
+        recordTypes.addAll((List<DnsRecordType>) config.get("recordTypes"));
+        acceptOnMatch = config.getBoolean("acceptOnMatch", true);
+    }
+
+    @Override
+    public int filterOutbound(DnsRecord req, DnsRecord resp, FlowContext context) {
+        if (!recordTypes.contains(req.getType())) {
+            return DUNNO;
+        }
+
+        return acceptOnMatch ? ACCEPT : DENY;
+    }
+
+    @Override
+    public FlowFilter instanceForWorker(int index) {
+        DnsRecordTypeFilter filter = new DnsRecordTypeFilter();
+        filter.recordTypes.addAll(recordTypes);
+        filter.acceptOnMatch = acceptOnMatch;
+        return filter;
+    }
+
+    public static class DnsRecordTypeFilterNodeDefCreator implements NodeDefinitionCreator {
+
+        @Override
+        public NodeDefinition create() {
+            return mapDef(
+                    tupleDef("acceptOnMatch", valueDefBool()).setRequired(false),
+                    tupleDef("recordTypes", listDef(enumDef(DnsRecordType.class)))
+            );
+        }
+
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMatchFilter.java	Tue Mar 24 12:51:41 2020 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMatchFilter.java	Tue Mar 24 14:36:47 2020 +0100
@@ -11,8 +11,7 @@
 import com.passus.st.client.FlowContext;
 import com.passus.st.plugin.PluginConstants;
 
-import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
-import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
 
 /**
  * @author Mirosław Hawrot
@@ -77,6 +76,7 @@
         @Override
         public NodeDefinition create() {
             return mapDef(
+                    tupleDef("acceptOnMatch", valueDefBool()),
                     tupleDef("matches", new HttpFilterMessagePredicateNodeDefinition())
             );
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/client/dns/filter/DnsRecordTypeFilterTest.java	Tue Mar 24 14:36:47 2020 +0100
@@ -0,0 +1,43 @@
+package com.passus.st.client.dns.filter;
+
+import com.passus.config.NodeException;
+import com.passus.config.validation.Errors;
+import com.passus.net.dns.DnsRecordType;
+import com.passus.st.client.filter.FlowFilter;
+import com.passus.st.client.filter.FlowFiltersConfigurator;
+import org.testng.annotations.Test;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Set;
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertTrue;
+
+public class DnsRecordTypeFilterTest {
+
+    @Test
+    public void testConfigure() throws IOException, NodeException {
+        String filterConfig = "filters:\n"
+                + "    - type: dns.recordType\n"
+                + "      acceptOnMatch: true\n"
+                + "      recordTypes: ['A', 'TXT', 'PTR']\n";
+
+        Errors errors = new Errors();
+        List<FlowFilter> filters = FlowFiltersConfigurator.getFilters(filterConfig, errors, null);
+
+        errors.getAllErrors().forEach(System.out::println);
+        assertEquals(0, errors.getErrorCount());
+        assertEquals(1, filters.size());
+        assertTrue(filters.get(0) instanceof DnsRecordTypeFilter);
+
+        DnsRecordTypeFilter filter = (DnsRecordTypeFilter) filters.get(0);
+        assertTrue(filter.isAcceptOnMatch());
+        Set<DnsRecordType> recordTypes = filter.getRecordTypes();
+        assertEquals(3, recordTypes.size());
+        assertTrue(recordTypes.contains(DnsRecordType.A));
+        assertTrue(recordTypes.contains(DnsRecordType.TXT));
+        assertTrue(recordTypes.contains(DnsRecordType.PTR));
+    }
+
+}
\ No newline at end of file