2021-05-12 08:55:36 +08:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"gopkg.in/check.v1"
|
2021-05-15 06:05:41 +08:00
|
|
|
"gorm.io/datatypes"
|
2021-05-12 08:55:36 +08:00
|
|
|
"inet.af/netaddr"
|
|
|
|
"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)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, 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)
|
|
|
|
|
|
|
|
route, err := netaddr.ParseIPPrefix("10.0.0.0/24")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
2021-05-12 08:55:36 +08:00
|
|
|
RoutableIPs: []netaddr.IPPrefix{route},
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
hostinfo, err := json.Marshal(hostInfo)
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
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",
|
2021-08-21 21:49:46 +08:00
|
|
|
Name: "test_get_route_machine",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-05-12 08:55:36 +08:00
|
|
|
Registered: true,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-12 08:55:36 +08:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2021-05-15 06:05:41 +08:00
|
|
|
HostInfo: datatypes.JSON(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
|
|
|
|
2021-11-16 00:16:35 +08:00
|
|
|
advertisedRoutes, err := app.GetAdvertisedNodeRoutes(
|
|
|
|
"test",
|
|
|
|
"test_get_route_machine",
|
|
|
|
)
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-16 00:16:04 +08:00
|
|
|
c.Assert(len(*advertisedRoutes), check.Equals, 1)
|
2021-05-12 08:55:36 +08:00
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
err = app.EnableNodeRoute("test", "test_get_route_machine", "192.168.0.0/24")
|
2021-05-12 08:55:36 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
err = app.EnableNodeRoute("test", "test_get_route_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)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, 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)
|
|
|
|
|
|
|
|
route, err := netaddr.ParseIPPrefix(
|
|
|
|
"10.0.0.0/24",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
route2, err := netaddr.ParseIPPrefix(
|
|
|
|
"150.0.10.0/25",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
2021-08-21 21:49:46 +08:00
|
|
|
RoutableIPs: []netaddr.IPPrefix{route, route2},
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
hostinfo, err := json.Marshal(hostInfo)
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
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",
|
|
|
|
Name: "test_enable_route_machine",
|
2021-11-16 00:16:04 +08:00
|
|
|
NamespaceID: namespace.ID,
|
2021-08-21 21:49:46 +08:00
|
|
|
Registered: true,
|
2021-11-18 16:49:55 +08:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-08-21 21:49:46 +08:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
HostInfo: datatypes.JSON(hostinfo),
|
|
|
|
}
|
2021-11-16 00:16:04 +08:00
|
|
|
app.db.Save(&machine)
|
2021-08-21 21:49:46 +08:00
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
availableRoutes, err := app.GetAdvertisedNodeRoutes(
|
2021-11-13 16:36:45 +08:00
|
|
|
"test",
|
|
|
|
"test_enable_route_machine",
|
|
|
|
)
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(*availableRoutes), check.Equals, 2)
|
|
|
|
|
2021-11-16 00:16:35 +08:00
|
|
|
noEnabledRoutes, err := app.GetEnabledNodeRoutes(
|
|
|
|
"test",
|
|
|
|
"test_enable_route_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
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
err = app.EnableNodeRoute("test", "test_enable_route_machine", "192.168.0.0/24")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
err = app.EnableNodeRoute("test", "test_enable_route_machine", "10.0.0.0/24")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
enabledRoutes, err := app.GetEnabledNodeRoutes("test", "test_enable_route_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
|
2021-11-16 00:16:04 +08:00
|
|
|
err = app.EnableNodeRoute("test", "test_enable_route_machine", "10.0.0.0/24")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
enableRoutesAfterDoubleApply, err := app.GetEnabledNodeRoutes(
|
|
|
|
"test",
|
|
|
|
"test_enable_route_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
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
err = app.EnableNodeRoute("test", "test_enable_route_machine", "150.0.10.0/25")
|
2021-08-21 21:49:46 +08:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-16 00:16:04 +08:00
|
|
|
enabledRoutesWithAdditionalRoute, err := app.GetEnabledNodeRoutes(
|
|
|
|
"test",
|
|
|
|
"test_enable_route_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
|
|
|
}
|