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

+67 -42
View File
@@ -781,21 +781,21 @@ func handleAuditRead(ctx context.Context, c *apiContext) (apiResponse, error) {
func zoneAPIHandlers(s *WebServer) map[string]apiHandler {
h := zoneHandlers{s}
return map[string]apiHandler{
"zone_list": h.listZones,
"zone_create": h.createZone,
"zone_read": h.readZone,
"zone_delete": h.deleteZone,
"record_list": h.listRecords,
"record_create": h.createRecord,
"record_update": h.updateRecord,
"record_delete": h.deleteRecord,
"record_bulk_delete": h.bulkDelete,
"soa_update": h.updateSOA,
"zone_check": h.checkZone,
"check_all": h.checkAll,
"zone_export": h.exportZone,
"zone_import_preview": h.previewImport,
"zone_import": h.applyImport,
opZoneList: h.listZones,
opZoneCreate: h.createZone,
opZoneRead: h.readZone,
opZoneDelete: h.deleteZone,
opRecordList: h.listRecords,
opRecordCreate: h.createRecord,
opRecordUpdate: h.updateRecord,
opRecordDelete: h.deleteRecord,
opRecordBulkDelete: h.bulkDelete,
opSOAUpdate: h.updateSOA,
opZoneCheck: h.checkZone,
opCheckAll: h.checkAll,
opZoneExport: h.exportZone,
opZoneImportPreview: h.previewImport,
opZoneImport: h.applyImport,
}
}
@@ -803,10 +803,6 @@ type zoneHandlers struct {
server *WebServer
}
func zoneAPINotFound() *apiError {
return &apiError{Status: http.StatusNotFound, Message: "zone not found"}
}
func zoneAPIFields(fields map[string]string) *apiError {
return &apiError{Status: http.StatusUnprocessableEntity, Message: "validation failed", Fields: fields}
}
@@ -837,7 +833,7 @@ func (h zoneHandlers) visibleZones(ctx context.Context, c *apiContext) ([]zoneSu
func (h zoneHandlers) readRows(ctx context.Context, zone string) ([]recordRow, error) {
rows, err := newRecordStore(h.server.db, h.server.tables).ZoneRows(ctx, zone)
if err == nil && len(rows) == 0 {
return nil, zoneAPINotFound()
return nil, errZoneNotFound()
}
return rows, err
}
@@ -861,7 +857,7 @@ func (h zoneHandlers) writeZone(ctx context.Context, c *apiContext, creating boo
return err
}
if err != nil || actor.DeletedAt.Valid || actor.Role != c.Principal.User.Role {
return zoneAPINotFound()
return errZoneNotFound()
}
if actor.Role == "user" {
allowed, err := w.delegations.HasDelegationForShare(ctx, c.Zone, actor.ID)
@@ -869,7 +865,7 @@ func (h zoneHandlers) writeZone(ctx context.Context, c *apiContext, creating boo
return err
}
if !allowed {
return zoneAPINotFound()
return errZoneNotFound()
}
}
w.rows, err = w.records.ZoneRowsForUpdate(ctx, c.Zone)
@@ -880,7 +876,7 @@ func (h zoneHandlers) writeZone(ctx context.Context, c *apiContext, creating boo
return &apiError{Status: http.StatusConflict, Message: "zone already exists"}
}
if !creating && len(w.rows) == 0 {
return zoneAPINotFound()
return errZoneNotFound()
}
response, err = change(w)
if err != nil {
@@ -988,6 +984,11 @@ type zoneAPIView struct {
SOA *zoneAPISOAView `json:"soa"`
}
type zoneAPICreated struct {
zoneAPIView
Warnings []zoneAPIProblem `json:"warnings"`
}
func zoneAPISerial(row recordRow) *uint32 {
serial, _ := storedSOASerial(row)
return zoneAPINullable(serial, serial != 0)
@@ -1008,6 +1009,15 @@ func zoneAPIFindSOA(rows []recordRow) recordRow {
return first
}
func zoneAPISelectSOA(rows []recordRow, etag string) recordRow {
for _, row := range rows {
if row.RecordType == "SOA" && recordETag(row) == etag {
return row
}
}
return zoneAPIFindSOA(rows)
}
func zoneAPISOADetails(row recordRow, soa soaData) zoneAPISOAFields {
return zoneAPISOAFields{soa.NS, soa.MBox, zoneAPINullable(row.TTL.Int64, row.TTL.Valid), soa.Refresh, soa.Retry, soa.Expire, soa.MinTTL}
}
@@ -1124,6 +1134,10 @@ func (h zoneHandlers) createZone(ctx context.Context, c *apiContext) (apiRespons
}
return apiResponse{}, zoneAPIFields(mapped)
}
problems, warnings := validateZoneChanges(zone, nil, zoneChanges{Creates: rows})
if err := zoneAPIRuleError(problems, "name"); err != nil {
return apiResponse{}, err
}
deleted, err := w.delegations.DeleteZoneDelegations(ctx, zone)
if err != nil {
return apiResponse{}, err
@@ -1141,7 +1155,9 @@ func (h zoneHandlers) createZone(ctx context.Context, c *apiContext) (apiRespons
if err := zoneAPIAuditChange(c, zoneAPIAuditRows(nil, deleted), zoneAPIAuditRows(rows, nil), true); err != nil {
return apiResponse{}, err
}
return zoneAPISummary(zone, rows, c.Now, http.StatusCreated), nil
response := zoneAPISummary(zone, rows, c.Now, http.StatusCreated)
response.Body = zoneAPICreated{response.Body.(zoneAPIView), zoneAPIProblems(warnings)}
return response, nil
})
}
@@ -1206,9 +1222,11 @@ func zoneAPIRecordView(row recordRow, problems []zoneProblem) zoneAPIRecord {
var raw *string
if row.Content.Valid {
decoded, err := inspectRecordContent(row.RecordType, row.Content.String)
if err == nil && len(decoded.fields) == 0 && decoded.decodeErr == nil {
parsed := err == nil && len(decoded.fields) == 0 && decoded.decodeErr == nil
if parsed {
data = decoded.data
} else {
}
if !parsed || slices.ContainsFunc(problems, func(p zoneProblem) bool { return p.Severity == "error" }) {
raw = &row.Content.String
}
}
@@ -1578,14 +1596,14 @@ func (h zoneHandlers) deleteRecord(ctx context.Context, c *apiContext) (apiRespo
return apiResponse{}, err
}
changes := zoneChanges{Deletes: []int64{id}}
problems, _ := validateZoneChanges(c.Zone, w.rows, changes)
problems, warnings := validateZoneChanges(c.Zone, w.rows, changes)
if err := zoneAPIRuleError(problems, "type"); err != nil {
return apiResponse{}, err
}
if _, err := w.persist(changes, false); err != nil {
return apiResponse{}, err
}
return apiResponse{Status: http.StatusNoContent}, nil
return apiResponse{Status: http.StatusOK, Body: map[string]any{"warnings": zoneAPIProblems(warnings)}}, nil
})
}
@@ -1622,14 +1640,14 @@ func (h zoneHandlers) bulkDelete(ctx context.Context, c *apiContext) (apiRespons
seen[id] = true
changes.Deletes = append(changes.Deletes, id)
}
problems, _ := validateZoneChanges(c.Zone, w.rows, changes)
problems, warnings := validateZoneChanges(c.Zone, w.rows, changes)
if err := zoneAPIRuleError(problems, "records"); err != nil {
return apiResponse{}, err
}
if _, err := w.persist(changes, true); err != nil {
return apiResponse{}, err
}
return apiResponse{Status: http.StatusOK, Body: map[string]int{"deleted": len(changes.Deletes)}}, nil
return apiResponse{Status: http.StatusOK, Body: map[string]any{"deleted": len(changes.Deletes), "warnings": zoneAPIProblems(warnings)}}, nil
})
}
@@ -1640,7 +1658,7 @@ func (h zoneHandlers) updateSOA(ctx context.Context, c *apiContext) (apiResponse
}
decodeErr := c.decodeJSON(&in)
return h.writeZone(ctx, c, false, func(w zoneAPIWrite) (apiResponse, error) {
current := zoneAPIFindSOA(w.rows)
current := zoneAPISelectSOA(w.rows, c.Request.Header.Get("If-Match"))
if current.ID != 0 {
c.Entry.RecordID = sql.NullInt64{Int64: current.ID, Valid: true}
}
@@ -1688,9 +1706,10 @@ func (h zoneHandlers) updateSOA(ctx context.Context, c *apiContext) (apiResponse
if err != nil {
return apiResponse{}, err
}
soa := zoneAPISOA(zoneAPIUpdatedRow(after, current), c.Now)
row := zoneAPIUpdatedRow(after, current)
soa := zoneAPISOA(row, c.Now)
return apiResponse{
Status: http.StatusOK, Header: zoneAPIETag(soa.ETag),
Status: http.StatusOK, Header: zoneAPIETag(recordETag(row)),
Body: map[string]any{"soa": soa, "warnings": zoneAPIProblems(warnings)},
}, nil
})
@@ -1809,11 +1828,10 @@ type zoneAPIImportPreview struct {
func zoneAPIImportPreviewView(diff zoneImportDiff, lines []int, now time.Time) zoneAPIImportPreview {
result := zoneAPIImportPreview{
Fingerprint: diff.Fingerprint, Unchanged: len(diff.Unchanged),
Added: make([]zoneAPIImportRow, 0, len(diff.Changes.Creates)),
Updated: make([]zoneAPIImportUpdate, 0, len(diff.Updated)),
Deleted: make([]zoneAPIImportRow, 0, len(diff.Deleted)),
Skipped: make([]zoneAPIImportSkip, 0, len(diff.Skipped)),
Warnings: make([]zoneAPIImportMessage, 0, len(diff.Warnings)),
Added: make([]zoneAPIImportRow, 0, len(diff.Changes.Creates)),
Updated: make([]zoneAPIImportUpdate, 0, len(diff.Updated)),
Deleted: make([]zoneAPIImportRow, 0, len(diff.Deleted)),
Skipped: make([]zoneAPIImportSkip, 0, len(diff.Skipped)),
}
view := func(row recordRow) zoneAPIImportRow {
return zoneAPIImportRow{row.Name, row.RecordType, zoneAPINullable(row.TTL.Int64, row.TTL.Valid), recordValue(row, now)}
@@ -1836,14 +1854,20 @@ func zoneAPIImportPreviewView(diff zoneImportDiff, lines []int, now time.Time) z
for _, item := range diff.Skipped {
result.Skipped = append(result.Skipped, zoneAPIImportSkip{item.Line, item.Text, item.Reason})
}
result.Warnings = zoneAPIImportWarnings(diff, lines)
return result
}
func zoneAPIImportWarnings(diff zoneImportDiff, lines []int) []zoneAPIImportMessage {
warnings := make([]zoneAPIImportMessage, 0, len(diff.Warnings))
for i, problem := range diff.Warnings {
message := zoneAPIImportMessage{Message: problem.Message}
if i < len(lines) {
message.Line = lines[i]
}
result.Warnings = append(result.Warnings, message)
warnings = append(warnings, message)
}
return result
return warnings
}
func (h zoneHandlers) previewImport(ctx context.Context, c *apiContext) (apiResponse, error) {
@@ -1881,15 +1905,16 @@ func (h zoneHandlers) applyImport(ctx context.Context, c *apiContext) (apiRespon
if in.Fingerprint != zoneFingerprint(w.rows) {
return apiResponse{}, &apiError{Status: http.StatusConflict, Message: "zone changed since preview"}
}
diff, _, err := in.diff(c, w.rows)
diff, lines, err := in.diff(c, w.rows)
if err != nil {
return apiResponse{}, err
}
if _, err := w.persist(diff.Changes, true); err != nil {
return apiResponse{}, err
}
return apiResponse{Status: http.StatusOK, Body: map[string]int{
return apiResponse{Status: http.StatusOK, Body: map[string]any{
"added": len(diff.Changes.Creates), "updated": len(diff.Updated), "deleted": len(diff.Deleted),
"warnings": zoneAPIImportWarnings(diff, lines),
}}, nil
})
}