2023-09-24 11:42:05 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2024-10-02 09:41:58 +00:00
|
|
|
"encoding/json"
|
2023-09-24 11:42:05 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
2024-10-02 07:06:09 +00:00
|
|
|
"slices"
|
2023-09-24 11:42:05 +00:00
|
|
|
"sort"
|
2024-08-27 16:54:28 +00:00
|
|
|
"sync"
|
2023-09-24 11:42:05 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2024-04-21 16:28:17 +00:00
|
|
|
"github.com/puzpuzpuz/xsync/v3"
|
2023-09-24 11:42:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NodeGivenNameHashLength = 8
|
|
|
|
NodeGivenNameTrimSize = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNodeNotFound = errors.New("node not found")
|
|
|
|
ErrNodeRouteIsNotAvailable = errors.New("route is not available on node")
|
|
|
|
ErrNodeNotFoundRegistrationCache = errors.New(
|
|
|
|
"node not found in registration cache",
|
|
|
|
)
|
|
|
|
ErrCouldNotConvertNodeInterface = errors.New("failed to convert node interface")
|
|
|
|
ErrDifferentRegisteredUser = errors.New(
|
|
|
|
"node was previously registered with a different user",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
func (hsdb *HSDatabase) ListPeers(nodeID types.NodeID) (types.Nodes, error) {
|
2024-02-08 16:28:19 +00:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (types.Nodes, error) {
|
2024-02-23 09:59:24 +00:00
|
|
|
return ListPeers(rx, nodeID)
|
2024-02-08 16:28:19 +00:00
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// ListPeers returns all peers of node, regardless of any Policy or if the node is expired.
|
2024-02-23 09:59:24 +00:00
|
|
|
func ListPeers(tx *gorm.DB, nodeID types.NodeID) (types.Nodes, error) {
|
2023-09-24 11:42:05 +00:00
|
|
|
nodes := types.Nodes{}
|
2024-02-08 16:28:19 +00:00
|
|
|
if err := tx.
|
2023-09-24 11:42:05 +00:00
|
|
|
Preload("AuthKey").
|
|
|
|
Preload("AuthKey.User").
|
|
|
|
Preload("User").
|
|
|
|
Preload("Routes").
|
2024-02-23 09:59:24 +00:00
|
|
|
Where("id <> ?",
|
|
|
|
nodeID).Find(&nodes).Error; err != nil {
|
2023-09-24 11:42:05 +00:00
|
|
|
return types.Nodes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(nodes, func(i, j int) bool { return nodes[i].ID < nodes[j].ID })
|
|
|
|
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (hsdb *HSDatabase) ListNodes() (types.Nodes, error) {
|
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (types.Nodes, error) {
|
|
|
|
return ListNodes(rx)
|
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func ListNodes(tx *gorm.DB) (types.Nodes, error) {
|
|
|
|
nodes := types.Nodes{}
|
|
|
|
if err := tx.
|
2023-09-24 11:42:05 +00:00
|
|
|
Preload("AuthKey").
|
|
|
|
Preload("AuthKey.User").
|
|
|
|
Preload("User").
|
|
|
|
Preload("Routes").
|
|
|
|
Find(&nodes).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
2024-07-18 08:01:59 +00:00
|
|
|
func (hsdb *HSDatabase) ListEphemeralNodes() (types.Nodes, error) {
|
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (types.Nodes, error) {
|
|
|
|
nodes := types.Nodes{}
|
|
|
|
if err := rx.Joins("AuthKey").Where(`"AuthKey"."ephemeral" = true`).Find(&nodes).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (hsdb *HSDatabase) getNode(user string, name string) (*types.Node, error) {
|
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (*types.Node, error) {
|
|
|
|
return getNode(rx, user, name)
|
|
|
|
})
|
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// getNode finds a Node by name and user and returns the Node struct.
|
|
|
|
func getNode(tx *gorm.DB, user string, name string) (*types.Node, error) {
|
|
|
|
nodes, err := ListNodesByUser(tx, user)
|
2023-09-24 11:42:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range nodes {
|
|
|
|
if m.Hostname == name {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNodeNotFound
|
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
func (hsdb *HSDatabase) GetNodeByID(id types.NodeID) (*types.Node, error) {
|
2024-02-08 16:28:19 +00:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (*types.Node, error) {
|
|
|
|
return GetNodeByID(rx, id)
|
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetNodeByID finds a Node by ID and returns the Node struct.
|
2024-02-23 09:59:24 +00:00
|
|
|
func GetNodeByID(tx *gorm.DB, id types.NodeID) (*types.Node, error) {
|
2023-09-24 11:42:05 +00:00
|
|
|
mach := types.Node{}
|
2024-02-08 16:28:19 +00:00
|
|
|
if result := tx.
|
2023-09-24 11:42:05 +00:00
|
|
|
Preload("AuthKey").
|
|
|
|
Preload("AuthKey.User").
|
|
|
|
Preload("User").
|
|
|
|
Preload("Routes").
|
|
|
|
Find(&types.Node{ID: id}).First(&mach); result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mach, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (hsdb *HSDatabase) GetNodeByMachineKey(machineKey key.MachinePublic) (*types.Node, error) {
|
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (*types.Node, error) {
|
|
|
|
return GetNodeByMachineKey(rx, machineKey)
|
|
|
|
})
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// GetNodeByMachineKey finds a Node by its MachineKey and returns the Node struct.
|
|
|
|
func GetNodeByMachineKey(
|
|
|
|
tx *gorm.DB,
|
2023-12-09 17:09:24 +00:00
|
|
|
machineKey key.MachinePublic,
|
|
|
|
) (*types.Node, error) {
|
2023-09-24 11:42:05 +00:00
|
|
|
mach := types.Node{}
|
2024-02-08 16:28:19 +00:00
|
|
|
if result := tx.
|
2023-09-24 11:42:05 +00:00
|
|
|
Preload("AuthKey").
|
|
|
|
Preload("AuthKey.User").
|
|
|
|
Preload("User").
|
|
|
|
Preload("Routes").
|
2023-11-16 16:55:29 +00:00
|
|
|
First(&mach, "machine_key = ?", machineKey.String()); result.Error != nil {
|
2023-09-24 11:42:05 +00:00
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mach, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (hsdb *HSDatabase) GetNodeByAnyKey(
|
|
|
|
machineKey key.MachinePublic,
|
2023-09-24 11:42:05 +00:00
|
|
|
nodeKey key.NodePublic,
|
2024-02-08 16:28:19 +00:00
|
|
|
oldNodeKey key.NodePublic,
|
2023-09-24 11:42:05 +00:00
|
|
|
) (*types.Node, error) {
|
2024-02-08 16:28:19 +00:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (*types.Node, error) {
|
|
|
|
return GetNodeByAnyKey(rx, machineKey, nodeKey, oldNodeKey)
|
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetNodeByAnyKey finds a Node by its MachineKey, its current NodeKey or the old one, and returns the Node struct.
|
2024-02-08 16:28:19 +00:00
|
|
|
// TODO(kradalby): see if we can remove this.
|
|
|
|
func GetNodeByAnyKey(
|
|
|
|
tx *gorm.DB,
|
2023-09-24 11:42:05 +00:00
|
|
|
machineKey key.MachinePublic, nodeKey key.NodePublic, oldNodeKey key.NodePublic,
|
|
|
|
) (*types.Node, error) {
|
|
|
|
node := types.Node{}
|
2024-02-08 16:28:19 +00:00
|
|
|
if result := tx.
|
2023-09-24 11:42:05 +00:00
|
|
|
Preload("AuthKey").
|
|
|
|
Preload("AuthKey.User").
|
|
|
|
Preload("User").
|
|
|
|
Preload("Routes").
|
|
|
|
First(&node, "machine_key = ? OR node_key = ? OR node_key = ?",
|
2023-11-16 16:55:29 +00:00
|
|
|
machineKey.String(),
|
|
|
|
nodeKey.String(),
|
|
|
|
oldNodeKey.String()); result.Error != nil {
|
2023-09-24 11:42:05 +00:00
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return &node, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (hsdb *HSDatabase) SetTags(
|
2024-02-23 09:59:24 +00:00
|
|
|
nodeID types.NodeID,
|
2024-02-08 16:28:19 +00:00
|
|
|
tags []string,
|
|
|
|
) error {
|
|
|
|
return hsdb.Write(func(tx *gorm.DB) error {
|
|
|
|
return SetTags(tx, nodeID, tags)
|
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetTags takes a Node struct pointer and update the forced tags.
|
2024-02-08 16:28:19 +00:00
|
|
|
func SetTags(
|
|
|
|
tx *gorm.DB,
|
2024-02-23 09:59:24 +00:00
|
|
|
nodeID types.NodeID,
|
2023-09-24 11:42:05 +00:00
|
|
|
tags []string,
|
|
|
|
) error {
|
2023-12-09 17:09:24 +00:00
|
|
|
if len(tags) == 0 {
|
2024-04-17 22:08:32 +00:00
|
|
|
// if no tags are provided, we remove all forced tags
|
2024-10-02 09:41:58 +00:00
|
|
|
if err := tx.Model(&types.Node{}).Where("id = ?", nodeID).Update("forced_tags", "[]").Error; err != nil {
|
2024-04-17 22:08:32 +00:00
|
|
|
return fmt.Errorf("failed to remove tags for node in the database: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-02 09:41:58 +00:00
|
|
|
var newTags []string
|
2023-09-24 11:42:05 +00:00
|
|
|
for _, tag := range tags {
|
2024-10-02 07:06:09 +00:00
|
|
|
if !slices.Contains(newTags, tag) {
|
2023-09-24 11:42:05 +00:00
|
|
|
newTags = append(newTags, tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-02 09:41:58 +00:00
|
|
|
b, err := json.Marshal(newTags)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Model(&types.Node{}).Where("id = ?", nodeID).Update("forced_tags", string(b)).Error; err != nil {
|
2023-09-24 11:42:05 +00:00
|
|
|
return fmt.Errorf("failed to update tags for node in the database: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenameNode takes a Node struct and a new GivenName for the nodes
|
2024-09-11 16:27:49 +00:00
|
|
|
// and renames it. If the name is not unique, it will return an error.
|
2024-02-08 16:28:19 +00:00
|
|
|
func RenameNode(tx *gorm.DB,
|
2024-09-11 16:27:49 +00:00
|
|
|
nodeID types.NodeID, newName string,
|
2024-02-08 16:28:19 +00:00
|
|
|
) error {
|
2023-09-24 11:42:05 +00:00
|
|
|
err := util.CheckForFQDNRules(
|
|
|
|
newName,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-04-12 13:57:43 +00:00
|
|
|
return fmt.Errorf("renaming node: %w", err)
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 16:27:49 +00:00
|
|
|
uniq, err := isUnqiueName(tx, newName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("checking if name is unique: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !uniq {
|
|
|
|
return fmt.Errorf("name is not unique: %s", newName)
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
if err := tx.Model(&types.Node{}).Where("id = ?", nodeID).Update("given_name", newName).Error; err != nil {
|
2023-09-24 11:42:05 +00:00
|
|
|
return fmt.Errorf("failed to rename node in the database: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
func (hsdb *HSDatabase) NodeSetExpiry(nodeID types.NodeID, expiry time.Time) error {
|
2024-02-08 16:28:19 +00:00
|
|
|
return hsdb.Write(func(tx *gorm.DB) error {
|
|
|
|
return NodeSetExpiry(tx, nodeID, expiry)
|
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// NodeSetExpiry takes a Node struct and a new expiry time.
|
|
|
|
func NodeSetExpiry(tx *gorm.DB,
|
2024-02-23 09:59:24 +00:00
|
|
|
nodeID types.NodeID, expiry time.Time,
|
2024-02-08 16:28:19 +00:00
|
|
|
) error {
|
|
|
|
return tx.Model(&types.Node{}).Where("id = ?", nodeID).Update("expiry", expiry).Error
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 16:28:17 +00:00
|
|
|
func (hsdb *HSDatabase) DeleteNode(node *types.Node, isLikelyConnected *xsync.MapOf[types.NodeID, bool]) ([]types.NodeID, error) {
|
2024-02-23 09:59:24 +00:00
|
|
|
return Write(hsdb.DB, func(tx *gorm.DB) ([]types.NodeID, error) {
|
2024-04-21 16:28:17 +00:00
|
|
|
return DeleteNode(tx, node, isLikelyConnected)
|
2024-02-08 16:28:19 +00:00
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// DeleteNode deletes a Node from the database.
|
|
|
|
// Caller is responsible for notifying all of change.
|
|
|
|
func DeleteNode(tx *gorm.DB,
|
|
|
|
node *types.Node,
|
2024-04-21 16:28:17 +00:00
|
|
|
isLikelyConnected *xsync.MapOf[types.NodeID, bool],
|
2024-02-23 09:59:24 +00:00
|
|
|
) ([]types.NodeID, error) {
|
2024-04-21 16:28:17 +00:00
|
|
|
changed, err := deleteNodeRoutes(tx, node, isLikelyConnected)
|
2023-09-24 11:42:05 +00:00
|
|
|
if err != nil {
|
2024-02-23 09:59:24 +00:00
|
|
|
return changed, err
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unscoped causes the node to be fully removed from the database.
|
2024-05-16 00:40:14 +00:00
|
|
|
if err := tx.Unscoped().Delete(&types.Node{}, node.ID).Error; err != nil {
|
2024-02-23 09:59:24 +00:00
|
|
|
return changed, err
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
return changed, nil
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 08:01:59 +00:00
|
|
|
// DeleteEphemeralNode deletes a Node from the database, note that this method
|
|
|
|
// will remove it straight, and not notify any changes or consider any routes.
|
|
|
|
// It is intended for Ephemeral nodes.
|
|
|
|
func (hsdb *HSDatabase) DeleteEphemeralNode(
|
|
|
|
nodeID types.NodeID,
|
|
|
|
) error {
|
|
|
|
return hsdb.Write(func(tx *gorm.DB) error {
|
|
|
|
if err := tx.Unscoped().Delete(&types.Node{}, nodeID).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
// SetLastSeen sets a node's last seen field indicating that we
|
2023-09-24 11:42:05 +00:00
|
|
|
// have recently communicating with this node.
|
2024-02-23 09:59:24 +00:00
|
|
|
func SetLastSeen(tx *gorm.DB, nodeID types.NodeID, lastSeen time.Time) error {
|
2024-02-08 16:28:19 +00:00
|
|
|
return tx.Model(&types.Node{}).Where("id = ?", nodeID).Update("last_seen", lastSeen).Error
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
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 12:50:17 +00:00
|
|
|
func (hsdb *HSDatabase) RegisterNodeFromAuthCallback(
|
2023-11-19 21:37:04 +00:00
|
|
|
mkey key.MachinePublic,
|
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 12:50:17 +00:00
|
|
|
userID types.UserID,
|
2023-09-24 11:42:05 +00:00
|
|
|
nodeExpiry *time.Time,
|
|
|
|
registrationMethod string,
|
2024-04-17 05:03:06 +00:00
|
|
|
ipv4 *netip.Addr,
|
|
|
|
ipv6 *netip.Addr,
|
2023-09-24 11:42:05 +00:00
|
|
|
) (*types.Node, error) {
|
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 12:50:17 +00:00
|
|
|
return Write(hsdb.DB, func(tx *gorm.DB) (*types.Node, error) {
|
|
|
|
if node, ok := hsdb.regCache.Get(mkey.String()); ok {
|
|
|
|
user, err := GetUserByID(tx, userID)
|
2023-09-24 11:42:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"failed to find user in register node from auth callback, %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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 12:50:17 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("machine_key", mkey.ShortString()).
|
|
|
|
Str("username", user.Username()).
|
|
|
|
Str("registrationMethod", registrationMethod).
|
|
|
|
Str("expiresAt", fmt.Sprintf("%v", nodeExpiry)).
|
|
|
|
Msg("Registering node from API/CLI or auth callback")
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
// Registration of expired node with different user
|
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 12:50:17 +00:00
|
|
|
if node.ID != 0 &&
|
|
|
|
node.UserID != user.ID {
|
2023-09-24 11:42:05 +00:00
|
|
|
return nil, ErrDifferentRegisteredUser
|
|
|
|
}
|
|
|
|
|
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 12:50:17 +00:00
|
|
|
node.UserID = user.ID
|
|
|
|
node.User = *user
|
|
|
|
node.RegisterMethod = registrationMethod
|
2023-09-24 11:42:05 +00:00
|
|
|
|
|
|
|
if nodeExpiry != nil {
|
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 12:50:17 +00:00
|
|
|
node.Expiry = nodeExpiry
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
node, err := RegisterNode(
|
|
|
|
tx,
|
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 12:50:17 +00:00
|
|
|
node,
|
2024-04-17 05:03:06 +00:00
|
|
|
ipv4, ipv6,
|
2023-09-24 11:42:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err == nil {
|
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 12:50:17 +00:00
|
|
|
hsdb.regCache.Delete(mkey.String())
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
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 12:50:17 +00:00
|
|
|
return nil, ErrNodeNotFoundRegistrationCache
|
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 05:03:06 +00:00
|
|
|
func (hsdb *HSDatabase) RegisterNode(node types.Node, ipv4 *netip.Addr, ipv6 *netip.Addr) (*types.Node, error) {
|
2024-02-08 16:28:19 +00:00
|
|
|
return Write(hsdb.DB, func(tx *gorm.DB) (*types.Node, error) {
|
2024-04-17 05:03:06 +00:00
|
|
|
return RegisterNode(tx, node, ipv4, ipv6)
|
2024-02-08 16:28:19 +00:00
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// RegisterNode is executed from the CLI to register a new Node using its MachineKey.
|
2024-04-17 05:03:06 +00:00
|
|
|
func RegisterNode(tx *gorm.DB, node types.Node, ipv4 *netip.Addr, ipv6 *netip.Addr) (*types.Node, error) {
|
2023-09-24 11:42:05 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("node", node.Hostname).
|
2023-11-19 21:37:04 +00:00
|
|
|
Str("machine_key", node.MachineKey.ShortString()).
|
|
|
|
Str("node_key", node.NodeKey.ShortString()).
|
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 12:50:17 +00:00
|
|
|
Str("user", node.User.Username()).
|
2023-09-24 11:42:05 +00:00
|
|
|
Msg("Registering node")
|
|
|
|
|
2024-04-17 05:03:06 +00:00
|
|
|
// If the node exists and it already has IP(s), we just save it
|
2023-09-24 11:42:05 +00:00
|
|
|
// so we store the node.Expire and node.Nodekey that has been set when
|
|
|
|
// adding it to the registrationCache
|
2024-04-17 05:03:06 +00:00
|
|
|
if node.IPv4 != nil || node.IPv6 != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
if err := tx.Save(&node).Error; err != nil {
|
2023-09-24 11:42:05 +00:00
|
|
|
return nil, fmt.Errorf("failed register existing node in the database: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Caller().
|
|
|
|
Str("node", node.Hostname).
|
2023-11-19 21:37:04 +00:00
|
|
|
Str("machine_key", node.MachineKey.ShortString()).
|
|
|
|
Str("node_key", node.NodeKey.ShortString()).
|
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 12:50:17 +00:00
|
|
|
Str("user", node.User.Username()).
|
2023-09-24 11:42:05 +00:00
|
|
|
Msg("Node authorized again")
|
|
|
|
|
|
|
|
return &node, nil
|
|
|
|
}
|
|
|
|
|
2024-04-17 05:03:06 +00:00
|
|
|
node.IPv4 = ipv4
|
|
|
|
node.IPv6 = ipv6
|
2023-09-24 11:42:05 +00:00
|
|
|
|
2024-09-11 16:27:49 +00:00
|
|
|
if node.GivenName == "" {
|
|
|
|
givenName, err := ensureUniqueGivenName(tx, node.Hostname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to ensure unique given name: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
node.GivenName = givenName
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
if err := tx.Save(&node).Error; err != nil {
|
2023-09-24 11:42:05 +00:00
|
|
|
return nil, fmt.Errorf("failed register(save) node in the database: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Caller().
|
|
|
|
Str("node", node.Hostname).
|
|
|
|
Msg("Node registered with the database")
|
|
|
|
|
|
|
|
return &node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeSetNodeKey sets the node key of a node and saves it to the database.
|
2024-02-08 16:28:19 +00:00
|
|
|
func NodeSetNodeKey(tx *gorm.DB, node *types.Node, nodeKey key.NodePublic) error {
|
|
|
|
return tx.Model(node).Updates(types.Node{
|
2023-11-19 21:37:04 +00:00
|
|
|
NodeKey: nodeKey,
|
2024-02-08 16:28:19 +00:00
|
|
|
}).Error
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) NodeSetMachineKey(
|
|
|
|
node *types.Node,
|
|
|
|
machineKey key.MachinePublic,
|
|
|
|
) error {
|
2024-02-08 16:28:19 +00:00
|
|
|
return hsdb.Write(func(tx *gorm.DB) error {
|
|
|
|
return NodeSetMachineKey(tx, node, machineKey)
|
|
|
|
})
|
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// NodeSetMachineKey sets the node key of a node and saves it to the database.
|
|
|
|
func NodeSetMachineKey(
|
|
|
|
tx *gorm.DB,
|
|
|
|
node *types.Node,
|
|
|
|
machineKey key.MachinePublic,
|
|
|
|
) error {
|
|
|
|
return tx.Model(node).Updates(types.Node{
|
2023-11-19 21:37:04 +00:00
|
|
|
MachineKey: machineKey,
|
2024-02-08 16:28:19 +00:00
|
|
|
}).Error
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeSave saves a node object to the database, prefer to use a specific save method rather
|
|
|
|
// than this. It is intended to be used when we are changing or.
|
2024-02-08 16:28:19 +00:00
|
|
|
// TODO(kradalby): Remove this func, just use Save.
|
|
|
|
func NodeSave(tx *gorm.DB, node *types.Node) error {
|
|
|
|
return tx.Save(node).Error
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) GetAdvertisedRoutes(node *types.Node) ([]netip.Prefix, error) {
|
2024-02-08 16:28:19 +00:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) ([]netip.Prefix, error) {
|
|
|
|
return GetAdvertisedRoutes(rx, node)
|
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// GetAdvertisedRoutes returns the routes that are be advertised by the given node.
|
|
|
|
func GetAdvertisedRoutes(tx *gorm.DB, node *types.Node) ([]netip.Prefix, error) {
|
2023-09-24 11:42:05 +00:00
|
|
|
routes := types.Routes{}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.
|
2023-09-24 11:42:05 +00:00
|
|
|
Preload("Node").
|
|
|
|
Where("node_id = ? AND advertised = ?", node.ID, true).Find(&routes).Error
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2024-04-12 13:57:43 +00:00
|
|
|
return nil, fmt.Errorf("getting advertised routes for node(%d): %w", node.ID, err)
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 20:06:50 +00:00
|
|
|
var prefixes []netip.Prefix
|
2023-09-24 11:42:05 +00:00
|
|
|
for _, route := range routes {
|
|
|
|
prefixes = append(prefixes, netip.Prefix(route.Prefix))
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefixes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) GetEnabledRoutes(node *types.Node) ([]netip.Prefix, error) {
|
2024-02-08 16:28:19 +00:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) ([]netip.Prefix, error) {
|
|
|
|
return GetEnabledRoutes(rx, node)
|
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// GetEnabledRoutes returns the routes that are enabled for the node.
|
|
|
|
func GetEnabledRoutes(tx *gorm.DB, node *types.Node) ([]netip.Prefix, error) {
|
2023-09-24 11:42:05 +00:00
|
|
|
routes := types.Routes{}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.
|
2023-09-24 11:42:05 +00:00
|
|
|
Preload("Node").
|
|
|
|
Where("node_id = ? AND advertised = ? AND enabled = ?", node.ID, true, true).
|
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2024-04-12 13:57:43 +00:00
|
|
|
return nil, fmt.Errorf("getting enabled routes for node(%d): %w", node.ID, err)
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 20:06:50 +00:00
|
|
|
var prefixes []netip.Prefix
|
2023-09-24 11:42:05 +00:00
|
|
|
for _, route := range routes {
|
|
|
|
prefixes = append(prefixes, netip.Prefix(route.Prefix))
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefixes, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func IsRoutesEnabled(tx *gorm.DB, node *types.Node, routeStr string) bool {
|
2023-09-24 11:42:05 +00:00
|
|
|
route, err := netip.ParsePrefix(routeStr)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
enabledRoutes, err := GetEnabledRoutes(tx, node)
|
2023-09-24 11:42:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, enabledRoute := range enabledRoutes {
|
|
|
|
if route == enabledRoute {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (hsdb *HSDatabase) enableRoutes(
|
|
|
|
node *types.Node,
|
2024-10-02 07:06:09 +00:00
|
|
|
newRoutes ...netip.Prefix,
|
2024-02-08 16:28:19 +00:00
|
|
|
) (*types.StateUpdate, error) {
|
|
|
|
return Write(hsdb.DB, func(tx *gorm.DB) (*types.StateUpdate, error) {
|
2024-10-02 07:06:09 +00:00
|
|
|
return enableRoutes(tx, node, newRoutes...)
|
2024-02-08 16:28:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
// enableRoutes enables new routes based on a list of new routes.
|
2024-02-08 16:28:19 +00:00
|
|
|
func enableRoutes(tx *gorm.DB,
|
2024-10-02 07:06:09 +00:00
|
|
|
node *types.Node, newRoutes ...netip.Prefix,
|
2024-02-08 16:28:19 +00:00
|
|
|
) (*types.StateUpdate, error) {
|
|
|
|
advertisedRoutes, err := GetAdvertisedRoutes(tx, node)
|
2023-09-24 11:42:05 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, err
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, newRoute := range newRoutes {
|
2024-10-02 07:06:09 +00:00
|
|
|
if !slices.Contains(advertisedRoutes, newRoute) {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, fmt.Errorf(
|
2023-09-24 11:42:05 +00:00
|
|
|
"route (%s) is not available on node %s: %w",
|
|
|
|
node.Hostname,
|
|
|
|
newRoute, ErrNodeRouteIsNotAvailable,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Separate loop so we don't leave things in a half-updated state
|
|
|
|
for _, prefix := range newRoutes {
|
|
|
|
route := types.Route{}
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.Preload("Node").
|
2024-10-02 09:41:58 +00:00
|
|
|
Where("node_id = ? AND prefix = ?", node.ID, prefix.String()).
|
2023-09-24 11:42:05 +00:00
|
|
|
First(&route).Error
|
|
|
|
if err == nil {
|
|
|
|
route.Enabled = true
|
|
|
|
|
|
|
|
// Mark already as primary if there is only this node offering this subnet
|
|
|
|
// (and is not an exit route)
|
|
|
|
if !route.IsExitRoute() {
|
2024-02-08 16:28:19 +00:00
|
|
|
route.IsPrimary = isUniquePrefix(tx, route)
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
err = tx.Save(&route).Error
|
2023-09-24 11:42:05 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, fmt.Errorf("failed to enable route: %w", err)
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, fmt.Errorf("failed to find route: %w", err)
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
// Ensure the node has the latest routes when notifying the other
|
|
|
|
// nodes
|
2024-02-08 16:28:19 +00:00
|
|
|
nRoutes, err := GetNodeRoutes(tx, node)
|
2023-12-09 17:09:24 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, fmt.Errorf("failed to read back routes: %w", err)
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node.Routes = nRoutes
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
return &types.StateUpdate{
|
2023-12-09 17:09:24 +00:00
|
|
|
Type: types.StatePeerChanged,
|
2024-02-23 09:59:24 +00:00
|
|
|
ChangeNodes: []types.NodeID{node.ID},
|
2024-02-08 16:28:19 +00:00
|
|
|
Message: "created in db.enableRoutes",
|
|
|
|
}, nil
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateGivenName(suppliedName string, randomSuffix bool) (string, error) {
|
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 12:50:17 +00:00
|
|
|
if len(suppliedName) > util.LabelHostnameLength {
|
|
|
|
return "", types.ErrHostnameTooLong
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if randomSuffix {
|
|
|
|
// Trim if a hostname will be longer than 63 chars after adding the hash.
|
|
|
|
trimmedHostnameLength := util.LabelHostnameLength - NodeGivenNameHashLength - NodeGivenNameTrimSize
|
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 12:50:17 +00:00
|
|
|
if len(suppliedName) > trimmedHostnameLength {
|
|
|
|
suppliedName = suppliedName[:trimmedHostnameLength]
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
suffix, err := util.GenerateRandomStringDNSSafe(NodeGivenNameHashLength)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
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 12:50:17 +00:00
|
|
|
suppliedName += "-" + suffix
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
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 12:50:17 +00:00
|
|
|
return suppliedName, nil
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 16:27:49 +00:00
|
|
|
func isUnqiueName(tx *gorm.DB, name string) (bool, error) {
|
|
|
|
nodes := types.Nodes{}
|
|
|
|
if err := tx.
|
|
|
|
Where("given_name = ?", name).Find(&nodes).Error; err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(nodes) == 0, nil
|
2024-02-08 16:28:19 +00:00
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
|
2024-09-11 16:27:49 +00:00
|
|
|
func ensureUniqueGivenName(
|
2024-02-08 16:28:19 +00:00
|
|
|
tx *gorm.DB,
|
2024-09-11 16:27:49 +00:00
|
|
|
name string,
|
2024-02-08 16:28:19 +00:00
|
|
|
) (string, error) {
|
2024-09-11 16:27:49 +00:00
|
|
|
givenName, err := generateGivenName(name, false)
|
2023-09-24 11:42:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-09-11 16:27:49 +00:00
|
|
|
unique, err := isUnqiueName(tx, givenName)
|
2023-09-24 11:42:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-09-11 16:27:49 +00:00
|
|
|
if !unique {
|
|
|
|
postfixedName, err := generateGivenName(name, true)
|
2023-11-19 21:37:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
2023-11-19 21:37:04 +00:00
|
|
|
|
|
|
|
givenName = postfixedName
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return givenName, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func ExpireExpiredNodes(tx *gorm.DB,
|
|
|
|
lastCheck time.Time,
|
|
|
|
) (time.Time, types.StateUpdate, bool) {
|
2023-09-24 11:42:05 +00:00
|
|
|
// use the time of the start of the function to ensure we
|
|
|
|
// dont miss some nodes by returning it _after_ we have
|
|
|
|
// checked everything.
|
|
|
|
started := time.Now()
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
expired := make([]*tailcfg.PeerChange, 0)
|
2023-12-09 17:09:24 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
nodes, err := ListNodes(tx)
|
2023-09-24 11:42:05 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return time.Unix(0, 0), types.StateUpdate{}, false
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
2024-02-23 09:59:24 +00:00
|
|
|
for _, node := range nodes {
|
|
|
|
if node.IsExpired() && node.Expiry.After(lastCheck) {
|
2024-02-08 16:28:19 +00:00
|
|
|
expired = append(expired, &tailcfg.PeerChange{
|
|
|
|
NodeID: tailcfg.NodeID(node.ID),
|
|
|
|
KeyExpiry: node.Expiry,
|
|
|
|
})
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
if len(expired) > 0 {
|
|
|
|
return started, types.StateUpdate{
|
|
|
|
Type: types.StatePeerChangedPatch,
|
|
|
|
ChangePatches: expired,
|
|
|
|
}, true
|
2024-01-05 09:41:56 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
return started, types.StateUpdate{}, false
|
2023-09-24 11:42:05 +00:00
|
|
|
}
|
2024-07-18 08:01:59 +00:00
|
|
|
|
|
|
|
// EphemeralGarbageCollector is a garbage collector that will delete nodes after
|
|
|
|
// a certain amount of time.
|
|
|
|
// It is used to delete ephemeral nodes that have disconnected and should be
|
|
|
|
// cleaned up.
|
|
|
|
type EphemeralGarbageCollector struct {
|
2024-08-27 16:54:28 +00:00
|
|
|
mu sync.Mutex
|
2024-07-18 08:01:59 +00:00
|
|
|
|
|
|
|
deleteFunc func(types.NodeID)
|
|
|
|
toBeDeleted map[types.NodeID]*time.Timer
|
|
|
|
|
|
|
|
deleteCh chan types.NodeID
|
|
|
|
cancelCh chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEphemeralGarbageCollector creates a new EphemeralGarbageCollector, it takes
|
|
|
|
// a deleteFunc that will be called when a node is scheduled for deletion.
|
|
|
|
func NewEphemeralGarbageCollector(deleteFunc func(types.NodeID)) *EphemeralGarbageCollector {
|
|
|
|
return &EphemeralGarbageCollector{
|
|
|
|
toBeDeleted: make(map[types.NodeID]*time.Timer),
|
|
|
|
deleteCh: make(chan types.NodeID, 10),
|
|
|
|
cancelCh: make(chan struct{}),
|
|
|
|
deleteFunc: deleteFunc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops the garbage collector.
|
|
|
|
func (e *EphemeralGarbageCollector) Close() {
|
|
|
|
e.cancelCh <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule schedules a node for deletion after the expiry duration.
|
|
|
|
func (e *EphemeralGarbageCollector) Schedule(nodeID types.NodeID, expiry time.Duration) {
|
|
|
|
e.mu.Lock()
|
|
|
|
timer := time.NewTimer(expiry)
|
|
|
|
e.toBeDeleted[nodeID] = timer
|
2024-08-27 16:54:28 +00:00
|
|
|
e.mu.Unlock()
|
2024-07-18 08:01:59 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case _, ok := <-timer.C:
|
|
|
|
if ok {
|
|
|
|
e.deleteCh <- nodeID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel cancels the deletion of a node.
|
|
|
|
func (e *EphemeralGarbageCollector) Cancel(nodeID types.NodeID) {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
if timer, ok := e.toBeDeleted[nodeID]; ok {
|
|
|
|
timer.Stop()
|
|
|
|
delete(e.toBeDeleted, nodeID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the garbage collector.
|
|
|
|
func (e *EphemeralGarbageCollector) Start() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-e.cancelCh:
|
|
|
|
return
|
|
|
|
case nodeID := <-e.deleteCh:
|
|
|
|
e.mu.Lock()
|
|
|
|
delete(e.toBeDeleted, nodeID)
|
|
|
|
e.mu.Unlock()
|
|
|
|
|
|
|
|
go e.deleteFunc(nodeID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|