From 6faa1d2e4acd7645475732300f03b16edda1c865 Mon Sep 17 00:00:00 2001 From: Benjamin George Roberts Date: Thu, 25 Aug 2022 20:12:41 +1000 Subject: [PATCH] Fix tests broken by preauthkey tags --- acls_test.go | 12 ++++++------ dns_test.go | 8 ++++++++ machine_test.go | 16 ++++++++-------- namespaces_test.go | 10 +++++++--- preauth_keys.go | 2 +- preauth_keys_test.go | 20 ++++++++++---------- routes_test.go | 4 ++-- utils_test.go | 6 +++--- 8 files changed, 45 insertions(+), 33 deletions(-) diff --git a/acls_test.go b/acls_test.go index fe2217c3..e25e71b5 100644 --- a/acls_test.go +++ b/acls_test.go @@ -114,7 +114,7 @@ func (s *Suite) TestValidExpandTagOwnersInSources(c *check.C) { namespace, err := app.CreateNamespace("user1") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("user1", "testmachine") @@ -164,7 +164,7 @@ func (s *Suite) TestValidExpandTagOwnersInDestinations(c *check.C) { namespace, err := app.CreateNamespace("user1") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("user1", "testmachine") @@ -214,7 +214,7 @@ func (s *Suite) TestInvalidTagValidNamespace(c *check.C) { namespace, err := app.CreateNamespace("user1") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("user1", "testmachine") @@ -263,7 +263,7 @@ func (s *Suite) TestValidTagInvalidNamespace(c *check.C) { namespace, err := app.CreateNamespace("user1") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("user1", "webserver") @@ -395,7 +395,7 @@ func (s *Suite) TestPortNamespace(c *check.C) { namespace, err := app.CreateNamespace("testnamespace") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("testnamespace", "testmachine") @@ -437,7 +437,7 @@ func (s *Suite) TestPortGroup(c *check.C) { namespace, err := app.CreateNamespace("testnamespace") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("testnamespace", "testmachine") diff --git a/dns_test.go b/dns_test.go index a2bb9aec..2de6e083 100644 --- a/dns_test.go +++ b/dns_test.go @@ -126,6 +126,7 @@ func (s *Suite) TestDNSConfigMapResponseWithMagicDNS(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -134,6 +135,7 @@ func (s *Suite) TestDNSConfigMapResponseWithMagicDNS(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -142,6 +144,7 @@ func (s *Suite) TestDNSConfigMapResponseWithMagicDNS(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -150,6 +153,7 @@ func (s *Suite) TestDNSConfigMapResponseWithMagicDNS(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -269,6 +273,7 @@ func (s *Suite) TestDNSConfigMapResponseWithoutMagicDNS(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -277,6 +282,7 @@ func (s *Suite) TestDNSConfigMapResponseWithoutMagicDNS(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -285,6 +291,7 @@ func (s *Suite) TestDNSConfigMapResponseWithoutMagicDNS(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -293,6 +300,7 @@ func (s *Suite) TestDNSConfigMapResponseWithoutMagicDNS(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) diff --git a/machine_test.go b/machine_test.go index 5da0906f..86dbabfd 100644 --- a/machine_test.go +++ b/machine_test.go @@ -18,7 +18,7 @@ func (s *Suite) TestGetMachine(c *check.C) { namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("test", "testmachine") @@ -44,7 +44,7 @@ func (s *Suite) TestGetMachineByID(c *check.C) { namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachineByID(0) @@ -70,7 +70,7 @@ func (s *Suite) TestGetMachineByNodeKey(c *check.C) { namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachineByID(0) @@ -98,7 +98,7 @@ func (s *Suite) TestGetMachineByAnyNodeKey(c *check.C) { namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachineByID(0) @@ -171,7 +171,7 @@ func (s *Suite) TestListPeers(c *check.C) { namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachineByID(0) @@ -214,7 +214,7 @@ func (s *Suite) TestGetACLFilteredPeers(c *check.C) { for _, name := range []string{"test", "admin"} { namespace, err := app.CreateNamespace(name) c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) stor = append(stor, base{namespace, pak}) } @@ -294,7 +294,7 @@ func (s *Suite) TestExpireMachine(c *check.C) { namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("test", "testmachine") @@ -350,7 +350,7 @@ func (s *Suite) TestSetTags(c *check.C) { namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("test", "testmachine") diff --git a/namespaces_test.go b/namespaces_test.go index 6f33585f..a8022883 100644 --- a/namespaces_test.go +++ b/namespaces_test.go @@ -31,7 +31,7 @@ func (s *Suite) TestDestroyNamespaceErrors(c *check.C) { namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) err = app.DestroyNamespace("test") @@ -44,7 +44,7 @@ func (s *Suite) TestDestroyNamespaceErrors(c *check.C) { namespace, err = app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err = app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err = app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) machine := Machine{ @@ -107,6 +107,7 @@ func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -115,6 +116,7 @@ func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -123,6 +125,7 @@ func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -131,6 +134,7 @@ func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) { false, false, nil, + nil, ) c.Assert(err, check.IsNil) @@ -380,7 +384,7 @@ func (s *Suite) TestSetMachineNamespace(c *check.C) { newNamespace, err := app.CreateNamespace("new") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(oldNamespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(oldNamespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) machine := Machine{ diff --git a/preauth_keys.go b/preauth_keys.go index 8359d287..7995acb6 100644 --- a/preauth_keys.go +++ b/preauth_keys.go @@ -129,7 +129,7 @@ func (h *Headscale) GetPreAuthKey(namespace string, key string) (*PreAuthKey, er // does not exist. func (h *Headscale) DestroyPreAuthKey(pak PreAuthKey) error { return h.db.Transaction(func(db *gorm.DB) error { - if result := db.Unscoped().Delete(PreAuthKeyAclTag{PreAuthKeyID: pak.ID}); result.Error != nil { + if result := db.Unscoped().Where(PreAuthKeyAclTag{PreAuthKeyID: pak.ID}).Delete(&PreAuthKeyAclTag{}); result.Error != nil { return result.Error } diff --git a/preauth_keys_test.go b/preauth_keys_test.go index cd9c66aa..fe108337 100644 --- a/preauth_keys_test.go +++ b/preauth_keys_test.go @@ -7,14 +7,14 @@ import ( ) func (*Suite) TestCreatePreAuthKey(c *check.C) { - _, err := app.CreatePreAuthKey("bogus", true, false, nil) + _, err := app.CreatePreAuthKey("bogus", true, false, nil, nil) c.Assert(err, check.NotNil) namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - key, err := app.CreatePreAuthKey(namespace.Name, true, false, nil) + key, err := app.CreatePreAuthKey(namespace.Name, true, false, nil, nil) c.Assert(err, check.IsNil) // Did we get a valid key? @@ -40,7 +40,7 @@ func (*Suite) TestExpiredPreAuthKey(c *check.C) { c.Assert(err, check.IsNil) now := time.Now() - pak, err := app.CreatePreAuthKey(namespace.Name, true, false, &now) + pak, err := app.CreatePreAuthKey(namespace.Name, true, false, &now, nil) c.Assert(err, check.IsNil) key, err := app.checkKeyValidity(pak.Key) @@ -58,7 +58,7 @@ func (*Suite) TestValidateKeyOk(c *check.C) { namespace, err := app.CreateNamespace("test3") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, true, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, true, false, nil, nil) c.Assert(err, check.IsNil) key, err := app.checkKeyValidity(pak.Key) @@ -70,7 +70,7 @@ func (*Suite) TestAlreadyUsedKey(c *check.C) { namespace, err := app.CreateNamespace("test4") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) machine := Machine{ @@ -94,7 +94,7 @@ func (*Suite) TestReusableBeingUsedKey(c *check.C) { namespace, err := app.CreateNamespace("test5") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, true, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, true, false, nil, nil) c.Assert(err, check.IsNil) machine := Machine{ @@ -118,7 +118,7 @@ func (*Suite) TestNotReusableNotBeingUsedKey(c *check.C) { namespace, err := app.CreateNamespace("test6") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) key, err := app.checkKeyValidity(pak.Key) @@ -130,7 +130,7 @@ func (*Suite) TestEphemeralKey(c *check.C) { namespace, err := app.CreateNamespace("test7") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, true, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, true, nil, nil) c.Assert(err, check.IsNil) now := time.Now() @@ -165,7 +165,7 @@ func (*Suite) TestExpirePreauthKey(c *check.C) { namespace, err := app.CreateNamespace("test3") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, true, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, true, false, nil, nil) c.Assert(err, check.IsNil) c.Assert(pak.Expiration, check.IsNil) @@ -182,7 +182,7 @@ func (*Suite) TestNotReusableMarkedAsUsed(c *check.C) { namespace, err := app.CreateNamespace("test6") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) pak.Used = true app.db.Save(&pak) diff --git a/routes_test.go b/routes_test.go index 89b712b5..e0b7cafd 100644 --- a/routes_test.go +++ b/routes_test.go @@ -10,7 +10,7 @@ func (s *Suite) TestGetRoutes(c *check.C) { namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("test", "test_get_route_machine") @@ -54,7 +54,7 @@ func (s *Suite) TestGetEnableRoutes(c *check.C) { namespace, err := app.CreateNamespace("test") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("test", "test_enable_route_machine") diff --git a/utils_test.go b/utils_test.go index 07fa62d8..6ef6f834 100644 --- a/utils_test.go +++ b/utils_test.go @@ -23,7 +23,7 @@ func (s *Suite) TestGetUsedIps(c *check.C) { namespace, err := app.CreateNamespace("test-ip") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("test", "testmachine") @@ -71,7 +71,7 @@ func (s *Suite) TestGetMultiIp(c *check.C) { ips, err := app.getAvailableIPs() c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("test", "testmachine") @@ -161,7 +161,7 @@ func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) { namespace, err := app.CreateNamespace("test-ip") c.Assert(err, check.IsNil) - pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil) + pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil) c.Assert(err, check.IsNil) _, err = app.GetMachine("test", "testmachine")