Skip to content

Commit

Permalink
feat(powerdns): add hex value for MessageId and InitialRequestorId (#907
Browse files Browse the repository at this point in the history
)

* feat(powerdns): add hex value for MessageId and InitialRequestorId
  • Loading branch information
dmachard authored Dec 18, 2024
1 parent 163bc3e commit db16141
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<p align="center">
<img src="https://goreportcard.com/badge/github.com/dmachard/go-dns-collector" alt="Go Report"/>
<img src="https://img.shields.io/badge/go%20version-min%201.21-green" alt="Go version"/>
<img src="https://img.shields.io/badge/go%20tests-517-green" alt="Go tests"/>
<img src="https://img.shields.io/badge/go%20tests-518-green" alt="Go tests"/>
<img src="https://img.shields.io/badge/go%20bench-21-green" alt="Go bench"/>
<img src="https://img.shields.io/badge/go%20lines-32537-green" alt="Go lines"/>
<img src="https://img.shields.io/badge/go%20lines-32573-green" alt="Go lines"/>
</p>

<p align="center">
Expand Down
2 changes: 2 additions & 0 deletions dnsutils/dnsmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ type PowerDNS struct {
AppliedPolicyType string `json:"applied-policy-type"`
Metadata map[string]string `json:"metadata"`
HTTPVersion string `json:"http-version"`
MessageID string `json:"message-id"`
InitialRequestorID string `json:"initial-requestor-id"`
}

type TransformDNSGeo struct {
Expand Down
6 changes: 5 additions & 1 deletion dnsutils/dnsmessage_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func TestDnsMessage_Json_Collectors_Reference(t *testing.T) {
Tags: []string{"tag1"},
Metadata: map[string]string{"stream_id": "collector"},
HTTPVersion: "http3",
MessageID: "27c3e94ad6284eec9a50cfc5bd7384d6",
InitialRequestorID: "5e006236c8a74f7eafc6af126e6d0689",
}},

jsonRef: `{
Expand All @@ -125,7 +127,9 @@ func TestDnsMessage_Json_Collectors_Reference(t *testing.T) {
"metadata": {
"stream_id": "collector"
},
"http-version": "http3"
"http-version": "http3",
"message-id": "27c3e94ad6284eec9a50cfc5bd7384d6",
"initial-requestor-id": "5e006236c8a74f7eafc6af126e6d0689"
}
}`,
},
Expand Down
12 changes: 12 additions & 0 deletions dnsutils/dnsmessage_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ func (dm *DNSMessage) handlePdnsDirectives(directive string, s *strings.Builder)
} else {
s.WriteString("-")
}
case directive == "powerdns-message-id":
if len(dm.PowerDNS.MessageID) > 0 {
s.WriteString(dm.PowerDNS.MessageID)
} else {
s.WriteString("-")
}
case directive == "powerdns-initial-requestor-id":
if len(dm.PowerDNS.InitialRequestorID) > 0 {
s.WriteString(dm.PowerDNS.InitialRequestorID)
} else {
s.WriteString("-")
}
case directive == "powerdns-original-request-subnet":
if len(dm.PowerDNS.OriginalRequestSubnet) > 0 {
s.WriteString(dm.PowerDNS.OriginalRequestSubnet)
Expand Down
14 changes: 10 additions & 4 deletions dnsutils/dnsmessage_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,16 @@ func TestDnsMessage_TextFormat_Directives_Pdns(t *testing.T) {
expected: "-",
},
{
name: "http_version",
format: "powerdns-http-version",
dm: DNSMessage{PowerDNS: &PowerDNS{HTTPVersion: "HTTP2"}},
expected: "HTTP2",
name: "message_id",
format: "powerdns-message-id",
dm: DNSMessage{PowerDNS: &PowerDNS{MessageID: "27c3e94ad6284eec9a50cfc5bd7384d6"}},
expected: "27c3e94ad6284eec9a50cfc5bd7384d6",
},
{
name: "initial_requestor_id",
format: "powerdns-initial-requestor-id",
dm: DNSMessage{PowerDNS: &PowerDNS{InitialRequestorID: "5e006236c8a74f7eafc6af126e6d0689"}},
expected: "5e006236c8a74f7eafc6af126e6d0689",
},
}

Expand Down
39 changes: 38 additions & 1 deletion docs/collectors/collector_powerdns.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ If you logs your DNS traffic in basic text format, you can use the specific dire
* `powerdns-applied-policy-type`: get applied policy type
* `powerdns-metadata[:KEY]`: get all metadata separated by comma or specific one if a valid [KEY](https://dnsdist.org/rules-actions.html#RemoteLogAction) is provided
* `powerdns-http-version`: http version used with DoH queries
* `powerdns-message-id`: message id
* `powerdns-initial-requestor-id`: initial requestor id

Configuration example:

Expand Down Expand Up @@ -96,7 +98,9 @@ If you logs your DNS traffic in JSON output, the following part will be added in
"agent":"Go-http-client/1.1",
"selected_pool":"pool_internet"
},
"http-version": "HTTP3"
"http-version": "HTTP3",
"message-id": "27c3e94ad6284eec9a50cfc5bd7384d6",
"initial-requestor-id": "5e006236c8a74f7eafc6af126e6d0689"
}
```

Expand All @@ -118,6 +122,39 @@ addCacheHitResponseAction(AllRule(), RemoteLogResponseAction(rl, nil, true, {ser

Example to enable logging in your **pdns-recursor**

Since version > 5

```yaml
recursor:
include_dir: /etc/powerdns/recursor.d
incoming:
listen:
- 0.0.0.0:53
- '[::]:53'
allow_from:
- 0.0.0.0/0
- ::/0
logging:
protobuf_servers:
- servers: [192.168.1.16:6000]
logQueries: true
logResponses: true
outgoing_protobuf_servers:
- servers: [192.168.1.16:6000]
logQueries: true
logResponses: true
dnstap_framestream_servers:
- servers: [192.168.1.16:6000]
logQueries: true
logResponses: true
```

Old configuration style

*/etc/pdns-recursor/recursor.conf*

```lua
Expand Down
5 changes: 5 additions & 0 deletions workers/powerdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package workers

import (
"bufio"
"encoding/hex"
"errors"
"io"
"net"
Expand Down Expand Up @@ -357,6 +358,10 @@ func (w *PdnsProcessor) StartCollect() {
pdns.HTTPVersion = pbdm.GetHttpVersion().String()
}

// get id
pdns.MessageID = hex.EncodeToString(pbdm.MessageId)
pdns.InitialRequestorID = hex.EncodeToString(pbdm.InitialRequestId)

// finally set pdns to dns message
dm.PowerDNS = &pdns

Expand Down

0 comments on commit db16141

Please sign in to comment.