1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00
juanfont.headscale/sharing_test.go

342 lines
10 KiB
Go
Raw Normal View History

2021-09-02 16:59:12 +02:00
package headscale
import (
"gopkg.in/check.v1"
2022-01-16 14:16:59 +01:00
"inet.af/netaddr"
2021-09-02 16:59:12 +02:00
)
2021-11-13 09:36:45 +01:00
func CreateNodeNamespace(
c *check.C,
2021-11-15 17:16:04 +01:00
namespaceName, node, key, ip string,
2021-11-13 09:36:45 +01:00
) (*Namespace, *Machine) {
2021-11-15 17:16:04 +01:00
namespace, err := app.CreateNamespace(namespaceName)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
pak1, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespace.Name, node)
c.Assert(err, check.NotNil)
2021-11-15 17:16:04 +01:00
machine := &Machine{
ID: 0,
MachineKey: key,
NodeKey: key,
DiscoKey: key,
Name: node,
2021-11-15 17:16:04 +01:00
NamespaceID: namespace.ID,
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
2022-01-16 14:16:59 +01:00
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.1")},
AuthKeyID: uint(pak1.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machine)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespace.Name, machine.Name)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
return namespace, machine
}
2021-09-02 16:59:12 +02:00
func (s *Suite) TestBasicSharedNodesInNamespace(c *check.C) {
2021-11-15 17:16:04 +01:00
namespace1, machine1 := CreateNodeNamespace(
c,
"shared1",
"test_get_shared_nodes_1",
"686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
"100.64.0.1",
)
2021-11-15 17:16:04 +01:00
_, machine2 := CreateNodeNamespace(
c,
"shared2",
"test_get_shared_nodes_2",
"dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
"100.64.0.2",
)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
peersOfMachine1BeforeShared, err := app.getPeers(machine1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1BeforeShared), check.Equals, 0)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
err = app.AddSharedMachineToNamespace(machine2, namespace1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
peersOfMachine1AfterShared, err := app.getPeers(machine1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1AfterShared), check.Equals, 1)
c.Assert(peersOfMachine1AfterShared[0].ID, check.Equals, machine2.ID)
2021-09-02 16:59:12 +02:00
}
func (s *Suite) TestSameNamespace(c *check.C) {
2021-11-15 17:16:04 +01:00
namespace1, machine1 := CreateNodeNamespace(
c,
"shared1",
"test_get_shared_nodes_1",
"686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
"100.64.0.1",
)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
peersOfMachine1BeforeShare, err := app.getPeers(machine1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 0)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
err = app.AddSharedMachineToNamespace(machine1, namespace1)
2021-11-15 17:33:16 +01:00
c.Assert(err, check.Equals, errSameNamespace)
2021-09-02 16:59:12 +02:00
}
func (s *Suite) TestUnshare(c *check.C) {
2021-11-15 17:16:04 +01:00
namespace1, machine1 := CreateNodeNamespace(
c,
"shared1",
"test_unshare_1",
"686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
"100.64.0.1",
)
2021-11-15 17:16:04 +01:00
_, machine2 := CreateNodeNamespace(
c,
"shared2",
"test_unshare_2",
"dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
"100.64.0.2",
)
2021-11-15 17:16:04 +01:00
peersOfMachine1BeforeShare, err := app.getPeers(machine1)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 0)
2021-11-15 17:16:04 +01:00
err = app.AddSharedMachineToNamespace(machine2, namespace1)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
peersOfMachine1BeforeShare, err = app.getShared(machine1)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 1)
2021-11-15 17:16:04 +01:00
err = app.RemoveSharedMachineFromNamespace(machine2, namespace1)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
peersOfMachine1BeforeShare, err = app.getShared(machine1)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 0)
2021-11-15 17:16:04 +01:00
err = app.RemoveSharedMachineFromNamespace(machine2, namespace1)
2021-11-15 17:33:16 +01:00
c.Assert(err, check.Equals, errMachineNotShared)
2021-11-15 17:16:04 +01:00
err = app.RemoveSharedMachineFromNamespace(machine1, namespace1)
2021-11-15 17:33:16 +01:00
c.Assert(err, check.Equals, errMachineNotShared)
}
2021-09-02 16:59:12 +02:00
func (s *Suite) TestAlreadyShared(c *check.C) {
2021-11-15 17:16:04 +01:00
namespace1, machine1 := CreateNodeNamespace(
c,
"shared1",
"test_get_shared_nodes_1",
"686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
"100.64.0.1",
)
2021-11-15 17:16:04 +01:00
_, machine2 := CreateNodeNamespace(
c,
"shared2",
"test_get_shared_nodes_2",
"dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
"100.64.0.2",
)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
peersOfMachine1BeforeShare, err := app.getPeers(machine1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 0)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
err = app.AddSharedMachineToNamespace(machine2, namespace1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
err = app.AddSharedMachineToNamespace(machine2, namespace1)
2021-11-15 17:33:16 +01:00
c.Assert(err, check.Equals, errMachineAlreadyShared)
2021-09-02 16:59:12 +02:00
}
func (s *Suite) TestDoNotIncludeRoutesOnShared(c *check.C) {
2021-11-15 17:16:04 +01:00
namespace1, machine1 := CreateNodeNamespace(
c,
"shared1",
"test_get_shared_nodes_1",
"686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
"100.64.0.1",
)
2021-11-15 17:16:04 +01:00
_, machine2 := CreateNodeNamespace(
c,
"shared2",
"test_get_shared_nodes_2",
"dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
"100.64.0.2",
)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
peersOfMachine1BeforeShare, err := app.getPeers(machine1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 0)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
err = app.AddSharedMachineToNamespace(machine2, namespace1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
peersOfMachine1AfterShare, err := app.getPeers(machine1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1AfterShare), check.Equals, 1)
c.Assert(peersOfMachine1AfterShare[0].Name, check.Equals, "test_get_shared_nodes_2")
2021-09-02 16:59:12 +02:00
}
func (s *Suite) TestComplexSharingAcrossNamespaces(c *check.C) {
2021-11-15 17:16:04 +01:00
namespace1, machine1 := CreateNodeNamespace(
c,
"shared1",
"test_get_shared_nodes_1",
"686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
"100.64.0.1",
)
2021-11-15 17:16:04 +01:00
_, machine2 := CreateNodeNamespace(
c,
"shared2",
"test_get_shared_nodes_2",
"dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
"100.64.0.2",
)
2021-11-15 17:16:04 +01:00
_, machine3 := CreateNodeNamespace(
c,
"shared3",
"test_get_shared_nodes_3",
"6e704bee83eb93db6fc2c417d7882964cd3f8cc87082cbb645982e34020c76c8",
"100.64.0.3",
)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
pak4, err := app.CreatePreAuthKey(namespace1.Name, false, false, nil)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
machine4 := &Machine{
ID: 4,
2021-10-18 23:52:38 +02:00
MachineKey: "4c3e07c3ecd40e9c945bb6797557c451850691c0409740578325e17009dd298f",
NodeKey: "4c3e07c3ecd40e9c945bb6797557c451850691c0409740578325e17009dd298f",
DiscoKey: "4c3e07c3ecd40e9c945bb6797557c451850691c0409740578325e17009dd298f",
2021-09-02 16:59:12 +02:00
Name: "test_get_shared_nodes_4",
2021-11-15 17:16:04 +01:00
NamespaceID: namespace1.ID,
2021-09-02 16:59:12 +02:00
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
2022-01-16 14:16:59 +01:00
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.4")},
2021-09-02 16:59:12 +02:00
AuthKeyID: uint(pak4.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machine4)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespace1.Name, machine4.Name)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
peersOfMachine1BeforeShare, err := app.getPeers(machine1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 1) // node1 can see node4
c.Assert(peersOfMachine1BeforeShare[0].Name, check.Equals, machine4.Name)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
err = app.AddSharedMachineToNamespace(machine2, namespace1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
peersOfMachine1AfterShare, err := app.getPeers(machine1)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-13 09:36:45 +01:00
c.Assert(
2021-11-15 17:16:04 +01:00
len(peersOfMachine1AfterShare),
2021-11-13 09:36:45 +01:00
check.Equals,
2,
) // node1 can see node2 (shared) and node4 (same namespace)
2021-11-15 17:16:04 +01:00
c.Assert(peersOfMachine1AfterShare[0].Name, check.Equals, machine2.Name)
c.Assert(peersOfMachine1AfterShare[1].Name, check.Equals, machine4.Name)
2021-11-15 17:16:04 +01:00
sharedOfMachine1, err := app.getShared(machine1)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(sharedOfMachine1), check.Equals, 1) // node1 can see node2 as shared
c.Assert(sharedOfMachine1[0].Name, check.Equals, machine2.Name)
2021-09-02 16:59:12 +02:00
2021-11-15 17:16:04 +01:00
peersOfMachine3, err := app.getPeers(machine3)
2021-09-02 16:59:12 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine3), check.Equals, 0) // node3 is alone
2021-11-15 17:16:04 +01:00
peersOfMachine2, err := app.getPeers(machine2)
c.Assert(err, check.IsNil)
c.Assert(
2021-11-15 17:16:04 +01:00
len(peersOfMachine2),
check.Equals,
2,
) // node2 should see node1 (sharedTo) and node4 (sharedTo), as is shared in namespace1
2021-11-15 17:16:04 +01:00
c.Assert(peersOfMachine2[0].Name, check.Equals, machine1.Name)
c.Assert(peersOfMachine2[1].Name, check.Equals, machine4.Name)
2021-09-02 16:59:12 +02:00
}
2021-10-10 23:55:18 +02:00
func (s *Suite) TestDeleteSharedMachine(c *check.C) {
2021-11-15 17:16:04 +01:00
namespace1, machine1 := CreateNodeNamespace(
c,
"shared1",
"test_get_shared_nodes_1",
"686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
"100.64.0.1",
)
2021-11-15 17:16:04 +01:00
_, machine2 := CreateNodeNamespace(
c,
"shared2",
"test_get_shared_nodes_2",
"dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
"100.64.0.2",
)
2021-11-15 17:16:04 +01:00
_, machine3 := CreateNodeNamespace(
c,
"shared3",
"test_get_shared_nodes_3",
"6e704bee83eb93db6fc2c417d7882964cd3f8cc87082cbb645982e34020c76c8",
"100.64.0.3",
)
2021-10-10 23:55:18 +02:00
2021-11-15 17:16:04 +01:00
pak4n1, err := app.CreatePreAuthKey(namespace1.Name, false, false, nil)
2021-10-10 23:55:18 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
machine4 := &Machine{
ID: 4,
2021-10-18 23:52:38 +02:00
MachineKey: "4c3e07c3ecd40e9c945bb6797557c451850691c0409740578325e17009dd298f",
NodeKey: "4c3e07c3ecd40e9c945bb6797557c451850691c0409740578325e17009dd298f",
DiscoKey: "4c3e07c3ecd40e9c945bb6797557c451850691c0409740578325e17009dd298f",
2021-10-10 23:55:18 +02:00
Name: "test_get_shared_nodes_4",
2021-11-15 17:16:04 +01:00
NamespaceID: namespace1.ID,
2021-10-10 23:55:18 +02:00
Registered: true,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
2022-01-16 14:16:59 +01:00
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.4")},
2021-10-10 23:55:18 +02:00
AuthKeyID: uint(pak4n1.ID),
}
2021-11-15 17:16:04 +01:00
app.db.Save(machine4)
2021-10-10 23:55:18 +02:00
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine(namespace1.Name, machine4.Name)
2021-10-10 23:55:18 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
peersOfMachine1BeforeShare, err := app.getPeers(machine1)
2021-10-10 23:55:18 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1BeforeShare), check.Equals, 1) // nodes 1 and 4
c.Assert(peersOfMachine1BeforeShare[0].Name, check.Equals, machine4.Name)
2021-10-10 23:55:18 +02:00
2021-11-15 17:16:04 +01:00
err = app.AddSharedMachineToNamespace(machine2, namespace1)
2021-10-10 23:55:18 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
peersOfMachine1AfterShare, err := app.getPeers(machine1)
2021-10-10 23:55:18 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine1AfterShare), check.Equals, 2) // nodes 1, 2, 4
c.Assert(peersOfMachine1AfterShare[0].Name, check.Equals, machine2.Name)
c.Assert(peersOfMachine1AfterShare[1].Name, check.Equals, machine4.Name)
2021-10-10 23:55:18 +02:00
2021-11-15 17:16:04 +01:00
sharedOfMachine1, err := app.getShared(machine1)
2021-10-10 23:55:18 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(sharedOfMachine1), check.Equals, 1) // nodes 1, 2, 4
c.Assert(sharedOfMachine1[0].Name, check.Equals, machine2.Name)
2021-10-10 23:55:18 +02:00
2021-11-15 17:16:04 +01:00
peersOfMachine3, err := app.getPeers(machine3)
2021-10-10 23:55:18 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(peersOfMachine3), check.Equals, 0) // node 3 is alone
2021-10-10 23:55:18 +02:00
2021-11-15 17:16:35 +01:00
sharedMachinesInNamespace1, err := app.ListSharedMachinesInNamespace(
namespace1.Name,
)
2021-10-10 23:55:18 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(sharedMachinesInNamespace1), check.Equals, 1)
2021-10-10 23:55:18 +02:00
2021-11-15 17:16:04 +01:00
err = app.DeleteMachine(machine2)
2021-10-10 23:55:18 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
sharedMachinesInNamespace1, err = app.ListSharedMachinesInNamespace(namespace1.Name)
2021-10-10 23:55:18 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(sharedMachinesInNamespace1), check.Equals, 0)
2021-10-10 23:55:18 +02:00
}