headscale/sharing_test.go

341 lines
10 KiB
Go
Raw Normal View History

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