2021-05-12 08:55:36 +08:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2022-09-02 06:06:19 +08:00
|
|
|
"net/netip"
|
2022-11-25 06:41:11 +08:00
|
|
|
"time"
|
2022-09-02 06:06:19 +08:00
|
|
|
|
2021-05-12 08:55:36 +08:00
|
|
|
"gopkg.in/check.v1"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestGetRoutes(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-08-25 18:12:41 +08:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = app.GetMachine("test", "test_get_route_machine")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-09-02 06:06:19 +08:00
|
|
|
route, err := netip.ParsePrefix("10.0.0.0/24")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
2022-09-02 06:06:19 +08:00
|
|
|
RoutableIPs: []netip.Prefix{route},
|
2021-05-12 08:55:36 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
machine := Machine{
|
2021-05-12 08:55:36 +08:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-06-26 17:43:17 +08:00
|
|
|
Hostname: "test_get_route_machine",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-12 08:55:36 +08:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-03-02 00:34:35 +08:00
|
|
|
HostInfo: HostInfo(hostInfo),
|
2021-05-12 08:55:36 +08:00
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
2021-05-12 08:55:36 +08:00
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
err = app.processMachineRoutes(&machine)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
advertisedRoutes, err := app.GetAdvertisedRoutes(&machine)
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2022-11-25 00:00:40 +08:00
|
|
|
c.Assert(len(advertisedRoutes), check.Equals, 1)
|
2021-05-12 08:55:36 +08:00
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
err = app.EnableRoutes(&machine, "192.168.0.0/24")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
err = app.EnableRoutes(&machine, "10.0.0.0/24")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetEnableRoutes(c *check.C) {
|
2021-11-16 00:16:04 +08:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-08-25 18:12:41 +08:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
_, err = app.GetMachine("test", "test_enable_route_machine")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-09-02 06:06:19 +08:00
|
|
|
route, err := netip.ParsePrefix(
|
2021-08-21 21:49:46 +08:00
|
|
|
"10.0.0.0/24",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-09-02 06:06:19 +08:00
|
|
|
route2, err := netip.ParsePrefix(
|
2021-08-21 21:49:46 +08:00
|
|
|
"150.0.10.0/25",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
2022-09-02 06:06:19 +08:00
|
|
|
RoutableIPs: []netip.Prefix{route, route2},
|
2021-08-21 21:49:46 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
machine := Machine{
|
2021-08-21 21:49:46 +08:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-06-26 17:43:17 +08:00
|
|
|
Hostname: "test_enable_route_machine",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-08-21 21:49:46 +08:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-03-02 00:34:35 +08:00
|
|
|
HostInfo: HostInfo(hostInfo),
|
2021-08-21 21:49:46 +08:00
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
2021-08-21 21:49:46 +08:00
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
err = app.processMachineRoutes(&machine)
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
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)
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
c.Assert(len(noEnabledRoutes), check.Equals, 0)
|
2021-08-21 21:49:46 +08:00
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
err = app.EnableRoutes(&machine, "192.168.0.0/24")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
err = app.EnableRoutes(&machine, "10.0.0.0/24")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
enabledRoutes, err := app.GetEnabledRoutes(&machine)
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
c.Assert(len(enabledRoutes), check.Equals, 1)
|
2021-08-21 21:49:46 +08:00
|
|
|
|
|
|
|
// Adding it twice will just let it pass through
|
2022-11-25 00:00:40 +08:00
|
|
|
err = app.EnableRoutes(&machine, "10.0.0.0/24")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
enableRoutesAfterDoubleApply, err := app.GetEnabledRoutes(&machine)
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
c.Assert(len(enableRoutesAfterDoubleApply), check.Equals, 1)
|
2021-08-21 21:49:46 +08:00
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
err = app.EnableRoutes(&machine, "150.0.10.0/25")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-25 00:00:40 +08:00
|
|
|
enabledRoutesWithAdditionalRoute, err := app.GetEnabledRoutes(&machine)
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
c.Assert(len(enabledRoutesWithAdditionalRoute), check.Equals, 2)
|
2021-05-12 08:55:36 +08:00
|
|
|
}
|
2022-11-25 00:00:40 +08:00
|
|
|
|
|
|
|
func (s *Suite) TestIsUniquePrefix(c *check.C) {
|
|
|
|
namespace, err := app.CreateNamespace("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = app.GetMachine("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},
|
|
|
|
}
|
|
|
|
machine1 := Machine{
|
2022-11-25 06:41:11 +08:00
|
|
|
ID: 1,
|
2022-11-25 00:00:40 +08:00
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Hostname: "test_enable_route_machine",
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
HostInfo: HostInfo(hostInfo1),
|
|
|
|
}
|
|
|
|
app.db.Save(&machine1)
|
|
|
|
|
|
|
|
err = app.processMachineRoutes(&machine1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = app.EnableRoutes(&machine1, route.String())
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = app.EnableRoutes(&machine1, route2.String())
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
hostInfo2 := tailcfg.Hostinfo{
|
|
|
|
RoutableIPs: []netip.Prefix{route2},
|
|
|
|
}
|
|
|
|
machine2 := Machine{
|
2022-11-25 06:41:11 +08:00
|
|
|
ID: 2,
|
2022-11-25 00:00:40 +08:00
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Hostname: "test_enable_route_machine",
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
HostInfo: HostInfo(hostInfo2),
|
|
|
|
}
|
|
|
|
app.db.Save(&machine2)
|
|
|
|
|
|
|
|
err = app.processMachineRoutes(&machine2)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
routes, err := app.getMachinePrimaryRoutes(&machine1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 2)
|
|
|
|
|
|
|
|
routes, err = app.getMachinePrimaryRoutes(&machine2)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 0)
|
|
|
|
}
|
2022-11-25 06:41:11 +08:00
|
|
|
|
|
|
|
func (s *Suite) TestSubnetFailover(c *check.C) {
|
|
|
|
namespace, err := app.CreateNamespace("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = app.GetMachine("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()
|
|
|
|
machine1 := Machine{
|
|
|
|
ID: 1,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Hostname: "test_enable_route_machine",
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
HostInfo: HostInfo(hostInfo1),
|
|
|
|
LastSeen: &now,
|
|
|
|
}
|
|
|
|
app.db.Save(&machine1)
|
|
|
|
|
|
|
|
err = app.processMachineRoutes(&machine1)
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
c.Assert(route.MachineID, check.Equals, machine1.ID)
|
|
|
|
|
|
|
|
hostInfo2 := tailcfg.Hostinfo{
|
|
|
|
RoutableIPs: []netip.Prefix{prefix2},
|
|
|
|
}
|
|
|
|
machine2 := Machine{
|
|
|
|
ID: 2,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
|
|
|
Hostname: "test_enable_route_machine",
|
|
|
|
NamespaceID: namespace.ID,
|
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
HostInfo: HostInfo(hostInfo2),
|
|
|
|
LastSeen: &now,
|
|
|
|
}
|
|
|
|
app.db.Save(&machine2)
|
|
|
|
|
|
|
|
err = app.processMachineRoutes(&machine2)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
routes, err := app.getMachinePrimaryRoutes(&machine1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 2)
|
|
|
|
|
|
|
|
routes, err = app.getMachinePrimaryRoutes(&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)
|
|
|
|
|
|
|
|
routes, err = app.getMachinePrimaryRoutes(&machine1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 1)
|
|
|
|
|
|
|
|
routes, err = app.getMachinePrimaryRoutes(&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)
|
|
|
|
|
|
|
|
err = app.processMachineRoutes(&machine2)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = app.EnableRoutes(&machine2, prefix.String())
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = app.handlePrimarySubnetFailover()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
routes, err = app.getMachinePrimaryRoutes(&machine1)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 0)
|
|
|
|
|
|
|
|
routes, err = app.getMachinePrimaryRoutes(&machine2)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 2)
|
|
|
|
}
|