feat(api): add zone and record handlers

- feat(api): add zone and record handlers
- test(api): exercise zone handlers against mysql
- fix(api): compute etags from stored rows
This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent b5dc255e17
commit 23bec143e3
6 files changed
+2391 -12

No files matched your search

+46 -11
View File
@@ -4,6 +4,7 @@
package main
import (
"bufio"
"bytes"
"crypto/sha256"
"errors"
@@ -59,18 +60,22 @@ func exportZoneFile(zone string, rows []recordRow, now time.Time) (string, error
}
type zoneFileSkip struct {
Line int
Text string
Name string `json:"name"`
Type string `json:"type"`
Reason string `json:"reason"`
}
type zoneFileRecords struct {
Rows []recordRow
Skipped []zoneFileSkip
Warnings []zoneProblem
Rows []recordRow
Skipped []zoneFileSkip
Warnings []zoneProblem
WarningLines []int
}
type zoneFileRecordError struct {
Line int
Index int
Name string
Type string
@@ -81,6 +86,32 @@ func (e *zoneFileRecordError) Error() string {
return fmt.Sprintf("zone file record %d (%s %s) has invalid fields", e.Index, e.Name, e.Type)
}
type zoneFileLocatedError struct {
line int
err error
}
func (e *zoneFileLocatedError) Error() string { return e.err.Error() }
func (e *zoneFileLocatedError) Unwrap() error { return e.err }
type zoneFilePosition struct {
*bufio.Reader
line int
lastLine int
}
func (p *zoneFilePosition) ReadByte() (byte, error) {
b, err := p.Reader.ReadByte()
if err == nil {
p.lastLine = p.line
if b == '\n' {
p.line++
}
}
return b, err
}
func parseZoneFile(zone string, source io.Reader, now time.Time) (zoneFileRecords, error) {
zone, err := normalizeZone(zone)
if err != nil {
@@ -93,7 +124,8 @@ func parseZoneFile(zone string, source io.Reader, now time.Time) (zoneFileRecord
if len(content) > zoneFileMaxBytes {
return zoneFileRecords{}, errZoneFileTooLarge
}
parser := dns.NewZoneParser(zoneFileTTLSource(content), zone, "")
position := &zoneFilePosition{Reader: bufio.NewReader(zoneFileTTLSource(content))}
parser := dns.NewZoneParser(position, zone, "")
parser.SetIncludeAllowed(false)
var result zoneFileRecords
@@ -102,37 +134,40 @@ func parseZoneFile(zone string, source io.Reader, now time.Time) (zoneFileRecord
if !ok {
break
}
line := max(1, position.lastLine)
located := func(err error) error { return &zoneFileLocatedError{line, err} }
if i > zoneFileMaxRecords {
return zoneFileRecords{}, errors.New("zone file exceeds 10000 records")
return zoneFileRecords{}, located(errors.New("zone file exceeds 10000 records"))
}
header := rr.Header()
recordType := dns.Type(header.Rrtype).String()
if header.Class != dns.ClassINET {
return zoneFileRecords{}, fmt.Errorf("zone file record %d must have class IN", i)
return zoneFileRecords{}, located(errors.New("record must have class IN"))
}
if !dns.IsSubDomain(zone, header.Name) {
return zoneFileRecords{}, fmt.Errorf("zone file record %d has an owner outside the zone", i)
return zoneFileRecords{}, located(errors.New("record has an owner outside the zone"))
}
if _, ok := recordRules[recordType]; !ok {
result.Skipped = append(result.Skipped, zoneFileSkip{header.Name, recordType, "record type is not supported"})
result.Skipped = append(result.Skipped, zoneFileSkip{Line: line, Text: rr.String(), Name: header.Name, Type: recordType, Reason: "record type is not supported"})
continue
}
data, err := zoneFileRecordData(rr)
if err != nil {
return zoneFileRecords{}, fmt.Errorf("zone file record %d: %w", i, err)
return zoneFileRecords{}, located(err)
}
encoded, _ := encodeRecord(data)
row, fields := validateRecord(zone, header.Name, recordType, int64(header.Ttl), []byte(encoded), now)
if len(fields) != 0 {
return zoneFileRecords{}, &zoneFileRecordError{i, header.Name, recordType, fields}
return zoneFileRecords{}, &zoneFileRecordError{line, i, header.Name, recordType, fields}
}
if txt, ok := rr.(*dns.TXT); ok && len(txt.Txt) > 1 {
result.Warnings = append(result.Warnings, recordProblem(row, "warning", "txt_joined", "TXT strings were joined and the plugin will re-split them at 255 bytes"))
result.WarningLines = append(result.WarningLines, line)
}
result.Rows = append(result.Rows, row)
}
if err := parser.Err(); err != nil {
return zoneFileRecords{}, err
return zoneFileRecords{}, &zoneFileLocatedError{max(1, position.lastLine), err}
}
return result, nil
}