| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | package dnstap
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	"github.com/coredns/coredns/plugin/dnstap/msg"
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metadata"
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	test "github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2021-01-24 18:28:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	tap "github.com/dnstap/golang-dnstap"
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | func testCase(t *testing.T, tapq, tapr *tap.Dnstap, q, r *dns.Msg, extraFormat string) {
 | 
					
						
							| 
									
										
										
										
											2025-06-05 00:36:04 +03:00
										 |  |  | 	t.Helper()
 | 
					
						
							| 
									
										
										
										
											2017-09-26 17:45:33 +02:00
										 |  |  | 	w := writer{t: t}
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	w.queue = append(w.queue, tapq, tapr)
 | 
					
						
							|  |  |  | 	h := Dnstap{
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		Next: test.HandlerFunc(func(_ context.Context,
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 			w dns.ResponseWriter, _ *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 			return 0, w.WriteMsg(r)
 | 
					
						
							|  |  |  | 		}),
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 		io:          &w,
 | 
					
						
							|  |  |  | 		ExtraFormat: extraFormat,
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 	ctx := metadata.ContextWithMetadata(context.TODO())
 | 
					
						
							|  |  |  | 	ok := metadata.SetValueFunc(ctx, "metadata/test", func() string {
 | 
					
						
							|  |  |  | 		return "MetadataValue"
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		t.Fatal("Failed to set metadata")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	_, err := h.ServeDNS(ctx, &test.ResponseWriter{}, q)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type writer struct {
 | 
					
						
							| 
									
										
										
										
											2017-09-26 17:45:33 +02:00
										 |  |  | 	t     *testing.T
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 	queue []*tap.Dnstap
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 20:06:33 +02:00
										 |  |  | func (w *writer) Dnstap(e *tap.Dnstap) {
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	if len(w.queue) == 0 {
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		w.t.Error("Message not expected")
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 	ex := w.queue[0].GetMessage()
 | 
					
						
							|  |  |  | 	got := e.GetMessage()
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 	eaddr := string(ex.GetQueryAddress())
 | 
					
						
							|  |  |  | 	gaddr := string(got.GetQueryAddress())
 | 
					
						
							|  |  |  | 	if eaddr != gaddr {
 | 
					
						
							|  |  |  | 		w.t.Errorf("Expected source address %s, got %s", eaddr, gaddr)
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	eraddr := string(ex.GetResponseAddress())
 | 
					
						
							|  |  |  | 	graddr := string(got.GetResponseAddress())
 | 
					
						
							|  |  |  | 	if eraddr != graddr {
 | 
					
						
							|  |  |  | 		w.t.Errorf("Expected response address %s, got %s", eraddr, graddr)
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ep := ex.GetQueryPort()
 | 
					
						
							|  |  |  | 	gp := got.GetQueryPort()
 | 
					
						
							|  |  |  | 	if ep != gp {
 | 
					
						
							|  |  |  | 		w.t.Errorf("Expected port %d, got %d", ep, gp)
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ef := ex.GetSocketFamily()
 | 
					
						
							|  |  |  | 	sf := got.GetSocketFamily()
 | 
					
						
							|  |  |  | 	if ef != sf {
 | 
					
						
							|  |  |  | 		w.t.Errorf("Expected socket family %d, got %d", ef, sf)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	eext := string(w.queue[0].GetExtra())
 | 
					
						
							|  |  |  | 	gext := string(e.GetExtra())
 | 
					
						
							|  |  |  | 	if eext != gext {
 | 
					
						
							|  |  |  | 		w.t.Errorf("Expected extra %s, got %s", eext, gext)
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	w.queue = w.queue[1:]
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestDnstap(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	q := test.Case{Qname: "example.org", Qtype: dns.TypeA}.Msg()
 | 
					
						
							|  |  |  | 	r := test.Case{
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 		Qname: "example.org.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 			test.A("example.org. 3600	IN	A 10.0.0.1"),
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	}.Msg()
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tapq := &tap.Dnstap{
 | 
					
						
							|  |  |  | 		Message: testMessage(),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 	msg.SetType(tapq.GetMessage(), tap.Message_CLIENT_QUERY)
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 	tapr := &tap.Dnstap{
 | 
					
						
							|  |  |  | 		Message: testMessage(),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 	msg.SetType(tapr.GetMessage(), tap.Message_CLIENT_RESPONSE)
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 	testCase(t, tapq, tapr, q, r, "")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tapq_with_extra := &tap.Dnstap{
 | 
					
						
							|  |  |  | 		Message: testMessage(), // leave type unset for deepEqual
 | 
					
						
							|  |  |  | 		Extra:   []byte("extra_field_MetadataValue_A_example.org._IN_udp_29_10.240.0.1_40212_127.0.0.1"),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 	msg.SetType(tapq_with_extra.GetMessage(), tap.Message_CLIENT_QUERY)
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 	tapr_with_extra := &tap.Dnstap{
 | 
					
						
							|  |  |  | 		Message: testMessage(),
 | 
					
						
							|  |  |  | 		Extra:   []byte("extra_field_MetadataValue_A_example.org._IN_udp_29_10.240.0.1_40212_127.0.0.1"),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 	msg.SetType(tapr_with_extra.GetMessage(), tap.Message_CLIENT_RESPONSE)
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 	extraFormat := "extra_field_{/metadata/test}_{type}_{name}_{class}_{proto}_{size}_{remote}_{port}_{local}"
 | 
					
						
							|  |  |  | 	testCase(t, tapq_with_extra, tapr_with_extra, q, r, extraFormat)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | func testMessage() *tap.Message {
 | 
					
						
							|  |  |  | 	inet := tap.SocketFamily_INET
 | 
					
						
							|  |  |  | 	udp := tap.SocketProtocol_UDP
 | 
					
						
							|  |  |  | 	port := uint32(40212)
 | 
					
						
							|  |  |  | 	return &tap.Message{
 | 
					
						
							|  |  |  | 		SocketFamily:   &inet,
 | 
					
						
							|  |  |  | 		SocketProtocol: &udp,
 | 
					
						
							|  |  |  | 		QueryAddress:   net.ParseIP("10.240.0.1"),
 | 
					
						
							|  |  |  | 		QueryPort:      &port,
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestTapMessage(t *testing.T) {
 | 
					
						
							|  |  |  | 	extraFormat := "extra_field_no_replacement_{/metadata/test}_{type}_{name}_{class}_{proto}_{size}_{remote}_{port}_{local}"
 | 
					
						
							|  |  |  | 	tapq := &tap.Dnstap{
 | 
					
						
							|  |  |  | 		Message: testMessage(),
 | 
					
						
							|  |  |  | 		// extra field would not be replaced, since TapMessage won't pass context
 | 
					
						
							|  |  |  | 		Extra: []byte(extraFormat),
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 	msg.SetType(tapq.GetMessage(), tap.Message_CLIENT_QUERY)
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	w := writer{t: t}
 | 
					
						
							|  |  |  | 	w.queue = append(w.queue, tapq)
 | 
					
						
							|  |  |  | 	h := Dnstap{
 | 
					
						
							|  |  |  | 		Next: test.HandlerFunc(func(_ context.Context,
 | 
					
						
							|  |  |  | 			w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 			return 0, w.WriteMsg(r)
 | 
					
						
							|  |  |  | 		}),
 | 
					
						
							|  |  |  | 		io:          &w,
 | 
					
						
							|  |  |  | 		ExtraFormat: extraFormat,
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-31 01:29:32 +03:00
										 |  |  | 	h.TapMessage(tapq.GetMessage())
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | }
 |