Compare commits
4
Commits
c0647eef26
...
e9cac1a471
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9cac1a471 | ||
|
|
008ec5d13a | ||
|
|
e46db71821 | ||
|
|
1218df9467 |
@@ -80,7 +80,14 @@ notification endpoint set, changes show only on the dashboard; see
|
||||
different addresses than on the previous check. A nameserver added or
|
||||
removed gets only the NS change notification. When the lookup of a
|
||||
nameserver's addresses fails or finds none, its previous addresses are
|
||||
kept and nothing is sent.
|
||||
kept and nothing is sent. The lookup fails when no nameserver it asks
|
||||
answers every one of its queries, for A, AAAA and CNAME.
|
||||
- Also watches the domain's own records as a hostname's are watched (see DNS
|
||||
Hostname Monitoring below): its A, AAAA, CNAME, MX, TXT, SRV, CAA and NS
|
||||
records, stored per nameserver. Their changes are notified as a hostname's
|
||||
are, as a record change, NS query failure, NS recovery, inconsistency or CNAME
|
||||
address change, in a message that starts `Domain:` where a hostname's starts
|
||||
`Hostname:`.
|
||||
|
||||
### DNS Hostname Monitoring (Subdomains)
|
||||
|
||||
@@ -91,6 +98,19 @@ notification endpoint set, changes show only on the dashboard; see
|
||||
its last two labels (a name under `co.uk`, or in a delegated subdomain).
|
||||
- Queries **each** authoritative nameserver independently for **all** record
|
||||
types: A, AAAA, CNAME, MX, TXT, SRV, CAA, NS.
|
||||
- Each record type is a query of its own. When a nameserver answers some types
|
||||
but the query for another gets no usable reply (no reply after two tries, an
|
||||
error reply such as SERVFAIL, a referral, or a reply too large for UDP whose
|
||||
retry over TCP fails), the failure is logged with the reason, and the type is
|
||||
listed in the nameserver's `failedTypes` and keeps the records saved for the
|
||||
nameserver by the previous check. On that check those records are not compared
|
||||
with the other nameservers', so no record change or inconsistency is reported
|
||||
for the type; on the next check they are compared with the nameserver's answer
|
||||
as usual. When the previous check did not know the type's records either,
|
||||
because the nameserver was new or failing then or the type was already listed
|
||||
in `unknownTypes`, the type is also listed in `unknownTypes` and left out of
|
||||
every comparison until it answers. A nameserver none of whose queries got a
|
||||
usable reply has failed (see NS query failure below).
|
||||
- Stores results **per nameserver**. The state for a hostname is not a merged
|
||||
view — it is a map from nameserver to record set.
|
||||
- DNS names inside record values (CNAME, MX, SRV and NS targets) are stored in
|
||||
@@ -119,8 +139,9 @@ notification endpoint set, changes show only on the dashboard; see
|
||||
they keep disagreeing, including after a restart. A nameserver that was
|
||||
not in the previous check (newly added, or back after dropping out), or
|
||||
failed on it, and answers differently is reported on the check where it
|
||||
answers. If a pair agrees again and later disagrees, the alert is sent
|
||||
again.
|
||||
answers. So is a pair that differs in a record type whose query to either
|
||||
nameserver failed on the previous check. If a pair agrees again and later
|
||||
disagrees, the alert is sent again.
|
||||
- **CNAME address change**: The addresses at the end of a name's CNAME chain
|
||||
differ from those of the previous check. They are found when its
|
||||
nameservers answer with a CNAME and no address; a name that answers with
|
||||
@@ -183,18 +204,18 @@ Supported notification backends:
|
||||
All configured endpoints receive every notification. Notification content
|
||||
includes:
|
||||
|
||||
- **DNS record changes**: Which hostname, which nameserver, what record type,
|
||||
old values, new values.
|
||||
- **DNS record changes**: Which hostname or domain, which nameserver, what
|
||||
record type, old values, new values.
|
||||
- **DNS NS changes**: Which domain, which nameservers were added/removed.
|
||||
- **NS address changes**: Which domain, which nameserver, its old and new
|
||||
addresses.
|
||||
- **CNAME address changes**: Which hostname, the old and new addresses at the
|
||||
end of its CNAME chain.
|
||||
- **CNAME address changes**: Which hostname or domain, the old and new addresses
|
||||
at the end of its CNAME chain.
|
||||
- **NS query failures**: Which nameserver failed, error type (timeout, SERVFAIL,
|
||||
REFUSED, network error), which hostname/domain affected.
|
||||
- **NS recoveries**: Which nameserver recovered, which hostname/domain.
|
||||
- **NS inconsistencies**: Which nameservers disagree, what each one returned,
|
||||
which hostname affected.
|
||||
which hostname or domain affected.
|
||||
- **Port changes**: Which IP:port, its new state, all associated hostnames.
|
||||
- **TLS expiry warnings**: Expiry date and days remaining, CN, associated
|
||||
hostname and IP.
|
||||
@@ -229,7 +250,8 @@ dnswatcher includes an unauthenticated, read-only web dashboard at the root URL
|
||||
(`/`). It displays:
|
||||
|
||||
- **Summary counts** for monitored domains, hostnames, ports, and certificates.
|
||||
- **Domains** with their discovered nameservers.
|
||||
- **Domains** with their discovered nameservers, and each domain's own records
|
||||
per nameserver and status, shown as a hostname's are.
|
||||
- **Hostnames** with per-nameserver DNS records and status. For a nameserver
|
||||
whose query failed, the reason is shown in place of the records.
|
||||
- **Ports** with open/closed state and associated hostnames.
|
||||
@@ -263,7 +285,9 @@ dnswatcher exposes a lightweight HTTP API for operational visibility:
|
||||
|
||||
In `/api/v1/status`, each nameserver entry and certificate entry whose `status`
|
||||
is `error` also has `error`, the reason, as in the state file (see State File
|
||||
Format).
|
||||
Format). A domain's own records are in its entry in `domains`, under
|
||||
`recordsByNameserver`, in the form a hostname's entry in `hostnames` has them
|
||||
under `nameservers`; `hostnames` and `counts.hostnames` hold no domain.
|
||||
|
||||
`/metrics` is served only when `DNSWATCHER_METRICS_USERNAME` is set, behind
|
||||
Basic Auth. It has the Prometheus Go client's default metrics only (Go runtime,
|
||||
@@ -432,7 +456,12 @@ anew each time, so no one root server gets every first query. A server that does
|
||||
not reply, refuses the query, or gives an error reply such as SERVFAIL or a
|
||||
referral that leads no closer to the name is passed over for the next one. When
|
||||
a referral names a zone's nameservers without their addresses, the addresses of
|
||||
all of them are looked up, so that each can be asked.
|
||||
all of them are looked up, so that each can be asked. When it gives addresses
|
||||
for only some of them, those are asked first, and the others are looked up and
|
||||
asked only if none of those gives a usable reply. Both hold in the walk to a
|
||||
name's nameservers and in the lookup of a nameserver's own address. Such a
|
||||
lookup can need others in turn; lookups go at most three deep, one inside
|
||||
another, so delegations that point at each other still end.
|
||||
|
||||
This approach ensures:
|
||||
|
||||
@@ -455,7 +484,8 @@ resolver: the HTTP client looks up the webhook's or Sentry's host name with it.
|
||||
|
||||
The state file (`DATA_DIR/state.json`) contains the complete monitoring
|
||||
snapshot. Hostname records are stored **per authoritative nameserver**, not as a
|
||||
merged view, to enable inconsistency detection.
|
||||
merged view, to enable inconsistency detection. `hostnames` also holds each
|
||||
domain's own records, under the domain's name.
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -525,7 +555,7 @@ reachability:
|
||||
|
||||
| Status | Meaning |
|
||||
| ------- | -------------------------------------------------------- |
|
||||
| `ok` | Query succeeded, records are current |
|
||||
| `ok` | Query succeeded, records are current except as below |
|
||||
| `error` | Query failed (timeout, SERVFAIL, REFUSED, network error) |
|
||||
|
||||
A nameserver that answers NXDOMAIN or with no records has status `ok` and empty
|
||||
@@ -534,6 +564,13 @@ nameservers, has status `error`, empty `records`, and the reason in `error`. A
|
||||
certificate entry whose TLS connection or handshake failed likewise has status
|
||||
`error`, the reason in `error`, and the certificate fields left empty or zero.
|
||||
|
||||
A nameserver with status `ok` whose query for one record type failed lists that
|
||||
type in `failedTypes` and holds its records from the previous check, which may
|
||||
not be current. When the previous check did not know the type's records either,
|
||||
because the nameserver was new or failing then or the type was already listed in
|
||||
`unknownTypes`, the type is also listed in `unknownTypes`, and `records` holds
|
||||
nothing for it. Both lists are left out when empty.
|
||||
|
||||
`nameserverAddresses` lists, by nameserver, the sorted addresses its name
|
||||
resolves to. A state file without it loads, and the next check fills it in
|
||||
without a notification.
|
||||
@@ -541,10 +578,10 @@ without a notification.
|
||||
`cnameAddresses` lists the sorted addresses at the end of the chain of every
|
||||
CNAME target a hostname's nameservers gave, found when they answered with a
|
||||
CNAME and no address; it is empty when they answered with an address. When a
|
||||
chain cannot be followed, or none of the name's nameservers answered, the
|
||||
previous check's list is kept, or `null` when no earlier check saved one. A
|
||||
state file without it loads, and the first check after that saves it without a
|
||||
notification.
|
||||
chain cannot be followed, or none of the name's nameservers answered its queries
|
||||
for A, AAAA and CNAME, the previous check's list is kept, or `null` when no
|
||||
earlier check saved one. A state file without it loads, and the first check
|
||||
after that saves it without a notification.
|
||||
|
||||
A port entry in the older format, with one `hostname` instead of the `hostnames`
|
||||
list, loads as a list of that one name.
|
||||
|
||||
@@ -19,6 +19,14 @@ trial run of the finished image: https://git.eeqj.de/sneak/dnswatcher/issues/149
|
||||
|
||||
# Completed Steps
|
||||
|
||||
- 2026-10-02: a record type whose query to a nameserver fails keeps its previous
|
||||
records and alerts nothing; the other types are still saved (closes #231).
|
||||
- 2026-10-02: nameservers a referral names without addresses are looked up,
|
||||
three deep at most; `pool.ntp.org`'s nameservers resolve (closes #221).
|
||||
- 2026-10-02: an apex domain is not counted or listed as a hostname; its records
|
||||
show under Domains, and notifications about them say `Domain:` (closes #224).
|
||||
- 2026-10-02: the dashboard lists each nameserver's record types in one fixed
|
||||
order, the README's, then any other type, not a random one (closes #226).
|
||||
- 2026-10-02: the dashboard and `/api/v1/status` show why a nameserver query or
|
||||
a certificate check failed, which only the state file showed (closes #225).
|
||||
- 2026-10-02: a name's CNAME is stored once per nameserver, not once per record
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"maps"
|
||||
"math"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -40,9 +43,13 @@ func newDashboardTemplate() *template.Template {
|
||||
)
|
||||
}
|
||||
|
||||
// dashboardData is the data passed to the dashboard template.
|
||||
// dashboardData is the data passed to the dashboard template. Hostnames
|
||||
// and DomainRecords split the records in Snapshot.Hostnames, which also
|
||||
// holds the apex domains' own (see splitHostnames).
|
||||
type dashboardData struct {
|
||||
Snapshot state.Snapshot
|
||||
Hostnames map[string]*state.HostnameState
|
||||
DomainRecords map[string]*state.HostnameState
|
||||
Alerts []notify.AlertEntry
|
||||
StateAge string
|
||||
GeneratedAt string
|
||||
@@ -58,9 +65,12 @@ func (h *Handlers) HandleDashboard() http.HandlerFunc {
|
||||
) {
|
||||
snap := h.state.GetSnapshot()
|
||||
alerts := h.notifyHistory.Recent()
|
||||
hostnames, domainRecords := splitHostnames(snap)
|
||||
|
||||
data := dashboardData{
|
||||
Snapshot: snap,
|
||||
Hostnames: hostnames,
|
||||
DomainRecords: domainRecords,
|
||||
Alerts: alerts,
|
||||
StateAge: relTime(snap.LastUpdated),
|
||||
GeneratedAt: time.Now().UTC().Format("2006-01-02 15:04:05"),
|
||||
@@ -122,16 +132,37 @@ func joinStrings(items []string, sep string) string {
|
||||
}
|
||||
|
||||
// formatRecords formats a map of record type → values into a
|
||||
// compact display string.
|
||||
// compact display string. Record types are listed in the order the
|
||||
// README lists them, any other type after them in alphabetical order,
|
||||
// so rows of nameservers with the same records read the same.
|
||||
func formatRecords(records map[string][]string) string {
|
||||
if len(records) == 0 {
|
||||
return "-"
|
||||
}
|
||||
|
||||
order := []string{"A", "AAAA", "CNAME", "MX", "TXT", "SRV", "CAA", "NS"}
|
||||
|
||||
position := func(rtype string) int {
|
||||
i := slices.Index(order, rtype)
|
||||
if i < 0 {
|
||||
return len(order)
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
rtypes := slices.Collect(maps.Keys(records))
|
||||
slices.SortFunc(rtypes, func(a, b string) int {
|
||||
return cmp.Or(
|
||||
cmp.Compare(position(a), position(b)),
|
||||
strings.Compare(a, b),
|
||||
)
|
||||
})
|
||||
|
||||
var parts []string
|
||||
|
||||
for rtype, values := range records {
|
||||
for _, v := range values {
|
||||
for _, rtype := range rtypes {
|
||||
for _, v := range records[rtype] {
|
||||
parts = append(parts, rtype+": "+v)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -80,6 +81,45 @@ func TestFormatRecords(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestFormatRecordsTypeOrder checks that record types are listed in
|
||||
// the README's order (A, AAAA, CNAME, MX, TXT, SRV, CAA, NS), with
|
||||
// any other type after them in alphabetical order.
|
||||
func TestFormatRecordsTypeOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := handlers.FormatRecords(map[string][]string{
|
||||
"SOA": {"ns1.example.com. hostmaster.example.com. 1 2 3 4 5"},
|
||||
"NS": {"ns1.example.com.", "ns2.example.com."},
|
||||
"CAA": {`0 issue "letsencrypt.org"`},
|
||||
"DNAME": {"example.net."},
|
||||
"TXT": {"v=spf1 -all"},
|
||||
"SRV": {"10 5 443 www.example.com."},
|
||||
"MX": {"10 mail.example.com."},
|
||||
"CNAME": {"www.example.com."},
|
||||
"AAAA": {"2001:db8::1"},
|
||||
"A": {"192.0.2.1"},
|
||||
})
|
||||
|
||||
want := strings.Join([]string{
|
||||
"A: 192.0.2.1",
|
||||
"AAAA: 2001:db8::1",
|
||||
"CNAME: www.example.com.",
|
||||
"MX: 10 mail.example.com.",
|
||||
"TXT: v=spf1 -all",
|
||||
"SRV: 10 5 443 www.example.com.",
|
||||
`CAA: 0 issue "letsencrypt.org"`,
|
||||
"NS: ns1.example.com.",
|
||||
"NS: ns2.example.com.",
|
||||
"DNAME: example.net.",
|
||||
"SOA: ns1.example.com. hostmaster.example.com. 1 2 3 4 5",
|
||||
}, ", ")
|
||||
|
||||
if got != want {
|
||||
t.Errorf("FormatRecords lists types out of order:\n got %q\nwant %q",
|
||||
got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// dashboardRow returns the table row of page that contains name.
|
||||
func dashboardRow(t *testing.T, page string, name string) string {
|
||||
t.Helper()
|
||||
@@ -115,3 +155,53 @@ func TestDashboardShowsFailureReasons(t *testing.T) {
|
||||
t.Errorf("row of %s does not show %q", certKey, certFailedReason)
|
||||
}
|
||||
}
|
||||
|
||||
// dashboardSection returns the section of page under heading.
|
||||
func dashboardSection(t *testing.T, page string, heading string) string {
|
||||
t.Helper()
|
||||
|
||||
for section := range strings.SplitSeq(page, "<section") {
|
||||
words := strings.Join(strings.Fields(section), " ")
|
||||
if strings.Contains(words, "> "+heading+" </h2>") {
|
||||
return section
|
||||
}
|
||||
}
|
||||
|
||||
t.Fatalf("dashboard has no section headed %q", heading)
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// TestDashboardShowsDomainRecordsUnderDomains checks that the dashboard
|
||||
// shows an apex domain's own records in the Domains section, and
|
||||
// neither lists nor counts the domain as a hostname.
|
||||
func TestDashboardShowsDomainRecordsUnderDomains(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
page := get(t, newHandlersWithFailures(t).HandleDashboard())
|
||||
|
||||
domains := dashboardSection(t, page, "Domains")
|
||||
if !strings.Contains(dashboardRow(t, domains, domainAddress), testDomain) {
|
||||
t.Errorf("row of %s does not name %s", domainAddress, testDomain)
|
||||
}
|
||||
|
||||
if strings.Contains(dashboardSection(t, page, "Hostnames"), testDomain) {
|
||||
t.Errorf("Hostnames section lists the domain %s", testDomain)
|
||||
}
|
||||
|
||||
words := strings.Join(strings.Fields(page), " ")
|
||||
|
||||
footer := "monitoring 1 domains + 1 hostnames"
|
||||
if !strings.Contains(words, footer) {
|
||||
t.Errorf("dashboard does not say %q", footer)
|
||||
}
|
||||
|
||||
// With the tags taken out, the summary bar starts "Domains 1
|
||||
// Hostnames 1".
|
||||
text := regexp.MustCompile(`<[^>]*>`).ReplaceAllString(page, " ")
|
||||
summary := "Domains 1 Hostnames 1"
|
||||
|
||||
if !strings.Contains(strings.Join(strings.Fields(text), " "), summary) {
|
||||
t.Errorf("summary bar does not say %q", summary)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,11 @@ import (
|
||||
)
|
||||
|
||||
// statusDomainInfo holds status information for a monitored domain.
|
||||
// RecordsByNameserver holds the domain's own records, in the form a
|
||||
// hostname's Nameservers holds the hostname's.
|
||||
type statusDomainInfo struct {
|
||||
Nameservers []string `json:"nameservers"`
|
||||
RecordsByNameserver map[string]*statusHostnameNSInfo `json:"recordsByNameserver"`
|
||||
LastChecked time.Time `json:"lastChecked"`
|
||||
}
|
||||
|
||||
@@ -100,8 +103,10 @@ func buildStatusResponse(
|
||||
Certificates: make(map[string]*statusCertificateInfo),
|
||||
}
|
||||
|
||||
buildDomains(snap, resp)
|
||||
buildHostnames(snap, resp)
|
||||
hostnames, domainRecords := splitHostnames(snap)
|
||||
|
||||
buildDomains(snap, domainRecords, resp)
|
||||
buildHostnames(hostnames, resp)
|
||||
buildPorts(snap, resp)
|
||||
buildCertificates(snap, resp)
|
||||
buildCounts(resp)
|
||||
@@ -109,8 +114,30 @@ func buildStatusResponse(
|
||||
return resp
|
||||
}
|
||||
|
||||
// splitHostnames returns the records saved in snap.Hostnames in two
|
||||
// maps: the hostnames' and the apex domains' own. The watcher saves a
|
||||
// domain's own records there under the domain's name, which has an
|
||||
// entry in snap.Domains too.
|
||||
func splitHostnames(
|
||||
snap state.Snapshot,
|
||||
) (map[string]*state.HostnameState, map[string]*state.HostnameState) {
|
||||
hostnames := make(map[string]*state.HostnameState)
|
||||
domainRecords := make(map[string]*state.HostnameState)
|
||||
|
||||
for name, hs := range snap.Hostnames {
|
||||
if _, isDomain := snap.Domains[name]; isDomain {
|
||||
domainRecords[name] = hs
|
||||
} else {
|
||||
hostnames[name] = hs
|
||||
}
|
||||
}
|
||||
|
||||
return hostnames, domainRecords
|
||||
}
|
||||
|
||||
func buildDomains(
|
||||
snap state.Snapshot,
|
||||
domainRecords map[string]*state.HostnameState,
|
||||
resp *statusResponse,
|
||||
) {
|
||||
for name, ds := range snap.Domains {
|
||||
@@ -118,22 +145,36 @@ func buildDomains(
|
||||
copy(ns, ds.Nameservers)
|
||||
sort.Strings(ns)
|
||||
|
||||
records := make(map[string]*statusHostnameNSInfo)
|
||||
if hs, ok := domainRecords[name]; ok {
|
||||
records = nameserverInfo(hs)
|
||||
}
|
||||
|
||||
resp.Domains[name] = &statusDomainInfo{
|
||||
Nameservers: ns,
|
||||
RecordsByNameserver: records,
|
||||
LastChecked: ds.LastChecked,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func buildHostnames(
|
||||
snap state.Snapshot,
|
||||
hostnames map[string]*state.HostnameState,
|
||||
resp *statusResponse,
|
||||
) {
|
||||
for name, hs := range snap.Hostnames {
|
||||
info := &statusHostnameInfo{
|
||||
Nameservers: make(map[string]*statusHostnameNSInfo),
|
||||
for name, hs := range hostnames {
|
||||
resp.Hostnames[name] = &statusHostnameInfo{
|
||||
Nameservers: nameserverInfo(hs),
|
||||
LastChecked: hs.LastChecked,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// nameserverInfo copies each nameserver's answer saved in hs.
|
||||
func nameserverInfo(
|
||||
hs *state.HostnameState,
|
||||
) map[string]*statusHostnameNSInfo {
|
||||
info := make(map[string]*statusHostnameNSInfo)
|
||||
|
||||
for ns, nsState := range hs.RecordsByNameserver {
|
||||
recs := make(map[string][]string, len(nsState.Records))
|
||||
@@ -143,7 +184,7 @@ func buildHostnames(
|
||||
recs[rtype] = copied
|
||||
}
|
||||
|
||||
info.Nameservers[ns] = &statusHostnameNSInfo{
|
||||
info[ns] = &statusHostnameNSInfo{
|
||||
Records: recs,
|
||||
Status: nsState.Status,
|
||||
Error: nsState.Error,
|
||||
@@ -151,8 +192,7 @@ func buildHostnames(
|
||||
}
|
||||
}
|
||||
|
||||
resp.Hostnames[name] = info
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
func buildPorts(
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -19,7 +20,8 @@ import (
|
||||
|
||||
// The state the handler tests serve: www.example.com has one nameserver
|
||||
// that answered and one whose query failed, and its certificate check
|
||||
// failed.
|
||||
// failed. example.net is an apex domain, whose own records are saved
|
||||
// with the hostnames' records, as the watcher saves them.
|
||||
const (
|
||||
testHostname = "www.example.com"
|
||||
answeringNS = "ns1.example.com."
|
||||
@@ -27,6 +29,9 @@ const (
|
||||
nsFailureReason = "server returned a referral"
|
||||
certKey = "192.0.2.1:443:www.example.com"
|
||||
certFailedReason = "x509: certificate has expired or is not yet valid"
|
||||
testDomain = "example.net"
|
||||
domainNS = "a.iana-servers.net."
|
||||
domainAddress = "192.0.2.2"
|
||||
)
|
||||
|
||||
// newHandlersWithFailures builds real Handlers whose state holds the
|
||||
@@ -85,6 +90,22 @@ func newHandlersWithFailures(t *testing.T) *handlers.Handlers {
|
||||
LastChecked: now,
|
||||
})
|
||||
|
||||
st.SetDomainState(testDomain, &state.DomainState{
|
||||
Nameservers: []string{domainNS},
|
||||
LastChecked: now,
|
||||
})
|
||||
|
||||
st.SetHostnameState(testDomain, &state.HostnameState{
|
||||
RecordsByNameserver: map[string]*state.NameserverRecordState{
|
||||
domainNS: {
|
||||
Records: map[string][]string{"A": {domainAddress}},
|
||||
Status: "ok",
|
||||
LastChecked: now,
|
||||
},
|
||||
},
|
||||
LastChecked: now,
|
||||
})
|
||||
|
||||
hnd, err := handlers.New(nil, handlers.Params{
|
||||
Logger: log,
|
||||
Globals: glob,
|
||||
@@ -156,3 +177,43 @@ func TestStatusGivesFailureReasons(t *testing.T) {
|
||||
got, certFailedReason)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStatusGivesDomainRecordsUnderTheDomain checks that /api/v1/status
|
||||
// gives an apex domain's own records in its domain entry, and neither
|
||||
// lists nor counts the domain as a hostname.
|
||||
func TestStatusGivesDomainRecordsUnderTheDomain(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
body := get(t, newHandlersWithFailures(t).HandleStatus())
|
||||
|
||||
var resp struct {
|
||||
Counts struct {
|
||||
Hostnames int `json:"hostnames"`
|
||||
} `json:"counts"`
|
||||
Domains map[string]struct {
|
||||
RecordsByNameserver map[string]struct {
|
||||
Records map[string][]string `json:"records"`
|
||||
} `json:"recordsByNameserver"`
|
||||
} `json:"domains"`
|
||||
Hostnames map[string]any `json:"hostnames"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal([]byte(body), &resp)
|
||||
if err != nil {
|
||||
t.Fatalf("decoding response: %v", err)
|
||||
}
|
||||
|
||||
if resp.Counts.Hostnames != 1 {
|
||||
t.Errorf("counts.hostnames = %d, want 1", resp.Counts.Hostnames)
|
||||
}
|
||||
|
||||
if _, listed := resp.Hostnames[testDomain]; listed {
|
||||
t.Errorf("hostnames lists the domain %s", testDomain)
|
||||
}
|
||||
|
||||
records := resp.Domains[testDomain].RecordsByNameserver[domainNS].Records
|
||||
if !slices.Equal(records["A"], []string{domainAddress}) {
|
||||
t.Errorf("domain %s records at %s = %v, want A %s",
|
||||
testDomain, domainNS, records, domainAddress)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
Hostnames
|
||||
</div>
|
||||
<div class="text-2xl font-bold text-teal-400 mt-1">
|
||||
{{ len .Snapshot.Hostnames }}
|
||||
{{ len .Hostnames }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-surface-800 border border-slate-700/50 rounded-lg p-4">
|
||||
@@ -94,6 +94,24 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{{ if .DomainRecords }}
|
||||
<div class="overflow-x-auto mt-4">
|
||||
<table class="w-full text-left text-xs">
|
||||
<thead>
|
||||
<tr class="text-slate-500 uppercase tracking-wider">
|
||||
<th class="py-2 px-3">Domain</th>
|
||||
<th class="py-2 px-3">NS</th>
|
||||
<th class="py-2 px-3">Status</th>
|
||||
<th class="py-2 px-3">Records</th>
|
||||
<th class="py-2 px-3">Checked</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-slate-800">
|
||||
{{ template "records" .DomainRecords }}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
<p class="text-slate-600 italic text-xs">
|
||||
No domains configured.
|
||||
@@ -108,7 +126,7 @@
|
||||
>
|
||||
Hostnames
|
||||
</h2>
|
||||
{{ if .Snapshot.Hostnames }}
|
||||
{{ if .Hostnames }}
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full text-left text-xs">
|
||||
<thead>
|
||||
@@ -121,43 +139,7 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-slate-800">
|
||||
{{ range $name, $hs := .Snapshot.Hostnames }}
|
||||
{{ range $ns, $nsr := $hs.RecordsByNameserver }}
|
||||
<tr class="hover:bg-surface-800/50">
|
||||
<td class="py-2 px-3 text-slate-200 font-medium">
|
||||
{{ $name }}
|
||||
</td>
|
||||
<td class="py-2 px-3 text-slate-400 break-all">
|
||||
{{ $ns }}
|
||||
</td>
|
||||
<td class="py-2 px-3">
|
||||
{{ if eq $nsr.Status "ok" }}
|
||||
<span
|
||||
class="inline-block px-1.5 py-0.5 rounded text-[10px] font-bold uppercase bg-teal-900/50 text-teal-400 border border-teal-700/30"
|
||||
>ok</span
|
||||
>
|
||||
{{ else }}
|
||||
<span
|
||||
class="inline-block px-1.5 py-0.5 rounded text-[10px] font-bold uppercase bg-red-900/50 text-red-400 border border-red-700/30"
|
||||
>{{ $nsr.Status }}</span
|
||||
>
|
||||
{{ end }}
|
||||
</td>
|
||||
<td
|
||||
class="py-2 px-3 text-slate-400 break-all max-w-xs"
|
||||
>
|
||||
{{ if $nsr.Error }}
|
||||
<span class="text-red-400">{{ $nsr.Error }}</span>
|
||||
{{ else }}
|
||||
{{ formatRecords $nsr.Records }}
|
||||
{{ end }}
|
||||
</td>
|
||||
<td class="py-2 px-3 text-slate-500 whitespace-nowrap">
|
||||
{{ relTime $nsr.LastChecked }}
|
||||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ template "records" .Hostnames }}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@@ -373,8 +355,48 @@
|
||||
class="text-[11px] text-slate-700 border-t border-slate-800 pt-4 mt-8"
|
||||
>
|
||||
dnswatcher · monitoring {{ len .Snapshot.Domains }} domains +
|
||||
{{ len .Snapshot.Hostnames }} hostnames
|
||||
{{ len .Hostnames }} hostnames
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
{{/* ---- One row per nameserver of each name in the map it is given ---- */}}
|
||||
{{ define "records" }}
|
||||
{{ range $name, $hs := . }}
|
||||
{{ range $ns, $nsr := $hs.RecordsByNameserver }}
|
||||
<tr class="hover:bg-surface-800/50">
|
||||
<td class="py-2 px-3 text-slate-200 font-medium">
|
||||
{{ $name }}
|
||||
</td>
|
||||
<td class="py-2 px-3 text-slate-400 break-all">
|
||||
{{ $ns }}
|
||||
</td>
|
||||
<td class="py-2 px-3">
|
||||
{{ if eq $nsr.Status "ok" }}
|
||||
<span
|
||||
class="inline-block px-1.5 py-0.5 rounded text-[10px] font-bold uppercase bg-teal-900/50 text-teal-400 border border-teal-700/30"
|
||||
>ok</span
|
||||
>
|
||||
{{ else }}
|
||||
<span
|
||||
class="inline-block px-1.5 py-0.5 rounded text-[10px] font-bold uppercase bg-red-900/50 text-red-400 border border-red-700/30"
|
||||
>{{ $nsr.Status }}</span
|
||||
>
|
||||
{{ end }}
|
||||
</td>
|
||||
<td
|
||||
class="py-2 px-3 text-slate-400 break-all max-w-xs"
|
||||
>
|
||||
{{ if $nsr.Error }}
|
||||
<span class="text-red-400">{{ $nsr.Error }}</span>
|
||||
{{ else }}
|
||||
{{ formatRecords $nsr.Records }}
|
||||
{{ end }}
|
||||
</td>
|
||||
<td class="py-2 px-3 text-slate-500 whitespace-nowrap">
|
||||
{{ relTime $nsr.LastChecked }}
|
||||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
@@ -22,6 +22,12 @@ var (
|
||||
"reply is an error or a referral that leads no closer",
|
||||
)
|
||||
|
||||
// ErrTruncated is the reason given for a reply too large for UDP
|
||||
// whose retry over TCP failed.
|
||||
ErrTruncated = errors.New(
|
||||
"reply truncated and its retry over TCP failed",
|
||||
)
|
||||
|
||||
// ErrIntercepted is returned when every root server refused a
|
||||
// query. Root servers refuse no query, so the refusals came from
|
||||
// something on the network answering in their place.
|
||||
@@ -33,6 +39,13 @@ var (
|
||||
"CNAME chain depth exceeded",
|
||||
)
|
||||
|
||||
// ErrLookupDepthExceeded is returned when nameserver addresses
|
||||
// were not looked up because lookups were already maxLookupDepth
|
||||
// deep, one inside another.
|
||||
ErrLookupDepthExceeded = errors.New(
|
||||
"lookups of nameserver addresses go too deep",
|
||||
)
|
||||
|
||||
// ErrContextCanceled wraps context cancellation for the
|
||||
// resolver's iterative queries.
|
||||
ErrContextCanceled = errors.New("context canceled")
|
||||
|
||||
@@ -2,10 +2,21 @@ package resolver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// NewWithFailingTCP returns a Resolver whose TCP client gives up before
|
||||
// it can connect, so the retry over TCP of every truncated reply fails.
|
||||
func NewWithFailingTCP(log *slog.Logger) *Resolver {
|
||||
r := NewFromLogger(log)
|
||||
r.tcp = &tcpClient{timeout: time.Nanosecond}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// ExtractRecordValue exports extractRecordValue for testing.
|
||||
func ExtractRecordValue(rr dns.RR) string {
|
||||
return extractRecordValue(rr)
|
||||
@@ -55,12 +66,33 @@ func (r *Resolver) QueryEachNS(
|
||||
return r.queryEachNS(ctx, nameservers, hostname, recordTypes())
|
||||
}
|
||||
|
||||
// ResolveNSIPs exports resolveNSIPs for testing.
|
||||
// ResolveNSIPs exports resolveNSIPs for testing, looking each name up
|
||||
// as a lookup that no other lookup started.
|
||||
func (r *Resolver) ResolveNSIPs(
|
||||
ctx context.Context,
|
||||
nsNames []string,
|
||||
) []string {
|
||||
return r.resolveNSIPs(ctx, nsNames)
|
||||
ips, _ := r.resolveNSIPs(ctx, nsNames, 1)
|
||||
|
||||
return ips
|
||||
}
|
||||
|
||||
// MaxLookupDepth exports maxLookupDepth for testing.
|
||||
const MaxLookupDepth = maxLookupDepth
|
||||
|
||||
// QueryZone exports queryZone for testing.
|
||||
func (r *Resolver) QueryZone(
|
||||
ctx context.Context,
|
||||
given []string,
|
||||
withoutAddresses []string,
|
||||
zone string,
|
||||
name string,
|
||||
qtype uint16,
|
||||
depth int,
|
||||
) (*dns.Msg, error) {
|
||||
return r.queryZone(
|
||||
ctx, given, withoutAddresses, zone, name, qtype, depth,
|
||||
)
|
||||
}
|
||||
|
||||
// RootServerList exports rootServerList for testing.
|
||||
|
||||
+216
-58
@@ -19,6 +19,16 @@ const (
|
||||
maxRetries = 2
|
||||
maxDelegation = 20
|
||||
timeoutMultiplier = 2
|
||||
|
||||
// maxLookupDepth is how many lookups of nameserver addresses may be
|
||||
// under way one inside another. Looking up a nameserver's address
|
||||
// can meet a referral that names nameservers without their
|
||||
// addresses, which are then looked up in turn; without a limit,
|
||||
// delegations that point at each other would never end. Each level
|
||||
// multiplies the queries sent. pool.ntp.org needs three: the
|
||||
// address of its nameserver g.ntpns.org can need a.ntpns.org's,
|
||||
// which needs a bitnames.com nameserver's.
|
||||
maxLookupDepth = 3
|
||||
)
|
||||
|
||||
// ErrRefused is returned when a DNS server refuses a query.
|
||||
@@ -89,6 +99,9 @@ func (r *Resolver) tryExchange(
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// retryTCP returns the reply to msg over TCP when resp, its reply over
|
||||
// UDP, is truncated. When that fails it returns resp, still truncated,
|
||||
// which holds only the records that fit.
|
||||
func (r *Resolver) retryTCP(
|
||||
ctx context.Context,
|
||||
msg *dns.Msg,
|
||||
@@ -198,13 +211,15 @@ func (r *Resolver) followDelegation(
|
||||
// servers are the root servers, the servers of zone ".".
|
||||
zone := "."
|
||||
|
||||
var withoutAddresses []string
|
||||
|
||||
for range maxDelegation {
|
||||
if checkCtx(ctx) != nil {
|
||||
return nil, ErrContextCanceled
|
||||
}
|
||||
|
||||
resp, err := r.queryServers(
|
||||
ctx, servers, zone, domain, dns.TypeNS,
|
||||
resp, err := r.queryZone(
|
||||
ctx, servers, withoutAddresses, zone, domain, dns.TypeNS, 0,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -229,18 +244,7 @@ func (r *Resolver) followDelegation(
|
||||
return r.resolveNSIterative(ctx, domain)
|
||||
}
|
||||
|
||||
glue := extractGlue(resp.Extra)
|
||||
nextServers := glueIPs(authNS, glue)
|
||||
|
||||
if len(nextServers) == 0 {
|
||||
nextServers = r.resolveNSIPs(ctx, authNS)
|
||||
}
|
||||
|
||||
if len(nextServers) == 0 {
|
||||
return nil, ErrNoNameservers
|
||||
}
|
||||
|
||||
servers = nextServers
|
||||
servers, withoutAddresses = referralNameservers(resp)
|
||||
zone = referralZone(resp)
|
||||
}
|
||||
|
||||
@@ -319,13 +323,20 @@ func (r *Resolver) queryServers(
|
||||
return nil, fmt.Errorf("all servers failed: %w", lastErr)
|
||||
}
|
||||
|
||||
// isErrorReply reports whether msg is an error reply: one with any code
|
||||
// but NOERROR and NXDOMAIN, such as SERVFAIL, NOTIMP or FORMERR. An error
|
||||
// reply says nothing about the name's records.
|
||||
func isErrorReply(msg *dns.Msg) bool {
|
||||
return msg.Rcode != dns.RcodeSuccess && msg.Rcode != dns.RcodeNameError
|
||||
}
|
||||
|
||||
// usableReply reports whether resp, a reply from one of the servers of
|
||||
// zone to a query about name, is usable. An error reply such as SERVFAIL
|
||||
// is not. Nor is a referral, unless it refers the query to a zone below
|
||||
// zone that name is in: a server that refers it back to zone, up or
|
||||
// sideways does not serve zone as it should.
|
||||
func usableReply(resp *dns.Msg, zone string, name string) bool {
|
||||
if resp.Rcode != dns.RcodeSuccess && resp.Rcode != dns.RcodeNameError {
|
||||
if isErrorReply(resp) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -366,24 +377,110 @@ func nsSetFrom(resp *dns.Msg, domain string) []string {
|
||||
return extractNSSet(resp.Answer)
|
||||
}
|
||||
|
||||
// referralNameservers returns the IPv4 addresses that resp, a referral,
|
||||
// gives for the nameservers it names, and the names of the nameservers
|
||||
// it gives no address for.
|
||||
func referralNameservers(resp *dns.Msg) ([]string, []string) {
|
||||
glue := extractGlue(resp.Extra)
|
||||
|
||||
var given, withoutAddresses []string
|
||||
|
||||
for _, ns := range extractNSSet(resp.Ns) {
|
||||
ips := glueIPs([]string{ns}, glue)
|
||||
if len(ips) == 0 {
|
||||
withoutAddresses = append(withoutAddresses, ns)
|
||||
}
|
||||
|
||||
given = append(given, ips...)
|
||||
}
|
||||
|
||||
return given, withoutAddresses
|
||||
}
|
||||
|
||||
// queryZone asks the servers of zone about name as queryServers does:
|
||||
// first those at given, the addresses a referral gave, and only when
|
||||
// none of them gives a usable reply, the nameservers named
|
||||
// withoutAddresses, once their addresses are looked up. depth is how
|
||||
// many lookups of a nameserver's address are under way, 0 in the walk
|
||||
// to a domain's nameservers; at maxLookupDepth, no address is looked
|
||||
// up. When the limit is why none was found, here or in a lookup this
|
||||
// one started, the error is ErrLookupDepthExceeded.
|
||||
func (r *Resolver) queryZone(
|
||||
ctx context.Context,
|
||||
given []string,
|
||||
withoutAddresses []string,
|
||||
zone string,
|
||||
name string,
|
||||
qtype uint16,
|
||||
depth int,
|
||||
) (*dns.Msg, error) {
|
||||
err := fmt.Errorf(
|
||||
"no address for any nameserver of %s: %w", zone, ErrNoNameservers,
|
||||
)
|
||||
|
||||
if len(given) > 0 {
|
||||
var resp *dns.Msg
|
||||
|
||||
resp, err = r.queryServers(ctx, given, zone, name, qtype)
|
||||
if err == nil {
|
||||
return resp, nil
|
||||
}
|
||||
}
|
||||
|
||||
if len(withoutAddresses) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if depth >= maxLookupDepth {
|
||||
return nil, fmt.Errorf(
|
||||
"addresses of the nameservers of %s not looked up: %w",
|
||||
zone, ErrLookupDepthExceeded,
|
||||
)
|
||||
}
|
||||
|
||||
lookedUp, limitErr := r.resolveNSIPs(ctx, withoutAddresses, depth+1)
|
||||
if limitErr != nil {
|
||||
return nil, limitErr
|
||||
}
|
||||
|
||||
if len(lookedUp) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r.queryServers(ctx, lookedUp, zone, name, qtype)
|
||||
}
|
||||
|
||||
// resolveNSIPs returns the addresses of every nameserver in nsNames
|
||||
// whose name resolves, for a referral that carries none. The walk can
|
||||
// then go on to the zone's other nameservers when one gives no usable
|
||||
// reply.
|
||||
// whose name resolves, each looked up at depth (see resolveARecord).
|
||||
// The walk can then go on to the zone's other nameservers when one
|
||||
// gives no usable reply. When none resolves and the depth limit
|
||||
// stopped one of the lookups, it returns that lookup's error.
|
||||
func (r *Resolver) resolveNSIPs(
|
||||
ctx context.Context,
|
||||
nsNames []string,
|
||||
) []string {
|
||||
var ips []string
|
||||
depth int,
|
||||
) ([]string, error) {
|
||||
var (
|
||||
ips []string
|
||||
limitErr error
|
||||
)
|
||||
|
||||
for _, ns := range nsNames {
|
||||
resolved, err := r.resolveARecord(ctx, ns)
|
||||
if err == nil {
|
||||
resolved, err := r.resolveARecord(ctx, ns, depth)
|
||||
|
||||
switch {
|
||||
case err == nil:
|
||||
ips = append(ips, resolved...)
|
||||
case errors.Is(err, ErrLookupDepthExceeded):
|
||||
limitErr = err
|
||||
}
|
||||
}
|
||||
|
||||
return ips
|
||||
if len(ips) > 0 {
|
||||
return ips, nil
|
||||
}
|
||||
|
||||
return nil, limitErr
|
||||
}
|
||||
|
||||
// resolveNSIterative queries for NS records using iterative
|
||||
@@ -438,11 +535,14 @@ func (r *Resolver) resolveNSIterative(
|
||||
return nil, ErrNoNameservers
|
||||
}
|
||||
|
||||
// resolveARecord resolves a hostname to IPv4 addresses using
|
||||
// iterative resolution through the delegation chain.
|
||||
// resolveARecord resolves a hostname, a nameserver's name, to IPv4
|
||||
// addresses using iterative resolution through the delegation chain.
|
||||
// depth is how many lookups of a nameserver's address are under way,
|
||||
// this one included: 1 for a lookup that no other lookup started.
|
||||
func (r *Resolver) resolveARecord(
|
||||
ctx context.Context,
|
||||
hostname string,
|
||||
depth int,
|
||||
) ([]string, error) {
|
||||
if checkCtx(ctx) != nil {
|
||||
return nil, ErrContextCanceled
|
||||
@@ -452,13 +552,16 @@ func (r *Resolver) resolveARecord(
|
||||
servers := rootServerList()
|
||||
zone := "."
|
||||
|
||||
var withoutAddresses []string
|
||||
|
||||
for range maxDelegation {
|
||||
if checkCtx(ctx) != nil {
|
||||
return nil, ErrContextCanceled
|
||||
}
|
||||
|
||||
resp, err := r.queryServers(
|
||||
ctx, servers, zone, hostname, dns.TypeA,
|
||||
resp, err := r.queryZone(
|
||||
ctx, servers, withoutAddresses, zone, hostname, dns.TypeA,
|
||||
depth,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
@@ -485,17 +588,7 @@ func (r *Resolver) resolveARecord(
|
||||
break
|
||||
}
|
||||
|
||||
glue := extractGlue(resp.Extra)
|
||||
nextServers := glueIPs(authNS, glue)
|
||||
|
||||
if len(nextServers) == 0 {
|
||||
// Resolve NS IPs iteratively — but guard
|
||||
// against infinite recursion by using only
|
||||
// already-resolved servers.
|
||||
break
|
||||
}
|
||||
|
||||
servers = nextServers
|
||||
servers, withoutAddresses = referralNameservers(resp)
|
||||
zone = referralZone(resp)
|
||||
}
|
||||
|
||||
@@ -584,7 +677,7 @@ func (r *Resolver) queryNameserver(
|
||||
return nil, ErrContextCanceled
|
||||
}
|
||||
|
||||
nsIPs, err := r.resolveARecord(ctx, nsHostname)
|
||||
nsIPs, err := r.resolveARecord(ctx, nsHostname, 1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("resolving NS %s: %w", nsHostname, err)
|
||||
}
|
||||
@@ -632,14 +725,19 @@ func (r *Resolver) queryTypes(
|
||||
|
||||
type queryState struct {
|
||||
gotNXDomain bool
|
||||
gotSERVFAIL bool
|
||||
errorReply string // code of an error reply, such as SERVFAIL
|
||||
gotRefused bool
|
||||
gotTimeout bool
|
||||
gotReferral bool
|
||||
netErr error
|
||||
hasRecords bool
|
||||
answered bool
|
||||
}
|
||||
|
||||
// queryEachType asks the nameserver at nsIP about hostname once for each
|
||||
// record type in qtypes, and lists in resp.FailedTypes the types whose
|
||||
// query got no usable reply, logging each with the reason unless ctx was
|
||||
// cancelled: shutdown cancels it, and a query it cut short did not fail.
|
||||
func (r *Resolver) queryEachType(
|
||||
ctx context.Context,
|
||||
nsIP string,
|
||||
@@ -654,7 +752,34 @@ func (r *Resolver) queryEachType(
|
||||
break
|
||||
}
|
||||
|
||||
r.querySingleType(ctx, nsIP, hostname, qtype, resp, &state)
|
||||
err := r.querySingleType(ctx, nsIP, hostname, qtype, resp, &state)
|
||||
if err == nil {
|
||||
state.answered = true
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
rtype := dns.TypeToString[qtype]
|
||||
resp.FailedTypes = append(resp.FailedTypes, rtype)
|
||||
|
||||
if errors.Is(ctx.Err(), context.Canceled) {
|
||||
continue
|
||||
}
|
||||
|
||||
r.log.Warn(
|
||||
"record type query failed",
|
||||
"hostname", hostname,
|
||||
"nameserver", resp.Nameserver,
|
||||
"type", rtype,
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
|
||||
// The reply about another type can carry the name's CNAME. When the
|
||||
// query for CNAME itself failed, that is left out too, so Records
|
||||
// holds nothing for a failed type.
|
||||
for _, rtype := range resp.FailedTypes {
|
||||
delete(resp.Records, rtype)
|
||||
}
|
||||
|
||||
for k := range resp.Records {
|
||||
@@ -664,6 +789,9 @@ func (r *Resolver) queryEachType(
|
||||
return state
|
||||
}
|
||||
|
||||
// querySingleType asks the nameserver at nsIP about hostname's records
|
||||
// of type qtype. It returns nil when the nameserver answered: with
|
||||
// records, with none, or with NXDOMAIN; otherwise it returns why not.
|
||||
func (r *Resolver) querySingleType(
|
||||
ctx context.Context,
|
||||
nsIP string,
|
||||
@@ -671,7 +799,7 @@ func (r *Resolver) querySingleType(
|
||||
qtype uint16,
|
||||
resp *NameserverResponse,
|
||||
state *queryState,
|
||||
) {
|
||||
) error {
|
||||
msg, err := r.queryDNS(ctx, nsIP, hostname, qtype)
|
||||
if err != nil {
|
||||
switch {
|
||||
@@ -683,19 +811,33 @@ func (r *Resolver) querySingleType(
|
||||
state.netErr = err
|
||||
}
|
||||
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
return readReply(msg, resp, state)
|
||||
}
|
||||
|
||||
// readReply adds to resp the records in msg, a nameserver's reply to a
|
||||
// query about one record type. It returns nil when the nameserver
|
||||
// answered: with records, with none, or with NXDOMAIN; otherwise it
|
||||
// returns why not.
|
||||
func readReply(
|
||||
msg *dns.Msg,
|
||||
resp *NameserverResponse,
|
||||
state *queryState,
|
||||
) error {
|
||||
if msg.Rcode == dns.RcodeNameError {
|
||||
state.gotNXDomain = true
|
||||
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
if msg.Rcode == dns.RcodeServerFailure {
|
||||
state.gotSERVFAIL = true
|
||||
if isErrorReply(msg) {
|
||||
state.errorReply = dns.RcodeToString[msg.Rcode]
|
||||
|
||||
return
|
||||
return fmt.Errorf(
|
||||
"server returned %s: %w", state.errorReply, ErrUnusableReply,
|
||||
)
|
||||
}
|
||||
|
||||
// A reply with no answer that lists other nameservers, from a server
|
||||
@@ -708,10 +850,20 @@ func (r *Resolver) querySingleType(
|
||||
len(extractNSSet(msg.Ns)) > 0 {
|
||||
state.gotReferral = true
|
||||
|
||||
return
|
||||
return fmt.Errorf("server returned a referral: %w", ErrUnusableReply)
|
||||
}
|
||||
|
||||
// A reply still truncated is one whose TCP retry failed, and holds
|
||||
// only the records that fit.
|
||||
if msg.Truncated {
|
||||
state.netErr = ErrTruncated
|
||||
|
||||
return ErrTruncated
|
||||
}
|
||||
|
||||
collectAnswerRecords(msg, resp, state)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// collectAnswerRecords adds the records in msg's answer to resp, each
|
||||
@@ -750,23 +902,26 @@ func isTimeout(err error) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// classifyResponse sets the nameserver's status. One that answered no
|
||||
// record type has failed, and Error says why; one that answered some has
|
||||
// the status of those answers.
|
||||
func classifyResponse(resp *NameserverResponse, state queryState) {
|
||||
switch {
|
||||
case state.gotNXDomain && !state.hasRecords:
|
||||
resp.Status = StatusNXDomain
|
||||
case state.gotTimeout && !state.hasRecords:
|
||||
case state.gotTimeout && !state.answered:
|
||||
resp.Status = StatusTimeout
|
||||
resp.Error = "all queries timed out"
|
||||
case state.gotSERVFAIL && !state.hasRecords:
|
||||
case state.errorReply != "" && !state.answered:
|
||||
resp.Status = StatusError
|
||||
resp.Error = "server returned SERVFAIL"
|
||||
case state.gotRefused && !state.hasRecords:
|
||||
resp.Error = "server returned " + state.errorReply
|
||||
case state.gotRefused && !state.answered:
|
||||
resp.Status = StatusError
|
||||
resp.Error = "server returned REFUSED"
|
||||
case state.netErr != nil && !state.hasRecords:
|
||||
case state.netErr != nil && !state.answered:
|
||||
resp.Status = StatusError
|
||||
resp.Error = "network error: " + state.netErr.Error()
|
||||
case state.gotReferral && !state.hasRecords:
|
||||
case state.gotReferral && !state.answered:
|
||||
resp.Status = StatusError
|
||||
resp.Error = "server returned a referral"
|
||||
case !state.hasRecords && !state.gotNXDomain:
|
||||
@@ -927,9 +1082,11 @@ func (r *Resolver) resolveIPWithCNAME(
|
||||
}
|
||||
|
||||
// collectIPs returns the addresses in the nameservers' answers and the
|
||||
// first CNAME target among them. It returns ErrNoNameserverAnswered when
|
||||
// every nameserver timed out, failed or returned a referral: that is not
|
||||
// a name with no addresses.
|
||||
// first CNAME target among them. A nameserver whose query for one of the
|
||||
// types failed gave only part of the addresses, and is left out. It
|
||||
// returns ErrNoNameserverAnswered when every nameserver timed out,
|
||||
// failed, returned a referral or was left out: that is not a name with
|
||||
// no addresses.
|
||||
func collectIPs(
|
||||
results map[string]*NameserverResponse,
|
||||
) ([]string, string, error) {
|
||||
@@ -942,7 +1099,8 @@ func collectIPs(
|
||||
answered := false
|
||||
|
||||
for _, resp := range results {
|
||||
if resp.Status == StatusTimeout || resp.Status == StatusError {
|
||||
if resp.Status == StatusTimeout || resp.Status == StatusError ||
|
||||
len(resp.FailedTypes) > 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package resolver
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestClassifyResponse sets a nameserver's status from the results of
|
||||
// its queries, built here. One that answered some record types, even
|
||||
// with no records, has not failed when its query for another type got
|
||||
// no usable reply, whatever the reason; one whose every query got none
|
||||
// has.
|
||||
func TestClassifyResponse(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
results queryState
|
||||
wantStatus string
|
||||
wantError string
|
||||
}{
|
||||
{
|
||||
"some types answered with no records, another timed out",
|
||||
queryState{answered: true, gotTimeout: true},
|
||||
StatusNoData, "",
|
||||
},
|
||||
{
|
||||
"some types answered with no records, another got SERVFAIL",
|
||||
queryState{answered: true, errorReply: "SERVFAIL"},
|
||||
StatusNoData, "",
|
||||
},
|
||||
{
|
||||
"some types answered with no records, another was refused",
|
||||
queryState{answered: true, gotRefused: true},
|
||||
StatusNoData, "",
|
||||
},
|
||||
{
|
||||
"some types answered with no records, another got a network error",
|
||||
queryState{answered: true, netErr: syscall.ECONNREFUSED},
|
||||
StatusNoData, "",
|
||||
},
|
||||
{
|
||||
"some types answered with no records, another's reply was " +
|
||||
"truncated and its retry over TCP failed",
|
||||
queryState{answered: true, netErr: ErrTruncated},
|
||||
StatusNoData, "",
|
||||
},
|
||||
{
|
||||
"some types answered with no records, another got a referral",
|
||||
queryState{answered: true, gotReferral: true},
|
||||
StatusNoData, "",
|
||||
},
|
||||
{
|
||||
"every query timed out",
|
||||
queryState{gotTimeout: true},
|
||||
StatusTimeout, "all queries timed out",
|
||||
},
|
||||
{
|
||||
"every query got NOTIMP",
|
||||
queryState{errorReply: "NOTIMP"},
|
||||
StatusError, "server returned NOTIMP",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
resp := &NameserverResponse{Status: StatusOK}
|
||||
classifyResponse(resp, tt.results)
|
||||
|
||||
assert.Equal(t, tt.wantStatus, resp.Status)
|
||||
assert.Equal(t, tt.wantError, resp.Error)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestReadReply checks which replies to a query about one record type,
|
||||
// built here, are an answer: one with the code NOERROR or NXDOMAIN. A
|
||||
// reply with any other code is not, and the type's query has failed.
|
||||
func TestReadReply(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
rcode int
|
||||
answered bool
|
||||
}{
|
||||
{dns.RcodeSuccess, true},
|
||||
{dns.RcodeNameError, true},
|
||||
{dns.RcodeServerFailure, false},
|
||||
{dns.RcodeNotImplemented, false},
|
||||
{dns.RcodeFormatError, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(dns.RcodeToString[tt.rcode], func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
msg := new(dns.Msg)
|
||||
msg.Authoritative = true
|
||||
msg.Rcode = tt.rcode
|
||||
|
||||
resp := &NameserverResponse{Records: map[string][]string{}}
|
||||
err := readReply(msg, resp, &queryState{})
|
||||
|
||||
if tt.answered {
|
||||
assert.NoError(t, err)
|
||||
} else {
|
||||
assert.ErrorIs(t, err, ErrUnusableReply)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,25 @@ func TestCollectIPs_FailedIsNoAnswer(t *testing.T) {
|
||||
assert.Empty(t, ips)
|
||||
}
|
||||
|
||||
// TestCollectIPs_FailedTypeIsNoAnswer checks that a nameserver whose
|
||||
// query for one of the types failed is no answer: its addresses are
|
||||
// only part of them.
|
||||
func TestCollectIPs_FailedTypeIsNoAnswer(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ips, _, err := resolver.CollectIPs(
|
||||
map[string]*resolver.NameserverResponse{
|
||||
nsExample1: {
|
||||
Records: map[string][]string{"A": {"192.0.2.1"}},
|
||||
FailedTypes: []string{"AAAA"},
|
||||
Status: resolver.StatusOK,
|
||||
},
|
||||
},
|
||||
)
|
||||
require.ErrorIs(t, err, resolver.ErrNoNameserverAnswered)
|
||||
assert.Empty(t, ips)
|
||||
}
|
||||
|
||||
const (
|
||||
// exampleCom is the zone most cases of TestUsableReply and
|
||||
// TestNSSetFrom are about, and wwwExampleCom a name in it.
|
||||
|
||||
@@ -31,9 +31,13 @@ type Params struct {
|
||||
}
|
||||
|
||||
// NameserverResponse holds one nameserver's response for a query.
|
||||
// FailedTypes lists the record types whose query got no usable reply,
|
||||
// and Records holds nothing for them: their records are not known. When
|
||||
// no record type got one, Status and Error say the nameserver failed.
|
||||
type NameserverResponse struct {
|
||||
Nameserver string
|
||||
Records map[string][]string
|
||||
FailedTypes []string
|
||||
Status string
|
||||
Error string
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package resolver_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -162,6 +164,112 @@ func TestResolveNSIPs_EveryNameserver(t *testing.T) {
|
||||
assert.ElementsMatch(t, want, got)
|
||||
}
|
||||
|
||||
// TestResolveNSIPs_ZoneDelegatedWithoutAddresses looks up the address
|
||||
// of a.ntpns.org, a nameserver of pool.ntp.org. The org servers delegate
|
||||
// ntpns.org to nameservers in other zones and give none of their
|
||||
// addresses, so those are looked up on the way.
|
||||
func TestResolveNSIPs_ZoneDelegatedWithoutAddresses(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
r := newTestResolver(t)
|
||||
ips := liveResolveNSIPs(t, r, []string{"a.ntpns.org."}, 1)
|
||||
|
||||
for _, ip := range ips {
|
||||
assert.NotNil(t, net.ParseIP(ip), "should be valid IP: %s", ip)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQueryZone_GivenAddressesFail asks the servers of ntp.org about
|
||||
// pool.ntp.org, as the walk to a name under ntp.org does after the org
|
||||
// servers' referral. That referral names four nameservers and gives an
|
||||
// address for ns1.everett.org alone; here the given address is
|
||||
// 192.0.2.1, where nothing answers, so the other three must be looked
|
||||
// up and asked.
|
||||
func TestQueryZone_GivenAddressesFail(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
r := newTestResolver(t)
|
||||
|
||||
var resp *dns.Msg
|
||||
|
||||
livednstest.Retry(
|
||||
t,
|
||||
"QueryZone(192.0.2.1 and three ntp.org nameservers, pool.ntp.org)",
|
||||
func(ctx context.Context) error {
|
||||
var err error
|
||||
|
||||
resp, err = r.QueryZone(
|
||||
ctx, []string{"192.0.2.1"},
|
||||
[]string{"anyns.pch.net.", "dns1.udel.edu.", "dns2.udel.edu."},
|
||||
"ntp.org.", "pool.ntp.org.", dns.TypeNS, 0,
|
||||
)
|
||||
|
||||
return err
|
||||
},
|
||||
)
|
||||
|
||||
assert.NotEmpty(t, resolver.NSSetFrom(resp, "pool.ntp.org."))
|
||||
}
|
||||
|
||||
// TestQueryZone_LookupDepth asks the servers of g.ntpns.org, a
|
||||
// nameserver of pool.ntp.org, for its address, as looking that address
|
||||
// up does when anyns.pch.net, one of the servers of ntpns.org, gives the
|
||||
// referral to g.ntpns.org without addresses. Their addresses are looked
|
||||
// up (here only a.ntpns.org's), and that needs a bitnames.com
|
||||
// nameserver's address, as the org servers delegate ntpns.org without
|
||||
// addresses. From depth 1, where looking up g.ntpns.org's address
|
||||
// starts, that makes three lookups and the address is found. From one
|
||||
// below maxLookupDepth, the bitnames.com lookup would be past the limit,
|
||||
// so nothing can be asked, and the error says the limit is why.
|
||||
func TestQueryZone_LookupDepth(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
r := newTestResolver(t)
|
||||
withoutAddresses := []string{"a.ntpns.org."}
|
||||
|
||||
var resp *dns.Msg
|
||||
|
||||
livednstest.Retry(
|
||||
t,
|
||||
"QueryZone(a.ntpns.org without its address, g.ntpns.org)",
|
||||
func(ctx context.Context) error {
|
||||
var err error
|
||||
|
||||
resp, err = r.QueryZone(
|
||||
ctx, nil, withoutAddresses, "g.ntpns.org.", "g.ntpns.org.",
|
||||
dns.TypeA, 1,
|
||||
)
|
||||
|
||||
return err
|
||||
},
|
||||
)
|
||||
|
||||
assert.NotEmpty(t, resp.Answer)
|
||||
|
||||
var limitErr error
|
||||
|
||||
// Any other error is live DNS not answering, and is retried.
|
||||
livednstest.Retry(
|
||||
t,
|
||||
"QueryZone(a.ntpns.org without its address, g.ntpns.org, "+
|
||||
"one below the limit)",
|
||||
func(ctx context.Context) error {
|
||||
_, limitErr = r.QueryZone(
|
||||
ctx, nil, withoutAddresses, "g.ntpns.org.", "g.ntpns.org.",
|
||||
dns.TypeA, resolver.MaxLookupDepth-1,
|
||||
)
|
||||
if limitErr == nil ||
|
||||
errors.Is(limitErr, resolver.ErrLookupDepthExceeded) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return limitErr
|
||||
},
|
||||
)
|
||||
|
||||
require.ErrorIs(t, limitErr, resolver.ErrLookupDepthExceeded)
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// QueryNameserver tests
|
||||
// ----------------------------------------------------------------
|
||||
@@ -185,6 +293,20 @@ func TestQueryNameserver_BasicA(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
// TestQueryNameserver_ZoneDelegatedWithoutAddresses asks a.ntpns.org, a
|
||||
// nameserver of pool.ntp.org, about pool.ntp.org, as the watcher does.
|
||||
// The org servers delegate ntpns.org without the addresses of its
|
||||
// nameservers, so finding a.ntpns.org's address needs a lookup inside
|
||||
// the one QueryNameserver starts.
|
||||
func TestQueryNameserver_ZoneDelegatedWithoutAddresses(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
r := newTestResolver(t)
|
||||
resp := liveQueryNameserver(t, r, "a.ntpns.org.", "pool.ntp.org", "A")
|
||||
|
||||
assert.Equal(t, resolver.StatusOK, resp.Status)
|
||||
}
|
||||
|
||||
func TestQueryNameserver_AAAA(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -245,6 +367,30 @@ func TestQueryNameserver_TXT(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
// TestQueryNameserver_TruncatedReplyWhoseTCPRetryFails asks a google.com
|
||||
// nameserver about google.com with a resolver whose retries over TCP
|
||||
// fail. google.com's TXT records do not fit in a reply over UDP, so TXT
|
||||
// is reported as failed, holding none of the records that fit, and
|
||||
// logged with the reason, while the nameserver, which answered the other
|
||||
// types, is ok.
|
||||
func TestQueryNameserver_TruncatedReplyWhoseTCPRetryFails(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ns := findOneNSForDomain(t, newTestResolver(t), "google.com")
|
||||
|
||||
var logs bytes.Buffer
|
||||
|
||||
r := resolver.NewWithFailingTCP(slog.New(slog.NewTextHandler(&logs, nil)))
|
||||
resp := liveQueryNameserver(t, r, ns, "google.com")
|
||||
|
||||
assert.Equal(t, resolver.StatusOK, resp.Status)
|
||||
assert.Contains(t, resp.FailedTypes, "TXT")
|
||||
assert.NotContains(t, resp.Records, "TXT")
|
||||
assert.Contains(t, logs.String(),
|
||||
"hostname=google.com. nameserver="+ns+" type=TXT error=",
|
||||
)
|
||||
}
|
||||
|
||||
func TestQueryNameserver_NXDomain(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -681,6 +827,21 @@ func TestLookupNS_MatchesFindAuthoritative(t *testing.T) {
|
||||
assert.Equal(t, fromFind, fromLookup)
|
||||
}
|
||||
|
||||
// TestLookupNS_ParentZoneDelegatedWithoutAddresses looks up the
|
||||
// nameservers of g.ntpns.org. The org servers delegate its parent zone,
|
||||
// ntpns.org, without the addresses of its nameservers, so the walk has
|
||||
// to look them up to ask them. If it did not, the walk for g.ntpns.org
|
||||
// would fail and LookupNS would return the nameservers of ntpns.org,
|
||||
// which a.ntpns.org is not one of.
|
||||
func TestLookupNS_ParentZoneDelegatedWithoutAddresses(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
r := newTestResolver(t)
|
||||
nameservers := liveLookupNS(t, r, "g.ntpns.org")
|
||||
|
||||
assert.Contains(t, nameservers, "a.ntpns.org.")
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// ResolveIPAddresses tests
|
||||
// ----------------------------------------------------------------
|
||||
@@ -865,6 +1026,29 @@ func TestQueryNameserverIP_Timeout(t *testing.T) {
|
||||
assert.NotEmpty(t, resp.Error)
|
||||
}
|
||||
|
||||
// TestQueryNameserverIP_CancelledLogsNothing cancels the context while
|
||||
// a query to 192.0.2.1, where nothing answers, is waiting for a reply,
|
||||
// as shutdown does. The query was cut short, not failed, so nothing is
|
||||
// logged.
|
||||
func TestQueryNameserverIP_CancelledLogsNothing(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var logs bytes.Buffer
|
||||
|
||||
r := resolver.NewFromLogger(slog.New(slog.NewTextHandler(&logs, nil)))
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
time.AfterFunc(100*time.Millisecond, cancel)
|
||||
|
||||
_, err := r.QueryNameserverIP(
|
||||
ctx, "unreachable.test.", "192.0.2.1", "example.com",
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Empty(t, logs.String())
|
||||
}
|
||||
|
||||
// TestCollectIPs_NoNameserverAnswered takes the response of a
|
||||
// nameserver at 192.0.2.1, where nothing answers, as
|
||||
// TestQueryNameserverIP_Timeout does. Addresses collected from
|
||||
|
||||
@@ -46,8 +46,15 @@ type DomainState struct {
|
||||
}
|
||||
|
||||
// NameserverRecordState holds one NS's response for a hostname.
|
||||
// FailedTypes lists the record types whose query to the nameserver
|
||||
// failed on this check: Records holds for them the records saved by the
|
||||
// previous check, which are kept. UnknownTypes lists those of them whose
|
||||
// records the previous check did not know either, as when the
|
||||
// nameserver was new or failing then: Records holds nothing for them.
|
||||
type NameserverRecordState struct {
|
||||
Records map[string][]string `json:"records"`
|
||||
FailedTypes []string `json:"failedTypes,omitempty"`
|
||||
UnknownTypes []string `json:"unknownTypes,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Error string `json:"error,omitempty"`
|
||||
LastChecked time.Time `json:"lastChecked"`
|
||||
@@ -122,6 +129,8 @@ type CertificateState struct {
|
||||
}
|
||||
|
||||
// Snapshot is the complete monitoring state persisted to disk.
|
||||
// Hostnames also holds each apex domain's own records, under the
|
||||
// domain's name, which has an entry in Domains too.
|
||||
type Snapshot struct {
|
||||
Version int `json:"version"`
|
||||
LastUpdated time.Time `json:"lastUpdated"`
|
||||
|
||||
@@ -236,6 +236,61 @@ func TestSaveLoadRoundTrip_CNAMEAddresses(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSaveLoadRoundTrip_FailedTypes checks that a nameserver's
|
||||
// failedTypes and unknownTypes survive a save and load. Without
|
||||
// unknownTypes, a type whose records were not known would load as one
|
||||
// with no records.
|
||||
func TestSaveLoadRoundTrip_FailedTypes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
s := state.NewForTestWithDataDir(dir)
|
||||
|
||||
failed := []string{"TXT", "CAA"}
|
||||
unknown := []string{"CAA"}
|
||||
|
||||
s.SetHostnameState(testHostname, &state.HostnameState{
|
||||
RecordsByNameserver: map[string]*state.NameserverRecordState{
|
||||
testNS1: {
|
||||
Records: map[string][]string{"TXT": {"v=spf1 -all"}},
|
||||
FailedTypes: failed,
|
||||
UnknownTypes: unknown,
|
||||
Status: "ok",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
err := s.Save()
|
||||
if err != nil {
|
||||
t.Fatalf("Save() error: %v", err)
|
||||
}
|
||||
|
||||
loaded := state.NewForTestWithDataDir(dir)
|
||||
|
||||
err = loaded.Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load() error: %v", err)
|
||||
}
|
||||
|
||||
hs, ok := loaded.GetHostnameState(testHostname)
|
||||
if !ok {
|
||||
t.Fatal("missing hostname " + testHostname)
|
||||
}
|
||||
|
||||
ns1 := hs.RecordsByNameserver[testNS1]
|
||||
if ns1 == nil {
|
||||
t.Fatal("missing nameserver " + testNS1)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(ns1.FailedTypes, failed) {
|
||||
t.Errorf("failedTypes: got %#v", ns1.FailedTypes)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(ns1.UnknownTypes, unknown) {
|
||||
t.Errorf("unknownTypes: got %#v", ns1.UnknownTypes)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadStateFromBeforeCNAMEAddresses loads a state file written
|
||||
// before the addresses at the end of a hostname's CNAME chain were
|
||||
// saved. They load as not known (nil), not as none.
|
||||
|
||||
@@ -202,6 +202,42 @@ func TestCNAMEWhoseNameserversAllFailedKeepsPrevious(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestCNAMEWhoseAddressQueryFailedKeepsPrevious checks a name whose
|
||||
// nameserver answered, but whose query for A, AAAA or CNAME failed with
|
||||
// nothing kept for it. That is not an answer with no address: the
|
||||
// addresses the previous check saved from following its CNAME are kept,
|
||||
// and nothing is looked up, the watcher having no resolver.
|
||||
func TestCNAMEWhoseAddressQueryFailedKeepsPrevious(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, rtype := range []string{"A", "AAAA", "CNAME"} {
|
||||
t.Run(rtype, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
w := watcher.NewForTest(nil, nil, nil, nil, nil, nil)
|
||||
|
||||
current := saved(map[string]*state.NameserverRecordState{
|
||||
nsA: {
|
||||
Records: map[string][]string{},
|
||||
FailedTypes: []string{rtype},
|
||||
UnknownTypes: []string{rtype},
|
||||
Status: "ok",
|
||||
},
|
||||
})
|
||||
prev := cnameState(oldIP)
|
||||
|
||||
w.ResolveCNAMEAddresses(t.Context(), host, current, prev)
|
||||
|
||||
if !slices.Equal(current.CNAMEAddresses, prev.CNAMEAddresses) {
|
||||
t.Errorf(
|
||||
"saved %v, want %v",
|
||||
current.CNAMEAddresses, prev.CNAMEAddresses,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// cnameTo builds the records of a nameserver that answered with a CNAME
|
||||
// to target and no address.
|
||||
func cnameTo(target string) map[string][]string {
|
||||
|
||||
@@ -10,7 +10,8 @@ import (
|
||||
"sneak.berlin/go/dnswatcher/internal/state"
|
||||
)
|
||||
|
||||
// NewForTest creates a Watcher without fx for unit testing.
|
||||
// NewForTest creates a Watcher without fx for unit testing. A nil cfg
|
||||
// is an empty configuration.
|
||||
func NewForTest(
|
||||
cfg *config.Config,
|
||||
st *state.State,
|
||||
@@ -19,6 +20,10 @@ func NewForTest(
|
||||
tc TLSChecker,
|
||||
n Notifier,
|
||||
) *Watcher {
|
||||
if cfg == nil {
|
||||
cfg = &config.Config{}
|
||||
}
|
||||
|
||||
return &Watcher{
|
||||
log: slog.Default(),
|
||||
config: cfg,
|
||||
@@ -96,6 +101,12 @@ func (w *Watcher) DetectNSAddressChanges(
|
||||
w.detectNSAddressChanges(ctx, domain, prev, current)
|
||||
}
|
||||
|
||||
// MaybeSendTestNotification exports maybeSendTestNotification for
|
||||
// testing.
|
||||
func (w *Watcher) MaybeSendTestNotification(ctx context.Context) {
|
||||
w.maybeSendTestNotification(ctx)
|
||||
}
|
||||
|
||||
// CheckAllPorts exports checkAllPorts for testing.
|
||||
func (w *Watcher) CheckAllPorts(ctx context.Context) {
|
||||
w.checkAllPorts(ctx)
|
||||
@@ -109,7 +120,8 @@ func (w *Watcher) RunTLSChecks(ctx context.Context) {
|
||||
// BuildHostnameState exports buildHostnameState for testing.
|
||||
func BuildHostnameState(
|
||||
results map[string]*resolver.NameserverResponse,
|
||||
prev *state.HostnameState,
|
||||
now time.Time,
|
||||
) *state.HostnameState {
|
||||
return buildHostnameState(results, now)
|
||||
return buildHostnameState(results, prev, now)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,364 @@
|
||||
package watcher_test
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/dnswatcher/internal/resolver"
|
||||
"sneak.berlin/go/dnswatcher/internal/state"
|
||||
"sneak.berlin/go/dnswatcher/internal/watcher"
|
||||
)
|
||||
|
||||
const (
|
||||
// txt is the record type whose query fails in these tests.
|
||||
txt = "TXT"
|
||||
spf1 = "v=spf1 -all"
|
||||
spf2 = "v=spf1 include:example.net -all"
|
||||
)
|
||||
|
||||
// response is a nameserver's response with these records, whose queries
|
||||
// for failedTypes failed.
|
||||
func response(
|
||||
records map[string][]string,
|
||||
failedTypes ...string,
|
||||
) *resolver.NameserverResponse {
|
||||
return &resolver.NameserverResponse{
|
||||
Records: records,
|
||||
FailedTypes: failedTypes,
|
||||
Status: resolver.StatusOK,
|
||||
}
|
||||
}
|
||||
|
||||
// savedChecks saves the state of each check in turn from the
|
||||
// nameservers' responses, each from the state the check before saved.
|
||||
func savedChecks(
|
||||
checks ...map[string]*resolver.NameserverResponse,
|
||||
) []*state.HostnameState {
|
||||
states := make([]*state.HostnameState, 0, len(checks))
|
||||
|
||||
var prev *state.HostnameState
|
||||
|
||||
for _, results := range checks {
|
||||
prev = watcher.BuildHostnameState(results, prev, time.Now())
|
||||
states = append(states, prev)
|
||||
}
|
||||
|
||||
return states
|
||||
}
|
||||
|
||||
// TestFailedTypeKeepsPreviousRecords saves a check in which nsA's query
|
||||
// for TXT failed, after previous checks of several kinds. TXT is always
|
||||
// saved in FailedTypes, and in UnknownTypes when there was nothing to
|
||||
// keep.
|
||||
func TestFailedTypeKeepsPreviousRecords(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
aOnly := map[string][]string{"A": {ip1}}
|
||||
withTXT := map[string][]string{"A": {ip1}, txt: {spf1}}
|
||||
txtKept := &state.NameserverRecordState{
|
||||
Records: withTXT, FailedTypes: []string{txt}, Status: "ok",
|
||||
}
|
||||
txtNotKnown := &state.NameserverRecordState{
|
||||
Records: aOnly,
|
||||
FailedTypes: []string{txt},
|
||||
UnknownTypes: []string{txt},
|
||||
Status: "ok",
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
prev *state.HostnameState
|
||||
wantRecords map[string][]string
|
||||
wantUnknown []string
|
||||
}{
|
||||
{
|
||||
"previous TXT records are kept",
|
||||
saved(map[string]*state.NameserverRecordState{nsA: answered(withTXT)}),
|
||||
withTXT, nil,
|
||||
},
|
||||
{
|
||||
"previous check had no TXT records",
|
||||
saved(map[string]*state.NameserverRecordState{nsA: answered(aOnly)}),
|
||||
aOnly, nil,
|
||||
},
|
||||
{
|
||||
"TXT failed on the previous check, which kept its records",
|
||||
saved(map[string]*state.NameserverRecordState{nsA: txtKept}),
|
||||
withTXT, nil,
|
||||
},
|
||||
{"first check", nil, aOnly, []string{txt}},
|
||||
{
|
||||
"nameserver new on this check",
|
||||
saved(map[string]*state.NameserverRecordState{nsB: answered(withTXT)}),
|
||||
aOnly, []string{txt},
|
||||
},
|
||||
{
|
||||
"nameserver failed on the previous check",
|
||||
saved(map[string]*state.NameserverRecordState{nsA: failed()}),
|
||||
aOnly, []string{txt},
|
||||
},
|
||||
{
|
||||
"TXT failed on the previous check with nothing to keep",
|
||||
saved(map[string]*state.NameserverRecordState{nsA: txtNotKnown}),
|
||||
aOnly, []string{txt},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
hs := watcher.BuildHostnameState(
|
||||
map[string]*resolver.NameserverResponse{
|
||||
nsA: response(map[string][]string{"A": {ip1}}, txt),
|
||||
},
|
||||
tt.prev, time.Now(),
|
||||
)
|
||||
|
||||
got := hs.RecordsByNameserver[nsA]
|
||||
if got.Status != "ok" ||
|
||||
!maps.EqualFunc(got.Records, tt.wantRecords, slices.Equal) ||
|
||||
!slices.Equal(got.FailedTypes, []string{txt}) ||
|
||||
!slices.Equal(got.UnknownTypes, tt.wantUnknown) {
|
||||
t.Errorf(
|
||||
"saved status %q, records %v, failed types %v, "+
|
||||
"unknown types %v; want ok, %v, [%s], %v",
|
||||
got.Status, got.Records, got.FailedTypes,
|
||||
got.UnknownTypes, tt.wantRecords, txt, tt.wantUnknown,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFailedTypeAlerts saves the checks of each case in turn from the
|
||||
// nameservers' responses, the first being the state loaded at startup,
|
||||
// and counts the alerts sent. nsB's TXT query fails on one check, and
|
||||
// nothing changes.
|
||||
func TestFailedTypeAlerts(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
records := map[string][]string{"A": {ip1}, txt: {spf1}}
|
||||
aOnly := map[string][]string{"A": {ip1}}
|
||||
|
||||
bothAnswer := map[string]*resolver.NameserverResponse{
|
||||
nsA: response(records), nsB: response(records),
|
||||
}
|
||||
bTXTFails := map[string]*resolver.NameserverResponse{
|
||||
nsA: response(records), nsB: response(aOnly, txt),
|
||||
}
|
||||
onlyA := map[string]*resolver.NameserverResponse{
|
||||
nsA: response(records),
|
||||
}
|
||||
bFails := map[string]*resolver.NameserverResponse{
|
||||
nsA: response(records),
|
||||
nsB: {
|
||||
Records: map[string][]string{},
|
||||
Status: resolver.StatusTimeout,
|
||||
Error: "all queries timed out",
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
checks []map[string]*resolver.NameserverResponse
|
||||
want alertCounts
|
||||
}{
|
||||
{
|
||||
"type failing at one nameserver alerts nothing, nor its next answer",
|
||||
[]map[string]*resolver.NameserverResponse{
|
||||
bothAnswer, bTXTFails, bothAnswer,
|
||||
},
|
||||
alertCounts{},
|
||||
},
|
||||
{
|
||||
"type failing on the first check alerts nothing on the next",
|
||||
[]map[string]*resolver.NameserverResponse{bTXTFails, bothAnswer},
|
||||
alertCounts{},
|
||||
},
|
||||
{
|
||||
"type failing at a nameserver new on that check alerts nothing",
|
||||
[]map[string]*resolver.NameserverResponse{
|
||||
onlyA, bTXTFails, bothAnswer,
|
||||
},
|
||||
alertCounts{},
|
||||
},
|
||||
{
|
||||
"type failing at a recovering nameserver alerts the recovery",
|
||||
[]map[string]*resolver.NameserverResponse{
|
||||
bFails, bTXTFails, bothAnswer,
|
||||
},
|
||||
alertCounts{recoveries: 1},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
states := savedChecks(tt.checks...)
|
||||
|
||||
got := countAlerts(t, states[0], states[1:])
|
||||
if got != tt.want {
|
||||
t.Errorf("sent %+v, want %+v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFailedTypeComparedOnceItAnswers saves the checks of each case in
|
||||
// turn as TestFailedTypeAlerts does. nsB's TXT query fails on one check,
|
||||
// and the TXT record changes: the change is sent as a Record Change for
|
||||
// each nameserver on the check where it answers it, and an Inconsistency
|
||||
// only when nsB still answers the old record.
|
||||
func TestFailedTypeComparedOnceItAnswers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
records := map[string][]string{"A": {ip1}, txt: {spf1}}
|
||||
changed := map[string][]string{"A": {ip1}, txt: {spf2}}
|
||||
aOnly := map[string][]string{"A": {ip1}}
|
||||
|
||||
bothAnswer := map[string]*resolver.NameserverResponse{
|
||||
nsA: response(records), nsB: response(records),
|
||||
}
|
||||
bTXTFails := map[string]*resolver.NameserverResponse{
|
||||
nsA: response(records), nsB: response(aOnly, txt),
|
||||
}
|
||||
bothChange := map[string]*resolver.NameserverResponse{
|
||||
nsA: response(changed), nsB: response(changed),
|
||||
}
|
||||
aChangesBTXTFails := map[string]*resolver.NameserverResponse{
|
||||
nsA: response(changed), nsB: response(aOnly, txt),
|
||||
}
|
||||
bStillOld := map[string]*resolver.NameserverResponse{
|
||||
nsA: response(changed), nsB: response(records),
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
checks []map[string]*resolver.NameserverResponse
|
||||
want alertCounts
|
||||
}{
|
||||
{
|
||||
"change made while the type failed",
|
||||
[]map[string]*resolver.NameserverResponse{
|
||||
bothAnswer, bTXTFails, bothChange,
|
||||
},
|
||||
alertCounts{recordChanges: 2},
|
||||
},
|
||||
{
|
||||
"change seen at one nameserver while the other's type failed",
|
||||
[]map[string]*resolver.NameserverResponse{
|
||||
bothAnswer, aChangesBTXTFails, bothChange,
|
||||
},
|
||||
alertCounts{recordChanges: 2},
|
||||
},
|
||||
{
|
||||
"old record answered after the type failed",
|
||||
[]map[string]*resolver.NameserverResponse{
|
||||
bothAnswer, aChangesBTXTFails, bStillOld,
|
||||
},
|
||||
alertCounts{recordChanges: 1, inconsistencies: 1},
|
||||
},
|
||||
{
|
||||
"change after the type failed on the first check and answered",
|
||||
[]map[string]*resolver.NameserverResponse{
|
||||
bTXTFails, bothAnswer, bothChange,
|
||||
},
|
||||
alertCounts{recordChanges: 2},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
states := savedChecks(tt.checks...)
|
||||
|
||||
got := countAlerts(t, states[0], states[1:])
|
||||
if got != tt.want {
|
||||
t.Errorf("sent %+v, want %+v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFailedTypeLeftOutOfMessages checks that a Record Change and an
|
||||
// Inconsistency name only the record types they compared. nsB's TXT
|
||||
// records are not known on the first check, and on the second either
|
||||
// answered or still not known; nsB's A record changes, so both alerts
|
||||
// are sent and name the A record alone.
|
||||
func TestFailedTypeLeftOutOfMessages(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
withTXT := map[string][]string{"A": {ip1}, txt: {spf1}}
|
||||
txtNotKnown := func(address string) *state.NameserverRecordState {
|
||||
return &state.NameserverRecordState{
|
||||
Records: map[string][]string{"A": {address}},
|
||||
FailedTypes: []string{txt},
|
||||
UnknownTypes: []string{txt},
|
||||
Status: "ok",
|
||||
}
|
||||
}
|
||||
|
||||
before := saved(map[string]*state.NameserverRecordState{
|
||||
nsA: answered(withTXT), nsB: txtNotKnown(ip1),
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
after *state.HostnameState
|
||||
}{
|
||||
{
|
||||
"TXT answers",
|
||||
saved(map[string]*state.NameserverRecordState{
|
||||
nsA: answered(withTXT),
|
||||
nsB: answered(map[string][]string{"A": {ip2}, txt: {spf1}}),
|
||||
}),
|
||||
},
|
||||
{
|
||||
"TXT still not known",
|
||||
saved(map[string]*state.NameserverRecordState{
|
||||
nsA: answered(withTXT), nsB: txtNotKnown(ip2),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
want := map[string]string{
|
||||
"Record Change: " + host: "Hostname: " + host +
|
||||
"\nNameserver: " + nsB + "\nType: A\nOld: " + ip1 + "\nNew: " + ip2,
|
||||
"Inconsistency: " + host: "Hostname: " + host +
|
||||
"\nType: A\n" + nsA + ": " + ip1 + "\n" + nsB + ": " + ip2,
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// The hostname change detection uses only the notifier.
|
||||
notifier := &mockNotifier{}
|
||||
w := watcher.NewForTest(nil, nil, nil, nil, nil, notifier)
|
||||
|
||||
w.DetectHostnameChanges(t.Context(), host, before, tt.after)
|
||||
|
||||
notifications := notifier.getNotifications()
|
||||
if len(notifications) != len(want) {
|
||||
t.Fatalf(
|
||||
"sent %d notifications, want %d: %v",
|
||||
len(notifications), len(want), notifications,
|
||||
)
|
||||
}
|
||||
|
||||
for _, n := range notifications {
|
||||
if n.Message != want[n.Title] {
|
||||
t.Errorf(
|
||||
"%s message:\n%s\nwant:\n%s",
|
||||
n.Title, n.Message, want[n.Title],
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
package watcher_test
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sneak.berlin/go/dnswatcher/internal/config"
|
||||
"sneak.berlin/go/dnswatcher/internal/state"
|
||||
"sneak.berlin/go/dnswatcher/internal/watcher"
|
||||
)
|
||||
|
||||
@@ -64,3 +68,100 @@ b.ns.example.net.: 192.0.2.2`,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Every kind of notification about a configured apex domain's own
|
||||
// records names it as a domain.
|
||||
func TestDomainRecordNotificationsNameTheDomain(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
notifier := &mockNotifier{}
|
||||
w := watcher.NewForTest(
|
||||
&config.Config{Domains: []string{domain}},
|
||||
nil, nil, nil, nil, notifier,
|
||||
)
|
||||
|
||||
// nsA's address changes, which also makes it differ from nsC; nsB
|
||||
// fails; nsC answers again; nsD is gone.
|
||||
nsD := "d.ns.example.net."
|
||||
w.DetectHostnameChanges(t.Context(), domain,
|
||||
saved(map[string]*state.NameserverRecordState{
|
||||
nsA: answered(map[string][]string{"A": {ip1}}),
|
||||
nsB: answered(map[string][]string{"A": {ip1}}),
|
||||
nsC: failed(),
|
||||
nsD: answered(map[string][]string{"A": {ip1}}),
|
||||
}),
|
||||
saved(map[string]*state.NameserverRecordState{
|
||||
nsA: answered(map[string][]string{"A": {ip2}}),
|
||||
nsB: failed(),
|
||||
nsC: answered(map[string][]string{"A": {ip1}}),
|
||||
}),
|
||||
)
|
||||
|
||||
// The address at the end of its CNAME chain changes.
|
||||
w.DetectHostnameChanges(
|
||||
t.Context(), domain, cnameState(ip1), cnameState(ip2),
|
||||
)
|
||||
|
||||
// NS Failure is sent for nsB failing and for nsD being gone.
|
||||
want := map[string]int{
|
||||
"Record Change": 1,
|
||||
"Inconsistency": 1,
|
||||
"NS Failure": 2,
|
||||
"NS Recovery": 1,
|
||||
"CNAME Address Change": 1,
|
||||
}
|
||||
|
||||
sent := make(map[string]int)
|
||||
|
||||
for _, n := range notifier.getNotifications() {
|
||||
kind, _, _ := strings.Cut(n.Title, ":")
|
||||
sent[kind]++
|
||||
|
||||
if !strings.HasPrefix(n.Message, "Domain: "+domain+"\n") {
|
||||
t.Errorf("%s message does not name the domain:\n%s",
|
||||
n.Title, n.Message)
|
||||
}
|
||||
}
|
||||
|
||||
if !maps.Equal(sent, want) {
|
||||
t.Errorf("sent %v, want %v", sent, want)
|
||||
}
|
||||
}
|
||||
|
||||
// The startup notification counts the configured domains and hostnames,
|
||||
// although the state's hostnames also hold the apex domain's own
|
||||
// records. Nothing is looked up: the watcher has no resolver.
|
||||
func TestStartupNotificationCountsConfiguredNames(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cfg := defaultTestConfig(t)
|
||||
cfg.SendTestNotification = true
|
||||
cfg.Domains = []string{domain}
|
||||
cfg.Hostnames = []string{host}
|
||||
|
||||
deps := newTestDeps(t, cfg)
|
||||
w := watcher.NewForTest(cfg, deps.state, nil, nil, nil, deps.notifier)
|
||||
|
||||
// The state a check of both names saves.
|
||||
deps.state.SetDomainState(domain, &state.DomainState{
|
||||
Nameservers: []string{nsA},
|
||||
})
|
||||
|
||||
for _, name := range []string{domain, host} {
|
||||
deps.state.SetHostnameState(name, saved(
|
||||
map[string]*state.NameserverRecordState{
|
||||
nsA: answered(map[string][]string{"A": {ip1}}),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
w.MaybeSendTestNotification(t.Context())
|
||||
|
||||
notifications := deps.notifier.getNotifications()
|
||||
|
||||
counts := "\nMonitoring 1 domain(s) and 1 hostname(s).\n"
|
||||
if len(notifications) != 1 ||
|
||||
!strings.Contains(notifications[0].Message, counts) {
|
||||
t.Errorf("sent %v, want one message with %q", notifications, counts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,7 +201,7 @@ func TestNameserverThatNeverAnswers(t *testing.T) {
|
||||
}
|
||||
|
||||
hs := watcher.BuildHostnameState(
|
||||
map[string]*resolver.NameserverResponse{nsA: resp}, time.Now(),
|
||||
map[string]*resolver.NameserverResponse{nsA: resp}, nil, time.Now(),
|
||||
)
|
||||
|
||||
got := hs.RecordsByNameserver[nsA]
|
||||
@@ -256,7 +256,7 @@ func TestNameserverThatAnswersNXDOMAIN(t *testing.T) {
|
||||
}
|
||||
|
||||
hs := watcher.BuildHostnameState(
|
||||
map[string]*resolver.NameserverResponse{ns: resp}, time.Now(),
|
||||
map[string]*resolver.NameserverResponse{ns: resp}, nil, time.Now(),
|
||||
)
|
||||
|
||||
got := hs.RecordsByNameserver[ns]
|
||||
@@ -320,7 +320,7 @@ func TestNameserverThatRefuses(t *testing.T) {
|
||||
}
|
||||
|
||||
hs := watcher.BuildHostnameState(
|
||||
map[string]*resolver.NameserverResponse{ns: resp}, time.Now(),
|
||||
map[string]*resolver.NameserverResponse{ns: resp}, nil, time.Now(),
|
||||
)
|
||||
|
||||
got := hs.RecordsByNameserver[ns]
|
||||
|
||||
+131
-36
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -271,8 +272,9 @@ func (w *Watcher) checkDomain(
|
||||
LastChecked: now,
|
||||
})
|
||||
|
||||
// The apex domain's records are also checked as a hostname's, so
|
||||
// that the port and TLS checks find its addresses.
|
||||
// The apex domain's records are also checked and saved as a
|
||||
// hostname's, so that the port and TLS checks find its addresses.
|
||||
// Notifications about them name it as a domain (see nameLine).
|
||||
w.checkHostname(ctx, domain)
|
||||
}
|
||||
|
||||
@@ -400,8 +402,10 @@ func (w *Watcher) checkHostname(
|
||||
return
|
||||
}
|
||||
|
||||
prev, _ := w.state.GetHostnameState(hostname)
|
||||
|
||||
w.updateHostnameState(
|
||||
ctx, hostname, buildHostnameState(results, time.Now().UTC()),
|
||||
ctx, hostname, buildHostnameState(results, prev, time.Now().UTC()),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -431,8 +435,9 @@ func (w *Watcher) updateHostnameState(
|
||||
// the addresses found for all of them are saved, so nameservers that
|
||||
// disagree on the target do not change the result from check to check.
|
||||
// The addresses saved in prev, which may be nil, are kept when none of
|
||||
// the name's nameservers answered, and when a target cannot be
|
||||
// followed, as when no nameserver of a zone in its chain answers.
|
||||
// the name's nameservers answered its queries for A, AAAA and CNAME,
|
||||
// and when a target cannot be followed, as when no nameserver of a zone
|
||||
// in its chain answers.
|
||||
func (w *Watcher) resolveCNAMEAddresses(
|
||||
ctx context.Context,
|
||||
hostname string,
|
||||
@@ -450,7 +455,10 @@ func (w *Watcher) resolveCNAMEAddresses(
|
||||
targets := make(map[string]bool)
|
||||
|
||||
for _, nsState := range current.RecordsByNameserver {
|
||||
if nsState.Status != statusOK {
|
||||
if nsState.Status != statusOK ||
|
||||
slices.Contains(nsState.FailedTypes, "A") ||
|
||||
slices.Contains(nsState.FailedTypes, "AAAA") ||
|
||||
slices.Contains(nsState.FailedTypes, "CNAME") {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -496,11 +504,13 @@ func (w *Watcher) resolveCNAMEAddresses(
|
||||
}
|
||||
|
||||
// buildHostnameState saves each nameserver's response. A nameserver
|
||||
// that answered, even with NXDOMAIN or no records, is saved as ok; one
|
||||
// that timed out or failed is saved as error with the reason, and its
|
||||
// empty record set is not an answer.
|
||||
// that answered, even with NXDOMAIN or no records, is saved as ok, with
|
||||
// the record types whose query failed; one that timed out or failed is
|
||||
// saved as error with the reason, and its empty record set is not an
|
||||
// answer. prev is the hostname's state from the previous check, or nil.
|
||||
func buildHostnameState(
|
||||
results map[string]*resolver.NameserverResponse,
|
||||
prev *state.HostnameState,
|
||||
now time.Time,
|
||||
) *state.HostnameState {
|
||||
hs := &state.HostnameState{
|
||||
@@ -512,7 +522,7 @@ func buildHostnameState(
|
||||
|
||||
for ns, resp := range results {
|
||||
nsState := &state.NameserverRecordState{
|
||||
Records: resp.Records,
|
||||
Records: maps.Clone(resp.Records),
|
||||
Status: statusOK,
|
||||
LastChecked: now,
|
||||
}
|
||||
@@ -521,6 +531,15 @@ func buildHostnameState(
|
||||
resp.Status == resolver.StatusError {
|
||||
nsState.Status = statusError
|
||||
nsState.Error = resp.Error
|
||||
} else {
|
||||
nsState.FailedTypes = resp.FailedTypes
|
||||
|
||||
var prevNS *state.NameserverRecordState
|
||||
if prev != nil {
|
||||
prevNS = prev.RecordsByNameserver[ns]
|
||||
}
|
||||
|
||||
keepFailedTypes(nsState, prevNS)
|
||||
}
|
||||
|
||||
hs.RecordsByNameserver[ns] = nsState
|
||||
@@ -529,6 +548,27 @@ func buildHostnameState(
|
||||
return hs
|
||||
}
|
||||
|
||||
// keepFailedTypes copies into nsState, for each record type in its
|
||||
// FailedTypes, the records prevNS, the nameserver's state from the
|
||||
// previous check, holds for that type, which may be none. When prevNS
|
||||
// does not know them either, because the nameserver was new or failing
|
||||
// then or the type was in its UnknownTypes, the type goes in
|
||||
// nsState.UnknownTypes instead.
|
||||
func keepFailedTypes(nsState, prevNS *state.NameserverRecordState) {
|
||||
for _, rtype := range nsState.FailedTypes {
|
||||
if prevNS == nil || prevNS.Status != statusOK ||
|
||||
slices.Contains(prevNS.UnknownTypes, rtype) {
|
||||
nsState.UnknownTypes = append(nsState.UnknownTypes, rtype)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if records, ok := prevNS.Records[rtype]; ok {
|
||||
nsState.Records[rtype] = records
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Watcher) detectHostnameChanges(
|
||||
ctx context.Context,
|
||||
hostname string,
|
||||
@@ -541,6 +581,17 @@ func (w *Watcher) detectHostnameChanges(
|
||||
w.detectCNAMEAddressChanges(ctx, hostname, prev, current)
|
||||
}
|
||||
|
||||
// nameLine is the line a notification about name's records starts with:
|
||||
// "Domain: " and the name for a configured apex domain, whose own
|
||||
// records are checked as a hostname's are, and "Hostname: " otherwise.
|
||||
func (w *Watcher) nameLine(name string) string {
|
||||
if slices.Contains(w.config.Domains, name) {
|
||||
return "Domain: " + name
|
||||
}
|
||||
|
||||
return "Hostname: " + name
|
||||
}
|
||||
|
||||
// detectCNAMEAddressChanges notifies when the addresses at the end of
|
||||
// hostname's CNAME chain differ from those the previous check saved,
|
||||
// including a change from or to none. When the previous addresses are
|
||||
@@ -557,8 +608,8 @@ func (w *Watcher) detectCNAMEAddressChanges(
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"Hostname: %s\nOld: %s\nNew: %s",
|
||||
hostname,
|
||||
"%s\nOld: %s\nNew: %s",
|
||||
w.nameLine(hostname),
|
||||
strings.Join(old, ", "),
|
||||
strings.Join(cur, ", "),
|
||||
)
|
||||
@@ -573,7 +624,10 @@ func (w *Watcher) detectCNAMEAddressChanges(
|
||||
|
||||
// detectRecordChanges compares each nameserver's records with those of
|
||||
// the previous check. Only answers are compared: a nameserver that
|
||||
// failed on either check has no records to compare.
|
||||
// failed on either check has no records to compare. The records kept
|
||||
// for a record type whose query failed are compared too, but not those
|
||||
// of a type in UnknownTypes on either check, which the message leaves
|
||||
// out as well.
|
||||
func (w *Watcher) detectRecordChanges(
|
||||
ctx context.Context,
|
||||
hostname string,
|
||||
@@ -585,16 +639,20 @@ func (w *Watcher) detectRecordChanges(
|
||||
continue
|
||||
}
|
||||
|
||||
if recordsEqual(prevNS.Records, cur.Records) {
|
||||
unknown := slices.Concat(prevNS.UnknownTypes, cur.UnknownTypes)
|
||||
oldRecords := withoutTypes(prevNS.Records, unknown)
|
||||
newRecords := withoutTypes(cur.Records, unknown)
|
||||
|
||||
if recordsEqual(oldRecords, newRecords) {
|
||||
continue
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"Hostname: %s\nNameserver: %s\n%s",
|
||||
hostname, ns,
|
||||
"%s\nNameserver: %s\n%s",
|
||||
w.nameLine(hostname), ns,
|
||||
recordDifferences(
|
||||
"Old", prevNS.Records,
|
||||
"New", cur.Records,
|
||||
"Old", oldRecords,
|
||||
"New", newRecords,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -618,8 +676,8 @@ func (w *Watcher) detectNSDisappearances(
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"Hostname: %s\nNameserver: %s disappeared",
|
||||
hostname, ns,
|
||||
"%s\nNameserver: %s disappeared",
|
||||
w.nameLine(hostname), ns,
|
||||
)
|
||||
|
||||
w.notify.SendNotification(
|
||||
@@ -648,8 +706,8 @@ func (w *Watcher) detectNSFailures(
|
||||
switch {
|
||||
case prevNS.Status == statusOK && cur.Status == statusError:
|
||||
msg := fmt.Sprintf(
|
||||
"Hostname: %s\nNameserver: %s\nError: %s",
|
||||
hostname, ns, cur.Error,
|
||||
"%s\nNameserver: %s\nError: %s",
|
||||
w.nameLine(hostname), ns, cur.Error,
|
||||
)
|
||||
|
||||
w.notify.SendNotification(
|
||||
@@ -660,8 +718,8 @@ func (w *Watcher) detectNSFailures(
|
||||
)
|
||||
case prevNS.Status == statusError && cur.Status == statusOK:
|
||||
msg := fmt.Sprintf(
|
||||
"Hostname: %s\nNameserver: %s recovered",
|
||||
hostname, ns,
|
||||
"%s\nNameserver: %s recovered",
|
||||
w.nameLine(hostname), ns,
|
||||
)
|
||||
|
||||
w.notify.SendNotification(
|
||||
@@ -681,13 +739,19 @@ func (w *Watcher) detectInconsistencies(
|
||||
) {
|
||||
for _, pair := range newlyDisagreeingPairs(prev, current) {
|
||||
ns1, ns2 := pair[0], pair[1]
|
||||
state1 := current.RecordsByNameserver[ns1]
|
||||
state2 := current.RecordsByNameserver[ns2]
|
||||
|
||||
// The record types left out of the comparison are left out of
|
||||
// the message too.
|
||||
failed := slices.Concat(state1.FailedTypes, state2.FailedTypes)
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"Hostname: %s\n%s",
|
||||
hostname,
|
||||
"%s\n%s",
|
||||
w.nameLine(hostname),
|
||||
recordDifferences(
|
||||
ns1, current.RecordsByNameserver[ns1].Records,
|
||||
ns2, current.RecordsByNameserver[ns2].Records,
|
||||
ns1, withoutTypes(state1.Records, failed),
|
||||
ns2, withoutTypes(state2.Records, failed),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -705,7 +769,9 @@ func (w *Watcher) detectInconsistencies(
|
||||
// except pairs where both nameservers answered in prev and already
|
||||
// differed there. A nameserver missing from prev, or that failed there,
|
||||
// is paired with every nameserver it differs from. A nameserver that
|
||||
// failed in current has no records to compare and is in no pair.
|
||||
// failed in current has no records to compare and is in no pair. In
|
||||
// both checks, a record type whose query failed at either nameserver is
|
||||
// not compared.
|
||||
func newlyDisagreeingPairs(
|
||||
prev, current *state.HostnameState,
|
||||
) [][2]string {
|
||||
@@ -722,9 +788,9 @@ func newlyDisagreeingPairs(
|
||||
|
||||
for i, ns1 := range nameservers {
|
||||
for _, ns2 := range nameservers[i+1:] {
|
||||
if recordsEqual(
|
||||
current.RecordsByNameserver[ns1].Records,
|
||||
current.RecordsByNameserver[ns2].Records,
|
||||
if nameserversAgree(
|
||||
current.RecordsByNameserver[ns1],
|
||||
current.RecordsByNameserver[ns2],
|
||||
) {
|
||||
continue
|
||||
}
|
||||
@@ -734,7 +800,7 @@ func newlyDisagreeingPairs(
|
||||
|
||||
if ok1 && ok2 &&
|
||||
prev1.Status == statusOK && prev2.Status == statusOK &&
|
||||
!recordsEqual(prev1.Records, prev2.Records) {
|
||||
!nameserversAgree(prev1, prev2) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -1180,7 +1246,9 @@ func (w *Watcher) saveState() {
|
||||
// after the first full scan completes, if SEND_TEST_NOTIFICATION
|
||||
// is enabled. The message is informational, not an error or anomaly
|
||||
// alert. It is written before it reaches any endpoint, so it claims
|
||||
// nothing about whether the endpoints work.
|
||||
// nothing about whether the endpoints work. Domains and hostnames are
|
||||
// counted from the configuration: the state's hostnames also hold each
|
||||
// apex domain's own records.
|
||||
func (w *Watcher) maybeSendTestNotification(ctx context.Context) {
|
||||
if !w.config.SendTestNotification {
|
||||
return
|
||||
@@ -1194,8 +1262,8 @@ func (w *Watcher) maybeSendTestNotification(ctx context.Context) {
|
||||
"Tracking %d port endpoint(s) and %d TLS certificate(s).\n"+
|
||||
"This is a test notification, sent to every configured "+
|
||||
"notification endpoint.",
|
||||
len(snap.Domains),
|
||||
len(snap.Hostnames),
|
||||
len(w.config.Domains),
|
||||
len(w.config.Hostnames),
|
||||
len(snap.Ports),
|
||||
len(snap.Certificates),
|
||||
)
|
||||
@@ -1221,6 +1289,33 @@ func toSet(items []string) map[string]bool {
|
||||
return set
|
||||
}
|
||||
|
||||
// nameserversAgree reports whether two nameservers' states from the same
|
||||
// check hold the same records, leaving out the record types either lists
|
||||
// in FailedTypes: the records held for those are kept from an earlier
|
||||
// check, or not known.
|
||||
func nameserversAgree(a, b *state.NameserverRecordState) bool {
|
||||
failed := slices.Concat(a.FailedTypes, b.FailedTypes)
|
||||
|
||||
return recordsEqual(
|
||||
withoutTypes(a.Records, failed), withoutTypes(b.Records, failed),
|
||||
)
|
||||
}
|
||||
|
||||
// withoutTypes returns a copy of records without the record types in
|
||||
// types.
|
||||
func withoutTypes(
|
||||
records map[string][]string,
|
||||
types []string,
|
||||
) map[string][]string {
|
||||
records = maps.Clone(records)
|
||||
|
||||
for _, rtype := range types {
|
||||
delete(records, rtype)
|
||||
}
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
func recordsEqual(
|
||||
a, b map[string][]string,
|
||||
) bool {
|
||||
|
||||
Reference in New Issue
Block a user