refactor(api): use audit operation constants and the shared zone not found error in zone handlers
- refactor(api): use audit operation constants and the shared zone not found error in zone handlers - fix(api): return the stored content of rows with check errors - fix(api): return warnings from record deletes, zone creation and import apply - fix(api): allow repairing rows stored with a non canonical zone and invalid SOA sets - fix(check): accept JSON column formatting and an absent SOA serial as canonical content - fix(ui): match audit diff items by identity instead of array index - fix(ui): count the SOA change once, report import warnings and check the request size - fix(ui): edit the selected SOA row, keep the server order, show error rows raw and report write warnings - test(check): cover triggering and clean pairs with exact codes and record ids - test(records): cover SRV port, SOA timer and CAA value boundaries - test(cli): check the exact check output and the exit code of warning-only zones - refactor(import): order current rows with cmp.Compare - test(check): cover malformed stored content and a mailbox stored as an address - test(api): cover zone write failure paths, ambiguous SOA updates and unchanged record detection
This commit is contained in:
1 parent
53444bec7a
commit
a7ba01fc77
21 files changed
+1315
-170
No files matched your search
+136
@@ -4,6 +4,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"database/sql"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -111,3 +112,138 @@ func TestCheckMixedBrokenRows(t *testing.T) {
|
||||
t.Fatalf("reported problems for good rows: %+v", problems)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckCanonicalContentStorage(t *testing.T) {
|
||||
soaJSONColumn := `{"ns": "ns.example.net.", "MBox": "hostmaster.example.org.", "retry": 600, "expire": 604800, "minttl": 300, "serial": 2026111800, "refresh": 3600}`
|
||||
for _, tc := range []struct {
|
||||
name, recordType, content string
|
||||
codes []string
|
||||
}{
|
||||
{"text canonical", "A", `{"ip":"192.0.2.1"}`, nil},
|
||||
{"json column spacing", "A", `{"ip": "192.0.2.1"}`, nil},
|
||||
{"text whitespace", "A", "{ \"ip\" :\n\"192.0.2.1\" }", nil},
|
||||
{"extra key", "A", `{"ip": "192.0.2.1", "x": 1}`, []string{"content_not_canonical"}},
|
||||
{"duplicate key", "A", `{"ip":"192.0.2.1","ip":"192.0.2.1"}`, []string{"content_not_canonical"}},
|
||||
{"json column MX", "MX", `{"host": "mx.example.org.", "preference": 10}`, []string{"target_no_address"}},
|
||||
{"reordered MX", "MX", `{"preference":10,"host":"mx.example.org."}`, []string{"content_not_canonical", "target_no_address"}},
|
||||
{"json column SRV", "SRV", `{"port": 443, "target": ".", "weight": 5, "priority": 10}`, nil},
|
||||
{"text canonical SOA", "SOA", modelSOAContent, nil},
|
||||
{"json column SOA", "SOA", soaJSONColumn, nil},
|
||||
{"text SOA in key length order", "SOA", strings.ReplaceAll(soaJSONColumn, ": ", ":"), nil},
|
||||
{"partly reordered SOA", "SOA", `{"MBox":"hostmaster.example.org.","ns":"ns.example.net.","refresh":3600,"retry":600,"expire":604800,"minttl":300,"serial":2026111800}`, []string{"content_not_canonical"}},
|
||||
{"lowercase mbox key", "SOA", strings.Replace(modelSOAContent, `"MBox"`, `"mbox"`, 1), []string{"content_not_canonical"}},
|
||||
{"dynamic SOA without serial", "SOA", strings.Replace(modelSOAContent, `,"serial":2026111800`, "", 1), []string{"soa_dynamic_serial"}},
|
||||
{"dynamic SOA with serial zero", "SOA", strings.Replace(modelSOAContent, `2026111800`, "0", 1), []string{"soa_dynamic_serial"}},
|
||||
{"json column dynamic SOA", "SOA", strings.Replace(soaJSONColumn, `"serial": 2026111800, `, "", 1), []string{"soa_dynamic_serial"}},
|
||||
{"text escaped character", "TXT", `{"text":"a<b"}`, nil},
|
||||
{"json column raw character", "TXT", `{"text": "a<b"}`, nil},
|
||||
{"nested extra key", "A", `{"ip":"192.0.2.1","x":{"y":1}}`, []string{"content_not_canonical"}},
|
||||
{"number spelled differently", "MX", `{"host":"mx.example.org.","preference":10.0}`, []string{"field_invalid"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
name := ""
|
||||
if tc.recordType == "SRV" {
|
||||
name = "_sip._tcp"
|
||||
}
|
||||
row := modelRawRow(9, name, tc.recordType, 3600, tc.content)
|
||||
rows := append(modelBaseZone(), row)
|
||||
if tc.recordType == "SOA" {
|
||||
rows = []recordRow{row, modelBaseZone()[1]}
|
||||
}
|
||||
var codes []string
|
||||
for _, p := range checkZone("example.org.", rows, modelNow) {
|
||||
if p.RecordID == 9 {
|
||||
codes = append(codes, p.Code)
|
||||
}
|
||||
}
|
||||
if !slices.Equal(codes, tc.codes) {
|
||||
t.Fatalf("codes = %v, want %v", codes, tc.codes)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, content := range []string{`[]`, `{"ip":{"nested":1}}`, `{"ip":"x"} {}`, `{"ip"`, `{1:2}`, `{"ip":"x"`} {
|
||||
if _, ok := contentMembers(content); ok {
|
||||
t.Fatalf("contentMembers accepted %q", content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type modelFinding struct {
|
||||
id int64
|
||||
severity string
|
||||
code string
|
||||
}
|
||||
|
||||
func modelFindings(problems []zoneProblem) []modelFinding {
|
||||
var findings []modelFinding
|
||||
for _, p := range problems {
|
||||
findings = append(findings, modelFinding{p.RecordID, p.Severity, p.Code})
|
||||
}
|
||||
slices.SortFunc(findings, func(a, b modelFinding) int {
|
||||
return cmp.Or(cmp.Compare(a.id, b.id), strings.Compare(a.severity, b.severity), strings.Compare(a.code, b.code))
|
||||
})
|
||||
return findings
|
||||
}
|
||||
|
||||
func TestCheckZonePairs(t *testing.T) {
|
||||
soa := func(content string) []recordRow {
|
||||
return []recordRow{modelRawRow(1, "", "SOA", 3600, content), modelRawRow(2, "", "NS", 3600, `{"host":"ns.example.net."}`)}
|
||||
}
|
||||
with := func(extra ...recordRow) []recordRow { return append(modelBaseZone(), extra...) }
|
||||
txt := func(size int) string { return `{"text":"` + strings.Repeat("x", size) + `"}` }
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
triggered []recordRow
|
||||
want []modelFinding
|
||||
control []recordRow
|
||||
}{
|
||||
{"MX priority key", with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.net.","priority":10}`)),
|
||||
[]modelFinding{{3, "error", "field_invalid"}, {3, "error", "mx_priority_key"}},
|
||||
with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.net.","preference":10}`))},
|
||||
{"relative NS host", with(modelRawRow(3, "", "NS", 3600, `{"host":"ns2.example.net"}`)),
|
||||
[]modelFinding{{3, "error", "name_not_fqdn"}, {3, "error", "pack_failed"}, {3, "warning", "content_not_canonical"}},
|
||||
with(modelRawRow(3, "", "NS", 3600, `{"host":"ns2.example.net."}`))},
|
||||
{"relative MX host", with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.net","preference":10}`)),
|
||||
[]modelFinding{{3, "error", "name_not_fqdn"}, {3, "error", "pack_failed"}, {3, "warning", "content_not_canonical"}},
|
||||
with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.net.","preference":10}`))},
|
||||
{"relative SRV target", with(modelRawRow(3, "_sip._tcp", "SRV", 3600, `{"priority":1,"weight":1,"port":5060,"target":"sip.example.net"}`)),
|
||||
[]modelFinding{{3, "error", "name_not_fqdn"}, {3, "error", "pack_failed"}, {3, "warning", "content_not_canonical"}},
|
||||
with(modelRawRow(3, "_sip._tcp", "SRV", 3600, `{"priority":1,"weight":1,"port":5060,"target":"sip.example.net."}`))},
|
||||
{"relative SOA ns", soa(strings.Replace(modelSOAContent, "ns.example.net.", "ns.example.net", 1)),
|
||||
[]modelFinding{{1, "error", "name_not_fqdn"}, {1, "error", "pack_failed"}, {1, "warning", "content_not_canonical"}},
|
||||
soa(modelSOAContent)},
|
||||
{"relative SOA MBox", soa(strings.Replace(modelSOAContent, "hostmaster.example.org.", "hostmaster.example.org", 1)),
|
||||
[]modelFinding{{1, "error", "name_not_fqdn"}, {1, "error", "pack_failed"}, {1, "warning", "content_not_canonical"}},
|
||||
soa(modelSOAContent)},
|
||||
{"SOA mailbox stored as an address", soa(strings.Replace(modelSOAContent, "hostmaster.example.org.", "hostmaster@example.org.", 1)),
|
||||
[]modelFinding{{1, "error", "field_invalid"}},
|
||||
soa(modelSOAContent)},
|
||||
{"TXT of 256 bytes", with(modelRawRow(3, "text", "TXT", 3600, txt(256))),
|
||||
[]modelFinding{{3, "warning", "txt_split"}},
|
||||
with(modelRawRow(3, "text", "TXT", 3600, txt(255)))},
|
||||
{"different TTL in an RRset", with(modelRawRow(3, "www", "A", 3600, `{"ip":"192.0.2.1"}`), modelRawRow(4, "www", "A", 7200, `{"ip":"192.0.2.2"}`)),
|
||||
[]modelFinding{{3, "warning", "rrset_ttl_mismatch"}, {4, "warning", "rrset_ttl_mismatch"}},
|
||||
with(modelRawRow(3, "www", "A", 3600, `{"ip":"192.0.2.1"}`), modelRawRow(4, "www", "A", 3600, `{"ip":"192.0.2.2"}`))},
|
||||
{"same canonical identity", with(modelRawRow(3, "www", "A", 3600, `{"ip":"192.0.2.1"}`), modelRawRow(4, "www", "A", 3600, `{"ip": "192.0.2.1"}`)),
|
||||
[]modelFinding{{3, "warning", "duplicate"}, {4, "warning", "duplicate"}},
|
||||
with(modelRawRow(3, "www", "A", 3600, `{"ip":"192.0.2.1"}`), modelRawRow(4, "www", "A", 3600, `{"ip": "192.0.2.10"}`))},
|
||||
{"below a zone cut", with(modelRawRow(3, "sub", "NS", 3600, `{"host":"ns.example.net."}`), modelRawRow(4, "www.sub", "A", 3600, `{"ip":"192.0.2.1"}`)),
|
||||
[]modelFinding{{4, "warning", "below_zone_cut"}},
|
||||
with(modelRawRow(3, "sub", "NS", 3600, `{"host":"ns.example.net."}`), modelRawRow(4, "sub", "A", 3600, `{"ip":"192.0.2.1"}`))},
|
||||
{"dynamic SOA serial", soa(strings.Replace(modelSOAContent, `,"serial":2026111800`, "", 1)),
|
||||
[]modelFinding{{1, "warning", "soa_dynamic_serial"}},
|
||||
soa(modelSOAContent)},
|
||||
{"in-zone target without address", with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.org.","preference":10}`)),
|
||||
[]modelFinding{{3, "warning", "target_no_address"}},
|
||||
with(modelRawRow(3, "", "MX", 3600, `{"host":"mail.example.org.","preference":10}`), modelRawRow(4, "mail", "AAAA", 3600, `{"ip":"2001:db8::1"}`))},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := modelFindings(checkZone("example.org.", tc.triggered, modelNow)); !slices.Equal(got, tc.want) {
|
||||
t.Errorf("triggered: got %v, want %v", got, tc.want)
|
||||
}
|
||||
if got := modelFindings(checkZone("example.org.", tc.control, modelNow)); len(got) != 0 {
|
||||
t.Errorf("control: got %v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user