db

package
v0.23.0-alpha5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2024 License: BSD-3-Clause Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NodeGivenNameHashLength = 8
	NodeGivenNameTrimSize   = 2
)

Variables

View Source
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",
	)
)
View Source
var (
	ErrPreAuthKeyNotFound          = errors.New("AuthKey not found")
	ErrPreAuthKeyExpired           = errors.New("AuthKey expired")
	ErrSingleUseAuthKeyHasBeenUsed = errors.New("AuthKey has already been used")
	ErrUserMismatch                = errors.New("user mismatch")
	ErrPreAuthKeyACLTagInvalid     = errors.New("AuthKey tag is invalid")
)
View Source
var (
	ErrUserExists        = errors.New("user already exists")
	ErrUserNotFound      = errors.New("user not found")
	ErrUserStillHasNodes = errors.New("user not empty: node(s) found")
)
View Source
var ErrAPIKeyFailedToParse = errors.New("failed to parse ApiKey")
View Source
var ErrCouldNotAllocateIP = errors.New("failed to allocate IP")
View Source
var ErrRouteIsNotAvailable = errors.New("route is not available")

Functions

func AssignNodeToUser

func AssignNodeToUser(tx *gorm.DB, node *types.Node, username string) error

AssignNodeToUser assigns a Node to a user.

func CreatePreAuthKey

func CreatePreAuthKey(
	tx *gorm.DB,
	userName string,
	reusable bool,
	ephemeral bool,
	expiration *time.Time,
	aclTags []string,
) (*types.PreAuthKey, error)

CreatePreAuthKey creates a new PreAuthKey in a user, and returns it.

func CreateUser

func CreateUser(tx *gorm.DB, name string) (*types.User, error)

CreateUser creates a new User. Returns error if could not be created or another user already exists.

func DeleteNode

func DeleteNode(tx *gorm.DB,
	node *types.Node,
	isConnected map[key.MachinePublic]bool,
) error

DeleteNode deletes a Node from the database. Caller is responsible for notifying all of change.

func DeleteRoute

func DeleteRoute(
	tx *gorm.DB,
	id uint64,
	isConnected map[key.MachinePublic]bool,
) (*types.StateUpdate, error)

func DestroyPreAuthKey

func DestroyPreAuthKey(tx *gorm.DB, pak types.PreAuthKey) error

DestroyPreAuthKey destroys a preauthkey. Returns error if the PreAuthKey does not exist.

func DestroyUser

func DestroyUser(tx *gorm.DB, name string) error

DestroyUser destroys a User. Returns error if the User does not exist or if there are nodes associated with it.

func DisableRoute

func DisableRoute(tx *gorm.DB,
	id uint64,
	isConnected map[key.MachinePublic]bool,
) (*types.StateUpdate, error)

func EnableAutoApprovedRoutes

func EnableAutoApprovedRoutes(
	tx *gorm.DB,
	aclPolicy *policy.ACLPolicy,
	node *types.Node,
) (*types.StateUpdate, error)

EnableAutoApprovedRoutes enables any routes advertised by a node that match the ACL autoApprovers policy.

func EnableRoute

func EnableRoute(tx *gorm.DB, id uint64) (*types.StateUpdate, error)

func EnsureFailoverRouteIsAvailable

func EnsureFailoverRouteIsAvailable(
	tx *gorm.DB,
	isConnected map[key.MachinePublic]bool,
	node *types.Node,
) (*types.StateUpdate, error)

EnsureFailoverRouteIsAvailable takes a node and checks if the node's route currently have a functioning host that exposes the network.

func ExpireEphemeralNodes

func ExpireEphemeralNodes(tx *gorm.DB,
	inactivityThreshhold time.Duration,
) (types.StateUpdate, bool)

func ExpireExpiredNodes

func ExpireExpiredNodes(tx *gorm.DB,
	lastCheck time.Time,
) (time.Time, types.StateUpdate, bool)

func ExpirePreAuthKey

func ExpirePreAuthKey(tx *gorm.DB, k *types.PreAuthKey) error

MarkExpirePreAuthKey marks a PreAuthKey as expired.

func GenerateGivenName

func GenerateGivenName(
	tx *gorm.DB,
	mkey key.MachinePublic,
	suppliedName string,
) (string, error)

func GetAdvertisedRoutes

func GetAdvertisedRoutes(tx *gorm.DB, node *types.Node) ([]netip.Prefix, error)

GetAdvertisedRoutes returns the routes that are be advertised by the given node.

func GetEnabledRoutes

