config: watch a target listed twice only once (closes #207)
check / check (push) Failing after 28s
check / check (push) Failing after 28s
ClassifyTargets kept a name every time it appeared in DNSWATCHER_TARGETS, so example.com,Example.com. put example.com in the domain list twice and every check looked it up, checked its ports and its certificates twice, sending two expiry warnings per TLS check. It now skips a name it has already kept, comparing after the lower-casing and trailing-dot removal it already did; the list keeps the order of first appearance. Model: opus-5-5
This commit is contained in:
@@ -57,17 +57,22 @@ func ClassifyDNSName(name string) (DNSNameType, error) {
|
||||
|
||||
// ClassifyTargets splits a list of DNS names into apex domains and
|
||||
// hostnames using the Public Suffix List. It returns an error if any
|
||||
// name cannot be classified.
|
||||
// name cannot be classified. A name given more than once, in any letter
|
||||
// case or with a trailing dot, is kept once.
|
||||
func ClassifyTargets(targets []string) ([]string, []string, error) {
|
||||
var domains, hostnames []string
|
||||
|
||||
seen := make(map[string]bool)
|
||||
|
||||
for _, t := range targets {
|
||||
normalized := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(t), "."))
|
||||
|
||||
if normalized == "" {
|
||||
if normalized == "" || seen[normalized] {
|
||||
continue
|
||||
}
|
||||
|
||||
seen[normalized] = true
|
||||
|
||||
typ, classErr := ClassifyDNSName(normalized)
|
||||
if classErr != nil {
|
||||
return nil, nil, classErr
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"sneak.berlin/go/dnswatcher/internal/config"
|
||||
@@ -93,6 +94,29 @@ func TestClassifyTargets(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyTargetsKeepsEachNameOnce(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
domains, hostnames, err := config.ClassifyTargets([]string{
|
||||
"example.com",
|
||||
"Example.com.",
|
||||
"www.example.com",
|
||||
"EXAMPLE.COM",
|
||||
"WWW.Example.com.",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if !slices.Equal(domains, []string{"example.com"}) {
|
||||
t.Errorf("domains = %v, want [example.com]", domains)
|
||||
}
|
||||
|
||||
if !slices.Equal(hostnames, []string{"www.example.com"}) {
|
||||
t.Errorf("hostnames = %v, want [www.example.com]", hostnames)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyTargetsRejectsPublicSuffix(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user