Add AddressOverride and DomainRule objects for what lived in a ConfigMap and the config file

The address redirects page rewrote an RPZ ConfigMap through the resolver's own
service account, and the domains page rewrote allow_entries/block_entries in the
config file. Both become cluster objects like the rest.

AddressOverride is one name answered with fixed addresses (the name and
everything under it), exactly what an RPZ "name A address" line did. DomainRule
is a block or allow list of inline names. The resolver reads both with the
existing watch: overrides are rendered into the managed RPZ source the blocklist
manager already has, and rule names join the config's inline entries in the
merged overlay, so the file's config is never written to.

Names are checked to be DNS names or wildcard patterns before they are used,
because they are joined into line-oriented parser input and a newline or a
comment character in one would otherwise add a rule of its own.

The console edits both: the address-overrides and domains endpoints now write
objects. The console keeps its added rules in one DomainRule per action
(inline-block, inline-allow) instead of an object per name, and removes a name
from whichever DomainRule holds it, wildcard or not. An end-to-end test drives
the pages' endpoints and checks the resolver's answers.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
matsandClaude Sonnet 5.5 committed 2026-10-16 10:08:08 +02:00
1 parent 7fd6a7e7a5
commit 88479b9a12
19 files changed
+912 -18

No files matched your search

+99
View File
@@ -20,6 +20,9 @@ func newEditor() *Editor {
kubesource.ZoneGVR: "DNSZoneList",
kubesource.GroupGVR: "ClientGroupList",
kubesource.ListGVR: "BlockListList",
kubesource.OverrideGVR: "AddressOverrideList",
kubesource.RuleGVR: "DomainRuleList",
})
return New(c, "dns")
}
@@ -216,3 +219,99 @@ func TestListEditing(t *testing.T) {
t.Fatal(err)
}
}
func TestOverrideEditing(t *testing.T) {
e := newEditor()
if err := e.SetOverride(ctx, "Tornberg.ME.", "10.10.3.10", "a"); err != nil {
t.Fatal(err)
}
// Setting again replaces rather than adds, as the RPZ file editor did.
if err := e.SetOverride(ctx, "tornberg.me", "10.10.3.11", "a"); err != nil {
t.Fatal(err)
}
if err := e.SetOverride(ctx, "slask_x.local", "10.10.3.10", "a"); err != nil {
t.Fatalf("a name that is not a legal object name must still work: %v", err)
}
got, _ := e.Overrides(ctx)
if len(got) != 2 {
t.Fatalf("overrides = %+v", got)
}
byDomain := map[string][]string{}
for _, o := range got {
byDomain[o.Domain] = o.Addresses
}
if a := byDomain["tornberg.me"]; len(a) != 1 || a[0] != "10.10.3.11" {
t.Fatalf("tornberg.me = %v", a)
}
if err := e.SetOverride(ctx, "x.test", "not-an-ip", "a"); err == nil {
t.Fatal("a bad address was accepted")
}
if err := e.DeleteOverride(ctx, "TORNBERG.me."); err != nil {
t.Fatal(err)
}
if err := e.DeleteOverride(ctx, "tornberg.me"); !errors.Is(err, ErrNotFound) {
t.Fatalf("err = %v", err)
}
if got, _ := e.Overrides(ctx); len(got) != 1 {
t.Fatalf("after delete: %+v", got)
}
}
func TestRuleEditing(t *testing.T) {
e := newEditor()
n, err := e.AddRules(ctx, "block", []string{"Ads.Example.com", "*.tracker.example", "ads.example.com"}, "a")
if err != nil || n != 3 {
t.Fatalf("add: %d %v", n, err)
}
if _, err := e.AddRules(ctx, "allow", []string{"good.example"}, "a"); err != nil {
t.Fatal(err)
}
if _, err := e.AddRules(ctx, "block", []string{"bad name\nother.test"}, "a"); err == nil {
t.Fatal("a name with a newline was accepted")
}
if _, err := e.AddRules(ctx, "drop", []string{"x.test"}, "a"); err == nil {
t.Fatal("an unknown action was accepted")
}
items, _ := e.list(ctx, kubesource.RuleGVR)
if len(items) != 2 {
t.Fatalf("%d DomainRule objects, want one per action", len(items))
}
var spec kubesource.DomainRuleSpec
for i := range items {
if items[i].GetName() == inlineBlock {
_ = decodeSpec(&items[i], &spec)
}
}
if len(spec.Names) != 2 { // ads.example.com once, and the wildcard
t.Fatalf("block names = %v", spec.Names)
}
// A wildcard rule is deleted by its plain name, as the console sends it.
if removed, err := e.DeleteRule(ctx, "tracker.example", "a"); err != nil || removed != 1 {
t.Fatalf("delete: %d %v", removed, err)
}
if _, err := e.DeleteRule(ctx, "never.added", "a"); !errors.Is(err, ErrNotFound) {
t.Fatalf("err = %v", err)
}
// Removing the last name of the console's own list removes the object.
if _, err := e.DeleteRule(ctx, "good.example", "a"); err != nil {
t.Fatal(err)
}
if items, _ = e.list(ctx, kubesource.RuleGVR); len(items) != 1 {
t.Fatalf("%d objects left, want only the block list", len(items))
}
}
func TestObjectNames(t *testing.T) {
for in, same := range map[string]bool{"tornberg.me": true, "a-b.example": true, "slask_x.local": false, "UPPER": false} {
got := objectName(in)
if (got == in) != same {
t.Errorf("objectName(%q) = %q", in, got)
}
if err := checkName("x", got); err != nil {
t.Errorf("objectName(%q) = %q is not a legal object name: %v", in, got, err)
}
}
if objectName("a_b.test") == objectName("a-b.test") {
t.Error("distinct names collided")
}
}