2023-05-21 16:37:59 +00:00
|
|
|
package db
|
2021-05-12 00:55:36 +00:00
|
|
|
|
|
|
|
import (
|
2022-09-01 22:06:19 +00:00
|
|
|
"net/netip"
|
2022-11-24 22:41:11 +00:00
|
|
|
"time"
|
2022-09-01 22:06:19 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-05-11 07:09:18 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2021-05-12 00:55:36 +00:00
|
|
|
"gopkg.in/check.v1"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestGetRoutes(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
_, err = db.GetNode("test", "test_get_route_node")
|
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-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2021-05-12 00:55:36 +00:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_get_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2021-05-12 00:55:36 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
HostInfo: types.HostInfo(hostInfo),
|
2021-05-12 00:55:36 +00:00
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
db.db.Save(&node)
|
2021-05-12 00:55:36 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.SaveNodeRoutes(&node)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
advertisedRoutes, err := db.GetAdvertisedRoutes(&node)
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(len(advertisedRoutes), check.Equals, 1)
|
2021-05-12 00:55:36 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node, "192.168.0.0/24")
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node, "10.0.0.0/24")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetEnableRoutes(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
_, err = db.GetNode("test", "test_enable_route_node")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-09-01 22:06:19 +00:00
|
|
|
route, err := netip.ParsePrefix(
|
2021-08-21 13:49:46 +00:00
|
|
|
"10.0.0.0/24",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-09-01 22:06:19 +00:00
|
|
|
route2, err := netip.ParsePrefix(
|
2021-08-21 13:49:46 +00:00
|
|
|
"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},
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2021-08-21 13:49:46 +00:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2021-08-21 13:49:46 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
HostInfo: types.HostInfo(hostInfo),
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
db.db.Save(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.SaveNodeRoutes(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
availableRoutes, err := db.GetAdvertisedRoutes(&node)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(availableRoutes), check.Equals, 2)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
noEnabledRoutes, err := db.GetEnabledRoutes(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(noEnabledRoutes), check.Equals, 0)
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node, "192.168.0.0/24")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node, "10.0.0.0/24")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutes, err := db.GetEnabledRoutes(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(enabledRoutes), check.Equals, 1)
|
2021-08-21 13:49:46 +00:00
|
|
|
|
|
|
|
// Adding it twice will just let it pass through
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node, "10.0.0.0/24")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enableRoutesAfterDoubleApply, err := db.GetEnabledRoutes(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(enableRoutesAfterDoubleApply), check.Equals, 1)
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node, "150.0.10.0/25")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutesWithAdditionalRoute, err := db.GetEnabledRoutes(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
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
|
|
|
}
|
2022-11-24 16:00:40 +00:00
|
|
|
|
|
|
|
func (s *Suite) TestIsUniquePrefix(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
_, err = db.GetNode("test", "test_enable_route_node")
|
2022-11-24 16:00:40 +00:00
|
|
|
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-09-24 11:42:05 +00:00
|
|
|
node1 := types.Node{
|
2022-11-24 22:41:11 +00:00
|
|
|
ID: 1,
|
2022-11-24 16:00:40 +00:00
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2022-11-24 16:00:40 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
HostInfo: types.HostInfo(hostInfo1),
|
2022-11-24 16:00:40 +00:00
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
db.db.Save(&node1)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.SaveNodeRoutes(&node1)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node1, route.String())
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node1, route2.String())
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
hostInfo2 := tailcfg.Hostinfo{
|
|
|
|
RoutableIPs: []netip.Prefix{route2},
|
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
node2 := types.Node{
|
2022-11-24 22:41:11 +00:00
|
|
|
ID: 2,
|
2022-11-24 16:00:40 +00:00
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2022-11-24 16:00:40 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
HostInfo: types.HostInfo(hostInfo2),
|
2022-11-24 16:00:40 +00:00
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
db.db.Save(&node2)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.SaveNodeRoutes(&node2)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node2, route2.String())
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutes1, err := db.GetEnabledRoutes(&node1)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(enabledRoutes1), check.Equals, 2)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutes2, err := db.GetEnabledRoutes(&node2)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(enabledRoutes2), check.Equals, 1)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err := db.GetNodePrimaryRoutes(&node1)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 2)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err = db.GetNodePrimaryRoutes(&node2)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 0)
|
|
|
|
}
|
2022-11-24 22:41:11 +00:00
|
|
|
|
|
|
|
func (s *Suite) TestSubnetFailover(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
_, err = db.GetNode("test", "test_enable_route_node")
|
2022-11-24 22:41:11 +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-09-24 11:42:05 +00:00
|
|
|
node1 := types.Node{
|
2022-11-24 22:41:11 +00:00
|
|
|
ID: 1,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2022-11-24 22:41:11 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
HostInfo: types.HostInfo(hostInfo1),
|
2022-11-24 22:41:11 +00:00
|
|
|
LastSeen: &now,
|
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
db.db.Save(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.SaveNodeRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node1, prefix.String())
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node1, prefix2.String())
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
err = db.HandlePrimarySubnetFailover()
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutes1, err := db.GetEnabledRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(enabledRoutes1), check.Equals, 2)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
route, err := db.getPrimaryRoute(prefix)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-09-24 11:42:05 +00:00
|
|
|
c.Assert(route.NodeID, check.Equals, node1.ID)
|
2022-11-24 22:41:11 +00:00
|
|
|
|
|
|
|
hostInfo2 := tailcfg.Hostinfo{
|
|
|
|
RoutableIPs: []netip.Prefix{prefix2},
|
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
node2 := types.Node{
|
2022-11-24 22:41:11 +00:00
|
|
|
ID: 2,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2022-11-24 22:41:11 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
HostInfo: types.HostInfo(hostInfo2),
|
2022-11-24 22:41:11 +00:00
|
|
|
LastSeen: &now,
|
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
db.db.Save(&node2)
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.saveNodeRoutes(&node2)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node2, prefix2.String())
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
err = db.HandlePrimarySubnetFailover()
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutes1, err = db.GetEnabledRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(enabledRoutes1), check.Equals, 2)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutes2, err := db.GetEnabledRoutes(&node2)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(enabledRoutes2), check.Equals, 1)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err := db.GetNodePrimaryRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 2)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err = db.GetNodePrimaryRoutes(&node2)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 0)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
// lets make node1 lastseen 10 mins ago
|
2022-11-24 22:41:11 +00:00
|
|
|
before := now.Add(-10 * time.Minute)
|
2023-09-24 11:42:05 +00:00
|
|
|
node1.LastSeen = &before
|
|
|
|
err = db.db.Save(&node1).Error
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
err = db.HandlePrimarySubnetFailover()
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err = db.GetNodePrimaryRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 1)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err = db.GetNodePrimaryRoutes(&node2)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 1)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node2.HostInfo = types.HostInfo(tailcfg.Hostinfo{
|
2022-11-24 22:41:11 +00:00
|
|
|
RoutableIPs: []netip.Prefix{prefix, prefix2},
|
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.db.Save(&node2).Error
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.SaveNodeRoutes(&node2)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node2, prefix.String())
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
err = db.HandlePrimarySubnetFailover()
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err = db.GetNodePrimaryRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 0)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err = db.GetNodePrimaryRoutes(&node2)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 2)
|
|
|
|
}
|
2023-01-01 11:53:49 +00:00
|
|
|
|
2023-03-06 08:05:40 +00:00
|
|
|
func (s *Suite) TestDeleteRoutes(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
2023-03-06 08:05:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2023-03-06 08:05:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
_, err = db.GetNode("test", "test_enable_route_node")
|
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-09-24 11:42:05 +00:00
|
|
|
node1 := types.Node{
|
2023-03-06 08:05:40 +00:00
|
|
|
ID: 1,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-03-06 08:05:40 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2023-03-06 08:05:40 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-05-21 16:37:59 +00:00
|
|
|
HostInfo: types.HostInfo(hostInfo1),
|
2023-03-06 08:05:40 +00:00
|
|
|
LastSeen: &now,
|
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
db.db.Save(&node1)
|
2023-03-06 08:05:40 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.SaveNodeRoutes(&node1)
|
2023-03-06 08:05:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node1, prefix.String())
|
2023-03-06 08:05:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
err = db.enableRoutes(&node1, prefix2.String())
|
2023-03-06 08:05:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err := db.GetNodeRoutes(&node1)
|
2023-03-06 08:05:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
err = db.DeleteRoute(uint64(routes[0].ID))
|
2023-03-06 08:05:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutes1, err := db.GetEnabledRoutes(&node1)
|
2023-03-06 08:05:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(enabledRoutes1), check.Equals, 1)
|
|
|
|
}
|