Skip to content

Commit

Permalink
change resolvedFromSearch content (#1629)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanthao authored Sep 30, 2024
1 parent 668b7ed commit d60f9a6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/collect/host_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func queryDNS(name, query, server string) DNSEntry {

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

// populate record detail
switch rec {
Expand Down Expand Up @@ -207,3 +207,13 @@ func queryDNS(name, query, server string) DNSEntry {
func (c *CollectHostDNS) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}

func extractSearchFromFQDN(fqdn, name string) string {
// no search domain
if fqdn == name {
return ""
}
search := strings.TrimPrefix(fqdn, name+".") // remove name
search = strings.TrimSuffix(search, ".") // remove root dot
return search
}
26 changes: 26 additions & 0 deletions pkg/collect/host_dns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package collect

import (
"testing"
)

func TestExtractSearchFromFQDN(t *testing.T) {
tests := []struct {
fqdn string
name string
expected string
}{
{"foo.com.", "foo.com", ""},
{"bar.com", "bar.com", ""},
{"*.foo.testcluster.net.", "*", "foo.testcluster.net"},
}

for _, test := range tests {
t.Run(test.fqdn, func(t *testing.T) {
result := extractSearchFromFQDN(test.fqdn, test.name)
if result != test.expected {
t.Errorf("extractSearchFromFQDN(%q, %q) = %q; want %q", test.fqdn, test.name, result, test.expected)
}
})
}
}

0 comments on commit d60f9a6

Please sign in to comment.