diff --git a/v2/pkg/protocols/dns/operators.go b/v2/pkg/protocols/dns/operators.go index 44b352964..d2b6be7fa 100644 --- a/v2/pkg/protocols/dns/operators.go +++ b/v2/pkg/protocols/dns/operators.go @@ -96,7 +96,10 @@ func (request *Request) responseToDSLMap(req, resp *dns.Msg, host, matched strin "type": request.Type().String(), "trace": traceToString(traceData, false), } - return generators.MergeMaps(ret, recordsKeyValue(resp.Answer)) + if len(resp.Answer) > 0 { + ret = generators.MergeMaps(ret, recordsKeyValue(resp.Answer)) + } + return ret } // MakeResultEvent creates a result event from internal wrapped event @@ -156,12 +159,14 @@ func recordsKeyValue(resourceRecords []dns.RR) output.InternalEvent { key := strings.ToLower(dns.TypeToString[resourceRecord.Header().Rrtype]) value := strings.ReplaceAll(resourceRecord.String(), resourceRecord.Header().String(), "") - if preVal, ok := oe[key]; ok { - switch v := oe[key].(type) { + // if the key is already present, we need to convert the value to a slice + // if the key has slice, then append the value to the slice + if previous, ok := oe[key]; ok { + switch v := previous.(type) { case string: - oe[key] = []string{value, preVal.(string)} + oe[key] = []string{v, value} case []string: - oe[key] = append(v, preVal.([]string)...) + oe[key] = append(v, value) } continue } diff --git a/v2/pkg/protocols/dns/operators_test.go b/v2/pkg/protocols/dns/operators_test.go index b5223622b..8cbcbdcd2 100644 --- a/v2/pkg/protocols/dns/operators_test.go +++ b/v2/pkg/protocols/dns/operators_test.go @@ -43,12 +43,12 @@ func TestResponseToDSLMap(t *testing.T) { resp := new(dns.Msg) resp.Rcode = dns.RcodeSuccess - resp.Answer = append(resp.Answer, &dns.A{A: net.ParseIP("1.1.1.1"), Hdr: dns.RR_Header{Name: "one.one.one.one.", Rrtype: dns.TypeA}}, &dns.A{A: net.ParseIP("2.2.2.2"), Hdr: dns.RR_Header{Name: "one.one.one.one.", Rrtype: dns.TypeA}}) + resp.Answer = append(resp.Answer, &dns.A{A: net.ParseIP("1.1.1.1"), Hdr: dns.RR_Header{Name: "one.one.one.one.", Rrtype: dns.TypeA}}, &dns.A{A: net.ParseIP("2.2.2.2"), Hdr: dns.RR_Header{Name: "one.one.one.one.", Rrtype: dns.TypeA}}, &dns.A{A: net.ParseIP("3.3.3.3"), Hdr: dns.RR_Header{Name: "one.one.one.one.", Rrtype: dns.TypeA}}) event := request.responseToDSLMap(req, resp, "one.one.one.one", "one.one.one.one", nil) require.Len(t, event, 15, "could not get correct number of items in dsl map") require.Equal(t, dns.RcodeSuccess, event["rcode"], "could not get correct rcode") - require.ElementsMatch(t, []string{net.ParseIP("1.1.1.1").String(), net.ParseIP("2.2.2.2").String()}, event["a"], "could not get correct a record") + require.ElementsMatch(t, []string{net.ParseIP("1.1.1.1").String(), net.ParseIP("2.2.2.2").String(), net.ParseIP("3.3.3.3").String()}, event["a"], "could not get correct a record") } func TestDNSOperatorMatch(t *testing.T) {