Split the console from the resolver: fancy-dns-console writes the cluster

fancy-dns-console is a second binary holding the web UI, authentication and
every edit. It writes ClientGroup, BlockList and DNSZone objects through the new
kubeedit package and forwards all other /api routes to a running resolver
with its own key, dropping the browser's credentials, so it can run in the
cluster or on a workstation with a kubeconfig and nothing about the resolver's
placement matters. The resolver never imports kubeedit and the console is not on
the DNS path.

kubeedit validates every spec with the runtime's own converters
(kubesource.CheckGroup/CheckList/CheckZone) before writing, so the console
cannot store an object the resolver would reject. Zone edits keep the
file-based writer's behaviour: duplicate and below-a-delegation records are
refused, a delegation in a new zone also gives the zone its servers, and a
zone with no apex NS is never stored. Each object records the editing user in
an annotation; updates retry on conflict.

The resolver reports groups from ClientGroup objects as source "kubernetes" and
counter-only rows (the "(default)" bucket, or a group removed since) as
"history", and lists DNSZone zones as the editable ones. The console uses that
to leave the YAML's groups alone and to drop history rows, which the UI would
otherwise send back on save. Its GET /api/clients overlays the cluster's
current objects on the resolver's list, because the resolver only sees an edit
after its watch fires and a save made just after another would delete the
first.

An end-to-end test runs both halves against one fake apiserver: a group saved
through the console refuses queries, a zone built record by record answers
authoritatively, and a request with the resolver's own key is rejected by the
console.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
matsandClaude Sonnet 5.5 committed 2026-10-16 08:43:41 +02:00
1 parent 4d38945d94
commit 1a536e7d0e
25 files changed
+1950 -8

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
package console
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
)
// Runtime is the resolver's admin API as the console sees it. The console never
// reads the resolver's state any other way: everything it shows that the
// resolver owns (statistics, query log, cache, health) comes through here.
type Runtime struct {
base *url.URL
key string
hc *http.Client
}
// NewRuntime points at a resolver's admin API, e.g. http://fancy-dns.dns:9053.
// key is sent as X-API-Key; the console's own users never see it.
func NewRuntime(rawURL, key string) (*Runtime, error) {
u, err := url.Parse(rawURL)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
return nil, fmt.Errorf("runtime URL %q must be http(s)://host[:port]", rawURL)
}
u.Path = strings.TrimSuffix(u.Path, "/")
return &Runtime{base: u, key: key, hc: &http.Client{Timeout: 10 * time.Second}}, nil
}
// Get fetches path (e.g. "/api/zones") and decodes the JSON answer into v.
func (r *Runtime) Get(ctx context.Context, path string, v any) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.base.String()+path, nil)
if err != nil {
return err
}
if r.key != "" {
req.Header.Set("X-API-Key", r.key)
}
res, err := r.hc.Do(req)
if err != nil {
return fmt.Errorf("resolver unreachable: %w", err)
}
defer res.Body.Close()
body, _ := io.ReadAll(io.LimitReader(res.Body, 32<<20))
if res.StatusCode != http.StatusOK {
return fmt.Errorf("resolver answered %d: %s", res.StatusCode, strings.TrimSpace(string(body)))
}
return json.Unmarshal(body, v)
}
// Proxy forwards a request to the resolver unchanged except for credentials:
// whatever the browser sent is dropped and the console's own key is used.
// Streaming responses (the live query log) are flushed as they arrive.
func (r *Runtime) Proxy() http.Handler {
p := &httputil.ReverseProxy{
Rewrite: func(pr *httputil.ProxyRequest) {
pr.SetURL(r.base)
h := pr.Out.Header
for _, k := range []string{"Cookie", "Authorization", "X-Api-Key", "X-Forwarded-For", "X-Forwarded-Host", "X-Forwarded-Proto", "Forwarded", "Origin", "Referer"} {
h.Del(k)
}
if r.key != "" {
h.Set("X-API-Key", r.key)
}
},
FlushInterval: -1,
ErrorHandler: func(w http.ResponseWriter, _ *http.Request, err error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadGateway)
_ = json.NewEncoder(w).Encode(map[string]string{"error": "resolver unreachable: " + err.Error()})
},
}
return p
}