func GetEnabledRoutes(tx *gorm.DB, node *types.Node) ([]netip.Prefix, error)

GetEnabledRoutes returns the routes that are enabled for the node.

func GetNodeAdvertisedRoutes

func GetNodeAdvertisedRoutes(tx *gorm.DB, node *types.Node) (types.Routes, error)

func GetNodeByAnyKey

func GetNodeByAnyKey(
	tx *gorm.DB,
	machineKey key.MachinePublic, nodeKey key.NodePublic, oldNodeKey key.NodePublic,
) (*types.Node, error)

GetNodeByAnyKey finds a Node by its MachineKey, its current NodeKey or the old one, and returns the Node struct. TODO(kradalby): see if we can remove this.

func GetNodeByID

func GetNodeByID(tx *gorm.DB, id uint64) (*types.Node, error)

GetNodeByID finds a Node by ID and returns the Node struct.

func GetNodeByMachineKey

func GetNodeByMachineKey(
	tx *gorm.DB,
	machineKey key.MachinePublic,
) (*types.Node, error)

GetNodeByMachineKey finds a Node by its MachineKey and returns the Node struct.

func GetNodePrimaryRoutes

func GetNodePrimaryRoutes(tx *gorm.DB, node *types.Node) (types.Routes, error)

getNodePrimaryRoutes returns the routes that are enabled and marked as primary (for subnet failover) Exit nodes are not considered for this, as they are never marked as Primary.

func GetNodeRoutes

func GetNodeRoutes(tx *gorm.DB, node *types.Node) (types.Routes, error)

func GetPreAuthKey

func GetPreAuthKey(tx *gorm.DB, user string, key string) (*types.PreAuthKey, error)

GetPreAuthKey returns a PreAuthKey for a given key.

func GetRoute

func GetRoute(tx *gorm.DB, id uint64) (*types.Route, error)

func GetRoutes

func GetRoutes(tx *gorm.DB) (types.Routes, error)

func GetUser

func GetUser(tx *gorm.DB, name string) (*types.User, error)

func IsRoutesEnabled

func IsRoutesEnabled(tx *gorm.DB, node *types.Node, routeStr string) bool

func ListNodes

func ListNodes(tx *gorm.DB) (types.Nodes, error)

func ListNodesByUser

func ListNodesByUser(tx *gorm.DB, name string) (types.Nodes, error)

ListNodesByUser gets all the nodes in a given user.

func ListPeers

func ListPeers(tx *gorm.DB, node *types.Node) (types.Nodes, error)

ListPeers returns all peers of node, regardless of any Policy or if the node is expired.

func ListPreAuthKeys

func ListPreAuthKeys(tx *gorm.DB, userName string) ([]types.PreAuthKey, error)

ListPreAuthKeys returns the list of PreAuthKeys for a user.

func ListUsers

func ListUsers(tx *gorm.DB) ([]types.User, error)

ListUsers gets all the existing users.

func NodeSave

func NodeSave(tx *gorm.DB, node *types.Node) error

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. TODO(kradalby): Remove this func, just use Save.

func NodeSetExpiry

func NodeSetExpiry(tx *gorm.DB,
	nodeID uint64, expiry time.Time,
) error

NodeSetExpiry takes a Node struct and a new expiry time.

func NodeSetMachineKey

func NodeSetMachineKey(
	tx *gorm.DB,
	node *types.Node,
	machineKey key.MachinePublic,
) error

NodeSetMachineKey sets the node key of a node and saves it to the database.

func NodeSetNodeKey

func NodeSetNodeKey(tx *gorm.DB, node *types.Node, nodeKey key.NodePublic) error

NodeSetNodeKey sets the node key of a node and saves it to the database.

func Read

func Read[T any](db *gorm.DB, fn func(rx *gorm.DB) (T, error)) (T, error)

func RegisterNode

func RegisterNode(tx *gorm.DB, node types.Node, addrs types.NodeAddresses) (*types.Node, error)

RegisterNode is executed from the CLI to register a new Node using its MachineKey.

func RegisterNodeFromAuthCallback

func RegisterNodeFromAuthCallback(
	tx *gorm.DB,
	cache *cache.Cache,
	mkey key.MachinePublic,
	userName string,
	nodeExpiry *time.Time,
	registrationMethod string,
	addrs types.NodeAddresses,
) (*types.Node, error)

func RenameNode

func RenameNode(tx *gorm.DB,
	nodeID uint64, newName string,
) error

RenameNode takes a Node struct and a new GivenName for the nodes and renames it.

func RenameUser

