policy/v2: move test-only helpers out of production code

mergeFilterRules and the peer-rule reference move to tests;
compileAutogroupSelf and newResolved go.
This commit is contained in:
Kristoffer Dalby committed 2026-11-05 12:32:04 +01:00
1 parent 91b920cc33
commit 3278461d6f
6 files changed
+35 -55

No files matched your search

-21
View File
@@ -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,
-15
View File
@@ -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.
+17 -2
View File
@@ -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))
}
+18
View File
@@ -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)
}
-8
View File
@@ -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.
//
-9
View File
@@ -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