Skip to content

Commit a28bad3

Browse files
authored
Merge branch 'main' into mitm-proxy
2 parents 2a7354a + d60f9a6 commit a28bad3

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pkg/collect/host_dns.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func queryDNS(name, query, server string) DNSEntry {
177177

178178
// remember the search domain that resolved the query
179179
// e.g. foo.test.com -> test.com
180-
entry.Search = strings.Replace(query, name, "", 1)
180+
entry.Search = extractSearchFromFQDN(query, name)
181181

182182
// populate record detail
183183
switch rec {
@@ -207,3 +207,13 @@ func queryDNS(name, query, server string) DNSEntry {
207207
func (c *CollectHostDNS) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
208208
return nil, ErrRemoteCollectorNotImplemented
209209
}
210+
211+
func extractSearchFromFQDN(fqdn, name string) string {
212+
// no search domain
213+
if fqdn == name {
214+
return ""
215+
}
216+
search := strings.TrimPrefix(fqdn, name+".") // remove name
217+
search = strings.TrimSuffix(search, ".") // remove root dot
218+
return search
219+
}

pkg/collect/host_dns_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package collect
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestExtractSearchFromFQDN(t *testing.T) {
8+
tests := []struct {
9+
fqdn string
10+
name string
11+
expected string
12+
}{
13+
{"foo.com.", "foo.com", ""},
14+
{"bar.com", "bar.com", ""},
15+
{"*.foo.testcluster.net.", "*", "foo.testcluster.net"},
16+
}
17+
18+
for _, test := range tests {
19+
t.Run(test.fqdn, func(t *testing.T) {
20+
result := extractSearchFromFQDN(test.fqdn, test.name)
21+
if result != test.expected {
22+
t.Errorf("extractSearchFromFQDN(%q, %q) = %q; want %q", test.fqdn, test.name, result, test.expected)
23+
}
24+
})
25+
}
26+
}

0 commit comments

Comments
 (0)