func RenameUser(tx *gorm.DB, oldName, newName string) error

RenameUser renames a User. Returns error if the User does not exist or if another User exists with the new name.

func SaveNodeRoutes

func SaveNodeRoutes(tx *gorm.DB, node *types.Node) (bool, error)

SaveNodeRoutes takes a node and updates the database with the new routes. It returns a bool whether an update should be sent as the saved route impacts nodes.

func SetTags

func SetTags(
	tx *gorm.DB,
	nodeID uint64,
	tags []string,
) error

SetTags takes a Node struct pointer and update the forced tags.

func UpdateLastSeen

func UpdateLastSeen(tx *gorm.DB, nodeID uint64, lastSeen time.Time) error

UpdateLastSeen sets a node's last seen field indicating that we have recently communicating with this node.

func UsePreAuthKey

func UsePreAuthKey(tx *gorm.DB, k *types.PreAuthKey) error

UsePreAuthKey marks a PreAuthKey as used.

func ValidatePreAuthKey

func ValidatePreAuthKey(tx *gorm.DB, k string) (*types.PreAuthKey, error)

ValidatePreAuthKey does the heavy lifting for validation of the PreAuthKey coming from a node If returns no error and a PreAuthKey, it can be used.

func Write

func Write[T any](db *gorm.DB, fn func(tx *gorm.DB) (T, error)) (T, error)

Types

type HSDatabase

type HSDatabase struct {
	DB *gorm.DB
	// contains filtered or unexported fields
}

func NewHeadscaleDatabase

func NewHeadscaleDatabase(
	cfg types.DatabaseConfig,
	baseDomain string,
) (*HSDatabase, error)

TODO(kradalby): assemble this struct from toptions or something typed rather than arguments.

func (*HSDatabase) AssignNodeToUser

func (hsdb *HSDatabase) AssignNodeToUser(node *types.Node, username string) error

func (*HSDatabase) Close

func (hsdb *HSDatabase) Close() error

func (*HSDatabase) CreateAPIKey

func (hsdb *HSDatabase) CreateAPIKey(
	expiration *time.Time,
) (string, *types.APIKey, error)

CreateAPIKey creates a new ApiKey in a user, and returns it.

func (*HSDatabase) CreatePreAuthKey

func (hsdb *HSDatabase) CreatePreAuthKey(
	userName string,
	reusable bool,
	ephemeral bool,
	expiration *time.Time,
	aclTags []string,
) (*types.PreAuthKey, error)

func (*HSDatabase) CreateUser

func (hsdb *HSDatabase) CreateUser(name string) (*types.User, error)

func (*HSDatabase) DeleteNode

func (hsdb *HSDatabase) DeleteNode(node *types.Node, isConnected map[key.MachinePublic]bool) error

func (*HSDatabase) DeleteRoute

func (hsdb *HSDatabase) DeleteRoute(
	id uint64,
	isConnected map[key.MachinePublic]bool,
) (*types.StateUpdate, error)

func (*HSDatabase) DestroyAPIKey

func (hsdb *HSDatabase) DestroyAPIKey(key types.APIKey) error

DestroyAPIKey destroys a ApiKey. Returns error if the ApiKey does not exist.

func (*HSDatabase) DestroyUser

func (hsdb *HSDatabase) DestroyUser(name string) error

func (*HSDatabase) EnableAutoApprovedRoutes

func (hsdb *HSDatabase) EnableAutoApprovedRoutes(
	aclPolicy *policy.ACLPolicy,
	node *types.Node,
) (*types.StateUpdate, error)

func (*HSDatabase) ExpireAPIKey

func (hsdb *HSDatabase) ExpireAPIKey(key *types.APIKey) error

ExpireAPIKey marks a ApiKey as expired.

func (*HSDatabase) ExpirePreAuthKey

func (hsdb *HSDatabase) ExpirePreAuthKey(k *types.PreAuthKey) error

func (*HSDatabase) GenerateGivenName

func (hsdb *HSDatabase) GenerateGivenName(
	mkey key.MachinePublic,
	suppliedName string,
) (string, error)

func (*HSDatabase) GetAPIKey

func (hsdb *HSDatabase) GetAPIKey(prefix string) (*types.APIKey, error)

GetAPIKey returns a ApiKey for a given key.

func (*HSDatabase) GetAPIKeyByID

func (hsdb *HSDatabase) GetAPIKeyByID(id uint64) (*types.APIKey, error)

GetAPIKeyByID returns a ApiKey for a given id.

func (*HSDatabase) GetAdvertisedRoutes

