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:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 53444bec7a
commit a7ba01fc77
21 files changed
+1315 -170

No files matched your search

+19 -10
View File
@@ -27,19 +27,28 @@ func newRecordStore(q dbtx, t tableNames) recordStore {
const recordColumns = "`id`, `zone`, `name`, `ttl`, `content`, `record_type`"
const zoneSummarySelect = "SELECT r.`zone`, COUNT(*), " +
"(SELECT s.`content` FROM {records} s WHERE s.`zone` = r.`zone` AND CAST(s.`record_type` AS BINARY) = 'SOA' " +
"ORDER BY s.`name` <> '', s.`id` LIMIT 1) FROM {records} r "
func recordZoneKey(column string) string {
return "CAST(CASE WHEN " + column + " LIKE '%.' THEN LOWER(" + column + ") ELSE CONCAT(LOWER(" + column + "), '.') END AS BINARY)"
}
var zoneRowsWhere = " WHERE " + recordZoneKey("`zone`") + " = ?"
func zoneSummarySelect(filter string) string {
return "SELECT k.`zone_key`, k.`record_count`, (SELECT s.`content` FROM {records} s WHERE " + recordZoneKey("s.`zone`") +
" = k.`zone_key` AND CAST(s.`record_type` AS BINARY) = 'SOA' ORDER BY s.`name` <> '', s.`id` LIMIT 1) FROM (SELECT " +
recordZoneKey("r.`zone`") + " AS `zone_key`, COUNT(*) AS `record_count` FROM {records} r" + filter +
" GROUP BY `zone_key`) k ORDER BY k.`zone_key`"
}
func (s recordStore) ListZones(ctx context.Context) ([]zoneSummary, error) {
defer traceStore(ctx, "recordStore.ListZones")()
return s.queryZones(ctx, s.t.sql(zoneSummarySelect+"GROUP BY r.`zone` ORDER BY r.`zone`"))
return s.queryZones(ctx, s.t.sql(zoneSummarySelect("")))
}
func (s recordStore) ListZonesOfUser(ctx context.Context, userID int64) ([]zoneSummary, error) {
defer traceStore(ctx, "recordStore.ListZonesOfUser")()
return s.queryZones(ctx, s.t.sql(zoneSummarySelect+
"WHERE r.`zone` IN (SELECT d.`zone` FROM {delegations} d WHERE d.`user_id` = ?) GROUP BY r.`zone` ORDER BY r.`zone`"), userID)
return s.queryZones(ctx, s.t.sql(zoneSummarySelect(" WHERE "+recordZoneKey("r.`zone`")+
" IN (SELECT d.`zone` FROM {delegations} d WHERE d.`user_id` = ?)")), userID)
}
func (s recordStore) queryZones(ctx context.Context, query string, args ...any) ([]zoneSummary, error) {
@@ -65,7 +74,7 @@ func (s recordStore) queryZones(ctx context.Context, query string, args ...any)
func (s recordStore) ZoneExists(ctx context.Context, zone string) (bool, error) {
defer traceStore(ctx, "recordStore.ZoneExists")()
var one int
err := s.q.QueryRowContext(ctx, s.t.sql("SELECT 1 FROM {records} WHERE `zone` = ? LIMIT 1"), zone).Scan(&one)
err := s.q.QueryRowContext(ctx, s.t.sql("SELECT 1 FROM {records}"+zoneRowsWhere+" LIMIT 1"), zone).Scan(&one)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
@@ -77,12 +86,12 @@ func (s recordStore) ZoneExists(ctx context.Context, zone string) (bool, error)
func (s recordStore) ZoneRows(ctx context.Context, zone string) ([]recordRow, error) {
defer traceStore(ctx, "recordStore.ZoneRows")()
return s.queryRecords(ctx, s.t.sql("SELECT "+recordColumns+" FROM {records} WHERE `zone` = ? ORDER BY `id`"), zone)
return s.queryRecords(ctx, s.t.sql("SELECT "+recordColumns+" FROM {records}"+zoneRowsWhere+" ORDER BY `id`"), zone)
}
func (s recordStore) ZoneRowsForUpdate(ctx context.Context, zone string) ([]recordRow, error) {
defer traceStore(ctx, "recordStore.ZoneRowsForUpdate")()
return s.queryRecords(ctx, s.t.sql("SELECT "+recordColumns+" FROM {records} WHERE `zone` = ? ORDER BY `id` FOR UPDATE"), zone)
return s.queryRecords(ctx, s.t.sql("SELECT "+recordColumns+" FROM {records}"+zoneRowsWhere+" ORDER BY `id` FOR UPDATE"), zone)
}
func (s recordStore) queryRecords(ctx context.Context, query string, args ...any) ([]recordRow, error) {
@@ -168,7 +177,7 @@ func (s recordStore) DeleteRecord(ctx context.Context, id int64) (bool, error) {
func (s recordStore) DeleteZoneRecords(ctx context.Context, zone string) (int64, error) {
defer traceStore(ctx, "recordStore.DeleteZoneRecords")()
res, err := s.q.ExecContext(ctx, s.t.sql("DELETE FROM {records} WHERE `zone` = ?"), zone)
res, err := s.q.ExecContext(ctx, s.t.sql("DELETE FROM {records}"+zoneRowsWhere), zone)
if err != nil {
return 0, fmt.Errorf("delete zone records: %w", err)
}