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:
1 parent
7fd6a7e7a5
commit
88479b9a12
19 files changed
+912
-18
No files matched your search
@@ -3,6 +3,7 @@ package kubesource
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -220,3 +221,128 @@ func zoneFromSpec(s DNSZoneSpec, generation int64) (zone, error) {
|
||||
}
|
||||
return zone{apex: apex, text: text}, nil
|
||||
}
|
||||
|
||||
// Override is a converted AddressOverride.
|
||||
type Override struct {
|
||||
Domain string `json:"domain"`
|
||||
Addresses []string `json:"addresses"`
|
||||
}
|
||||
|
||||
// Rules is a converted DomainRule.
|
||||
type Rules struct {
|
||||
Action string `json:"action"`
|
||||
Names []string `json:"names"`
|
||||
}
|
||||
|
||||
func convertOverride(u *unstructured.Unstructured) (Override, error) {
|
||||
var s AddressOverrideSpec
|
||||
if err := spec(u, &s); err != nil {
|
||||
return Override{}, err
|
||||
}
|
||||
return CheckOverride(s)
|
||||
}
|
||||
|
||||
// CheckOverride validates an AddressOverride spec and returns it in canonical
|
||||
// form (lower-case name without the trailing dot, addresses as printed by
|
||||
// netip).
|
||||
func CheckOverride(s AddressOverrideSpec) (Override, error) {
|
||||
domain := strings.TrimSuffix(strings.ToLower(strings.TrimSpace(s.Domain)), ".")
|
||||
if err := checkRuleName(domain, false); err != nil {
|
||||
return Override{}, fmt.Errorf("domain: %w", err)
|
||||
}
|
||||
if len(s.Addresses) == 0 {
|
||||
return Override{}, fmt.Errorf("at least one address is required")
|
||||
}
|
||||
out := Override{Domain: domain}
|
||||
seen := map[string]bool{}
|
||||
for _, raw := range s.Addresses {
|
||||
ip, err := netip.ParseAddr(strings.TrimSpace(raw))
|
||||
if err != nil {
|
||||
return Override{}, fmt.Errorf("%q is not an IPv4 or IPv6 address", raw)
|
||||
}
|
||||
ip = ip.Unmap()
|
||||
if !seen[ip.String()] {
|
||||
seen[ip.String()] = true
|
||||
out.Addresses = append(out.Addresses, ip.String())
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func convertRules(u *unstructured.Unstructured) (Rules, error) {
|
||||
var s DomainRuleSpec
|
||||
if err := spec(u, &s); err != nil {
|
||||
return Rules{}, err
|
||||
}
|
||||
return CheckRules(s)
|
||||
}
|
||||
|
||||
// CheckRules validates a DomainRule spec.
|
||||
func CheckRules(s DomainRuleSpec) (Rules, error) {
|
||||
if s.Action != "block" && s.Action != "allow" {
|
||||
return Rules{}, fmt.Errorf("action must be block or allow (got %q)", s.Action)
|
||||
}
|
||||
if len(s.Names) > maxRuleNames {
|
||||
return Rules{}, fmt.Errorf("%d names exceeds the limit of %d; use a BlockList for large sets", len(s.Names), maxRuleNames)
|
||||
}
|
||||
out := Rules{Action: s.Action}
|
||||
for _, n := range s.Names {
|
||||
n = strings.ToLower(strings.TrimSpace(n))
|
||||
if err := checkRuleName(n, true); err != nil {
|
||||
return Rules{}, fmt.Errorf("name %q: %w", n, err)
|
||||
}
|
||||
out.Names = append(out.Names, n)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// maxRuleNames bounds one DomainRule; an object is capped near 1.5 MiB by etcd
|
||||
// anyway, and a bigger set belongs in a BlockList that is fetched.
|
||||
const maxRuleNames = 5000
|
||||
|
||||
// checkRuleName accepts only what a DNS name or a wildcard pattern can contain.
|
||||
// This is a security check, not tidiness: names are joined into the blocklist
|
||||
// parser's line-oriented input, so a newline or a comment character in a name
|
||||
// could smuggle in a rule of its own.
|
||||
func checkRuleName(n string, wildcard bool) error {
|
||||
if n == "" {
|
||||
return fmt.Errorf("a name is required")
|
||||
}
|
||||
if wildcard {
|
||||
n = strings.TrimPrefix(strings.TrimPrefix(n, "**."), "*.")
|
||||
}
|
||||
n = strings.TrimSuffix(n, ".")
|
||||
if n == "" || len(n) > 253 {
|
||||
return fmt.Errorf("not a valid DNS name")
|
||||
}
|
||||
for _, label := range strings.Split(n, ".") {
|
||||
if label == "" || len(label) > 63 {
|
||||
return fmt.Errorf("not a valid DNS name")
|
||||
}
|
||||
for _, c := range label {
|
||||
ok := c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-' || c == '_'
|
||||
if !ok {
|
||||
return fmt.Errorf("not a valid DNS name (only letters, digits, '-' and '_' are allowed)")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RenderRPZ renders the overrides as the RPZ text the blocklist manager reads:
|
||||
// one "name A address" line per address, applying to the name and everything
|
||||
// under it.
|
||||
func RenderRPZ(in []Override) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("$TTL 60\n")
|
||||
for _, o := range in {
|
||||
for _, a := range o.Addresses {
|
||||
typ := "A"
|
||||
if strings.Contains(a, ":") {
|
||||
typ = "AAAA"
|
||||
}
|
||||
fmt.Fprintf(&b, "%s %s %s\n", o.Domain, typ, a)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
@@ -56,6 +56,9 @@ func TestCRDSchemasMatchTheGoSpecs(t *testing.T) {
|
||||
"DNSZone": reflect.TypeOf(DNSZoneSpec{}),
|
||||
"ClientGroup": reflect.TypeOf(ClientGroupSpec{}),
|
||||
"BlockList": reflect.TypeOf(BlockListSpec{}),
|
||||
|
||||
"AddressOverride": reflect.TypeOf(AddressOverrideSpec{}),
|
||||
"DomainRule": reflect.TypeOf(DomainRuleSpec{}),
|
||||
}
|
||||
seen := 0
|
||||
for _, doc := range yamlDocs(t, "../../deploy/k8s/crds/crds.yaml") {
|
||||
@@ -80,6 +83,7 @@ func TestCRDSchemasMatchTheGoSpecs(t *testing.T) {
|
||||
}
|
||||
if plural := names["plural"].(string); plural != map[string]string{
|
||||
"DNSZone": ZoneGVR.Resource, "ClientGroup": GroupGVR.Resource, "BlockList": ListGVR.Resource,
|
||||
"AddressOverride": OverrideGVR.Resource, "DomainRule": RuleGVR.Resource,
|
||||
}[kind] {
|
||||
t.Errorf("%s: plural %q does not match the GVR the runtime watches", kind, plural)
|
||||
}
|
||||
@@ -103,6 +107,10 @@ func TestExampleObjectsConvert(t *testing.T) {
|
||||
_, err = convertGroup(u)
|
||||
case "BlockList":
|
||||
_, err = convertList(u)
|
||||
case "AddressOverride":
|
||||
_, err = convertOverride(u)
|
||||
case "DomainRule":
|
||||
_, err = convertRules(u)
|
||||
default:
|
||||
t.Fatalf("unknown kind %s", u.GetKind())
|
||||
}
|
||||
@@ -111,7 +119,7 @@ func TestExampleObjectsConvert(t *testing.T) {
|
||||
}
|
||||
n++
|
||||
}
|
||||
if n != 3 {
|
||||
t.Fatalf("converted %d examples, want 3", n)
|
||||
if n != 5 {
|
||||
t.Fatalf("converted %d examples, want 5", n)
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,10 @@ func signature(s *Snapshot) string {
|
||||
G []config.Group
|
||||
L []config.ListSource
|
||||
Z map[string]string
|
||||
}{s.Groups, s.Lists, s.Zones})
|
||||
O []Override
|
||||
B []string
|
||||
A []string
|
||||
}{s.Groups, s.Lists, s.Zones, s.Overrides, s.BlockNames, s.AllowNames})
|
||||
sum := sha256.Sum256(b)
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
@@ -38,6 +41,10 @@ func Merge(base *config.Config, snap *Snapshot) (*config.Config, []string) {
|
||||
next.Block = base.Block
|
||||
next.Block.Lists = append([]config.ListSource(nil), base.Block.Lists...)
|
||||
|
||||
// Inline rules from DomainRule objects join the config's own.
|
||||
next.Block.BlockEntries = append(append([]string(nil), base.Block.BlockEntries...), snap.BlockNames...)
|
||||
next.Block.AllowEntries = append(append([]string(nil), base.Block.AllowEntries...), snap.AllowNames...)
|
||||
|
||||
var dropped []string
|
||||
groups := map[string]bool{}
|
||||
for _, g := range base.ACL.Groups {
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package kubesource
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.k8s-git-k6n.net/mats/fancy-dns/internal/config"
|
||||
)
|
||||
|
||||
func TestOverridesAndRulesReachTheSnapshot(t *testing.T) {
|
||||
first := obj("AddressOverride", "tornberg", map[string]any{"domain": "Tornberg.ME.", "addresses": []any{"10.10.3.10", "fd00::1"}})
|
||||
dup := obj("AddressOverride", "a-dup", map[string]any{"domain": "tornberg.me", "addresses": []any{"10.0.0.9"}})
|
||||
// The oldest claim wins, whatever the names sort like.
|
||||
first.SetCreationTimestamp(metav1Time(1000))
|
||||
dup.SetCreationTimestamp(metav1Time(2000))
|
||||
_, _, ch := start(t, first, dup,
|
||||
obj("AddressOverride", "bad", map[string]any{"domain": "x.test", "addresses": []any{"not-an-ip"}}),
|
||||
obj("DomainRule", "blocks", map[string]any{"action": "block", "names": []any{"Ads.Example.com", "*.tracker.example"}}),
|
||||
obj("DomainRule", "allows", map[string]any{"action": "allow", "names": []any{"**.good.example"}}),
|
||||
obj("DomainRule", "bogus", map[string]any{"action": "drop", "names": []any{"x.test"}}),
|
||||
)
|
||||
sn := next(t, ch)
|
||||
if len(sn.Overrides) != 1 || sn.Overrides[0].Domain != "tornberg.me" || len(sn.Overrides[0].Addresses) != 2 {
|
||||
t.Fatalf("overrides = %+v", sn.Overrides)
|
||||
}
|
||||
if got := strings.Join(sn.BlockNames, ","); got != "ads.example.com,*.tracker.example" {
|
||||
t.Fatalf("block names = %s", got)
|
||||
}
|
||||
if got := strings.Join(sn.AllowNames, ","); got != "**.good.example" {
|
||||
t.Fatalf("allow names = %s", got)
|
||||
}
|
||||
if len(sn.Problems) != 3 {
|
||||
t.Fatalf("problems = %+v, want the duplicate, the bad address and the bad action", sn.Problems)
|
||||
}
|
||||
rpz := RenderRPZ(sn.Overrides)
|
||||
for _, want := range []string{"$TTL 60", "tornberg.me A 10.10.3.10", "tornberg.me AAAA fd00::1"} {
|
||||
if !strings.Contains(rpz, want) {
|
||||
t.Errorf("RPZ is missing %q:\n%s", want, rpz)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRuleNamesCannotSmuggleRules: names are joined into line-oriented parser
|
||||
// input, so anything that is not a DNS name must be refused before it gets there.
|
||||
func TestRuleNamesCannotSmuggleRules(t *testing.T) {
|
||||
for _, bad := range []string{
|
||||
"a.test\nb.test", "a.test # comment", "a.test A 10.0.0.1", "a b", "", ".", "a..test",
|
||||
"-.test"[:0] + "$ORIGIN x.", "*.", "*",
|
||||
} {
|
||||
if _, err := CheckRules(DomainRuleSpec{Action: "block", Names: []string{bad}}); err == nil {
|
||||
t.Errorf("name %q accepted", bad)
|
||||
}
|
||||
if _, err := CheckOverride(AddressOverrideSpec{Domain: bad, Addresses: []string{"10.0.0.1"}}); err == nil {
|
||||
t.Errorf("override domain %q accepted", bad)
|
||||
}
|
||||
}
|
||||
if _, err := CheckRules(DomainRuleSpec{Action: "block", Names: []string{"*.ok.example", "**.ok2.example", "plain.example."}}); err != nil {
|
||||
t.Errorf("valid names rejected: %v", err)
|
||||
}
|
||||
// Surrounding whitespace is trimmed, never passed on.
|
||||
if r, err := CheckRules(DomainRuleSpec{Action: "block", Names: []string{" evil.test\r\n"}}); err != nil || r.Names[0] != "evil.test" {
|
||||
t.Errorf("trailing control characters survived: %q %v", r.Names, err)
|
||||
}
|
||||
if _, err := CheckOverride(AddressOverrideSpec{Domain: "x.test", Addresses: []string{"::ffff:10.0.0.1"}}); err != nil {
|
||||
t.Errorf("mapped IPv4 rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeAddsInlineRulesWithoutTouchingBase(t *testing.T) {
|
||||
base := config.Default()
|
||||
base.Block.BlockEntries = []string{"yaml.example"}
|
||||
merged, _ := Merge(base, &Snapshot{BlockNames: []string{"crd.example"}, AllowNames: []string{"ok.example"}})
|
||||
if got := strings.Join(merged.Block.BlockEntries, ","); got != "yaml.example,crd.example" {
|
||||
t.Fatalf("block entries = %s", got)
|
||||
}
|
||||
if len(merged.Block.AllowEntries) != 1 || len(base.Block.BlockEntries) != 1 || len(base.Block.AllowEntries) != 0 {
|
||||
t.Fatal("Merge modified its input or lost an entry")
|
||||
}
|
||||
}
|
||||
@@ -39,8 +39,14 @@ type Snapshot struct {
|
||||
Groups []config.Group `json:"groups"`
|
||||
Lists []config.ListSource `json:"lists"`
|
||||
// Zones maps an apex to its rendered master file.
|
||||
Zones map[string]string `json:"zones"`
|
||||
Problems []Problem `json:"problems,omitempty"`
|
||||
Zones map[string]string `json:"zones"`
|
||||
// Overrides are the address overrides, sorted by name.
|
||||
Overrides []Override `json:"overrides,omitempty"`
|
||||
// BlockNames and AllowNames are the inline domain rules from all
|
||||
// DomainRule objects.
|
||||
BlockNames []string `json:"block_names,omitempty"`
|
||||
AllowNames []string `json:"allow_names,omitempty"`
|
||||
Problems []Problem `json:"problems,omitempty"`
|
||||
}
|
||||
|
||||
// Options configure a Source.
|
||||
@@ -207,11 +213,11 @@ func (s *Source) objects(inf cache.SharedIndexInformer) []*unstructured.Unstruct
|
||||
|
||||
func (s *Source) rebuild(informers map[schema.GroupVersionResource]cache.SharedIndexInformer) {
|
||||
s.mu.Lock()
|
||||
snap := s.build(
|
||||
s.objects(informers[GroupGVR]),
|
||||
s.objects(informers[ListGVR]),
|
||||
s.objects(informers[ZoneGVR]),
|
||||
)
|
||||
in := make(map[schema.GroupVersionResource][]*unstructured.Unstructured, len(informers))
|
||||
for gvr, inf := range informers {
|
||||
in[gvr] = s.objects(inf)
|
||||
}
|
||||
snap := s.build(in)
|
||||
sig := signature(snap)
|
||||
changed := sig != s.sig
|
||||
if changed {
|
||||
@@ -232,6 +238,7 @@ func (s *Source) rebuild(informers map[schema.GroupVersionResource]cache.SharedI
|
||||
}
|
||||
s.log.Info("cluster configuration changed", "revision", snap.Revision,
|
||||
"groups", len(snap.Groups), "lists", len(snap.Lists), "zones", len(snap.Zones),
|
||||
"overrides", len(snap.Overrides), "block_names", len(snap.BlockNames), "allow_names", len(snap.AllowNames),
|
||||
"rejected", len(snap.Problems))
|
||||
for _, fn := range hooks {
|
||||
fn(snap)
|
||||
@@ -239,7 +246,8 @@ func (s *Source) rebuild(informers map[schema.GroupVersionResource]cache.SharedI
|
||||
}
|
||||
|
||||
// build converts every object. Caller holds s.mu.
|
||||
func (s *Source) build(groups, lists, zones []*unstructured.Unstructured) *Snapshot {
|
||||
func (s *Source) build(in map[schema.GroupVersionResource][]*unstructured.Unstructured) *Snapshot {
|
||||
groups, lists, zones := in[GroupGVR], in[ListGVR], in[ZoneGVR]
|
||||
snap := &Snapshot{Zones: map[string]string{}}
|
||||
live := map[string]bool{}
|
||||
|
||||
@@ -318,6 +326,35 @@ func (s *Source) build(groups, lists, zones []*unstructured.Unstructured) *Snaps
|
||||
snap.Zones[z.apex] = z.text
|
||||
}
|
||||
|
||||
seenOverride := map[string]bool{}
|
||||
for _, u := range in[OverrideGVR] {
|
||||
v, ok := resolve("AddressOverride", u, func() (any, error) { return convertOverride(u) })
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
o := v.(Override)
|
||||
if seenOverride[o.Domain] {
|
||||
problem("AddressOverride", u, "another AddressOverride already covers "+o.Domain, false)
|
||||
continue
|
||||
}
|
||||
seenOverride[o.Domain] = true
|
||||
snap.Overrides = append(snap.Overrides, o)
|
||||
}
|
||||
sort.Slice(snap.Overrides, func(i, j int) bool { return snap.Overrides[i].Domain < snap.Overrides[j].Domain })
|
||||
|
||||
for _, u := range in[RuleGVR] {
|
||||
v, ok := resolve("DomainRule", u, func() (any, error) { return convertRules(u) })
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
r := v.(Rules)
|
||||
if r.Action == "block" {
|
||||
snap.BlockNames = append(snap.BlockNames, r.Names...)
|
||||
} else {
|
||||
snap.AllowNames = append(snap.AllowNames, r.Names...)
|
||||
}
|
||||
}
|
||||
|
||||
for key := range s.good {
|
||||
if !live[key] {
|
||||
delete(s.good, key)
|
||||
|
||||
@@ -34,6 +34,9 @@ func fakeClient(objs ...runtime.Object) *dynfake.FakeDynamicClient {
|
||||
ZoneGVR: "DNSZoneList",
|
||||
GroupGVR: "ClientGroupList",
|
||||
ListGVR: "BlockListList",
|
||||
|
||||
OverrideGVR: "AddressOverrideList",
|
||||
RuleGVR: "DomainRuleList",
|
||||
}, objs...)
|
||||
}
|
||||
|
||||
|
||||
@@ -21,11 +21,12 @@ const (
|
||||
|
||||
// The resources the runtime watches.
|
||||
var (
|
||||
ZoneGVR = schema.GroupVersionResource{Group: APIGroup, Version: Version, Resource: "dnszones"}
|
||||
GroupGVR = schema.GroupVersionResource{Group: APIGroup, Version: Version, Resource: "clientgroups"}
|
||||
ListGVR = schema.GroupVersionResource{Group: APIGroup, Version: Version, Resource: "blocklists"}
|
||||
allGVRs = []schema.GroupVersionResource{ZoneGVR, GroupGVR, ListGVR}
|
||||
kindNames = map[string]string{ZoneGVR.Resource: "DNSZone", GroupGVR.Resource: "ClientGroup", ListGVR.Resource: "BlockList"}
|
||||
ZoneGVR = schema.GroupVersionResource{Group: APIGroup, Version: Version, Resource: "dnszones"}
|
||||
GroupGVR = schema.GroupVersionResource{Group: APIGroup, Version: Version, Resource: "clientgroups"}
|
||||
ListGVR = schema.GroupVersionResource{Group: APIGroup, Version: Version, Resource: "blocklists"}
|
||||
OverrideGVR = schema.GroupVersionResource{Group: APIGroup, Version: Version, Resource: "addressoverrides"}
|
||||
RuleGVR = schema.GroupVersionResource{Group: APIGroup, Version: Version, Resource: "domainrules"}
|
||||
allGVRs = []schema.GroupVersionResource{ZoneGVR, GroupGVR, ListGVR, OverrideGVR, RuleGVR}
|
||||
)
|
||||
|
||||
// DNSZoneSpec declares one authoritative zone. The zone's name is spec.zone,
|
||||
@@ -81,3 +82,24 @@ type BlockListSpec struct {
|
||||
Comment string `json:"comment,omitempty"`
|
||||
InsecureSkipTLSVerify bool `json:"insecure_skip_tls_verify,omitempty"`
|
||||
}
|
||||
|
||||
// AddressOverrideSpec answers a name, and everything under it, with fixed
|
||||
// addresses instead of asking upstream: "tornberg.me -> 10.10.3.10" for split
|
||||
// horizon DNS. It is what an RPZ "name A address" line does, one object per
|
||||
// name.
|
||||
type AddressOverrideSpec struct {
|
||||
Domain string `json:"domain"`
|
||||
// Addresses are IPv4 and IPv6 literals; each becomes an A or AAAA answer.
|
||||
Addresses []string `json:"addresses"`
|
||||
}
|
||||
|
||||
// DomainRuleSpec is an inline allow or block rule list, the objects behind the
|
||||
// console's Domains page. A name may carry a wildcard prefix: "*.example.com"
|
||||
// is the subdomains, "**.example.com" the name and its subdomains.
|
||||
type DomainRuleSpec struct {
|
||||
// Action is "block" or "allow". An allow rule rescues a name that any
|
||||
// blocklist would block.
|
||||
Action string `json:"action"`
|
||||
Names []string `json:"names"`
|
||||
Comment string `json:"comment,omitempty"`
|
||||
}
|
||||
Reference in new issue
Block a user