diff --git a/plugin/dnstap/README.md b/plugin/dnstap/README.md index 029a93907..8e5615772 100644 --- a/plugin/dnstap/README.md +++ b/plugin/dnstap/README.md @@ -15,11 +15,16 @@ Every message is sent to the socket as soon as it comes in, the *dnstap* plugin ## Syntax ~~~ txt -dnstap SOCKET [full] +dnstap SOCKET [full] { + [identity IDENTITY] + [version VERSION] +} ~~~ * **SOCKET** is the socket (path) supplied to the dnstap command line tool. * `full` to include the wire-format DNS message. +* **IDENTITY** to override the identity of the server. Defaults to the hostname. +* **VERSION** to override the version field. Defaults to the CoreDNS version. ## Examples @@ -47,6 +52,15 @@ Log to a remote endpoint by FQDN. dnstap tcp://example.com:6000 full ~~~ +Log to a socket, overriding the default identity and version. + +~~~ txt +dnstap /tmp/dnstap.sock { + identity my-dns-server1 + version MyDNSServer-1.2.3 +} +~~~ + ## Command Line Tool Dnstap has a command line tool that can be used to inspect the logging. The tool can be found diff --git a/plugin/dnstap/handler.go b/plugin/dnstap/handler.go index 04d29860e..e8a1fbc4a 100644 --- a/plugin/dnstap/handler.go +++ b/plugin/dnstap/handler.go @@ -18,12 +18,14 @@ type Dnstap struct { // IncludeRawMessage will include the raw DNS message into the dnstap messages if true. IncludeRawMessage bool + Identity []byte + Version []byte } // TapMessage sends the message m to the dnstap interface. func (h Dnstap) TapMessage(m *tap.Message) { t := tap.Dnstap_MESSAGE - h.io.Dnstap(&tap.Dnstap{Type: &t, Message: m}) + h.io.Dnstap(&tap.Dnstap{Type: &t, Message: m, Identity: h.Identity, Version: h.Version}) } func (h Dnstap) tapQuery(w dns.ResponseWriter, query *dns.Msg, queryTime time.Time) { diff --git a/plugin/dnstap/setup.go b/plugin/dnstap/setup.go index dfe63f38b..d7d1cdc1b 100644 --- a/plugin/dnstap/setup.go +++ b/plugin/dnstap/setup.go @@ -2,6 +2,7 @@ package dnstap import ( "net/url" + "os" "strings" "github.com/coredns/caddy" @@ -19,10 +20,14 @@ func parseConfig(c *caddy.Controller) (Dnstap, error) { d := Dnstap{} endpoint := "" - if !c.Args(&endpoint) { + args := c.RemainingArgs() + + if len(args) == 0 { return d, c.ArgErr() } + endpoint = args[0] + if strings.HasPrefix(endpoint, "tcp://") { // remote network endpoint endpointURL, err := url.Parse(endpoint) @@ -37,7 +42,30 @@ func parseConfig(c *caddy.Controller) (Dnstap, error) { d = Dnstap{io: dio} } - d.IncludeRawMessage = c.NextArg() && c.Val() == "full" + d.IncludeRawMessage = len(args) == 2 && args[1] == "full" + + hostname, _ := os.Hostname() + d.Identity = []byte(hostname) + d.Version = []byte(caddy.AppName + "-" + caddy.AppVersion) + + for c.NextBlock() { + switch c.Val() { + case "identity": + { + if !c.NextArg() { + return d, c.ArgErr() + } + d.Identity = []byte(c.Val()) + } + case "version": + { + if !c.NextArg() { + return d, c.ArgErr() + } + d.Version = []byte(c.Val()) + } + } + } return d, nil } diff --git a/plugin/dnstap/setup_test.go b/plugin/dnstap/setup_test.go index 0c680e813..9d5f20a92 100644 --- a/plugin/dnstap/setup_test.go +++ b/plugin/dnstap/setup_test.go @@ -1,25 +1,32 @@ package dnstap import ( + "os" "testing" "github.com/coredns/caddy" ) func TestConfig(t *testing.T) { + hostname, _ := os.Hostname() tests := []struct { in string endpoint string full bool proto string fail bool + identity []byte + version []byte }{ - {"dnstap dnstap.sock full", "dnstap.sock", true, "unix", false}, - {"dnstap unix://dnstap.sock", "dnstap.sock", false, "unix", false}, - {"dnstap tcp://127.0.0.1:6000", "127.0.0.1:6000", false, "tcp", false}, - {"dnstap tcp://[::1]:6000", "[::1]:6000", false, "tcp", false}, - {"dnstap tcp://example.com:6000", "example.com:6000", false, "tcp", false}, - {"dnstap", "fail", false, "tcp", true}, + {"dnstap dnstap.sock full", "dnstap.sock", true, "unix", false, []byte(hostname), []byte("-")}, + {"dnstap unix://dnstap.sock", "dnstap.sock", false, "unix", false, []byte(hostname), []byte("-")}, + {"dnstap tcp://127.0.0.1:6000", "127.0.0.1:6000", false, "tcp", false, []byte(hostname), []byte("-")}, + {"dnstap tcp://[::1]:6000", "[::1]:6000", false, "tcp", false, []byte(hostname), []byte("-")}, + {"dnstap tcp://example.com:6000", "example.com:6000", false, "tcp", false, []byte(hostname), []byte("-")}, + {"dnstap", "fail", false, "tcp", true, []byte(hostname), []byte("-")}, + {"dnstap dnstap.sock full {\nidentity NAME\nversion VER\n}\n", "dnstap.sock", true, "unix", false, []byte("NAME"), []byte("VER")}, + {"dnstap dnstap.sock {\nidentity NAME\nversion VER\n}\n", "dnstap.sock", false, "unix", false, []byte("NAME"), []byte("VER")}, + {"dnstap {\nidentity NAME\nversion VER\n}\n", "fail", false, "tcp", true, []byte("NAME"), []byte("VER")}, } for i, tc := range tests { c := caddy.NewTestController("dns", tc.in) @@ -43,5 +50,11 @@ func TestConfig(t *testing.T) { if x := tap.IncludeRawMessage; x != tc.full { t.Errorf("Test %d: expected IncludeRawMessage %t, got %t", i, tc.full, x) } + if x := string(tap.Identity); x != string(tc.identity) { + t.Errorf("Test %d: expected identity %s, got %s", i, tc.identity, x) + } + if x := string(tap.Version); x != string(tc.version) { + t.Errorf("Test %d: expected version %s, got %s", i, tc.version, x) + } } }