2023-05-21 12:37:59 -04:00
|
|
|
package db
|
2021-02-27 18:58:09 -05:00
|
|
|
|
|
|
|
import (
|
2021-06-24 09:44:19 -04:00
|
|
|
"errors"
|
2021-02-27 18:58:09 -05:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-05-11 03:09:18 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2021-08-05 13:23:02 -04:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-06-24 09:44:19 -04:00
|
|
|
"gorm.io/gorm"
|
2021-02-27 18:58:09 -05:00
|
|
|
)
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
var (
|
|
|
|
ErrUserExists = errors.New("user already exists")
|
|
|
|
ErrUserNotFound = errors.New("user not found")
|
|
|
|
ErrUserStillHasNodes = errors.New("user not empty: node(s) found")
|
2021-11-04 18:15:17 -04:00
|
|
|
)
|
2021-05-09 11:12:05 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// CreateUser creates a new User. Returns error if could not be created
|
|
|
|
// or another user already exists.
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) CreateUser(name string) (*types.User, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
err := util.CheckForFQDNRules(name)
|
2022-02-22 06:45:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-21 12:37:59 -04:00
|
|
|
user := types.User{}
|
2023-05-11 03:09:18 -04:00
|
|
|
if err := hsdb.db.Where("name = ?", name).First(&user).Error; err == nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
return nil, ErrUserExists
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
2023-01-17 11:43:44 -05:00
|
|
|
user.Name = name
|
2023-05-11 03:09:18 -04:00
|
|
|
if err := hsdb.db.Create(&user).Error; err != nil {
|
2021-08-05 13:23:02 -04:00
|
|
|
log.Error().
|
2023-01-17 11:43:44 -05:00
|
|
|
Str("func", "CreateUser").
|
2021-08-05 13:23:02 -04:00
|
|
|
Err(err).
|
|
|
|
Msg("Could not create row")
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2021-02-27 18:58:09 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return &user, nil
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// DestroyUser destroys a User. Returns error if the User does
|
2021-05-09 11:12:05 -04:00
|
|
|
// not exist or if there are machines associated with it.
|
2023-05-11 03:09:18 -04:00
|
|
|
func (hsdb *HSDatabase) DestroyUser(name string) error {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
|
|
|
user, err := hsdb.getUser(name)
|
2021-05-09 11:12:05 -04:00
|
|
|
if err != nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
return ErrUserNotFound
|
2021-05-09 11:12:05 -04:00
|
|
|
}
|
|
|
|
|
2023-07-17 07:35:05 -04:00
|
|
|
machines, err := hsdb.listMachinesByUser(name)
|
2021-05-09 11:12:05 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-14 14:32:03 -05:00
|
|
|
if len(machines) > 0 {
|
2023-01-17 11:43:44 -05:00
|
|
|
return ErrUserStillHasNodes
|
2021-11-13 14:01:05 -05:00
|
|
|
}
|
|
|
|
|
2023-07-17 07:35:05 -04:00
|
|
|
keys, err := hsdb.listPreAuthKeys(name)
|
2021-11-13 14:01:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-14 14:32:03 -05:00
|
|
|
for _, key := range keys {
|
2023-07-17 07:35:05 -04:00
|
|
|
err = hsdb.destroyPreAuthKey(key)
|
2021-11-13 15:24:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-09 11:12:05 -04:00
|
|
|
}
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
if result := hsdb.db.Unscoped().Delete(&user); result.Error != nil {
|
2021-10-16 11:14:37 -04:00
|
|
|
return result.Error
|
2021-05-09 11:12:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// RenameUser renames a User. Returns error if the User does
|
|
|
|
// not exist or if another User exists with the new name.
|
2023-05-11 03:09:18 -04:00
|
|
|
func (hsdb *HSDatabase) RenameUser(oldName, newName string) error {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2022-02-22 06:45:50 -05:00
|
|
|
var err error
|
2023-07-17 07:35:05 -04:00
|
|
|
oldUser, err := hsdb.getUser(oldName)
|
2021-10-16 11:20:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-21 12:37:59 -04:00
|
|
|
err = util.CheckForFQDNRules(newName)
|
2022-02-22 06:45:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-17 07:35:05 -04:00
|
|
|
_, err = hsdb.getUser(newName)
|
2021-10-16 11:20:06 -04:00
|
|
|
if err == nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
return ErrUserExists
|
2021-10-16 11:20:06 -04:00
|
|
|
}
|
2023-01-17 11:43:44 -05:00
|
|
|
if !errors.Is(err, ErrUserNotFound) {
|
2021-10-16 11:20:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
oldUser.Name = newName
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
if result := hsdb.db.Save(&oldUser); result.Error != nil {
|
2021-10-16 11:20:06 -04:00
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2021-05-09 11:12:05 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// GetUser fetches a user by name.
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) GetUser(name string) (*types.User, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
|
|
|
return hsdb.getUser(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) getUser(name string) (*types.User, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
user := types.User{}
|
2023-05-11 03:09:18 -04:00
|
|
|
if result := hsdb.db.First(&user, "name = ?", name); errors.Is(
|
2021-11-13 03:36:45 -05:00
|
|
|
result.Error,
|
|
|
|
gorm.ErrRecordNotFound,
|
|
|
|
) {
|
2023-01-17 11:43:44 -05:00
|
|
|
return nil, ErrUserNotFound
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return &user, nil
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// ListUsers gets all the existing users.
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) ListUsers() ([]types.User, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
|
|
|
return hsdb.listUsers()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) listUsers() ([]types.User, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
users := []types.User{}
|
2023-05-11 03:09:18 -04:00
|
|
|
if err := hsdb.db.Find(&users).Error; err != nil {
|
2021-02-27 18:58:09 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return users, nil
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// ListMachinesByUser gets all the nodes in a given user.
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) ListMachinesByUser(name string) (types.Machines, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
|
|
|
return hsdb.listMachinesByUser(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) listMachinesByUser(name string) (types.Machines, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
err := util.CheckForFQDNRules(name)
|
2022-02-22 06:45:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-17 07:35:05 -04:00
|
|
|
user, err := hsdb.getUser(name)
|
2021-02-27 18:58:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
machines := types.Machines{}
|
|
|
|
if err := hsdb.db.Preload("AuthKey").Preload("AuthKey.User").Preload("User").Where(&types.Machine{UserID: user.ID}).Find(&machines).Error; err != nil {
|
2021-02-27 18:58:09 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2021-11-04 18:15:17 -04:00
|
|
|
return machines, nil
|
2021-02-27 18:58:09 -05:00
|
|
|
}
|
|
|
|
|
2023-07-17 07:35:05 -04:00
|
|
|
// AssignMachineToUser assigns a Machine to a user.
|
|
|
|
func (hsdb *HSDatabase) AssignMachineToUser(machine *types.Machine, username string) error {
|
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
err := util.CheckForFQDNRules(username)
|
2022-02-22 06:45:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-17 07:35:05 -04:00
|
|
|
user, err := hsdb.getUser(username)
|
2021-02-27 18:58:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-17 11:43:44 -05:00
|
|
|
machine.User = *user
|
2023-05-11 03:09:18 -04:00
|
|
|
if result := hsdb.db.Save(&machine); result.Error != nil {
|
2022-05-02 05:47:21 -04:00
|
|
|
return result.Error
|
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2021-02-27 18:58:09 -05:00
|
|
|
return nil
|
|
|
|
}
|