func (hsdb *HSDatabase) GetAdvertisedRoutes(node *types.Node) ([]netip.Prefix, error)

func (*HSDatabase) GetEnabledRoutes

func (hsdb *HSDatabase) GetEnabledRoutes(node *types.Node) ([]netip.Prefix, error)

func (*HSDatabase) GetNodeByAnyKey

func (hsdb *HSDatabase) GetNodeByAnyKey(
	machineKey key.MachinePublic,
	nodeKey key.NodePublic,
	oldNodeKey key.NodePublic,
) (*types.Node, error)

func (*HSDatabase) GetNodeByID

func (hsdb *HSDatabase) GetNodeByID(id uint64) (*types.Node, error)

func (*HSDatabase) GetNodeByMachineKey

func (hsdb *HSDatabase) GetNodeByMachineKey(machineKey key.MachinePublic) (*types.Node, error)

func (*HSDatabase) GetNodePrimaryRoutes

func (hsdb *HSDatabase) GetNodePrimaryRoutes(node *types.Node) (types.Routes, error)

func (*HSDatabase) GetNodeRoutes

func (hsdb *HSDatabase) GetNodeRoutes(node *types.Node) (types.Routes, error)

func (*HSDatabase) GetUser

func (hsdb *HSDatabase) GetUser(name string) (*types.User, error)

func (*HSDatabase) ListAPIKeys

func (hsdb *HSDatabase) ListAPIKeys() ([]types.APIKey, error)

ListAPIKeys returns the list of ApiKeys for a user.

func (*HSDatabase) ListNodes

func (hsdb *HSDatabase) ListNodes() (types.Nodes, error)

func (*HSDatabase) ListPeers

func (hsdb *HSDatabase) ListPeers(node *types.Node) (types.Nodes, error)

func (*HSDatabase) ListPreAuthKeys

func (hsdb *HSDatabase) ListPreAuthKeys(userName string) ([]types.PreAuthKey, error)

func (*HSDatabase) ListUsers

func (hsdb *HSDatabase) ListUsers() ([]types.User, error)

func (*HSDatabase) NodeSetExpiry

func (hsdb *HSDatabase) NodeSetExpiry(nodeID uint64, expiry time.Time) error

func (*HSDatabase) NodeSetMachineKey

func (hsdb *HSDatabase) NodeSetMachineKey(
	node *types.Node,
	machineKey key.MachinePublic,
) error

func (*HSDatabase) PingDB

func (hsdb *HSDatabase) PingDB(ctx context.Context) error

func (*HSDatabase) Read

func (hsdb *HSDatabase) Read(fn func(rx *gorm.DB) error) error

func (*HSDatabase) RegisterNode

func (hsdb *HSDatabase) RegisterNode(node types.Node, addrs types.NodeAddresses) (*types.Node, error)

func (*HSDatabase) RenameUser

func (hsdb *HSDatabase) RenameUser(oldName, newName string) error

func (*HSDatabase) SaveNodeRoutes

func (hsdb *HSDatabase) SaveNodeRoutes(node *types.Node) (bool, error)

func (*HSDatabase) SetTags

func (hsdb *HSDatabase) SetTags(
	nodeID uint64,
	tags []string,
) error

func (*HSDatabase) ValidateAPIKey

func (hsdb *HSDatabase) ValidateAPIKey(keyStr string) (bool, error)

func (*HSDatabase) ValidatePreAuthKey

func (hsdb *HSDatabase) ValidatePreAuthKey(k string) (*types.PreAuthKey, error)

func (*HSDatabase) Write

func (hsdb *HSDatabase) Write(fn func(tx *gorm.DB) error) error

type IPAllocator

type IPAllocator struct {
	// contains filtered or unexported fields
}

IPAllocator is a singleton responsible for allocating IP addresses for nodes and making sure the same address is not handed out twice. There can only be one and it needs to be created before any other database writes occur.

func NewIPAllocator

func NewIPAllocator(db *HSDatabase, prefix4, prefix6 netip.Prefix) (*IPAllocator, error)

NewIPAllocator returns a new IPAllocator singleton which can be used to hand out unique IP addresses within the provided IPv4 and IPv6 prefix. It needs to be created when headscale starts and needs to finish its read transaction before any writes to the database occur.

func (*IPAllocator) Next

func (i *IPAllocator) Next() (types.NodeAddresses, error)

type KV

type KV struct {
	Key   string
	Value string
}

KV is a key-value store in a psql table. For future use... TODO(kradalby): Is this used for anything?

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL