headscale/namespaces_test.go

412 lines
11 KiB
Go
Raw Normal View History

package headscale
import (
"net/netip"
"testing"
"gopkg.in/check.v1"
"gorm.io/gorm"
)
func (s *Suite) TestCreateAndDestroyNamespace(c *check.C) {
2021-11-15 09:16:04 -07:00
namespace, err := app.CreateNamespace("test")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
c.Assert(namespace.Name, check.Equals, "test")
2021-11-15 09:16:04 -07:00
namespaces, err := app.ListNamespaces()
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
c.Assert(len(namespaces), check.Equals, 1)
2021-11-15 09:16:04 -07:00
err = app.DestroyNamespace("test")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
_, err = app.GetNamespace("test")
c.Assert(err, check.NotNil)
}
func (s *Suite) TestDestroyNamespaceErrors(c *check.C) {
2021-11-15 09:16:04 -07:00
err := app.DestroyNamespace("test")
2022-07-29 09:35:21 -06:00
c.Assert(err, check.Equals, ErrNamespaceNotFound)
2021-11-15 09:16:04 -07:00
namespace, err := app.CreateNamespace("test")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
err = app.DestroyNamespace("test")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
result := app.db.Preload("Namespace").First(&pak, "key = ?", pak.Key)
// destroying a namespace also deletes all associated preauthkeys
c.Assert(result.Error, check.Equals, gorm.ErrRecordNotFound)
2021-11-15 09:16:04 -07:00
namespace, err = app.CreateNamespace("test")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
pak, err = app.CreatePreAuthKey(namespace.Name, false, false, nil)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
machine := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
2022-05-16 13:41:46 -06:00
Hostname: "testmachine",
2021-11-15 09:16:04 -07:00
NamespaceID: namespace.ID,
2021-11-18 01:49:55 -07:00
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
}
2021-11-15 09:16:04 -07:00
app.db.Save(&machine)
2021-11-15 09:16:04 -07:00
err = app.DestroyNamespace("test")
2022-07-29 09:35:21 -06:00
c.Assert(err, check.Equals, ErrNamespaceNotEmptyOfNodes)
}
2021-10-16 09:20:06 -06:00
func (s *Suite) TestRenameNamespace(c *check.C) {
2021-11-15 09:16:04 -07:00
namespaceTest, err := app.CreateNamespace("test")
2021-10-16 09:20:06 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
c.Assert(namespaceTest.Name, check.Equals, "test")
2021-10-16 09:20:06 -06:00
2021-11-15 09:16:04 -07:00
namespaces, err := app.ListNamespaces()
2021-10-16 09:20:06 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
c.Assert(len(namespaces), check.Equals, 1)
2021-10-16 09:20:06 -06:00
err = app.RenameNamespace("test", "test-renamed")
2021-10-16 09:20:06 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
_, err = app.GetNamespace("test")
2022-07-29 09:35:21 -06:00
c.Assert(err, check.Equals, ErrNamespaceNotFound)
2021-10-16 09:20:06 -06:00
_, err = app.GetNamespace("test-renamed")
2021-10-16 09:20:06 -06:00
c.Assert(err, check.IsNil)
err = app.RenameNamespace("test-does-not-exit", "test")
2022-07-29 09:35:21 -06:00
c.Assert(err, check.Equals, ErrNamespaceNotFound)
2021-10-16 09:20:06 -06:00
2021-11-15 09:16:04 -07:00
namespaceTest2, err := app.CreateNamespace("test2")
2021-10-16 09:20:06 -06:00
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
c.Assert(namespaceTest2.Name, check.Equals, "test2")
2021-10-16 09:20:06 -06:00
err = app.RenameNamespace("test2", "test-renamed")
2022-07-29 09:35:21 -06:00
c.Assert(err, check.Equals, ErrNamespaceExists)
2021-10-16 09:20:06 -06:00
}
2021-10-17 15:59:44 -06:00
func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) {
2021-11-15 09:16:04 -07:00
namespaceShared1, err := app.CreateNamespace("shared1")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
namespaceShared2, err := app.CreateNamespace("shared2")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
namespaceShared3, err := app.CreateNamespace("shared3")
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
preAuthKeyShared1, err := app.CreatePreAuthKey(
namespaceShared1.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
preAuthKeyShared2, err := app.CreatePreAuthKey(
namespaceShared2.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
preAuthKeyShared3, err := app.CreatePreAuthKey(
namespaceShared3.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
preAuthKey2Shared1, err := app.CreatePreAuthKey(
namespaceShared1.Name,
false,
false,
nil,
)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
_, err = app.GetMachine(namespaceShared1.Name, "test_get_shared_nodes_1")
c.Assert(err, check.NotNil)
2021-11-15 09:16:04 -07:00
machineInShared1 := &Machine{
ID: 1,
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
2022-05-16 13:41:46 -06:00
Hostname: "test_get_shared_nodes_1",
2021-11-15 09:16:04 -07:00
NamespaceID: namespaceShared1.ID,
Namespace: *namespaceShared1,
2021-11-18 01:49:55 -07:00
RegisterMethod: RegisterMethodAuthKey,
IPAddresses: []netip.Addr{netip.MustParseAddr("100.64.0.1")},
2021-11-15 09:16:04 -07:00
AuthKeyID: uint(preAuthKeyShared1.ID),
}
2021-11-15 09:16:04 -07:00
app.db.Save(machineInShared1)
_, err = app.GetMachine(namespaceShared1.Name, machineInShared1.Hostname)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
machineInShared2 := &Machine{
ID: 2,
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
2022-05-16 13:41:46 -06:00
Hostname: "test_get_shared_nodes_2",
2021-11-15 09:16:04 -07:00
NamespaceID: namespaceShared2.ID,
Namespace: *namespaceShared2,
2021-11-18 01:49:55 -07:00
RegisterMethod: RegisterMethodAuthKey,
IPAddresses: []netip.Addr{netip.MustParseAddr("100.64.0.2")},
2021-11-15 09:16:04 -07:00
AuthKeyID: uint(preAuthKeyShared2.ID),
}
2021-11-15 09:16:04 -07:00
app.db.Save(machineInShared2)
_, err = app.GetMachine(namespaceShared2.Name, machineInShared2.Hostname)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
machineInShared3 := &Machine{
ID: 3,
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
2022-05-16 13:41:46 -06:00
Hostname: "test_get_shared_nodes_3",
2021-11-15 09:16:04 -07:00
NamespaceID: namespaceShared3.ID,
Namespace: *namespaceShared3,
2021-11-18 01:49:55 -07:00
RegisterMethod: RegisterMethodAuthKey,
IPAddresses: []netip.Addr{netip.MustParseAddr("100.64.0.3")},
2021-11-15 09:16:04 -07:00
AuthKeyID: uint(preAuthKeyShared3.ID),
}
2021-11-15 09:16:04 -07:00
app.db.Save(machineInShared3)
_, err = app.GetMachine(namespaceShared3.Name, machineInShared3.Hostname)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
machine2InShared1 := &Machine{
ID: 4,
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
2022-05-16 13:41:46 -06:00
Hostname: "test_get_shared_nodes_4",
2021-11-15 09:16:04 -07:00
NamespaceID: namespaceShared1.ID,
Namespace: *namespaceShared1,
2021-11-18 01:49:55 -07:00
RegisterMethod: RegisterMethodAuthKey,
IPAddresses: []netip.Addr{netip.MustParseAddr("100.64.0.4")},
2021-11-15 09:16:04 -07:00
AuthKeyID: uint(preAuthKey2Shared1.ID),
}
2021-11-15 09:16:04 -07:00
app.db.Save(machine2InShared1)
2021-11-15 09:16:04 -07:00
peersOfMachine1InShared1, err := app.getPeers(machineInShared1)
c.Assert(err, check.IsNil)
2021-11-15 09:16:04 -07:00
userProfiles := getMapResponseUserProfiles(
*machineInShared1,
peersOfMachine1InShared1,
)
c.Assert(len(userProfiles), check.Equals, 3)
found := false
2021-11-15 09:16:04 -07:00
for _, userProfiles := range userProfiles {
if userProfiles.DisplayName == namespaceShared1.Name {
found = true
2021-11-14 08:46:09 -07:00
break
}
}
c.Assert(found, check.Equals, true)
found = false
2021-11-15 09:16:04 -07:00
for _, userProfile := range userProfiles {
if userProfile.DisplayName == namespaceShared2.Name {
found = true
2021-11-14 08:46:09 -07:00
break
}
}
c.Assert(found, check.Equals, true)
}
func TestNormalizeToFQDNRules(t *testing.T) {
type args struct {
name string
stripEmailDomain bool
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "normalize simple name",
args: args{
name: "normalize-simple.name",
stripEmailDomain: false,
},
want: "normalize-simple.name",
wantErr: false,
},
{
name: "normalize an email",
args: args{
name: "foo.bar@example.com",
stripEmailDomain: false,
},
want: "foo.bar.example.com",
wantErr: false,
},
{
name: "normalize an email domain should be removed",
args: args{
name: "foo.bar@example.com",
stripEmailDomain: true,
},
want: "foo.bar",
wantErr: false,
},
{
name: "strip enabled no email passed as argument",
args: args{
name: "not-email-and-strip-enabled",
stripEmailDomain: true,
},
want: "not-email-and-strip-enabled",
wantErr: false,
},
{
name: "normalize complex email",
args: args{
name: "foo.bar+complex-email@example.com",
stripEmailDomain: false,
},
want: "foo.bar-complex-email.example.com",
wantErr: false,
},
{
name: "namespace name with space",
args: args{
name: "name space",
stripEmailDomain: false,
},
want: "name-space",
wantErr: false,
},
{
name: "namespace with quote",
args: args{
name: "Jamie's iPhone 5",
stripEmailDomain: false,
},
want: "jamies-iphone-5",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NormalizeToFQDNRules(tt.args.name, tt.args.stripEmailDomain)
if (err != nil) != tt.wantErr {
t.Errorf(
"NormalizeToFQDNRules() error = %v, wantErr %v",
err,
tt.wantErr,
)
2022-02-22 13:05:39 -07:00
return
}
if got != tt.want {
t.Errorf("NormalizeToFQDNRules() = %v, want %v", got, tt.want)
}
})
}
}
func TestCheckForFQDNRules(t *testing.T) {
type args struct {
name string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "valid: namespace",
args: args{name: "valid-namespace"},
wantErr: false,
},
{
name: "invalid: capitalized namespace",
args: args{name: "Invalid-CapItaLIzed-namespace"},
wantErr: true,
},
{
name: "invalid: email as namespace",
args: args{name: "foo.bar@example.com"},
wantErr: true,
},
{
name: "invalid: chars in namespace name",
args: args{name: "super-namespace+name"},
wantErr: true,
},
{
name: "invalid: too long name for namespace",
args: args{
name: "super-long-namespace-name-that-should-be-a-little-more-than-63-chars",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CheckForFQDNRules(tt.args.name); (err != nil) != tt.wantErr {
t.Errorf("CheckForFQDNRules() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2022-05-01 11:40:18 -06:00
func (s *Suite) TestSetMachineNamespace(c *check.C) {
oldNamespace, err := app.CreateNamespace("old")
c.Assert(err, check.IsNil)
newNamespace, err := app.CreateNamespace("new")
c.Assert(err, check.IsNil)
pak, err := app.CreatePreAuthKey(oldNamespace.Name, false, false, nil)
c.Assert(err, check.IsNil)
machine := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
2022-05-16 13:41:46 -06:00
Hostname: "testmachine",
2022-05-01 11:40:18 -06:00
NamespaceID: oldNamespace.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
}
app.db.Save(&machine)
c.Assert(machine.NamespaceID, check.Equals, oldNamespace.ID)
err = app.SetMachineNamespace(&machine, newNamespace.Name)
c.Assert(err, check.IsNil)
c.Assert(machine.NamespaceID, check.Equals, newNamespace.ID)
2022-05-02 03:47:21 -06:00
c.Assert(machine.Namespace.Name, check.Equals, newNamespace.Name)
2022-05-01 11:40:18 -06:00
err = app.SetMachineNamespace(&machine, "non-existing-namespace")
2022-07-29 09:35:21 -06:00
c.Assert(err, check.Equals, ErrNamespaceNotFound)
2022-05-01 11:40:18 -06:00
err = app.SetMachineNamespace(&machine, newNamespace.Name)
c.Assert(err, check.IsNil)
c.Assert(machine.NamespaceID, check.Equals, newNamespace.ID)
2022-05-02 03:47:21 -06:00
c.Assert(machine.Namespace.Name, check.Equals, newNamespace.Name)
2022-05-01 11:40:18 -06:00
}