From 3278461d6f4ac113d4dd73f7ecf4b17ff916fae0 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Wed, 4 Nov 2026 16:28:35 +0000 Subject: [PATCH] policy/v2: move test-only helpers out of production code mergeFilterRules and the peer-rule reference move to tests; compileAutogroupSelf and newResolved go. --- hscontrol/policy/v2/compiled.go | 21 --------------------- hscontrol/policy/v2/filter.go | 15 --------------- hscontrol/policy/v2/filter_test.go | 19 +++++++++++++++++-- hscontrol/policy/v2/peermap_scan_test.go | 18 ++++++++++++++++++ hscontrol/policy/v2/policy.go | 8 -------- hscontrol/policy/v2/types.go | 9 --------- 6 files changed, 35 insertions(+), 55 deletions(-) diff --git a/hscontrol/policy/v2/compiled.go b/hscontrol/policy/v2/compiled.go index 337f44aba..01de67d98 100644 --- a/hscontrol/policy/v2/compiled.go +++ b/hscontrol/policy/v2/compiled.go @@ -793,16 +793,6 @@ func filterRulesForNode( return rulesForNode(grants, node, userIdx, filterSelfRules, compileViaForNode) } -// peerRulesForNode returns the rules that decide a node's peers: its -// unreduced filter rules, with [viaPeerRule] in place of each via rule. -func peerRulesForNode( - grants []compiledGrant, - node types.NodeView, - userIdx userNodeIndex, -) []tailcfg.FilterRule { - return rulesForNode(grants, node, userIdx, ownSelfRules, viaPeerRule) -} - // nodeOnlyMatchers returns the matchers of the self and via rules a node // adds to the pre-compiled rules every node shares. Self matchers are // shared per user; via rules reuse their grant's parsed sources. @@ -871,17 +861,6 @@ func rulesForNode( return mergeKeyedRules(rules, keys) } -// compileAutogroupSelf produces filter rules for autogroup:self -// destinations for a specific node. Only called for grants with -// self destinations and only produces rules for untagged nodes. -func compileAutogroupSelf( - cg *compiledGrant, - node types.NodeView, - userIdx userNodeIndex, -) []tailcfg.FilterRule { - return ownSelfRules(cg, node, userIdx).rules -} - // ownSelfRules returns the autogroup:self rules of node's own user. func ownSelfRules( cg *compiledGrant, diff --git a/hscontrol/policy/v2/filter.go b/hscontrol/policy/v2/filter.go index e9c0775c2..15020ad31 100644 --- a/hscontrol/policy/v2/filter.go +++ b/hscontrol/policy/v2/filter.go @@ -760,21 +760,6 @@ func ruleKeys(rules []tailcfg.FilterRule) []string { return keys } -// mergeFilterRules merges rules with identical [tailcfg.FilterRule.SrcIPs] and -// [tailcfg.FilterRule.IPProto] by combining their [tailcfg.FilterRule.DstPorts]. -// DstPorts are NOT deduplicated to match Tailscale behavior. -// [tailcfg.CapGrant] rules (which have no [tailcfg.FilterRule.DstPorts]) are -// passed through without merging since [tailcfg.CapGrant] and -// [tailcfg.FilterRule.DstPorts] are mutually exclusive in a -// [tailcfg.FilterRule]. -func mergeFilterRules(rules []tailcfg.FilterRule) []tailcfg.FilterRule { - if len(rules) <= 1 { - return rules - } - - return mergeKeyedRules(rules, ruleKeys(rules)) -} - // mergeKeyedRules merges rules whose keys are equal into the first of them, // keeping first-appearance order, as Tailscale does. keys[i] is rules[i]'s // key; [tailcfg.CapGrant] rules pass through. diff --git a/hscontrol/policy/v2/filter_test.go b/hscontrol/policy/v2/filter_test.go index 0fcf5cb67..09dc40235 100644 --- a/hscontrol/policy/v2/filter_test.go +++ b/hscontrol/policy/v2/filter_test.go @@ -3520,7 +3520,7 @@ func TestBuildSrcIPStrings(t *testing.T) { var b netipx.IPSetBuilder b.AddPrefix(netip.MustParsePrefix("100.64.0.1/32")) - resolved, err := newResolved(&b) + resolved, err := newResolvedAddresses(b.IPSet()) require.NoError(t, err) // Node with approved subnet route. @@ -4273,7 +4273,7 @@ func TestCompileGrantWithAutogroupSelf_GrantPaths(t *testing.T) { var got []tailcfg.FilterRule if cg != nil { got = append(got, cg.rules...) - got = append(got, compileAutogroupSelf(cg, nodeView, userIdx)...) + got = append(got, ownSelfRules(cg, nodeView, userIdx).rules...) got = mergeFilterRules(got) } @@ -4501,3 +4501,18 @@ func TestMergeFilterRulesKeysSharedSrcIPsOnce(t *testing.T) { assert.Less(t, extra, 8*keyBytes, "merging %d rules that share %d sources", numRules, len(wide[0].SrcIPs)) } + +// mergeFilterRules merges rules with identical [tailcfg.FilterRule.SrcIPs] and +// [tailcfg.FilterRule.IPProto] by combining their [tailcfg.FilterRule.DstPorts]. +// DstPorts are NOT deduplicated to match Tailscale behavior. +// [tailcfg.CapGrant] rules (which have no [tailcfg.FilterRule.DstPorts]) are +// passed through without merging since [tailcfg.CapGrant] and +// [tailcfg.FilterRule.DstPorts] are mutually exclusive in a +// [tailcfg.FilterRule]. +func mergeFilterRules(rules []tailcfg.FilterRule) []tailcfg.FilterRule { + if len(rules) <= 1 { + return rules + } + + return mergeKeyedRules(rules, ruleKeys(rules)) +} diff --git a/hscontrol/policy/v2/peermap_scan_test.go b/hscontrol/policy/v2/peermap_scan_test.go index 6d6aec716..3d2766d67 100644 --- a/hscontrol/policy/v2/peermap_scan_test.go +++ b/hscontrol/policy/v2/peermap_scan_test.go @@ -859,3 +859,21 @@ func benchmarkPeerMapBuilders(b *testing.B, pm *PolicyManager, nodes types.Nodes }) } } + +// peerRulesForNode returns the rules that decide a node's peers: its +// unreduced filter rules, with [viaPeerRule] in place of each via rule. +func peerRulesForNode( + grants []compiledGrant, + node types.NodeView, + userIdx userNodeIndex, +) []tailcfg.FilterRule { + return rulesForNode(grants, node, userIdx, ownSelfRules, viaPeerRule) +} + +// peerRulesForNodeLocked returns the rules that decide a node's peers, see +// [peerRulesForNode]. +func (pm *PolicyManager) peerRulesForNodeLocked( + node types.NodeView, +) []tailcfg.FilterRule { + return peerRulesForNode(pm.compiledGrants, node, pm.userNodeIdx) +} diff --git a/hscontrol/policy/v2/policy.go b/hscontrol/policy/v2/policy.go index 7fab9d324..ea9fc484b 100644 --- a/hscontrol/policy/v2/policy.go +++ b/hscontrol/policy/v2/policy.go @@ -1019,14 +1019,6 @@ func (pm *PolicyManager) nodeMatchersLocked( nodeOnlyMatchers(pm.compiledGrants, node, pm.userNodeIdx, viaRules)) } -// peerRulesForNodeLocked returns the rules that decide a node's peers, see -// [peerRulesForNode]. -func (pm *PolicyManager) peerRulesForNodeLocked( - node types.NodeView, -) []tailcfg.FilterRule { - return peerRulesForNode(pm.compiledGrants, node, pm.userNodeIdx) -} - // filterForNodeLocked returns the filter rules for a specific node, // already reduced to only include rules relevant to that node. // diff --git a/hscontrol/policy/v2/types.go b/hscontrol/policy/v2/types.go index caf0aa301..ff9dacf4b 100644 --- a/hscontrol/policy/v2/types.go +++ b/hscontrol/policy/v2/types.go @@ -217,15 +217,6 @@ type resolved struct { wire []string } -func newResolved(ipb *netipx.IPSetBuilder) (resolved, error) { - ips, err := ipb.IPSet() - if err != nil { - return resolved{}, err - } - - return resolved{ips: *ips}, nil -} - func newResolvedAddresses(ips *netipx.IPSet, err error) (ResolvedAddresses, error) { if ips == nil { return nil, err