The console could not see local zones at all. `blocklist.local_zones` compiled a store that nothing read back: there was no API for a zone, its records, its delegations or the file a record came from, and no way to change one without editing YAML and reloading. The Domains page is the blocklist's inline rules, not local data, so nothing in the console covered the half of the server that answers authoritatively from disk. There is now a Zones page and the endpoints behind it: `GET /api/zones`, `POST`/`PUT`/`DELETE /api/zones/records`, `POST`/`DELETE /api/zones/delegations` and `DELETE /api/zones?zone=`. **The console owns one directory and nothing else.** `blocklist.managed_zones` holds one `<apex>.zone` per zone, with that zone's SOA, apex NS set and the records added there, and it is compiled by the same `localzone.Load` as the operator's files — so a record added from the console is served on the next query, with no reload and no restart. Files in `blocklist.local_zones` are listed and shown read-only and are never rewritten; the key defaults to empty, so a deployment that never sets it gets a zone browser and no zone writes. Which file a record came from is now part of the store (`zone.files`, keyed by the index the store already uses), so the snapshot can say "console" or "your file" per record; the query path does not read it. **The writer proves its own output.** A record is validated by the project's own master-file parser, so `www` means `www.<zone>` and a bare CNAME target is completed exactly as it would be in a zone file; the file is rendered, and the rendering is re-parsed and compared with the records it meant to write before the file is renamed into place. A writer bug that dropped a record would otherwise present as a zone that silently stopped answering. A zone that cannot answer is refused rather than created: no SOA means no NXDOMAIN or NODATA authority, and no apex NS means nothing is delegated and no server is named. The SOA's primary follows the apex NS set — a zone whose SOA names a server outside its own NS set hands out a referral no resolver can follow — and the serial moves on every write. **A delegation takes the names under it, so a record below a cut is refused.** The store answers those names as delegated without reading the zone file, which would make the record data that looks alive and never answers. `DELETE /api/zones?zone=` exists for the same reason from the other side: a zone the console created is a real zone, so it says NXDOMAIN for what it does not hold, and deleting a record is not what gives a name back to upstream. It is refused for a zone that also has records outside the managed directory, because removing that file would remove none of the zone. **Two bugs the work turned up.** `engine.applyConfig` published local-record metrics only when `blocklist.local_zones` was non-empty, so a zone the console created was served but never counted; the gate is now the store's own record count. And the record-value renderer fell back to `RR.String()` for every type it did not know, which is the whole line rather than the RDATA — it wrote a name and a TTL into the value column for DHCID, LOC, SSHFP and SVCB, and would have fed that back as a record value on the next edit. Every write recompiles the store and flushes the cache. Which names changed is not knowable from the API, and a cached upstream answer for a name the zone now answers is simply wrong, so the flush is blunt on purpose. The wire shapes are type aliases in `internal/api` rather than copies, so there is one set of json tags, and `TestZoneEndpointKeySets` pins the key set at every level with a record and a delegation actually present — an empty list only proves the envelope. `TestZoneWritesNeedAManagedDirectory` pins the read-only default, and `TestZoneConsoleRoundTrip` walks the whole thing: a record added through the API is answered authoritatively on the next query, a cut stops the parent answering below it, and deleting the zone hands the name back. Verified against a running instance built from this tree: records and wildcards answer with `aa`, relative names are qualified, a record below a cut is refused with a message that says why, and the file on disk is a zone file an operator can read. The console page is typechecked and built; the browser was not available in this round, so it has not been looked at.
262 lines
8.4 KiB
Go
262 lines
8.4 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.k8s-git-k6n.net/mats/fancy-dns/internal/localzone"
|
|
)
|
|
|
|
// The local-zone payloads are aliases rather than copies: the record, zone
|
|
// and delegation shapes are defined once in internal/localzone, where the
|
|
// writer produces them and the store's own snapshot test pins their fields.
|
|
// A copy here would be a second set of json tags to keep in step with the
|
|
// console, which is exactly the drift the contract tests in internal/app
|
|
// exist to catch.
|
|
type (
|
|
// ZoneRecord is one authoritative record.
|
|
ZoneRecord = localzone.Record
|
|
// Zone is one compiled local zone.
|
|
Zone = localzone.Zone
|
|
// ZoneDelegation is a cut below an apex.
|
|
ZoneDelegation = localzone.Delegation
|
|
)
|
|
|
|
// ZonesResponse is GET /api/zones.
|
|
//
|
|
// Every key is always present, and Types is the set of record types the write
|
|
// endpoints accept, so the console builds its type picker from the server
|
|
// instead of keeping its own list.
|
|
type ZonesResponse struct {
|
|
// Zones are the compiled local zones, sorted by apex.
|
|
Zones []localzone.Zone `json:"zones"`
|
|
// Files are every master file the zones were compiled from.
|
|
Files []string `json:"files"`
|
|
// ManagedDir is the directory the API may rewrite, "" when the console
|
|
// has no managed zone directory configured (blocklist.managed_zones).
|
|
ManagedDir string `json:"managed_dir"`
|
|
// Writable is whether a write to ManagedDir would succeed.
|
|
Writable bool `json:"writable"`
|
|
// Types are the record types the write endpoints accept.
|
|
Types []string `json:"types"`
|
|
// Records is the total number of records across every zone.
|
|
Records int `json:"records"`
|
|
// BuiltAt is when the zone store was compiled.
|
|
BuiltAt time.Time `json:"built_at"`
|
|
// Error is the reason the store is stale, "" when it is current. It is
|
|
// here so a broken master file is visible in the console rather than
|
|
// only in the log.
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
// ZoneRecordInput adds or replaces one record. A TTL of 0 means the zone's
|
|
// default, which is what a record written without a TTL column inherits.
|
|
type ZoneRecordInput struct {
|
|
Zone string `json:"zone"`
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
TTL uint32 `json:"ttl"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
// ZoneDelegationInput hands a name below an apex, and everything under it, to
|
|
// other servers.
|
|
type ZoneDelegationInput struct {
|
|
Zone string `json:"zone"`
|
|
Name string `json:"name"`
|
|
Targets []string `json:"targets"`
|
|
TTL uint32 `json:"ttl"`
|
|
}
|
|
|
|
// ZoneWriteResponse is what a successful record or delegation change answers.
|
|
type ZoneWriteResponse struct {
|
|
OK bool `json:"ok"`
|
|
// Zone is the canonical apex the change landed in.
|
|
Zone string `json:"zone"`
|
|
// Serial is the zone's SOA serial after the write. It moves on every
|
|
// change, so it is the cheapest way for the console to tell that what it
|
|
// is looking at has been rewritten.
|
|
Serial uint32 `json:"serial"`
|
|
// Removed is set on a delete that matched something.
|
|
Removed bool `json:"removed"`
|
|
}
|
|
|
|
// handleZones returns the compiled local zones.
|
|
func (s *Server) handleZones(w http.ResponseWriter, _ *http.Request) {
|
|
if s.hooks.ListZones == nil {
|
|
writeError(w, http.StatusNotImplemented, "local zones not available")
|
|
return
|
|
}
|
|
v, err := s.hooks.ListZones()
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, v)
|
|
}
|
|
|
|
// handleZoneDelete removes a zone the console owns.
|
|
func (s *Server) handleZoneDelete(w http.ResponseWriter, r *http.Request) {
|
|
if s.hooks.DeleteZone == nil {
|
|
writeError(w, http.StatusNotImplemented, "local zones not available")
|
|
return
|
|
}
|
|
zone := r.URL.Query().Get("zone")
|
|
if zone == "" {
|
|
writeError(w, http.StatusBadRequest, "zone is required")
|
|
return
|
|
}
|
|
if err := s.hooks.DeleteZone(r.Context(), zone); err != nil {
|
|
writeError(w, zoneWriteStatus(err), err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "zone": zone})
|
|
}
|
|
|
|
// zoneWriteStatus tells a configuration problem from a missing record: a
|
|
// console that is not allowed to write zone data is a 400 with something the
|
|
// operator can fix, while a name that is not there is a 404.
|
|
func zoneWriteStatus(err error) int {
|
|
if errors.Is(err, localzone.ErrNotManaged) {
|
|
return http.StatusBadRequest
|
|
}
|
|
return http.StatusNotFound
|
|
}
|
|
|
|
// handleZoneRecordAdd adds one record to a zone.
|
|
func (s *Server) handleZoneRecordAdd(w http.ResponseWriter, r *http.Request) {
|
|
if s.hooks.AddZoneRecord == nil {
|
|
writeError(w, http.StatusNotImplemented, "local zones not available")
|
|
return
|
|
}
|
|
var in ZoneRecordInput
|
|
if err := decodeJSON(r, &in); err != nil {
|
|
writeError(w, http.StatusBadRequest, "malformed request body")
|
|
return
|
|
}
|
|
if msg := validateZoneRecord(in); msg != "" {
|
|
writeError(w, http.StatusBadRequest, msg)
|
|
return
|
|
}
|
|
v, err := s.hooks.AddZoneRecord(r.Context(), in)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, v)
|
|
}
|
|
|
|
// handleZoneRecordPut replaces the RRset of one name and type.
|
|
func (s *Server) handleZoneRecordPut(w http.ResponseWriter, r *http.Request) {
|
|
if s.hooks.PutZoneRecord == nil {
|
|
writeError(w, http.StatusNotImplemented, "local zones not available")
|
|
return
|
|
}
|
|
var in ZoneRecordInput
|
|
if err := decodeJSON(r, &in); err != nil {
|
|
writeError(w, http.StatusBadRequest, "malformed request body")
|
|
return
|
|
}
|
|
if msg := validateZoneRecord(in); msg != "" {
|
|
writeError(w, http.StatusBadRequest, msg)
|
|
return
|
|
}
|
|
v, err := s.hooks.PutZoneRecord(r.Context(), in)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, v)
|
|
}
|
|
|
|
// handleZoneRecordDelete removes a record, or its whole RRset when no value is
|
|
// named.
|
|
func (s *Server) handleZoneRecordDelete(w http.ResponseWriter, r *http.Request) {
|
|
if s.hooks.DeleteZoneRecord == nil {
|
|
writeError(w, http.StatusNotImplemented, "local zones not available")
|
|
return
|
|
}
|
|
zone := r.URL.Query().Get("zone")
|
|
name := r.URL.Query().Get("name")
|
|
rrtype := r.URL.Query().Get("type")
|
|
if zone == "" || name == "" || rrtype == "" {
|
|
writeError(w, http.StatusBadRequest, "zone, name and type are required")
|
|
return
|
|
}
|
|
if err := s.hooks.DeleteZoneRecord(r.Context(), zone, name, rrtype, r.URL.Query().Get("value")); err != nil {
|
|
writeError(w, zoneWriteStatus(err), err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "zone": zone, "name": name, "type": rrtype})
|
|
}
|
|
|
|
// handleZoneDelegationAdd delegates a name to other servers.
|
|
func (s *Server) handleZoneDelegationAdd(w http.ResponseWriter, r *http.Request) {
|
|
if s.hooks.AddZoneDelegation == nil {
|
|
writeError(w, http.StatusNotImplemented, "local zones not available")
|
|
return
|
|
}
|
|
var in ZoneDelegationInput
|
|
if err := decodeJSON(r, &in); err != nil {
|
|
writeError(w, http.StatusBadRequest, "malformed request body")
|
|
return
|
|
}
|
|
if strings.TrimSpace(in.Zone) == "" {
|
|
writeError(w, http.StatusBadRequest, "zone is required")
|
|
return
|
|
}
|
|
if strings.TrimSpace(in.Name) == "" {
|
|
writeError(w, http.StatusBadRequest, "name is required")
|
|
return
|
|
}
|
|
if len(in.Targets) == 0 {
|
|
writeError(w, http.StatusBadRequest, "a delegation needs at least one NS target")
|
|
return
|
|
}
|
|
v, err := s.hooks.AddZoneDelegation(r.Context(), in)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, v)
|
|
}
|
|
|
|
// handleZoneDelegationDelete removes a cut.
|
|
func (s *Server) handleZoneDelegationDelete(w http.ResponseWriter, r *http.Request) {
|
|
if s.hooks.DeleteZoneDelegation == nil {
|
|
writeError(w, http.StatusNotImplemented, "local zones not available")
|
|
return
|
|
}
|
|
zone := r.URL.Query().Get("zone")
|
|
name := r.URL.Query().Get("name")
|
|
if zone == "" || name == "" {
|
|
writeError(w, http.StatusBadRequest, "zone and name are required")
|
|
return
|
|
}
|
|
if err := s.hooks.DeleteZoneDelegation(r.Context(), zone, name); err != nil {
|
|
writeError(w, zoneWriteStatus(err), err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "zone": zone, "name": name})
|
|
}
|
|
|
|
// validateZoneRecord rejects an obviously incomplete record here so the
|
|
// console gets a message about the field rather than one about the zone file.
|
|
func validateZoneRecord(in ZoneRecordInput) string {
|
|
if strings.TrimSpace(in.Zone) == "" {
|
|
return "zone is required"
|
|
}
|
|
if strings.TrimSpace(in.Name) == "" {
|
|
return "name is required"
|
|
}
|
|
if strings.TrimSpace(in.Type) == "" {
|
|
return "type is required"
|
|
}
|
|
if strings.TrimSpace(in.Value) == "" {
|
|
return "value is required"
|
|
}
|
|
return ""
|
|
}
|