mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-19 06:27:35 +00:00
7e62031444
* replace ephemeral deletion logic this commit replaces the way we remove ephemeral nodes, currently they are deleted in a loop and we look at last seen time. This time is now only set when a node disconnects and there was a bug (#2006) where nodes that had never disconnected was deleted since they did not have a last seen. The new logic will start an expiry timer when the node disconnects and delete the node from the database when the timer is up. If the node reconnects within the expiry, the timer is cancelled. Fixes #2006 Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * use uint64 as authekyid and ptr helper in tests Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * add test db helper Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * add list ephemeral node func Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * schedule ephemeral nodes for removal on startup Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * fix gorm query for postgres Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * add godoc Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> --------- Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
127 lines
3.3 KiB
Go
127 lines
3.3 KiB
Go
package db
|
|
|
|
import (
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
|
"gopkg.in/check.v1"
|
|
"gorm.io/gorm"
|
|
"tailscale.com/types/ptr"
|
|
)
|
|
|
|
func (s *Suite) TestCreateAndDestroyUser(c *check.C) {
|
|
user, err := db.CreateUser("test")
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(user.Name, check.Equals, "test")
|
|
|
|
users, err := db.ListUsers()
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(len(users), check.Equals, 1)
|
|
|
|
err = db.DestroyUser("test")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
_, err = db.GetUser("test")
|
|
c.Assert(err, check.NotNil)
|
|
}
|
|
|
|
func (s *Suite) TestDestroyUserErrors(c *check.C) {
|
|
err := db.DestroyUser("test")
|
|
c.Assert(err, check.Equals, ErrUserNotFound)
|
|
|
|
user, err := db.CreateUser("test")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
c.Assert(err, check.IsNil)
|
|
|
|
err = db.DestroyUser("test")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
result := db.DB.Preload("User").First(&pak, "key = ?", pak.Key)
|
|
// destroying a user also deletes all associated preauthkeys
|
|
c.Assert(result.Error, check.Equals, gorm.ErrRecordNotFound)
|
|
|
|
user, err = db.CreateUser("test")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
pak, err = db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
|
c.Assert(err, check.IsNil)
|
|
|
|
node := types.Node{
|
|
ID: 0,
|
|
Hostname: "testnode",
|
|
UserID: user.ID,
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
AuthKeyID: ptr.To(pak.ID),
|
|
}
|
|
trx := db.DB.Save(&node)
|
|
c.Assert(trx.Error, check.IsNil)
|
|
|
|
err = db.DestroyUser("test")
|
|
c.Assert(err, check.Equals, ErrUserStillHasNodes)
|
|
}
|
|
|
|
func (s *Suite) TestRenameUser(c *check.C) {
|
|
userTest, err := db.CreateUser("test")
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(userTest.Name, check.Equals, "test")
|
|
|
|
users, err := db.ListUsers()
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(len(users), check.Equals, 1)
|
|
|
|
err = db.RenameUser("test", "test-renamed")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
_, err = db.GetUser("test")
|
|
c.Assert(err, check.Equals, ErrUserNotFound)
|
|
|
|
_, err = db.GetUser("test-renamed")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
err = db.RenameUser("test-does-not-exit", "test")
|
|
c.Assert(err, check.Equals, ErrUserNotFound)
|
|
|
|
userTest2, err := db.CreateUser("test2")
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(userTest2.Name, check.Equals, "test2")
|
|
|
|
err = db.RenameUser("test2", "test-renamed")
|
|
c.Assert(err, check.Equals, ErrUserExists)
|
|
}
|
|
|
|
func (s *Suite) TestSetMachineUser(c *check.C) {
|
|
oldUser, err := db.CreateUser("old")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
newUser, err := db.CreateUser("new")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
pak, err := db.CreatePreAuthKey(oldUser.Name, false, false, nil, nil)
|
|
c.Assert(err, check.IsNil)
|
|
|
|
node := types.Node{
|
|
ID: 0,
|
|
Hostname: "testnode",
|
|
UserID: oldUser.ID,
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
|
AuthKeyID: ptr.To(pak.ID),
|
|
}
|
|
trx := db.DB.Save(&node)
|
|
c.Assert(trx.Error, check.IsNil)
|
|
c.Assert(node.UserID, check.Equals, oldUser.ID)
|
|
|
|
err = db.AssignNodeToUser(&node, newUser.Name)
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(node.UserID, check.Equals, newUser.ID)
|
|
c.Assert(node.User.Name, check.Equals, newUser.Name)
|
|
|
|
err = db.AssignNodeToUser(&node, "non-existing-user")
|
|
c.Assert(err, check.Equals, ErrUserNotFound)
|
|
|
|
err = db.AssignNodeToUser(&node, newUser.Name)
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(node.UserID, check.Equals, newUser.ID)
|
|
c.Assert(node.User.Name, check.Equals, newUser.Name)
|
|
}
|