2024-11-26 15:16:06 +01:00
|
|
|
package policy
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/netip"
|
2026-02-24 18:52:17 +00:00
|
|
|
"time"
|
2024-11-26 15:16:06 +01:00
|
|
|
|
2025-05-04 22:52:47 +03:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy/matcher"
|
2025-03-10 16:20:29 +01:00
|
|
|
policyv2 "github.com/juanfont/headscale/hscontrol/policy/v2"
|
2024-11-26 15:16:06 +01:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
|
|
|
"tailscale.com/tailcfg"
|
2025-07-05 23:31:13 +02:00
|
|
|
"tailscale.com/types/views"
|
2025-03-10 16:20:29 +01:00
|
|
|
)
|
|
|
|
|
|
2024-11-26 15:16:06 +01:00
|
|
|
type PolicyManager interface {
|
2025-05-01 07:06:30 +02:00
|
|
|
// Filter returns the current filter rules for the entire tailnet and the associated matchers.
|
|
|
|
|
Filter() ([]tailcfg.FilterRule, []matcher.Match)
|
2025-10-16 12:59:52 +02:00
|
|
|
// FilterForNode returns filter rules for a specific node, handling autogroup:self
|
|
|
|
|
FilterForNode(node types.NodeView) ([]tailcfg.FilterRule, error)
|
2025-10-23 17:57:41 +02:00
|
|
|
// MatchersForNode returns matchers for peer relationship determination (unreduced)
|
|
|
|
|
MatchersForNode(node types.NodeView) ([]matcher.Match, error)
|
|
|
|
|
// BuildPeerMap constructs peer relationship maps for the given nodes
|
|
|
|
|
BuildPeerMap(nodes views.Slice[types.NodeView]) map[types.NodeID][]types.NodeView
|
2026-02-24 18:50:18 +00:00
|
|
|
SSHPolicy(baseURL string, node types.NodeView) (*tailcfg.SSHPolicy, error)
|
2026-02-24 18:52:17 +00:00
|
|
|
// SSHCheckParams resolves the SSH check period for a (src, dst) pair
|
|
|
|
|
// from the current policy, avoiding trust of client-provided URL params.
|
|
|
|
|
SSHCheckParams(srcNodeID, dstNodeID types.NodeID) (time.Duration, bool)
|
2026-02-06 21:45:32 +01:00
|
|
|
SetPolicy(pol []byte) (bool, error)
|
2024-11-26 15:16:06 +01:00
|
|
|
SetUsers(users []types.User) (bool, error)
|
2025-07-05 23:31:13 +02:00
|
|
|
SetNodes(nodes views.Slice[types.NodeView]) (bool, error)
|
2025-03-10 16:20:29 +01:00
|
|
|
// NodeCanHaveTag reports whether the given node can have the given tag.
|
2026-02-06 21:45:32 +01:00
|
|
|
NodeCanHaveTag(node types.NodeView, tag string) bool
|
2025-02-26 07:22:55 -08:00
|
|
|
|
tags: process tags on registration, simplify policy (#2931)
This PR investigates, adds tests and aims to correctly implement Tailscale's model for how Tags should be accepted, assigned and used to identify nodes in the Tailscale access and ownership model.
When evaluating in Headscale's policy, Tags are now only checked against a nodes "tags" list, which defines the source of truth for all tags for a given node. This simplifies the code for dealing with tags greatly, and should help us have less access bugs related to nodes belonging to tags or users.
A node can either be owned by a user, or a tag.
Next, to ensure the tags list on the node is correctly implemented, we first add tests for every registration scenario and combination of user, pre auth key and pre auth key with tags with the same registration expectation as observed by trying them all with the Tailscale control server. This should ensure that we implement the correct behaviour and that it does not change or break over time.
Lastly, the missing parts of the auth has been added, or changed in the cases where it was wrong. This has in large parts allowed us to delete and simplify a lot of code.
Now, tags can only be changed when a node authenticates or if set via the CLI/API. Tags can only be fully overwritten/replaced and any use of either auth or CLI will replace the current set if different.
A user owned device can be converted to a tagged device, but it cannot be changed back. A tagged device can never remove the last tag either, it has to have a minimum of one.
2025-12-08 18:51:07 +01:00
|
|
|
// TagExists reports whether the given tag is defined in the policy.
|
|
|
|
|
TagExists(tag string) bool
|
|
|
|
|
|
2025-02-26 07:22:55 -08:00
|
|
|
// NodeCanApproveRoute reports whether the given node can approve the given route.
|
2026-02-06 21:45:32 +01:00
|
|
|
NodeCanApproveRoute(node types.NodeView, route netip.Prefix) bool
|
2024-11-26 15:16:06 +01:00
|
|
|
|
policy/v2,state,mapper: implement per-viewer via route steering
Via grants steer routes to specific nodes per viewer. Until now,
all clients saw the same routes for each peer because route
assembly was viewer-independent. This implements per-viewer route
visibility so that via-designated peers serve routes only to
matching viewers, while non-designated peers have those routes
withdrawn.
Add ViaRouteResult type (Include/Exclude prefix lists) and
ViaRoutesForPeer to the PolicyManager interface. The v2
implementation iterates via grants, resolves sources against the
viewer, matches destinations against the peer's advertised routes
(both subnet and exit), and categorizes prefixes by whether the
peer has the via tag.
Add RoutesForPeer to State which composes global primary election,
via Include/Exclude filtering, exit routes, and ACL reduction.
When no via grants exist, it falls back to existing behavior.
Update the mapper to call RoutesForPeer per-peer instead of using
a single route function for all peers. The route function now
returns all routes (subnet + exit), and TailNode filters exit
routes out of the PrimaryRoutes field for HA tracking.
Updates #2180
2026-03-22 20:43:28 +00:00
|
|
|
// ViaRoutesForPeer computes via grant effects for a viewer-peer pair.
|
|
|
|
|
// It returns which routes should be included (peer is via-designated for viewer)
|
|
|
|
|
// and excluded (steered to a different peer). When no via grants apply,
|
|
|
|
|
// both fields are empty and the caller falls back to existing behavior.
|
|
|
|
|
ViaRoutesForPeer(viewer, peer types.NodeView) types.ViaRouteResult
|
|
|
|
|
|
2025-03-10 16:20:29 +01:00
|
|
|
Version() int
|
|
|
|
|
DebugString() string
|
2024-11-26 15:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
2025-05-20 13:57:26 +02:00
|
|
|
// NewPolicyManager returns a new policy manager.
|
2025-07-05 23:31:13 +02:00
|
|
|
func NewPolicyManager(pol []byte, users []types.User, nodes views.Slice[types.NodeView]) (PolicyManager, error) {
|
2026-02-06 21:45:32 +01:00
|
|
|
var (
|
|
|
|
|
polMan PolicyManager
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
|
2025-05-20 13:57:26 +02:00
|
|
|
polMan, err = policyv2.NewPolicyManager(pol, users, nodes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2024-11-26 15:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-10 16:20:29 +01:00
|
|
|
return polMan, err
|
2024-11-26 15:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-10 16:20:29 +01:00
|
|
|
// PolicyManagersForTest returns all available PostureManagers to be used
|
|
|
|
|
// in tests to validate them in tests that try to determine that they
|
|
|
|
|
// behave the same.
|
2025-07-05 23:31:13 +02:00
|
|
|
func PolicyManagersForTest(pol []byte, users []types.User, nodes views.Slice[types.NodeView]) ([]PolicyManager, error) {
|
2025-03-10 16:20:29 +01:00
|
|
|
var polMans []PolicyManager
|
2024-11-26 15:16:06 +01:00
|
|
|
|
2025-03-10 16:20:29 +01:00
|
|
|
for _, pmf := range PolicyManagerFuncsForTest(pol) {
|
|
|
|
|
pm, err := pmf(users, nodes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-02-06 21:45:32 +01:00
|
|
|
|
2025-03-10 16:20:29 +01:00
|
|
|
polMans = append(polMans, pm)
|
2024-11-26 15:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-10 16:20:29 +01:00
|
|
|
return polMans, nil
|
2024-11-26 15:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
2025-07-05 23:31:13 +02:00
|
|
|
func PolicyManagerFuncsForTest(pol []byte) []func([]types.User, views.Slice[types.NodeView]) (PolicyManager, error) {
|
2026-02-06 21:45:32 +01:00
|
|
|
polmanFuncs := make([]func([]types.User, views.Slice[types.NodeView]) (PolicyManager, error), 0, 1)
|
2025-02-26 07:22:55 -08:00
|
|
|
|
2025-07-05 23:31:13 +02:00
|
|
|
polmanFuncs = append(polmanFuncs, func(u []types.User, n views.Slice[types.NodeView]) (PolicyManager, error) {
|
2025-03-10 16:20:29 +01:00
|
|
|
return policyv2.NewPolicyManager(pol, u, n)
|
|
|
|
|
})
|
2025-02-26 07:22:55 -08:00
|
|
|
|
2025-03-10 16:20:29 +01:00
|
|
|
return polmanFuncs
|
2025-02-26 07:22:55 -08:00
|
|
|
}
|