Skip to content

Commit 749354b

Browse files
Daniel McCarneyjsha
authored andcommitted
dns: add support for mocking SERVFAIL responses. (#10)
1 parent 285efd6 commit 749354b

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

challenge-servers.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ type mockDNSData struct {
7676
caaRecords map[string][]MockCAAPolicy
7777
// A map of host to CNAME records.
7878
cnameRecords map[string]string
79+
// A map of hostnames that should receive a SERVFAIL response for all queries.
80+
servFailRecords map[string]bool
7981
}
8082

8183
// MockCAAPolicy holds a tag and a value for a CAA record. See
@@ -133,12 +135,13 @@ func New(config Config) (*ChallSrv, error) {
133135
tlsALPNOne: make(map[string]string),
134136
redirects: make(map[string]string),
135137
dnsMocks: mockDNSData{
136-
defaultIPv4: defaultIPv4,
137-
defaultIPv6: defaultIPv6,
138-
aRecords: make(map[string][]string),
139-
aaaaRecords: make(map[string][]string),
140-
caaRecords: make(map[string][]MockCAAPolicy),
141-
cnameRecords: make(map[string]string),
138+
defaultIPv4: defaultIPv4,
139+
defaultIPv6: defaultIPv6,
140+
aRecords: make(map[string][]string),
141+
aaaaRecords: make(map[string][]string),
142+
caaRecords: make(map[string][]MockCAAPolicy),
143+
cnameRecords: make(map[string]string),
144+
servFailRecords: make(map[string]bool),
142145
},
143146
}
144147

dns.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ func (s *ChallSrv) dnsHandler(w dns.ResponseWriter, r *dns.Msg) {
170170
Question: q,
171171
})
172172

173+
// If there is a ServFail mock set then ignore the question and set the
174+
// SERVFAIL rcode and continue.
175+
if s.GetDNSServFailRecord(q.Name) {
176+
m.SetRcode(r, dns.RcodeServerFailure)
177+
continue
178+
}
179+
173180
// If a CNAME exists for the question include the CNAME record and modify
174181
// the question to instead lookup based on that CNAME's target
175182
if cname := s.GetDNSCNAMERecord(q.Name); cname != "" {

mockdns.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,30 @@ func (s *ChallSrv) GetDNSCAARecord(host string) []MockCAAPolicy {
145145
host = dns.Fqdn(host)
146146
return s.dnsMocks.caaRecords[host]
147147
}
148+
149+
// AddDNSServFailRecord configures the chall srv to return SERVFAIL responses
150+
// for all queries for the given host.
151+
func (s *ChallSrv) AddDNSServFailRecord(host string) {
152+
s.challMu.Lock()
153+
defer s.challMu.Unlock()
154+
host = dns.Fqdn(host)
155+
s.dnsMocks.servFailRecords[host] = true
156+
}
157+
158+
// DeleteDNSServFailRecord configures the chall srv to no longer return SERVFAIL
159+
// responses for all queries for the given host.
160+
func (s *ChallSrv) DeleteDNSServFailRecord(host string) {
161+
s.challMu.Lock()
162+
defer s.challMu.Unlock()
163+
host = dns.Fqdn(host)
164+
delete(s.dnsMocks.servFailRecords, host)
165+
}
166+
167+
// GetDNSServFailRecord returns true when the chall srv has been configured with
168+
// AddDNSServFailRecord to return SERVFAIL for all queries to the given host.
169+
func (s *ChallSrv) GetDNSServFailRecord(host string) bool {
170+
s.challMu.RLock()
171+
defer s.challMu.RUnlock()
172+
host = dns.Fqdn(host)
173+
return s.dnsMocks.servFailRecords[host]
174+
}

0 commit comments

Comments
 (0)