2021-05-12 00:55:36 +00:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2022-09-01 22:06:19 +00:00
|
|
|
"net/netip"
|
|
|
|
|
2021-05-12 00:55:36 +00:00
|
|
|
"gopkg.in/check.v1"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestGetRoutes(c *check.C) {
|
2021-11-15 16:16:04 +00:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-08-25 10:12:41 +00:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
_, err = app.GetMachine("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
|
|
|
}
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
machine := Machine{
|
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",
|
2021-11-15 16:16:04 +00:00
|
|
|
NamespaceID: namespace.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
|
|
|
|
2022-11-24 16:06:24 +00:00
|
|
|
err = app.processMachineRoutes(&machine)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
advertisedRoutes, err := app.GetAdvertisedRoutes(&machine)
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2022-11-24 16:06:24 +00:00
|
|
|
c.Assert(len(advertisedRoutes), check.Equals, 1)
|
2021-05-12 00:55:36 +00:00
|
|
|
|
2022-11-24 16:06:24 +00:00
|
|
|
err = app.EnableRoutes(&machine, "192.168.0.0/24")
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-11-24 16:06:24 +00:00
|
|
|
err = app.EnableRoutes(&machine, "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) {
|
2021-11-15 16:16:04 +00:00
|
|
|
namespace, err := app.CreateNamespace("test")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-08-25 10:12:41 +00:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
_, err = app.GetMachine("test", "test_enable_route_machine")
|
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
|
|
|
}
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
machine := Machine{
|
2021-08-21 13:49:46 +00:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-06-26 09:43:17 +00:00
|
|
|
Hostname: "test_enable_route_machine",
|
2021-11-15 16:16:04 +00:00
|
|
|
NamespaceID: namespace.ID,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-08-21 13:49:46 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2022-03-01 16:34:35 +00:00
|
|
|
HostInfo: HostInfo(hostInfo),
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(&machine)
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2022-11-24 16:06:24 +00:00
|
|
|
err = app.processMachineRoutes(&machine)
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-24 16:06:24 +00: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 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
|
|
|
|
2022-11-24 16:06:24 +00:00
|
|
|
err = app.EnableRoutes(&machine, "192.168.0.0/24")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-11-24 16:06:24 +00:00
|
|
|
err = app.EnableRoutes(&machine, "10.0.0.0/24")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-24 16:06:24 +00:00
|
|
|
enabledRoutes, err := app.GetEnabledRoutes(&machine)
|
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
|
2022-11-24 16:06:24 +00:00
|
|
|
err = app.EnableRoutes(&machine, "10.0.0.0/24")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-24 16:06:24 +00:00
|
|
|
enableRoutesAfterDoubleApply, err := app.GetEnabledRoutes(&machine)
|
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
|
|
|
|
2022-11-24 16:06:24 +00:00
|
|
|
err = app.EnableRoutes(&machine, "150.0.10.0/25")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-24 16:06:24 +00:00
|
|
|
enabledRoutesWithAdditionalRoute, err := app.GetEnabledRoutes(&machine)
|
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
|
|
|
}
|