watcher: Port Change notifications list domains apart from hostnames (closes #248)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
A Port Change notification's `Hosts:` line listed a port's apex domains and hostnames together. It now has a `Domains:` line and a `Hostnames:` line, and leaves out one that would name nothing. A name is a domain when it is a configured domain, the rule record notifications already use to start `Domain:` or `Hostname:`; that rule is now one method, isDomain, which both use. The port entries saved in the state are unchanged. README describes the new lines. Model: opus-5-5
This commit was merged in pull request #249.
This commit is contained in:
@@ -165,3 +165,51 @@ func TestStartupNotificationCountsConfiguredNames(t *testing.T) {
|
||||
t.Errorf("sent %v, want one message with %q", notifications, counts)
|
||||
}
|
||||
}
|
||||
|
||||
// A Port Change notification lists the configured apex domain and the
|
||||
// hostname that resolve to the port's address on separate lines. The
|
||||
// port checks read the saved hostname state and look nothing up, so the
|
||||
// watcher has no resolver.
|
||||
func TestPortChangeListsDomainsApartFromHostnames(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cfg := defaultTestConfig(t)
|
||||
cfg.Domains = []string{domain}
|
||||
cfg.Hostnames = []string{host}
|
||||
|
||||
deps := newTestDeps(t, cfg)
|
||||
w := watcher.NewForTest(
|
||||
cfg, deps.state, nil,
|
||||
deps.portChecker, deps.tlsChecker, deps.notifier,
|
||||
)
|
||||
w.SetFirstRun(false)
|
||||
|
||||
// Both names resolve to ip1, whose port 443 the previous check
|
||||
// found open. It is closed now.
|
||||
for _, name := range []string{domain, host} {
|
||||
deps.state.SetHostnameState(name, saved(
|
||||
map[string]*state.NameserverRecordState{
|
||||
nsA: answered(map[string][]string{"A": {ip1}}),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
key := ip1 + ":443"
|
||||
deps.state.SetPortState(key, &state.PortState{
|
||||
Open: true, Hostnames: []string{domain, host},
|
||||
})
|
||||
deps.portChecker.closed = true
|
||||
|
||||
w.CheckAllPorts(t.Context())
|
||||
|
||||
title := "Port Change: " + key
|
||||
want := `Domains: example.net
|
||||
Hostnames: www.example.net
|
||||
Address: 192.0.2.1:443
|
||||
Port now closed`
|
||||
|
||||
got := deps.notifier.getNotifications()
|
||||
if len(got) != 1 || got[0].Title != title || got[0].Message != want {
|
||||
t.Errorf("sent %v, want one %q with message:\n%s", got, title, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -542,17 +542,50 @@ func (w *Watcher) detectHostnameChanges(
|
||||
w.detectCNAMEAddressChanges(ctx, hostname, prev, current)
|
||||
}
|
||||
|
||||
// isDomain reports whether name is a configured apex domain, whose own
|
||||
// records are checked and saved as a hostname's are.
|
||||
func (w *Watcher) isDomain(name string) bool {
|
||||
return slices.Contains(w.config.Domains, name)
|
||||
}
|
||||
|
||||
// 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.
|
||||
// "Domain: " and the name for a configured apex domain, and
|
||||
// "Hostname: " otherwise.
|
||||
func (w *Watcher) nameLine(name string) string {
|
||||
if slices.Contains(w.config.Domains, name) {
|
||||
if w.isDomain(name) {
|
||||
return "Domain: " + name
|
||||
}
|
||||
|
||||
return "Hostname: " + name
|
||||
}
|
||||
|
||||
// portNameLines lists the names that resolve to a port's address, the
|
||||
// configured apex domains on one line and the hostnames on the next,
|
||||
// leaving out a line that would name nothing.
|
||||
func (w *Watcher) portNameLines(names []string) string {
|
||||
var domains, hostnames []string
|
||||
|
||||
for _, name := range names {
|
||||
if w.isDomain(name) {
|
||||
domains = append(domains, name)
|
||||
} else {
|
||||
hostnames = append(hostnames, name)
|
||||
}
|
||||
}
|
||||
|
||||
var lines []string
|
||||
|
||||
if len(domains) > 0 {
|
||||
lines = append(lines, "Domains: "+strings.Join(domains, ", "))
|
||||
}
|
||||
|
||||
if len(hostnames) > 0 {
|
||||
lines = append(lines, "Hostnames: "+strings.Join(hostnames, ", "))
|
||||
}
|
||||
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
// 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
|
||||
@@ -952,8 +985,8 @@ func (w *Watcher) checkSinglePort(
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"Hosts: %s\nAddress: %s\nPort now %s",
|
||||
strings.Join(hostnames, ", "), key, stateStr,
|
||||
"%s\nAddress: %s\nPort now %s",
|
||||
w.portNameLines(hostnames), key, stateStr,
|
||||
)
|
||||
|
||||
w.notify.SendNotification(
|
||||
|
||||
Reference in New Issue
Block a user