2023-05-21 19:37:59 +03:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
"gorm.io/gorm"
|
2024-07-18 10:01:59 +02:00
|
|
|
"tailscale.com/types/ptr"
|
2023-05-21 19:37:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 14:50:17 +02:00
|
|
|
_, err = db.GetUserByName("test")
|
2023-05-21 19:37:59 +03:00
|
|
|
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)
|
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
result := db.DB.Preload("User").First(&pak, "key = ?", pak.Key)
|
2023-05-21 19:37:59 +03:00
|
|
|
// 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)
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
node := types.Node{
|
2023-05-21 19:37:59 +03:00
|
|
|
ID: 0,
|
2023-09-24 13:42:05 +02:00
|
|
|
Hostname: "testnode",
|
2023-05-21 19:37:59 +03:00
|
|
|
UserID: user.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 10:01:59 +02:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-05-21 19:37:59 +03:00
|
|
|
}
|
2024-05-16 02:40:14 +02:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2023-05-21 19:37:59 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 14:50:17 +02:00
|
|
|
_, err = db.GetUserByName("test")
|
2023-05-21 19:37:59 +03:00
|
|
|
c.Assert(err, check.Equals, ErrUserNotFound)
|
|
|
|
|
Redo OIDC configuration (#2020)
expand user, add claims to user
This commit expands the user table with additional fields that
can be retrieved from OIDC providers (and other places) and
uses this data in various tailscale response objects if it is
available.
This is the beginning of implementing
https://docs.google.com/document/d/1X85PMxIaVWDF6T_UPji3OeeUqVBcGj_uHRM5CI-AwlY/edit
trying to make OIDC more coherant and maintainable in addition
to giving the user a better experience and integration with a
provider.
remove usernames in magic dns, normalisation of emails
this commit removes the option to have usernames as part of MagicDNS
domains and headscale will now align with Tailscale, where there is a
root domain, and the machine name.
In addition, the various normalisation functions for dns names has been
made lighter not caring about username and special character that wont
occur.
Email are no longer normalised as part of the policy processing.
untagle oidc and regcache, use typed cache
This commits stops reusing the registration cache for oidc
purposes and switches the cache to be types and not use any
allowing the removal of a bunch of casting.
try to make reauth/register branches clearer in oidc
Currently there was a function that did a bunch of stuff,
finding the machine key, trying to find the node, reauthing
the node, returning some status, and it was called validate
which was very confusing.
This commit tries to split this into what to do if the node
exists, if it needs to register etc.
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2024-10-02 14:50:17 +02:00
|
|
|
_, err = db.GetUserByName("test-renamed")
|
2023-05-21 19:37:59 +03:00
|
|
|
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)
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
node := types.Node{
|
2023-05-21 19:37:59 +03:00
|
|
|
ID: 0,
|
2023-09-24 13:42:05 +02:00
|
|
|
Hostname: "testnode",
|
2023-05-21 19:37:59 +03:00
|
|
|
UserID: oldUser.ID,
|
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 10:01:59 +02:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-05-21 19:37:59 +03:00
|
|
|
}
|
2024-05-16 02:40:14 +02:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2023-09-24 13:42:05 +02:00
|
|
|
c.Assert(node.UserID, check.Equals, oldUser.ID)
|
2023-05-21 19:37:59 +03:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
err = db.AssignNodeToUser(&node, newUser.Name)
|
2023-05-21 19:37:59 +03:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-09-24 13:42:05 +02:00
|
|
|
c.Assert(node.UserID, check.Equals, newUser.ID)
|
|
|
|
c.Assert(node.User.Name, check.Equals, newUser.Name)
|
2023-05-21 19:37:59 +03:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
err = db.AssignNodeToUser(&node, "non-existing-user")
|
2023-05-21 19:37:59 +03:00
|
|
|
c.Assert(err, check.Equals, ErrUserNotFound)
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
err = db.AssignNodeToUser(&node, newUser.Name)
|
2023-05-21 19:37:59 +03:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-09-24 13:42:05 +02:00
|
|
|
c.Assert(node.UserID, check.Equals, newUser.ID)
|
|
|
|
c.Assert(node.User.Name, check.Equals, newUser.Name)
|
2023-05-21 19:37:59 +03:00
|
|
|
}
|