headscale/routes_test.go

520 lines
13 KiB
Go
Raw Normal View History

2021-05-12 00:55:36 +00:00
package headscale
import (
2022-09-01 22:06:19 +00:00
"net/netip"
"time"
2022-09-01 22:06:19 +00:00
2021-05-12 00:55:36 +00:00
"gopkg.in/check.v1"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
2021-05-12 00:55:36 +00:00
)
func (s *Suite) TestGetRoutes(c *check.C) {
user, err := app.CreateUser("test")
2021-05-12 00:55:36 +00:00
c.Assert(err, check.IsNil)
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
2021-05-12 00:55:36 +00:00
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
_, err = app.GetNode("test", "test_get_route_machine")
2021-05-12 00:55:36 +00:00
c.Assert(err, check.NotNil)
2022-09-01 22:06:19 +00:00
route, err := netip.ParsePrefix("10.0.0.0/24")
2021-05-12 00:55:36 +00:00
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
hostInfo := tailcfg.Hostinfo{
2022-09-01 22:06:19 +00:00
RoutableIPs: []netip.Prefix{route},
2021-05-12 00:55:36 +00:00
}
2023-05-01 14:53:23 +00:00
machine := Node{
2021-05-12 00:55:36 +00:00
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
2022-06-26 09:43:17 +00:00
Hostname: "test_get_route_machine",
UserID: user.ID,
2021-11-18 08:49:55 +00:00
RegisterMethod: RegisterMethodAuthKey,
2021-05-12 00:55:36 +00:00
AuthKeyID: uint(pak.ID),
2022-03-01 16:34:35 +00:00
HostInfo: HostInfo(hostInfo),
2021-05-12 00:55:36 +00:00
}
2021-11-15 16:16:04 +00:00
app.db.Save(&machine)
2021-05-12 00:55:36 +00:00
2023-05-01 14:53:23 +00:00
err = app.processNodeRoutes(&machine)
c.Assert(err, check.IsNil)
advertisedRoutes, err := app.GetAdvertisedRoutes(&machine)
2021-05-12 00:55:36 +00:00
c.Assert(err, check.IsNil)
c.Assert(len(advertisedRoutes), check.Equals, 1)
2021-05-12 00:55:36 +00:00
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine, "192.168.0.0/24")
2021-05-12 00:55:36 +00:00
c.Assert(err, check.NotNil)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine, "10.0.0.0/24")
c.Assert(err, check.IsNil)
}
func (s *Suite) TestGetEnableRoutes(c *check.C) {
user, err := app.CreateUser("test")
c.Assert(err, check.IsNil)
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
2021-05-12 00:55:36 +00:00
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
_, err = app.GetNode("test", "test_enable_route_machine")
c.Assert(err, check.NotNil)
2022-09-01 22:06:19 +00:00
route, err := netip.ParsePrefix(
"10.0.0.0/24",
)
c.Assert(err, check.IsNil)
2022-09-01 22:06:19 +00:00
route2, err := netip.ParsePrefix(
"150.0.10.0/25",
)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
hostInfo := tailcfg.Hostinfo{
2022-09-01 22:06:19 +00:00
RoutableIPs: []netip.Prefix{route, route2},
}
2023-05-01 14:53:23 +00:00
machine := Node{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
2022-06-26 09:43:17 +00:00
Hostname: "test_enable_route_machine",
UserID: user.ID,
2021-11-18 08:49:55 +00:00
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
2022-03-01 16:34:35 +00:00
HostInfo: HostInfo(hostInfo),
}
2021-11-15 16:16:04 +00:00
app.db.Save(&machine)
2023-05-01 14:53:23 +00:00
err = app.processNodeRoutes(&machine)
c.Assert(err, check.IsNil)
availableRoutes, err := app.GetAdvertisedRoutes(&machine)
c.Assert(err, check.IsNil)
c.Assert(err, check.IsNil)
c.Assert(len(availableRoutes), check.Equals, 2)
noEnabledRoutes, err := app.GetEnabledRoutes(&machine)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
c.Assert(len(noEnabledRoutes), check.Equals, 0)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine, "192.168.0.0/24")
c.Assert(err, check.NotNil)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine, "10.0.0.0/24")
c.Assert(err, check.IsNil)
enabledRoutes, err := app.GetEnabledRoutes(&machine)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
c.Assert(len(enabledRoutes), check.Equals, 1)
// Adding it twice will just let it pass through
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine, "10.0.0.0/24")
c.Assert(err, check.IsNil)
enableRoutesAfterDoubleApply, err := app.GetEnabledRoutes(&machine)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
c.Assert(len(enableRoutesAfterDoubleApply), check.Equals, 1)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine, "150.0.10.0/25")
c.Assert(err, check.IsNil)
enabledRoutesWithAdditionalRoute, err := app.GetEnabledRoutes(&machine)
c.Assert(err, check.IsNil)
2021-11-15 16:16:04 +00:00
c.Assert(len(enabledRoutesWithAdditionalRoute), check.Equals, 2)
2021-05-12 00:55:36 +00:00
}
func (s *Suite) TestIsUniquePrefix(c *check.C) {
user, err := app.CreateUser("test")
c.Assert(err, check.IsNil)
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
_, err = app.GetNode("test", "test_enable_route_machine")
c.Assert(err, check.NotNil)
route, err := netip.ParsePrefix(
"10.0.0.0/24",
)
c.Assert(err, check.IsNil)
route2, err := netip.ParsePrefix(
"150.0.10.0/25",
)
c.Assert(err, check.IsNil)
hostInfo1 := tailcfg.Hostinfo{
RoutableIPs: []netip.Prefix{route, route2},
}
2023-05-01 14:53:23 +00:00
machine1 := Node{
ID: 1,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "test_enable_route_machine",
UserID: user.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
HostInfo: HostInfo(hostInfo1),
}
app.db.Save(&machine1)
2023-05-01 14:53:23 +00:00
err = app.processNodeRoutes(&machine1)
c.Assert(err, check.IsNil)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine1, route.String())
c.Assert(err, check.IsNil)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine1, route2.String())
c.Assert(err, check.IsNil)
hostInfo2 := tailcfg.Hostinfo{
RoutableIPs: []netip.Prefix{route2},
}
2023-05-01 14:53:23 +00:00
machine2 := Node{
ID: 2,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "test_enable_route_machine",
UserID: user.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
HostInfo: HostInfo(hostInfo2),
}
app.db.Save(&machine2)
2023-05-01 14:53:23 +00:00
err = app.processNodeRoutes(&machine2)
c.Assert(err, check.IsNil)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine2, route2.String())
c.Assert(err, check.IsNil)
enabledRoutes1, err := app.GetEnabledRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes1), check.Equals, 2)
enabledRoutes2, err := app.GetEnabledRoutes(&machine2)
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes2), check.Equals, 1)
2023-05-01 14:53:23 +00:00
routes, err := app.getNodePrimaryRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(routes), check.Equals, 2)
2023-05-01 14:53:23 +00:00
routes, err = app.getNodePrimaryRoutes(&machine2)
c.Assert(err, check.IsNil)
c.Assert(len(routes), check.Equals, 0)
}
func (s *Suite) TestSubnetFailover(c *check.C) {
user, err := app.CreateUser("test")
c.Assert(err, check.IsNil)
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
_, err = app.GetNode("test", "test_enable_route_machine")
c.Assert(err, check.NotNil)
prefix, err := netip.ParsePrefix(
"10.0.0.0/24",
)
c.Assert(err, check.IsNil)
prefix2, err := netip.ParsePrefix(
"150.0.10.0/25",
)
c.Assert(err, check.IsNil)
hostInfo1 := tailcfg.Hostinfo{
RoutableIPs: []netip.Prefix{prefix, prefix2},
}
now := time.Now()
2023-05-01 14:53:23 +00:00
machine1 := Node{
ID: 1,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "test_enable_route_machine",
UserID: user.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
HostInfo: HostInfo(hostInfo1),
LastSeen: &now,
}
app.db.Save(&machine1)
2023-05-01 14:53:23 +00:00
err = app.processNodeRoutes(&machine1)
c.Assert(err, check.IsNil)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine1, prefix.String())
c.Assert(err, check.IsNil)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine1, prefix2.String())
c.Assert(err, check.IsNil)
err = app.handlePrimarySubnetFailover()
c.Assert(err, check.IsNil)
enabledRoutes1, err := app.GetEnabledRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes1), check.Equals, 2)
route, err := app.getPrimaryRoute(prefix)
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
c.Assert(route.NodeID, check.Equals, machine1.ID)
hostInfo2 := tailcfg.Hostinfo{
RoutableIPs: []netip.Prefix{prefix2},
}
2023-05-01 14:53:23 +00:00
machine2 := Node{
ID: 2,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "test_enable_route_machine",
UserID: user.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
HostInfo: HostInfo(hostInfo2),
LastSeen: &now,
}
app.db.Save(&machine2)
2023-05-01 14:53:23 +00:00
err = app.processNodeRoutes(&machine2)
c.Assert(err, check.IsNil)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine2, prefix2.String())
c.Assert(err, check.IsNil)
err = app.handlePrimarySubnetFailover()
c.Assert(err, check.IsNil)
enabledRoutes1, err = app.GetEnabledRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes1), check.Equals, 2)
enabledRoutes2, err := app.GetEnabledRoutes(&machine2)
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes2), check.Equals, 1)
2023-05-01 14:53:23 +00:00
routes, err := app.getNodePrimaryRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(routes), check.Equals, 2)
2023-05-01 14:53:23 +00:00
routes, err = app.getNodePrimaryRoutes(&machine2)
c.Assert(err, check.IsNil)
c.Assert(len(routes), check.Equals, 0)
// lets make machine1 lastseen 10 mins ago
before := now.Add(-10 * time.Minute)
machine1.LastSeen = &before
err = app.db.Save(&machine1).Error
c.Assert(err, check.IsNil)
err = app.handlePrimarySubnetFailover()
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
routes, err = app.getNodePrimaryRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(routes), check.Equals, 1)
2023-05-01 14:53:23 +00:00
routes, err = app.getNodePrimaryRoutes(&machine2)
c.Assert(err, check.IsNil)
c.Assert(len(routes), check.Equals, 1)
machine2.HostInfo = HostInfo(tailcfg.Hostinfo{
RoutableIPs: []netip.Prefix{prefix, prefix2},
})
err = app.db.Save(&machine2).Error
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
err = app.processNodeRoutes(&machine2)
c.Assert(err, check.IsNil)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine2, prefix.String())
c.Assert(err, check.IsNil)
err = app.handlePrimarySubnetFailover()
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
routes, err = app.getNodePrimaryRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(routes), check.Equals, 0)
2023-05-01 14:53:23 +00:00
routes, err = app.getNodePrimaryRoutes(&machine2)
c.Assert(err, check.IsNil)
c.Assert(len(routes), check.Equals, 2)
}
// TestAllowedIPRoutes tests that the AllowedIPs are correctly set for a node,
// including both the primary routes the node is responsible for, and the
// exit node routes if enabled.
func (s *Suite) TestAllowedIPRoutes(c *check.C) {
user, err := app.CreateUser("test")
c.Assert(err, check.IsNil)
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
_, err = app.GetNode("test", "test_enable_route_machine")
c.Assert(err, check.NotNil)
prefix, err := netip.ParsePrefix(
"10.0.0.0/24",
)
c.Assert(err, check.IsNil)
prefix2, err := netip.ParsePrefix(
"150.0.10.0/25",
)
c.Assert(err, check.IsNil)
prefixExitNodeV4, err := netip.ParsePrefix(
"0.0.0.0/0",
)
c.Assert(err, check.IsNil)
prefixExitNodeV6, err := netip.ParsePrefix(
"::/0",
)
c.Assert(err, check.IsNil)
hostInfo1 := tailcfg.Hostinfo{
RoutableIPs: []netip.Prefix{prefix, prefix2, prefixExitNodeV4, prefixExitNodeV6},
}
nodeKey := key.NewNode()
discoKey := key.NewDisco()
machineKey := key.NewMachine()
now := time.Now()
2023-05-01 14:53:23 +00:00
machine1 := Node{
ID: 1,
MachineKey: MachinePublicKeyStripPrefix(machineKey.Public()),
NodeKey: NodePublicKeyStripPrefix(nodeKey.Public()),
DiscoKey: DiscoPublicKeyStripPrefix(discoKey.Public()),
Hostname: "test_enable_route_machine",
UserID: user.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
HostInfo: HostInfo(hostInfo1),
LastSeen: &now,
}
app.db.Save(&machine1)
2023-05-01 14:53:23 +00:00
err = app.processNodeRoutes(&machine1)
c.Assert(err, check.IsNil)
2023-01-22 22:42:09 +00:00
err = app.enableRoutes(&machine1, prefix.String())
c.Assert(err, check.IsNil)
// We do not enable this one on purpose to test that it is not enabled
2023-01-22 22:42:09 +00:00
// err = app.enableRoutes(&machine1, prefix2.String())
// c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
routes, err := app.GetNodeRoutes(&machine1)
c.Assert(err, check.IsNil)
2023-01-22 22:42:09 +00:00
for _, route := range routes {
if route.isExitRoute() {
err = app.EnableRoute(uint64(route.ID))
c.Assert(err, check.IsNil)
2023-01-22 22:42:09 +00:00
// We only enable one exit route, so we can test that both are enabled
break
}
}
err = app.handlePrimarySubnetFailover()
c.Assert(err, check.IsNil)
enabledRoutes1, err := app.GetEnabledRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes1), check.Equals, 3)
peer, err := app.toNode(machine1, "headscale.net", nil)
c.Assert(err, check.IsNil)
c.Assert(len(peer.AllowedIPs), check.Equals, 3)
foundExitNodeV4 := false
foundExitNodeV6 := false
for _, allowedIP := range peer.AllowedIPs {
if allowedIP == prefixExitNodeV4 {
foundExitNodeV4 = true
}
if allowedIP == prefixExitNodeV6 {
foundExitNodeV6 = true
}
}
c.Assert(foundExitNodeV4, check.Equals, true)
c.Assert(foundExitNodeV6, check.Equals, true)
}
2023-03-06 08:05:40 +00:00
func (s *Suite) TestDeleteRoutes(c *check.C) {
user, err := app.CreateUser("test")
c.Assert(err, check.IsNil)
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
_, err = app.GetNode("test", "test_enable_route_machine")
2023-03-06 08:05:40 +00:00
c.Assert(err, check.NotNil)
prefix, err := netip.ParsePrefix(
"10.0.0.0/24",
)
c.Assert(err, check.IsNil)
prefix2, err := netip.ParsePrefix(
"150.0.10.0/25",
)
c.Assert(err, check.IsNil)
hostInfo1 := tailcfg.Hostinfo{
RoutableIPs: []netip.Prefix{prefix, prefix2},
}
now := time.Now()
2023-05-01 14:53:23 +00:00
machine1 := Node{
2023-03-06 08:05:40 +00:00
ID: 1,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "test_enable_route_machine",
UserID: user.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
HostInfo: HostInfo(hostInfo1),
LastSeen: &now,
}
app.db.Save(&machine1)
2023-05-01 14:53:23 +00:00
err = app.processNodeRoutes(&machine1)
2023-03-06 08:05:40 +00:00
c.Assert(err, check.IsNil)
err = app.enableRoutes(&machine1, prefix.String())
c.Assert(err, check.IsNil)
err = app.enableRoutes(&machine1, prefix2.String())
c.Assert(err, check.IsNil)
2023-05-01 14:53:23 +00:00
routes, err := app.GetNodeRoutes(&machine1)
2023-03-06 08:05:40 +00:00
c.Assert(err, check.IsNil)
err = app.DeleteRoute(uint64(routes[0].ID))
c.Assert(err, check.IsNil)
enabledRoutes1, err := app.GetEnabledRoutes(&machine1)
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes1), check.Equals, 1)
}