Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard committed Dec 16, 2024
1 parent c837766 commit 72bce42
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
4 changes: 3 additions & 1 deletion docs/transformers/transform_trafficreducer.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Options:
> Interval in seconds to aggregate and process the traffic.
* `unique-fields` (array of strings)
> Define custom fields for uniqueness matching. This allows greater flexibility in detecting repetitive traffic.
> Define custom fields for uniqueness matching (limited to string and integer values).
> This allows greater flexibility in detecting repetitive traffic.
> Complete list of [fields](../dnsconversions.md#json-encoding) available.
Default values:
Expand All @@ -39,6 +40,7 @@ transforms:
- dnstap.identity
- dnstap.operation
- network.query-ip
- network.response-ip
- dns.qname
- dns.qtype
```
Expand Down
2 changes: 1 addition & 1 deletion pkgconfig/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type ConfigTransformers struct {
RepetitiveTrafficDetector bool `yaml:"repetitive-traffic-detector" default:"false"`
QnamePlusOne bool `yaml:"qname-plus-one" default:"false"`
WatchInterval int `yaml:"watch-interval" default:"5"`
UniqueFields []string `yaml:"unique-fields" default:"[\"dnstap.identity\", \"dnstap.operation\", \"network.query-ip\", \"dns.qname\", \"dns.qtype\"]"`
UniqueFields []string `yaml:"unique-fields" default:"[\"dnstap.identity\", \"dnstap.operation\", \"network.query-ip\", \"network.response-ip\", \"dns.qname\", \"dns.qtype\"]"`
} `yaml:"reducer"`
Filtering struct {
Enable bool `yaml:"enable" default:"false"`
Expand Down
9 changes: 8 additions & 1 deletion transformers/reducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,14 @@ func (t *ReducerTransform) repetitiveTrafficDetector(dm *dnsutils.DNSMessage) (i
dmValue := reflect.ValueOf(dm).Elem() // Get the struct value of the DNSMessage
for _, field := range t.config.Reducer.UniqueFields {
if value, found := dnsutils.GetFieldByJSONTag(dmValue, field); found {
t.strBuilder.WriteString(fmt.Sprintf("%v", value.Interface())) // Append field value
// Check if the field's kind is either int or string
switch value.Kind() {
case reflect.Int, reflect.String:
t.strBuilder.WriteString(fmt.Sprintf("%v", value.Interface())) // Append field value
default:
// Skip unsupported types
continue
}
}
}

Expand Down
35 changes: 29 additions & 6 deletions transformers/reducer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestReducer_RepetitiveTrafficDetector(t *testing.T) {
dnsMessagesIn []dnsutils.DNSMessage
}{
{
name: "norepeat",
name: "no_reduce",
dnsMessagesIn: []dnsutils.DNSMessage{
{
DNSTap: dnsutils.DNSTap{Operation: "CLIENT_QUERY"},
Expand All @@ -104,27 +104,50 @@ func TestReducer_RepetitiveTrafficDetector(t *testing.T) {
},
},
{
name: "reduce",
name: "reduce_default_unique_fields",
dnsMessagesIn: []dnsutils.DNSMessage{
{
DNSTap: dnsutils.DNSTap{Operation: "CLIENT_QUERY", Identity: "test"},
DNS: dnsutils.DNS{Qname: "hello.world", Qtype: "A"},
NetworkInfo: dnsutils.DNSNetInfo{QueryIP: "127.0.0.1", ResponseIP: "8.8.8.8"},
},
{
DNSTap: dnsutils.DNSTap{Operation: "CLIENT_QUERY", Identity: "test"},
DNS: dnsutils.DNS{Qname: "hello.world", Qtype: "A"},
NetworkInfo: dnsutils.DNSNetInfo{QueryIP: "127.0.0.1", ResponseIP: "8.8.8.8"},
},
},
dnsMessagesOut: []dnsutils.DNSMessage{
{
Reducer: &dnsutils.TransformReducer{Occurrences: 2},
},
},
},
{
name: "no_reduce_responseip_different",
dnsMessagesIn: []dnsutils.DNSMessage{
{
DNSTap: dnsutils.DNSTap{Operation: "CLIENT_QUERY"},
DNS: dnsutils.DNS{Qname: "hello.world", Qtype: "A"},
NetworkInfo: dnsutils.DNSNetInfo{QueryIP: "127.0.0.1"},
NetworkInfo: dnsutils.DNSNetInfo{QueryIP: "127.0.0.1", ResponseIP: "1.1.1.1"},
},
{
DNSTap: dnsutils.DNSTap{Operation: "CLIENT_QUERY"},
DNS: dnsutils.DNS{Qname: "hello.world", Qtype: "A"},
NetworkInfo: dnsutils.DNSNetInfo{QueryIP: "127.0.0.1"},
NetworkInfo: dnsutils.DNSNetInfo{QueryIP: "127.0.0.1", ResponseIP: "8.8.8.8"},
},
},
dnsMessagesOut: []dnsutils.DNSMessage{
{
Reducer: &dnsutils.TransformReducer{Occurrences: 2},
Reducer: &dnsutils.TransformReducer{Occurrences: 1},
},
{
Reducer: &dnsutils.TransformReducer{Occurrences: 1},
},
},
},
{
name: "norepeat_qtype",
name: "no_reduce_qtype_different",
dnsMessagesIn: []dnsutils.DNSMessage{
{
DNSTap: dnsutils.DNSTap{Operation: "CLIENT_QUERY"},
Expand Down

0 comments on commit 72bce42

Please sign in to comment.