-
Notifications
You must be signed in to change notification settings - Fork 93
/
host_dns.go
209 lines (170 loc) · 5.26 KB
/
host_dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package collect
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/miekg/dns"
"github.com/pkg/errors"
"github.com/replicatedhq/troubleshoot/internal/util"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"k8s.io/klog/v2"
)
type CollectHostDNS struct {
hostCollector *troubleshootv1beta2.HostDNS
BundlePath string
}
type DNSResult struct {
Query DNSQuery `json:"query"`
ResolvedFromSearch string `json:"resolvedFromSearch"`
}
type DNSQuery map[string][]DNSEntry
type DNSEntry struct {
Server string `json:"server"`
Search string `json:"search"`
Name string `json:"name"`
Answer string `json:"answer"`
Record string `json:"record"`
}
const (
HostDNSPath = "host-collectors/dns/"
resolvConf = "/etc/resolv.conf"
)
func (c *CollectHostDNS) Title() string {
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "dns")
}
func (c *CollectHostDNS) IsExcluded() (bool, error) {
return isExcluded(c.hostCollector.Exclude)
}
func (c *CollectHostDNS) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
names := c.hostCollector.Hostnames
if len(names) == 0 {
return nil, errors.New("hostnames is required")
}
// first, get DNS config from /etc/resolv.conf
dnsConfig, err := getDNSConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to read DNS resolve config")
}
// query DNS for each name
dnsEntries := make(map[string][]DNSEntry)
dnsResult := DNSResult{Query: dnsEntries}
allResolvedSearches := []string{}
for _, name := range names {
entries, resolvedSearches, err := resolveName(name, dnsConfig)
if err != nil {
klog.V(2).Infof("Failed to resolve name %s: %v", name, err)
}
dnsEntries[name] = entries
allResolvedSearches = append(allResolvedSearches, resolvedSearches...)
}
// deduplicate resolved searches
dnsResult.ResolvedFromSearch = strings.Join(util.Dedup(allResolvedSearches), ", ")
// convert dnsResult to a JSON string
dnsResultJSON, err := json.MarshalIndent(dnsResult, "", " ")
if err != nil {
return nil, errors.Wrap(err, "failed to marshal DNS query result to JSON")
}
output := NewResult()
outputFile := c.getOutputFilePath("result.json")
output.SaveResult(c.BundlePath, outputFile, bytes.NewBuffer(dnsResultJSON))
// write /etc/resolv.conf to a file
resolvConfData, err := getResolvConf()
if err != nil {
klog.V(2).Infof("failed to read DNS resolve config: %v", err)
} else {
outputFile = c.getOutputFilePath("resolv.conf")
output.SaveResult(c.BundlePath, outputFile, bytes.NewBuffer(resolvConfData))
}
return output, nil
}
func (c *CollectHostDNS) getOutputFilePath(name string) string {
// normalize title to be used as a directory name, replace spaces with underscores
title := strings.ReplaceAll(c.Title(), " ", "_")
return filepath.Join(HostDNSPath, title, name)
}
func getDNSConfig() (*dns.ClientConfig, error) {
file, err := os.Open(resolvConf)
if err != nil {
return nil, err
}
defer file.Close()
config, err := dns.ClientConfigFromFile(file.Name())
if err != nil {
return nil, fmt.Errorf("failed to parse resolv.conf: %v", err)
}
return config, nil
}
func resolveName(name string, config *dns.ClientConfig) ([]DNSEntry, []string, error) {
results := []DNSEntry{}
resolvedSearches := []string{}
// get a name list based on the config
queryList := config.NameList(name)
klog.V(2).Infof("DNS query list: %v", queryList)
// for each name in the list, query all the servers
// we will query all search domains for each name
for _, query := range queryList {
for _, server := range config.Servers {
klog.V(2).Infof("Querying DNS server %s for name %s", server, query)
entry := queryDNS(name, query, server+":"+config.Port)
results = append(results, entry)
if entry.Search != "" {
resolvedSearches = append(resolvedSearches, entry.Search)
}
}
}
return results, resolvedSearches, nil
}
func getResolvConf() ([]byte, error) {
data, err := os.ReadFile(resolvConf)
if err != nil {
return nil, err
}
return data, nil
}
func queryDNS(name, query, server string) DNSEntry {
recordTypes := []uint16{dns.TypeA, dns.TypeAAAA, dns.TypeCNAME}
entry := DNSEntry{Name: query, Server: server, Answer: ""}
for _, rec := range recordTypes {
m := &dns.Msg{}
m.SetQuestion(dns.Fqdn(query), rec)
in, err := dns.Exchange(m, server)
if err != nil {
klog.Errorf("failed to query DNS server %s for name %s: %v", server, query, err)
continue
}
if len(in.Answer) == 0 {
continue
}
entry.Answer = in.Answer[0].String()
// remember the search domain that resolved the query
// e.g. foo.test.com -> test.com
entry.Search = strings.Replace(query, name, "", 1)
// populate record detail
switch rec {
case dns.TypeA:
record, ok := in.Answer[0].(*dns.A)
if ok {
entry.Record = record.A.String()
}
case dns.TypeAAAA:
record, ok := in.Answer[0].(*dns.AAAA)
if ok {
entry.Record = record.AAAA.String()
}
case dns.TypeCNAME:
record, ok := in.Answer[0].(*dns.CNAME)
if ok {
entry.Record = record.Target
}
}
// break on the first successful query
break
}
return entry
}
func (c *CollectHostDNS) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}