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
+248
-8
@@ -20,6 +20,47 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestZoneAPIRecordViewErrorContent(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
row recordRow
|
||||
wantRaw bool
|
||||
wantData bool
|
||||
codes []string
|
||||
}{
|
||||
{"valid", modelRawRow(1, "www", "A", 3600, `{"ip":"192.0.2.1"}`), false, true, nil},
|
||||
{"invalid address", modelRawRow(1, "www", "A", 3600, `{"ip":"invalid"}`), true, true, []string{"field_invalid"}},
|
||||
{"malformed", modelRawRow(2, "www", "A", 3600, `{`), true, false, []string{"content_json"}},
|
||||
{"null", recordRow{ID: 3, Zone: "example.org.", Name: "www", RecordType: "A", TTL: sql.NullInt64{Int64: 3600, Valid: true}}, false, false, []string{"content_null"}},
|
||||
{"unknown", modelRawRow(4, "www", "UNKNOWN", 3600, `{}`), true, false, []string{"type_unknown"}},
|
||||
{"warning only", modelRawRow(5, "www", "A", 0, `{"ip":"192.0.2.1"}`), false, true, []string{"ttl_zero"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
view := zoneAPIRecordView(tc.row, checkRecord(tc.row, modelNow))
|
||||
if (view.Raw != nil) != tc.wantRaw || (view.Data != nil) != tc.wantData {
|
||||
t.Fatalf("unexpected view: %+v", view)
|
||||
}
|
||||
if view.Raw != nil && *view.Raw != tc.row.Content.String {
|
||||
t.Fatalf("raw = %q, want the stored content", *view.Raw)
|
||||
}
|
||||
var codes []string
|
||||
for _, p := range view.Problems {
|
||||
codes = append(codes, p.Code)
|
||||
}
|
||||
if !slices.Equal(codes, tc.codes) {
|
||||
t.Fatalf("problems = %v, want %v", codes, tc.codes)
|
||||
}
|
||||
})
|
||||
}
|
||||
rows := append(zoneAPITestRows(t), modelRawRow(9, "bad", "A", 60, `{"ip":"invalid"}`))
|
||||
f := zoneAPITestDBFor(t, rows)
|
||||
server, _ := f.server()
|
||||
resp := zoneAPITestStatus(t, t.Context(), zoneAPITestRequest(server, f.actor, "record_list", zoneAPITestName, "", "", ""), 200)
|
||||
if body := zoneAPITestJSON(t, resp.Body); !strings.Contains(body, `"data":{"ip":"invalid"},"raw":"{\"ip\":\"invalid\"}"`) {
|
||||
t.Fatalf("record list = %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *webHarness) stored(username string) userRow {
|
||||
h.t.Helper()
|
||||
u, err := h.store.UserByUsername(context.Background(), username)
|
||||
@@ -980,6 +1021,8 @@ type zoneAPITestDB struct {
|
||||
auditError error
|
||||
writeError error
|
||||
lastID int64
|
||||
fail func(query string) error
|
||||
hideInsert bool
|
||||
}
|
||||
|
||||
func zoneAPITestDBFor(t *testing.T, rows []recordRow) *zoneAPITestDB {
|
||||
@@ -1006,6 +1049,11 @@ func (f *zoneAPITestDB) server() (*WebServer, *fdbState) {
|
||||
|
||||
func (f *zoneAPITestDB) query(query string, args []any) fdbResponse {
|
||||
has := func(value string) bool { return strings.Contains(query, value) }
|
||||
if f.fail != nil {
|
||||
if err := f.fail(query); err != nil {
|
||||
return fdbResponse{err: err}
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case has("GET_LOCK"):
|
||||
if f.locked {
|
||||
@@ -1026,14 +1074,14 @@ func (f *zoneAPITestDB) query(query string, args []any) fdbResponse {
|
||||
}
|
||||
return fdbResponse{}
|
||||
case has("COUNT(*)") && has("GROUP BY"):
|
||||
if len(f.rows) == 0 || has("WHERE r.") && !f.delegated {
|
||||
if len(f.rows) == 0 || has("d.`user_id`") && !f.delegated {
|
||||
return fdbResponse{}
|
||||
}
|
||||
var rows [][]driver.Value
|
||||
soa := zoneAPIFindSOA(f.rows)
|
||||
content, _ := soa.Content.Value()
|
||||
rows = append(rows, []driver.Value{f.rows[0].Zone, int64(len(f.rows)), content})
|
||||
if !has("WHERE r.") && f.foreign.ID != 0 {
|
||||
if !has("d.`user_id`") && f.foreign.ID != 0 {
|
||||
rows = append(rows, []driver.Value{f.foreign.Zone, int64(1), nil})
|
||||
}
|
||||
return fdbResponse{rows: rows}
|
||||
@@ -1042,10 +1090,10 @@ func (f *zoneAPITestDB) query(query string, args []any) fdbResponse {
|
||||
return fdbResponse{rows: fdbRow(args[0], int64(18), "bob", modelNow, f.actor.ID)}
|
||||
}
|
||||
return fdbResponse{}
|
||||
case has("SELECT `id`") && has("WHERE `zone`"):
|
||||
case has("SELECT `id`") && has(zoneRowsWhere):
|
||||
var rows [][]driver.Value
|
||||
for _, row := range append(slices.Clone(f.rows), f.foreign) {
|
||||
if row.Zone == args[0] {
|
||||
if zoneAPITestKey(row.Zone) == args[0] {
|
||||
rows = append(rows, zoneAPITestValues(row))
|
||||
}
|
||||
}
|
||||
@@ -1059,6 +1107,9 @@ func (f *zoneAPITestDB) query(query string, args []any) fdbResponse {
|
||||
return fdbResponse{lastInsertID: 1, err: f.auditError}
|
||||
case has("INSERT INTO `coredns_records`"):
|
||||
f.lastID++
|
||||
if f.hideInsert {
|
||||
return fdbResponse{lastInsertID: f.lastID}
|
||||
}
|
||||
f.rows = append(f.rows, recordRow{
|
||||
ID: f.lastID, Zone: args[0].(string), Name: args[1].(string), TTL: sql.NullInt64{Int64: args[2].(int64), Valid: true},
|
||||
Content: sql.NullString{String: args[3].(string), Valid: true}, RecordType: args[4].(string),
|
||||
@@ -1074,7 +1125,7 @@ func (f *zoneAPITestDB) query(query string, args []any) fdbResponse {
|
||||
return fdbResponse{rowsAffected: 1, err: f.writeError}
|
||||
case has("DELETE FROM `coredns_records`"):
|
||||
f.rows = slices.DeleteFunc(f.rows, func(row recordRow) bool {
|
||||
return row.ID == args[0] || row.Zone == args[0]
|
||||
return row.ID == args[0] || has(zoneRowsWhere) && zoneAPITestKey(row.Zone) == args[0]
|
||||
})
|
||||
return fdbResponse{rowsAffected: 1, err: f.writeError}
|
||||
case has("DELETE FROM"):
|
||||
@@ -1085,6 +1136,14 @@ func (f *zoneAPITestDB) query(query string, args []any) fdbResponse {
|
||||
}
|
||||
}
|
||||
|
||||
func zoneAPITestKey(zone string) string {
|
||||
zone = strings.ToLower(zone)
|
||||
if !strings.HasSuffix(zone, ".") {
|
||||
zone += "."
|
||||
}
|
||||
return zone
|
||||
}
|
||||
|
||||
func zoneAPITestingSQL(state *fdbState, contains string) []fdbCall {
|
||||
var result []fdbCall
|
||||
for _, call := range state.Calls() {
|
||||
@@ -1180,7 +1239,7 @@ func TestZoneAPIWriteHandlers(t *testing.T) {
|
||||
{"zone_delete", "", "", `{"confirm":"EXAMPLE.ORG"}`, 204, []int64{1, 2, 3, 4}, nil, 0, false},
|
||||
{"record_create", "", "", zoneAPITestRecord, 201, []int64{1}, []int64{1, 5}, 5, true},
|
||||
{"record_update", "3", recordETag(rows[2]), zoneAPITestRecord, 200, []int64{1, 3}, []int64{1, 3}, 3, true},
|
||||
{"record_delete", "3", recordETag(rows[2]), "", 204, []int64{1, 3}, []int64{1}, 3, true},
|
||||
{"record_delete", "3", recordETag(rows[2]), "", 200, []int64{1, 3}, []int64{1}, 3, true},
|
||||
{"record_bulk_delete", "", "", bulk, 200, []int64{1, 3, 4}, []int64{1}, 0, true},
|
||||
{"soa_update", "", recordETag(rows[0]), strings.Replace(zoneAPITestSOA, "3600", "1500", 1), 200, []int64{1}, []int64{1}, 1, true},
|
||||
} {
|
||||
@@ -1219,9 +1278,15 @@ func TestZoneAPIWriteHandlers(t *testing.T) {
|
||||
t.Fatalf("serials = %v", serials)
|
||||
}
|
||||
}
|
||||
if tc.status != 204 && tc.operation != "record_bulk_delete" && resp.Header.Get("ETag") == "" {
|
||||
if tc.status != 204 && tc.operation != "record_bulk_delete" && tc.operation != "record_delete" && resp.Header.Get("ETag") == "" {
|
||||
t.Fatal("missing response ETag")
|
||||
}
|
||||
if body := zoneAPITestJSON(t, resp.Body); tc.status != 204 && !strings.Contains(body, `"warnings":[`) {
|
||||
t.Fatalf("response without warnings: %s", body)
|
||||
}
|
||||
if f.stale && strings.Contains(string(c.Entry.After), `"username"`) {
|
||||
t.Fatalf("delegations in the after snapshot: %s", c.Entry.After)
|
||||
}
|
||||
calls := state.Queries()
|
||||
if !strings.Contains(calls[0], "GET_LOCK") || !strings.HasSuffix(calls[1], "FOR SHARE") ||
|
||||
!strings.HasSuffix(calls[2], "FOR UPDATE") || !strings.Contains(calls[len(calls)-2], "INSERT INTO `coredns_webui_audit`") ||
|
||||
@@ -1465,7 +1530,7 @@ func TestZoneAPIImport(t *testing.T) {
|
||||
}
|
||||
c = zoneAPITestRequest(server, f.actor, "zone_import", zoneAPITestName, "", "", makeBody(content, true))
|
||||
resp = zoneAPITestStatus(t, t.Context(), c, 200)
|
||||
if zoneAPITestJSON(t, resp.Body) != `{"added":1,"deleted":1,"updated":2}` {
|
||||
if zoneAPITestJSON(t, resp.Body) != `{"added":1,"deleted":1,"updated":2,"warnings":[{"line":3,"message":"TXT strings were joined and the plugin will re-split them at 255 bytes"}]}` {
|
||||
t.Fatalf("apply response = %s", zoneAPITestJSON(t, resp.Body))
|
||||
}
|
||||
zoneAPITestAuditIDs(t, c.Entry.Before, []int64{1, 3, 4})
|
||||
@@ -1543,3 +1608,178 @@ func TestZoneAPIReadErrors(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func zoneAPITestSOARecord(name string) string {
|
||||
return `{"name":"` + name + `","type":"SOA","ttl":3600,"data":{"ns":"ns.example.net","MBox":"hostmaster.example.org","refresh":3600,"retry":600,"expire":604800,"minttl":300}}`
|
||||
}
|
||||
|
||||
func zoneAPITestRepair(t *testing.T, f *zoneAPITestDB, operation, id, etag, body string, status int) apiResponse {
|
||||
t.Helper()
|
||||
server, _ := f.server()
|
||||
return zoneAPITestStatus(t, t.Context(), zoneAPITestRequest(server, f.actor, operation, zoneAPITestName, id, etag, body), status)
|
||||
}
|
||||
|
||||
func zoneAPITestPreserved(t *testing.T, f *zoneAPITestDB, original []recordRow, ids ...int64) {
|
||||
t.Helper()
|
||||
byID := zoneAPIRowsByID(f.rows)
|
||||
for _, row := range original {
|
||||
if slices.Contains(ids, row.ID) && byID[row.ID] != row {
|
||||
t.Fatalf("record %d changed: %+v, want %+v", row.ID, byID[row.ID], row)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func zoneAPITestSerialOf(t *testing.T, row recordRow, want uint32) {
|
||||
t.Helper()
|
||||
if serial, _ := storedSOASerial(row); serial != want {
|
||||
t.Fatalf("serial of %d = %d, want %d", row.ID, serial, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestZoneAPIRepairNonCanonicalZone(t *testing.T) {
|
||||
serial := newSOASerial(modelNow)
|
||||
for _, stored := range []string{"example.org", "EXAMPLE.ORG.", "Example.Org"} {
|
||||
t.Run(stored, func(t *testing.T) {
|
||||
rows := zoneAPITestRows(t)
|
||||
rows[3].Zone = stored
|
||||
f := zoneAPITestDBFor(t, rows)
|
||||
f.foreign = recordRow{ID: 90, Zone: "example.org.example.", Name: "x", RecordType: "A"}
|
||||
list := zoneAPITestJSON(t, zoneAPITestRepair(t, f, "record_list", "", "", "", 200).Body)
|
||||
if !strings.Contains(list, `"id":4,"name":"second"`) || strings.Contains(list, `"id":90`) {
|
||||
t.Fatalf("record list = %s", list)
|
||||
}
|
||||
check := zoneAPITestJSON(t, zoneAPITestRepair(t, f, "zone_check", "", "", "", 200).Body)
|
||||
code := "zone_case"
|
||||
if !strings.HasSuffix(stored, ".") {
|
||||
code = "zone_not_fqdn"
|
||||
}
|
||||
if !strings.Contains(check, `"code":"`+code+`","severity":"error"`) || !strings.Contains(check, `"id":4`) {
|
||||
t.Fatalf("check = %s", check)
|
||||
}
|
||||
body := `{"name":"second","type":"A","ttl":60,"data":{"ip":"192.0.2.1"}}`
|
||||
zoneAPITestRepair(t, f, "record_update", "4", recordETag(rows[3]), body, 200)
|
||||
if f.rows[3].Zone != zoneAPITestName || f.rows[3].Content != rows[3].Content || f.rows[3].Name != "second" {
|
||||
t.Fatalf("row was not repaired: %+v", f.rows[3])
|
||||
}
|
||||
zoneAPITestPreserved(t, f, rows, 2, 3)
|
||||
zoneAPITestSerialOf(t, f.rows[0], serial+1)
|
||||
after := zoneAPITestJSON(t, zoneAPITestRepair(t, f, "zone_check", "", "", "", 200).Body)
|
||||
if after != `{"problems":[]}` {
|
||||
t.Fatalf("problems after the repair: %s", after)
|
||||
}
|
||||
})
|
||||
}
|
||||
t.Run("every row", func(t *testing.T) {
|
||||
rows := zoneAPITestRows(t)
|
||||
for i := range rows {
|
||||
rows[i].Zone = "Example.ORG"
|
||||
}
|
||||
f := zoneAPITestDBFor(t, rows)
|
||||
summary := zoneAPITestRepair(t, f, "zone_read", "", "", "", 200)
|
||||
if body := zoneAPITestJSON(t, summary.Body); !strings.Contains(body, `"name":"example.org.","records":4`) {
|
||||
t.Fatalf("summary = %s", body)
|
||||
}
|
||||
zoneAPITestRepair(t, f, "record_delete", "4", recordETag(rows[3]), "", 200)
|
||||
if len(f.rows) != 3 || slices.ContainsFunc(f.rows, func(r recordRow) bool { return r.ID == 4 }) {
|
||||
t.Fatalf("rows after delete: %+v", f.rows)
|
||||
}
|
||||
zoneAPITestRepair(t, f, "zone_delete", "", "", `{"confirm":"example.org"}`, 204)
|
||||
if len(f.rows) != 0 {
|
||||
t.Fatalf("zone delete left %+v", f.rows)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestZoneAPIRepairSOASet(t *testing.T) {
|
||||
serial := newSOASerial(modelNow)
|
||||
t.Run("non-apex SOA through soa_update", func(t *testing.T) {
|
||||
rows := zoneAPITestRows(t)
|
||||
rows[0].Name = "below"
|
||||
f := zoneAPITestDBFor(t, rows)
|
||||
summary := zoneAPITestJSON(t, zoneAPITestRepair(t, f, "zone_read", "", "", "", 200).Body)
|
||||
if !strings.Contains(summary, `"soa":null`) {
|
||||
t.Fatalf("summary = %s", summary)
|
||||
}
|
||||
resp := zoneAPITestRepair(t, f, "soa_update", "", recordETag(rows[0]), zoneAPITestSOA, 200)
|
||||
if f.rows[0].Name != "" || f.rows[0].ID != 1 || resp.Header.Get("ETag") != recordETag(f.rows[0]) ||
|
||||
!strings.Contains(zoneAPITestJSON(t, resp.Body), `"soa":{"ns":"ns.example.net."`) {
|
||||
t.Fatalf("SOA not moved: %+v, %+v", f.rows[0], resp)
|
||||
}
|
||||
zoneAPITestSerialOf(t, f.rows[0], serial+1)
|
||||
zoneAPITestPreserved(t, f, rows, 2, 3, 4)
|
||||
})
|
||||
t.Run("non-apex SOA through record_update", func(t *testing.T) {
|
||||
rows := zoneAPITestRows(t)
|
||||
rows[0].Name = "below"
|
||||
f := zoneAPITestDBFor(t, rows)
|
||||
zoneAPITestRepair(t, f, "record_update", "1", recordETag(rows[0]), zoneAPITestSOARecord("below"), 422)
|
||||
zoneAPITestRepair(t, f, "record_update", "1", recordETag(rows[0]), zoneAPITestSOARecord("@"), 200)
|
||||
if f.rows[0].Name != "" {
|
||||
t.Fatalf("SOA not moved: %+v", f.rows[0])
|
||||
}
|
||||
zoneAPITestSerialOf(t, f.rows[0], serial+1)
|
||||
zoneAPITestPreserved(t, f, rows, 2, 3, 4)
|
||||
})
|
||||
t.Run("duplicate apex SOA", func(t *testing.T) {
|
||||
rows := zoneAPITestRows(t)
|
||||
extra := rows[0]
|
||||
extra.ID = 5
|
||||
extra.Content.String = strings.Replace(extra.Content.String, "ns.example.net.", "other.example.net.", 1)
|
||||
rows = append(rows, extra)
|
||||
f := zoneAPITestDBFor(t, rows)
|
||||
for _, tc := range []struct{ operation, id, etag, body string }{
|
||||
{"record_create", "", "", zoneAPITestRecord},
|
||||
{"record_delete", "3", recordETag(rows[2]), ""},
|
||||
{"soa_update", "", recordETag(rows[0]), strings.Replace(zoneAPITestSOA, "3600", "1500", 1)},
|
||||
{"record_bulk_delete", "", "", `{"records":[{"id":1,"etag":` + zoneAPITestJSON(t, recordETag(rows[0])) + `},{"id":5,"etag":` + zoneAPITestJSON(t, recordETag(extra)) + `}]}`},
|
||||
} {
|
||||
server, _ := f.server()
|
||||
c := zoneAPITestRequest(server, f.actor, tc.operation, zoneAPITestName, tc.id, tc.etag, tc.body)
|
||||
if _, err := zoneAPITestCall(t.Context(), c); err == nil || err.(*apiError).Status != 422 {
|
||||
t.Fatalf("%s in a zone with two SOA records: %v", tc.operation, err)
|
||||
}
|
||||
}
|
||||
resp := zoneAPITestRepair(t, f, "record_delete", "5", recordETag(extra), "", 200)
|
||||
if len(f.rows) != 4 || zoneAPITestJSON(t, resp.Body) != `{"warnings":[]}` {
|
||||
t.Fatalf("duplicate SOA not deleted: %+v, %+v", f.rows, resp)
|
||||
}
|
||||
zoneAPITestSerialOf(t, f.rows[0], serial+1)
|
||||
zoneAPITestPreserved(t, f, rows, 2, 3, 4)
|
||||
zoneAPITestRepair(t, f, "record_delete", "1", recordETag(f.rows[0]), "", 422)
|
||||
})
|
||||
t.Run("two non-apex SOA", func(t *testing.T) {
|
||||
rows := zoneAPITestRows(t)
|
||||
rows[0].Name = "one"
|
||||
extra := rows[0]
|
||||
extra.ID, extra.Name = 5, "two"
|
||||
rows = append(rows, extra)
|
||||
f := zoneAPITestDBFor(t, rows)
|
||||
zoneAPITestRepair(t, f, "record_delete", "5", recordETag(extra), "", 200)
|
||||
zoneAPITestSerialOf(t, f.rows[0], serial+1)
|
||||
zoneAPITestRepair(t, f, "record_delete", "1", recordETag(f.rows[0]), "", 422)
|
||||
zoneAPITestRepair(t, f, "soa_update", "", recordETag(f.rows[0]), zoneAPITestSOA, 200)
|
||||
if f.rows[0].Name != "" || len(f.rows) != 4 {
|
||||
t.Fatalf("SOA set not repaired: %+v", f.rows)
|
||||
}
|
||||
zoneAPITestSerialOf(t, f.rows[0], serial+2)
|
||||
zoneAPITestPreserved(t, f, rows, 2, 3, 4)
|
||||
})
|
||||
t.Run("apex and non-apex SOA selected by ETag", func(t *testing.T) {
|
||||
rows := zoneAPITestRows(t)
|
||||
extra := rows[0]
|
||||
extra.ID, extra.Name = 5, "below"
|
||||
rows = append(rows, extra)
|
||||
f := zoneAPITestDBFor(t, rows)
|
||||
server, _ := f.server()
|
||||
body := strings.NewReplacer(`"ttl":3600`, `"ttl":1200`, "ns.example.net", "other.example.net").Replace(zoneAPITestSOA)
|
||||
c := zoneAPITestRequest(server, f.actor, "soa_update", zoneAPITestName, "", recordETag(extra), body)
|
||||
resp := zoneAPITestStatus(t, t.Context(), c, 200)
|
||||
if c.Entry.RecordID.Int64 != 5 || f.rows[4].Name != "" || f.rows[4].TTL.Int64 != 1200 || f.rows[0] != rows[0] {
|
||||
t.Fatalf("selected SOA not updated: %+v, %+v", f.rows, resp)
|
||||
}
|
||||
zoneAPITestSerialOf(t, f.rows[4], serial)
|
||||
zoneAPITestRepair(t, f, "record_delete", "1", recordETag(rows[0]), "", 200)
|
||||
zoneAPITestSerialOf(t, f.rows[3], serial+1)
|
||||
zoneAPITestPreserved(t, f, rows, 2, 3, 4)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user