2023-05-31 09:59:37 +02:00
|
|
|
package mapper
|
|
|
|
|
|
|
|
|
|
import (
|
2024-09-03 00:22:17 -07:00
|
|
|
"encoding/json"
|
2023-05-31 09:59:37 +02:00
|
|
|
"net/netip"
|
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
|
|
|
"slices"
|
2023-05-31 09:59:37 +02:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2024-10-02 09:06:09 +02:00
|
|
|
"tailscale.com/net/tsaddr"
|
2023-05-31 09:59:37 +02:00
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestTailNode(t *testing.T) {
|
2026-05-11 14:53:09 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
2023-05-31 09:59:37 +02:00
|
|
|
mustNK := func(str string) key.NodePublic {
|
|
|
|
|
var k key.NodePublic
|
2026-02-06 21:45:32 +01:00
|
|
|
|
2023-05-31 09:59:37 +02:00
|
|
|
_ = k.UnmarshalText([]byte(str))
|
|
|
|
|
|
|
|
|
|
return k
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mustDK := func(str string) key.DiscoPublic {
|
|
|
|
|
var k key.DiscoPublic
|
2026-02-06 21:45:32 +01:00
|
|
|
|
2023-05-31 09:59:37 +02:00
|
|
|
_ = k.UnmarshalText([]byte(str))
|
|
|
|
|
|
|
|
|
|
return k
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mustMK := func(str string) key.MachinePublic {
|
|
|
|
|
var k key.MachinePublic
|
2026-02-06 21:45:32 +01:00
|
|
|
|
2023-05-31 09:59:37 +02:00
|
|
|
_ = k.UnmarshalText([]byte(str))
|
|
|
|
|
|
|
|
|
|
return k
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hiview := func(hoin tailcfg.Hostinfo) tailcfg.HostinfoView {
|
|
|
|
|
return hoin.View()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
created := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
|
|
|
|
|
lastSeen := time.Date(2009, time.November, 10, 23, 9, 0, 0, time.UTC)
|
|
|
|
|
expire := time.Date(2500, time.November, 11, 23, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
2023-06-12 15:29:34 +02:00
|
|
|
name string
|
2023-09-24 13:42:05 +02:00
|
|
|
node *types.Node
|
2023-06-12 15:29:34 +02:00
|
|
|
dnsConfig *tailcfg.DNSConfig
|
|
|
|
|
baseDomain string
|
|
|
|
|
want *tailcfg.Node
|
|
|
|
|
wantErr bool
|
2023-05-31 09:59:37 +02:00
|
|
|
}{
|
|
|
|
|
{
|
2023-11-21 18:20:06 +01:00
|
|
|
name: "empty-node",
|
|
|
|
|
node: &types.Node{
|
2024-08-19 11:41:05 +02:00
|
|
|
GivenName: "empty",
|
|
|
|
|
Hostinfo: &tailcfg.Hostinfo{},
|
2023-11-21 18:20:06 +01:00
|
|
|
},
|
2023-06-12 15:29:34 +02:00
|
|
|
dnsConfig: &tailcfg.DNSConfig{},
|
|
|
|
|
baseDomain: "",
|
2023-11-19 22:37:04 +01:00
|
|
|
want: &tailcfg.Node{
|
2024-08-19 11:41:05 +02:00
|
|
|
Name: "empty",
|
2023-11-19 22:37:04 +01:00
|
|
|
StableID: "0",
|
2025-02-05 07:17:51 +01:00
|
|
|
HomeDERP: 0,
|
|
|
|
|
LegacyDERPString: "127.3.3.40:0",
|
2023-11-19 22:37:04 +01:00
|
|
|
Hostinfo: hiview(tailcfg.Hostinfo{}),
|
|
|
|
|
MachineAuthorized: true,
|
2024-09-24 18:34:20 +02:00
|
|
|
|
|
|
|
|
CapMap: tailcfg.NodeCapMap{
|
2026-05-11 16:28:09 +00:00
|
|
|
tailcfg.CapabilityAdmin: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilitySSH: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilityFileSharing: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.NodeAttrDefaultAutoUpdate: []tailcfg.RawMessage{tailcfg.RawMessage("false")},
|
2023-11-19 22:37:04 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
wantErr: false,
|
2023-05-31 09:59:37 +02:00
|
|
|
},
|
|
|
|
|
{
|
2023-09-24 13:42:05 +02:00
|
|
|
name: "minimal-node",
|
|
|
|
|
node: &types.Node{
|
2023-11-19 22:37:04 +01:00
|
|
|
ID: 0,
|
|
|
|
|
MachineKey: mustMK(
|
|
|
|
|
"mkey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
|
),
|
|
|
|
|
NodeKey: mustNK(
|
|
|
|
|
"nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
|
),
|
|
|
|
|
DiscoKey: mustDK(
|
|
|
|
|
"discokey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
|
|
|
|
),
|
2024-04-17 07:03:06 +02:00
|
|
|
IPv4: iap("100.64.0.1"),
|
2023-05-31 09:59:37 +02:00
|
|
|
Hostname: "mini",
|
|
|
|
|
GivenName: "mini",
|
2026-02-06 21:39:35 +00:00
|
|
|
UserID: new(uint(0)),
|
2025-12-02 12:01:25 +01:00
|
|
|
User: &types.User{
|
2023-05-31 09:59:37 +02:00
|
|
|
Name: "mini",
|
|
|
|
|
},
|
2025-12-02 12:01:25 +01:00
|
|
|
Tags: []string{},
|
|
|
|
|
AuthKey: &types.PreAuthKey{},
|
|
|
|
|
LastSeen: &lastSeen,
|
|
|
|
|
Expiry: &expire,
|
2025-02-26 07:22:55 -08:00
|
|
|
Hostinfo: &tailcfg.Hostinfo{
|
|
|
|
|
RoutableIPs: []netip.Prefix{
|
|
|
|
|
tsaddr.AllIPv4(),
|
2025-11-01 14:28:32 +01:00
|
|
|
tsaddr.AllIPv6(),
|
2025-02-26 07:22:55 -08:00
|
|
|
netip.MustParsePrefix("192.168.0.0/24"),
|
|
|
|
|
netip.MustParsePrefix("172.0.0.0/10"),
|
2023-05-31 15:11:09 +02:00
|
|
|
},
|
2023-05-31 09:59:37 +02:00
|
|
|
},
|
2025-11-01 14:28:32 +01:00
|
|
|
ApprovedRoutes: []netip.Prefix{tsaddr.AllIPv4(), tsaddr.AllIPv6(), netip.MustParsePrefix("192.168.0.0/24")},
|
2025-02-26 07:22:55 -08:00
|
|
|
CreatedAt: created,
|
2023-05-31 09:59:37 +02:00
|
|
|
},
|
2023-06-12 15:29:34 +02:00
|
|
|
dnsConfig: &tailcfg.DNSConfig{},
|
|
|
|
|
baseDomain: "",
|
2023-05-31 09:59:37 +02:00
|
|
|
want: &tailcfg.Node{
|
|
|
|
|
ID: 0,
|
|
|
|
|
StableID: "0",
|
|
|
|
|
Name: "mini",
|
|
|
|
|
|
|
|
|
|
User: 0,
|
|
|
|
|
|
|
|
|
|
Key: mustNK(
|
|
|
|
|
"nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
|
),
|
|
|
|
|
KeyExpiry: expire,
|
|
|
|
|
|
|
|
|
|
Machine: mustMK(
|
|
|
|
|
"mkey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
|
),
|
|
|
|
|
DiscoKey: mustDK(
|
|
|
|
|
"discokey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
|
|
|
|
),
|
|
|
|
|
Addresses: []netip.Prefix{netip.MustParsePrefix("100.64.0.1/32")},
|
|
|
|
|
AllowedIPs: []netip.Prefix{
|
2024-10-02 09:06:09 +02:00
|
|
|
tsaddr.AllIPv4(),
|
2025-03-21 11:49:32 +01:00
|
|
|
netip.MustParsePrefix("100.64.0.1/32"),
|
2026-02-06 21:39:35 +00:00
|
|
|
netip.MustParsePrefix("192.168.0.0/24"),
|
2025-03-21 11:49:32 +01:00
|
|
|
tsaddr.AllIPv6(),
|
|
|
|
|
},
|
|
|
|
|
PrimaryRoutes: []netip.Prefix{
|
|
|
|
|
netip.MustParsePrefix("192.168.0.0/24"),
|
2023-05-31 09:59:37 +02:00
|
|
|
},
|
2025-02-05 07:17:51 +01:00
|
|
|
HomeDERP: 0,
|
|
|
|
|
LegacyDERPString: "127.3.3.40:0",
|
2025-02-26 07:22:55 -08:00
|
|
|
Hostinfo: hiview(tailcfg.Hostinfo{
|
|
|
|
|
RoutableIPs: []netip.Prefix{
|
|
|
|
|
tsaddr.AllIPv4(),
|
2025-11-01 14:28:32 +01:00
|
|
|
tsaddr.AllIPv6(),
|
2025-02-26 07:22:55 -08:00
|
|
|
netip.MustParsePrefix("192.168.0.0/24"),
|
|
|
|
|
netip.MustParsePrefix("172.0.0.0/10"),
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
Created: created,
|
2023-05-31 09:59:37 +02:00
|
|
|
|
|
|
|
|
Tags: []string{},
|
|
|
|
|
|
|
|
|
|
MachineAuthorized: true,
|
|
|
|
|
|
2024-09-24 18:34:20 +02:00
|
|
|
CapMap: tailcfg.NodeCapMap{
|
2026-05-11 16:28:09 +00:00
|
|
|
tailcfg.CapabilityAdmin: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilitySSH: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilityFileSharing: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.NodeAttrDefaultAutoUpdate: []tailcfg.RawMessage{tailcfg.RawMessage("false")},
|
2023-05-31 09:59:37 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
wantErr: false,
|
|
|
|
|
},
|
2025-04-11 12:39:08 +02:00
|
|
|
{
|
|
|
|
|
name: "check-dot-suffix-on-node-name",
|
|
|
|
|
node: &types.Node{
|
|
|
|
|
GivenName: "minimal",
|
|
|
|
|
Hostinfo: &tailcfg.Hostinfo{},
|
|
|
|
|
},
|
|
|
|
|
dnsConfig: &tailcfg.DNSConfig{},
|
|
|
|
|
baseDomain: "example.com",
|
|
|
|
|
want: &tailcfg.Node{
|
|
|
|
|
// a node name should have a dot appended
|
|
|
|
|
Name: "minimal.example.com.",
|
|
|
|
|
StableID: "0",
|
|
|
|
|
HomeDERP: 0,
|
|
|
|
|
LegacyDERPString: "127.3.3.40:0",
|
|
|
|
|
Hostinfo: hiview(tailcfg.Hostinfo{}),
|
|
|
|
|
MachineAuthorized: true,
|
|
|
|
|
|
|
|
|
|
CapMap: tailcfg.NodeCapMap{
|
2026-05-11 16:28:09 +00:00
|
|
|
tailcfg.CapabilityAdmin: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilitySSH: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilityFileSharing: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.NodeAttrDefaultAutoUpdate: []tailcfg.RawMessage{tailcfg.RawMessage("false")},
|
2025-04-11 12:39:08 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
wantErr: false,
|
|
|
|
|
},
|
2023-05-31 09:59:37 +02:00
|
|
|
// TODO: Add tests to check other aspects of the node conversion:
|
|
|
|
|
// - With tags and policy
|
|
|
|
|
// - dnsconfig and basedomain
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2026-05-11 14:53:09 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
2024-02-23 10:59:24 +01:00
|
|
|
cfg := &types.Config{
|
2026-05-11 14:49:36 +00:00
|
|
|
BaseDomain: tt.baseDomain,
|
|
|
|
|
TailcfgDNSConfig: tt.dnsConfig,
|
|
|
|
|
Taildrop: types.TaildropConfig{Enabled: true},
|
2024-02-23 10:59:24 +01:00
|
|
|
}
|
2025-02-26 07:22:55 -08:00
|
|
|
|
2026-04-28 12:17:04 +00:00
|
|
|
// Stub primary-route lookup: tt.node owns its SubnetRoutes,
|
|
|
|
|
// node ID 2 owns 192.168.0.0/24 (a hack carried over from
|
|
|
|
|
// the original routes-package-driven version of this test —
|
|
|
|
|
// avoids spinning up a second node just to validate that
|
|
|
|
|
// other nodes' primaries don't leak into tt.node's TailNode
|
|
|
|
|
// output).
|
|
|
|
|
primaries := map[types.NodeID][]netip.Prefix{
|
|
|
|
|
tt.node.ID: tt.node.SubnetRoutes(),
|
|
|
|
|
2: {netip.MustParsePrefix("192.168.0.0/24")},
|
|
|
|
|
}
|
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
|
|
|
nv := tt.node.View()
|
|
|
|
|
got, err := nv.TailNode(
|
2023-09-28 12:33:53 -07:00
|
|
|
0,
|
2025-05-04 22:52:47 +03:00
|
|
|
func(id types.NodeID) []netip.Prefix {
|
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
|
|
|
// Route function returns primaries + exit routes
|
|
|
|
|
// (matching the real caller contract).
|
2026-04-28 12:17:04 +00:00
|
|
|
return slices.Concat(primaries[id], nv.ExitRoutes())
|
2025-05-04 22:52:47 +03:00
|
|
|
},
|
2024-02-23 10:59:24 +01:00
|
|
|
cfg,
|
2026-05-11 14:53:09 +00:00
|
|
|
nil,
|
2023-05-31 09:59:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (err != nil) != tt.wantErr {
|
2025-12-10 09:16:22 +01:00
|
|
|
t.Errorf("TailNode() error = %v, wantErr %v", err, tt.wantErr)
|
2023-05-31 09:59:37 +02:00
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tt.want, got, cmpopts.EquateEmpty()); diff != "" {
|
2025-12-10 09:16:22 +01:00
|
|
|
t.Errorf("TailNode() unexpected result (-want +got):\n%s", diff)
|
2023-05-31 09:59:37 +02:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-03 00:22:17 -07:00
|
|
|
|
2026-05-11 16:28:09 +00:00
|
|
|
// TestTailNodeBaselineGates focuses on the cfg-driven baseline cap
|
|
|
|
|
// emission: cfg.Taildrop.Enabled gates [tailcfg.CapabilityFileSharing]
|
|
|
|
|
// and cfg.AutoUpdate.Enabled controls the value of
|
|
|
|
|
// [tailcfg.NodeAttrDefaultAutoUpdate]. Admin and SSH are unconditional
|
|
|
|
|
// baseline.
|
|
|
|
|
func TestTailNodeBaselineGates(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
autoUpdate := func(b bool) []tailcfg.RawMessage {
|
|
|
|
|
if b {
|
|
|
|
|
return []tailcfg.RawMessage{tailcfg.RawMessage("true")}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return []tailcfg.RawMessage{tailcfg.RawMessage("false")}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
cfg *types.Config
|
|
|
|
|
want tailcfg.NodeCapMap
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "taildrop_on_autoupdate_off",
|
|
|
|
|
cfg: &types.Config{
|
|
|
|
|
Taildrop: types.TaildropConfig{Enabled: true},
|
|
|
|
|
AutoUpdate: types.AutoUpdateConfig{Enabled: false},
|
|
|
|
|
},
|
|
|
|
|
want: tailcfg.NodeCapMap{
|
|
|
|
|
tailcfg.CapabilityAdmin: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilitySSH: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilityFileSharing: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.NodeAttrDefaultAutoUpdate: autoUpdate(false),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "taildrop_off_autoupdate_off",
|
|
|
|
|
cfg: &types.Config{
|
|
|
|
|
Taildrop: types.TaildropConfig{Enabled: false},
|
|
|
|
|
AutoUpdate: types.AutoUpdateConfig{Enabled: false},
|
|
|
|
|
},
|
|
|
|
|
want: tailcfg.NodeCapMap{
|
|
|
|
|
tailcfg.CapabilityAdmin: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilitySSH: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.NodeAttrDefaultAutoUpdate: autoUpdate(false),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "taildrop_on_autoupdate_on",
|
|
|
|
|
cfg: &types.Config{
|
|
|
|
|
Taildrop: types.TaildropConfig{Enabled: true},
|
|
|
|
|
AutoUpdate: types.AutoUpdateConfig{Enabled: true},
|
|
|
|
|
},
|
|
|
|
|
want: tailcfg.NodeCapMap{
|
|
|
|
|
tailcfg.CapabilityAdmin: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilitySSH: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilityFileSharing: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.NodeAttrDefaultAutoUpdate: autoUpdate(true),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "taildrop_off_autoupdate_on",
|
|
|
|
|
cfg: &types.Config{
|
|
|
|
|
Taildrop: types.TaildropConfig{Enabled: false},
|
|
|
|
|
AutoUpdate: types.AutoUpdateConfig{Enabled: true},
|
|
|
|
|
},
|
|
|
|
|
want: tailcfg.NodeCapMap{
|
|
|
|
|
tailcfg.CapabilityAdmin: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.CapabilitySSH: []tailcfg.RawMessage{},
|
|
|
|
|
tailcfg.NodeAttrDefaultAutoUpdate: autoUpdate(true),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
node := &types.Node{GivenName: "baseline-node", Hostinfo: &tailcfg.Hostinfo{}}
|
|
|
|
|
|
|
|
|
|
got, err := node.View().TailNode(
|
|
|
|
|
0,
|
|
|
|
|
func(types.NodeID) []netip.Prefix { return nil },
|
|
|
|
|
tt.cfg,
|
|
|
|
|
nil,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("TailNode: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tt.want, got.CapMap, cmpopts.EquateEmpty()); diff != "" {
|
|
|
|
|
t.Errorf("CapMap mismatch (-want +got):\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 09:58:11 +00:00
|
|
|
// TestTailNodeDisableIPv4 asserts that a node with the disable-ipv4
|
|
|
|
|
// nodeAttr has its own IPv4 (the CGNAT /32) stripped from Addresses
|
|
|
|
|
// and AllowedIPs, while subnet routes the node advertises -- even
|
|
|
|
|
// IPv4 ones -- remain in AllowedIPs and PrimaryRoutes. Matches the
|
|
|
|
|
// SaaS behaviour captured in
|
|
|
|
|
// hscontrol/policy/v2/testdata/nodeattrs_results/nodeattrs-attr-c1{5,6}-disable-ipv4*.hujson.
|
|
|
|
|
func TestTailNodeDisableIPv4(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
const NodeAttrDisableIPv4 tailcfg.NodeCapability = "disable-ipv4"
|
|
|
|
|
|
|
|
|
|
v4 := iap("100.64.0.1")
|
|
|
|
|
v6Addr := netip.MustParseAddr("fd7a:115c:a1e0::1")
|
|
|
|
|
v6 := &v6Addr
|
|
|
|
|
subnet := netip.MustParsePrefix("10.33.0.0/16")
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
hasCap bool
|
|
|
|
|
approved []netip.Prefix
|
|
|
|
|
wantAllowed []netip.Prefix
|
|
|
|
|
wantPrimary []netip.Prefix
|
|
|
|
|
wantAddrs []netip.Prefix
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "no-cap_emits_both_families",
|
|
|
|
|
hasCap: false,
|
|
|
|
|
wantAllowed: []netip.Prefix{netip.MustParsePrefix("100.64.0.1/32"), netip.MustParsePrefix("fd7a:115c:a1e0::1/128")},
|
|
|
|
|
wantAddrs: []netip.Prefix{netip.MustParsePrefix("100.64.0.1/32"), netip.MustParsePrefix("fd7a:115c:a1e0::1/128")},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "cap_strips_own_ipv4",
|
|
|
|
|
hasCap: true,
|
|
|
|
|
wantAllowed: []netip.Prefix{netip.MustParsePrefix("fd7a:115c:a1e0::1/128")},
|
|
|
|
|
wantAddrs: []netip.Prefix{netip.MustParsePrefix("fd7a:115c:a1e0::1/128")},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "cap_keeps_advertised_subnet_route",
|
|
|
|
|
hasCap: true,
|
|
|
|
|
approved: []netip.Prefix{subnet},
|
|
|
|
|
// AllowedIPs is sorted by netip.Prefix.Compare so IPv4
|
|
|
|
|
// sorts before IPv6.
|
|
|
|
|
wantAllowed: []netip.Prefix{
|
|
|
|
|
subnet,
|
|
|
|
|
netip.MustParsePrefix("fd7a:115c:a1e0::1/128"),
|
|
|
|
|
},
|
|
|
|
|
wantPrimary: []netip.Prefix{subnet},
|
|
|
|
|
wantAddrs: []netip.Prefix{netip.MustParsePrefix("fd7a:115c:a1e0::1/128")},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
node := &types.Node{
|
|
|
|
|
GivenName: "ipv4-disabled-node",
|
|
|
|
|
IPv4: v4,
|
|
|
|
|
IPv6: v6,
|
|
|
|
|
Hostinfo: &tailcfg.Hostinfo{
|
|
|
|
|
RoutableIPs: tt.approved,
|
|
|
|
|
},
|
|
|
|
|
ApprovedRoutes: tt.approved,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var selfCaps tailcfg.NodeCapMap
|
|
|
|
|
if tt.hasCap {
|
|
|
|
|
selfCaps = tailcfg.NodeCapMap{NodeAttrDisableIPv4: nil}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
got, err := node.View().TailNode(
|
|
|
|
|
0,
|
|
|
|
|
func(types.NodeID) []netip.Prefix {
|
|
|
|
|
return tt.approved
|
|
|
|
|
},
|
|
|
|
|
&types.Config{Taildrop: types.TaildropConfig{Enabled: true}},
|
|
|
|
|
selfCaps,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("TailNode: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prefStrings := func(ps []netip.Prefix) []string {
|
|
|
|
|
out := make([]string, len(ps))
|
|
|
|
|
for i, p := range ps {
|
|
|
|
|
out[i] = p.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if diff := cmp.Diff(prefStrings(tt.wantAddrs), prefStrings(got.Addresses), cmpopts.EquateEmpty()); diff != "" {
|
|
|
|
|
t.Errorf("Addresses (-want +got):\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if diff := cmp.Diff(prefStrings(tt.wantAllowed), prefStrings(got.AllowedIPs), cmpopts.EquateEmpty()); diff != "" {
|
|
|
|
|
t.Errorf("AllowedIPs (-want +got):\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if diff := cmp.Diff(prefStrings(tt.wantPrimary), prefStrings(got.PrimaryRoutes), cmpopts.EquateEmpty()); diff != "" {
|
|
|
|
|
t.Errorf("PrimaryRoutes (-want +got):\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 00:22:17 -07:00
|
|
|
func TestNodeExpiry(t *testing.T) {
|
|
|
|
|
tp := func(t time.Time) *time.Time {
|
|
|
|
|
return &t
|
|
|
|
|
}
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
exp *time.Time
|
|
|
|
|
wantTime time.Time
|
|
|
|
|
wantTimeZero bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "no-expiry",
|
|
|
|
|
exp: nil,
|
|
|
|
|
wantTimeZero: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "zero-expiry",
|
|
|
|
|
exp: &time.Time{},
|
|
|
|
|
wantTimeZero: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "localtime",
|
2026-02-06 21:45:32 +01:00
|
|
|
exp: tp(time.Time{}.Local()), //nolint:gosmopolitan
|
2024-09-03 00:22:17 -07:00
|
|
|
wantTimeZero: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
node := &types.Node{
|
2025-05-04 22:52:47 +03:00
|
|
|
ID: 0,
|
2024-09-03 00:22:17 -07:00
|
|
|
GivenName: "test",
|
|
|
|
|
Expiry: tt.exp,
|
|
|
|
|
}
|
2025-05-04 22:52:47 +03:00
|
|
|
|
2025-12-10 09:16:22 +01:00
|
|
|
tn, err := node.View().TailNode(
|
2024-09-03 00:22:17 -07:00
|
|
|
0,
|
2025-05-04 22:52:47 +03:00
|
|
|
func(id types.NodeID) []netip.Prefix {
|
|
|
|
|
return []netip.Prefix{}
|
|
|
|
|
},
|
2025-12-12 11:35:16 +01:00
|
|
|
&types.Config{Taildrop: types.TaildropConfig{Enabled: true}},
|
2026-05-11 14:53:09 +00:00
|
|
|
nil,
|
2024-09-03 00:22:17 -07:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("nodeExpiry() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Round trip the node through JSON to ensure the time is serialized correctly
|
|
|
|
|
seri, err := json.Marshal(tn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("nodeExpiry() error = %v", err)
|
|
|
|
|
}
|
2026-02-06 21:45:32 +01:00
|
|
|
|
2024-09-03 00:22:17 -07:00
|
|
|
var deseri tailcfg.Node
|
2026-02-06 21:45:32 +01:00
|
|
|
|
2024-09-03 00:22:17 -07:00
|
|
|
err = json.Unmarshal(seri, &deseri)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("nodeExpiry() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if tt.wantTimeZero {
|
|
|
|
|
if !deseri.KeyExpiry.IsZero() {
|
|
|
|
|
t.Errorf("nodeExpiry() = %v, want zero", deseri.KeyExpiry)
|
|
|
|
|
}
|
|
|
|
|
} else if deseri.KeyExpiry != tt.wantTime {
|
|
|
|
|
t.Errorf("nodeExpiry() = %v, want %v", deseri.KeyExpiry, tt.wantTime)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|