2023-05-21 18:37:59 +02:00
|
|
|
package db
|
2021-04-28 16:55:29 +02:00
|
|
|
|
|
|
|
import (
|
2022-11-25 16:29:45 +01:00
|
|
|
"errors"
|
2022-09-02 00:06:19 +02:00
|
|
|
"net/netip"
|
2022-11-23 19:50:30 +01:00
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2022-11-24 23:41:11 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-12-09 18:09:24 +01:00
|
|
|
"github.com/samber/lo"
|
2022-11-23 19:50:30 +01:00
|
|
|
"gorm.io/gorm"
|
2023-12-09 18:09:24 +01:00
|
|
|
"tailscale.com/types/key"
|
2021-04-28 16:55:29 +02:00
|
|
|
)
|
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
var ErrRouteIsNotAvailable = errors.New("route is not available")
|
2021-08-21 15:49:46 +02:00
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
func (hsdb *HSDatabase) GetRoutes() (types.Routes, error) {
|
2023-07-17 13:35:05 +02:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
|
|
|
return hsdb.getRoutes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) getRoutes() (types.Routes, error) {
|
2023-05-21 18:37:59 +02:00
|
|
|
var routes types.Routes
|
2023-12-09 18:09:24 +01:00
|
|
|
err := hsdb.db.
|
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) getAdvertisedAndEnabledRoutes() (types.Routes, error) {
|
|
|
|
var routes types.Routes
|
|
|
|
err := hsdb.db.
|
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
Where("advertised = ? AND enabled = ?", true, true).
|
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) getRoutesByPrefix(pref netip.Prefix) (types.Routes, error) {
|
|
|
|
var routes types.Routes
|
|
|
|
err := hsdb.db.
|
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
Where("prefix = ?", types.IPPrefix(pref)).
|
|
|
|
Find(&routes).Error
|
2023-05-21 18:37:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-21 15:49:46 +02:00
|
|
|
}
|
2022-11-25 16:29:45 +01:00
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
return routes, nil
|
2021-08-21 15:49:46 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
func (hsdb *HSDatabase) GetNodeAdvertisedRoutes(node *types.Node) (types.Routes, error) {
|
2023-07-17 13:35:05 +02:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
return hsdb.getNodeAdvertisedRoutes(node)
|
2023-07-17 13:35:05 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
func (hsdb *HSDatabase) getNodeAdvertisedRoutes(node *types.Node) (types.Routes, error) {
|
2023-05-21 18:37:59 +02:00
|
|
|
var routes types.Routes
|
|
|
|
err := hsdb.db.
|
2023-09-24 13:42:05 +02:00
|
|
|
Preload("Node").
|
2023-12-09 18:09:24 +01:00
|
|
|
Preload("Node.User").
|
2023-09-24 13:42:05 +02:00
|
|
|
Where("node_id = ? AND advertised = true", node.ID).
|
2023-05-21 18:37:59 +02:00
|
|
|
Find(&routes).Error
|
2022-11-25 18:50:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
func (hsdb *HSDatabase) GetNodeRoutes(node *types.Node) (types.Routes, error) {
|
2023-07-17 13:35:05 +02:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
return hsdb.getNodeRoutes(node)
|
2023-07-17 13:35:05 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
func (hsdb *HSDatabase) getNodeRoutes(node *types.Node) (types.Routes, error) {
|
2023-05-21 18:37:59 +02:00
|
|
|
var routes types.Routes
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.
|
2023-09-24 13:42:05 +02:00
|
|
|
Preload("Node").
|
2023-12-09 18:09:24 +01:00
|
|
|
Preload("Node.User").
|
2023-09-24 13:42:05 +02:00
|
|
|
Where("node_id = ?", node.ID).
|
2022-11-25 18:50:12 +01:00
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
func (hsdb *HSDatabase) GetRoute(id uint64) (*types.Route, error) {
|
2023-07-17 13:35:05 +02:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
|
|
|
return hsdb.getRoute(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) getRoute(id uint64) (*types.Route, error) {
|
2023-05-21 18:37:59 +02:00
|
|
|
var route types.Route
|
2023-12-09 18:09:24 +01:00
|
|
|
err := hsdb.db.
|
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
First(&route, id).Error
|
2022-11-25 18:50:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &route, nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
func (hsdb *HSDatabase) EnableRoute(id uint64) error {
|
2023-07-17 13:35:05 +02:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
|
|
|
return hsdb.enableRoute(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) enableRoute(id uint64) error {
|
|
|
|
route, err := hsdb.getRoute(id)
|
2022-11-25 18:50:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-22 23:39:42 +01:00
|
|
|
// Tailscale requires both IPv4 and IPv6 exit routes to
|
|
|
|
// be enabled at the same time, as per
|
|
|
|
// https://github.com/juanfont/headscale/issues/804#issuecomment-1399314002
|
2023-05-21 18:37:59 +02:00
|
|
|
if route.IsExitRoute() {
|
|
|
|
return hsdb.enableRoutes(
|
2023-09-24 13:42:05 +02:00
|
|
|
&route.Node,
|
2023-05-21 18:37:59 +02:00
|
|
|
types.ExitRouteV4.String(),
|
|
|
|
types.ExitRouteV6.String(),
|
|
|
|
)
|
2023-01-22 23:39:42 +01:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
return hsdb.enableRoutes(&route.Node, netip.Prefix(route.Prefix).String())
|
2022-11-25 18:50:12 +01:00
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
func (hsdb *HSDatabase) DisableRoute(id uint64) error {
|
2023-07-17 13:35:05 +02:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
|
|
|
route, err := hsdb.getRoute(id)
|
2022-11-25 18:50:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
var routes types.Routes
|
|
|
|
node := route.Node
|
|
|
|
|
2023-05-07 12:17:16 +02:00
|
|
|
// Tailscale requires both IPv4 and IPv6 exit routes to
|
|
|
|
// be enabled at the same time, as per
|
|
|
|
// https://github.com/juanfont/headscale/issues/804#issuecomment-1399314002
|
2023-05-21 18:37:59 +02:00
|
|
|
if !route.IsExitRoute() {
|
2023-12-09 18:09:24 +01:00
|
|
|
err = hsdb.failoverRouteWithNotify(route)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-07 12:17:16 +02:00
|
|
|
route.Enabled = false
|
|
|
|
route.IsPrimary = false
|
2023-05-11 09:09:18 +02:00
|
|
|
err = hsdb.db.Save(route).Error
|
2023-05-07 12:17:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-09 18:09:24 +01:00
|
|
|
} else {
|
|
|
|
routes, err = hsdb.getNodeRoutes(&node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-07 12:17:16 +02:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
for i := range routes {
|
|
|
|
if routes[i].IsExitRoute() {
|
|
|
|
routes[i].Enabled = false
|
|
|
|
routes[i].IsPrimary = false
|
|
|
|
err = hsdb.db.Save(&routes[i]).Error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 12:17:16 +02:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
if routes == nil {
|
|
|
|
routes, err = hsdb.getNodeRoutes(&node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-25 18:50:12 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
node.Routes = routes
|
|
|
|
|
|
|
|
stateUpdate := types.StateUpdate{
|
|
|
|
Type: types.StatePeerChanged,
|
|
|
|
ChangeNodes: types.Nodes{&node},
|
|
|
|
Message: "called from db.DisableRoute",
|
|
|
|
}
|
|
|
|
if stateUpdate.Valid() {
|
|
|
|
hsdb.notifier.NotifyAll(stateUpdate)
|
2023-05-07 12:17:16 +02:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
return nil
|
2022-11-25 18:50:12 +01:00
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
func (hsdb *HSDatabase) DeleteRoute(id uint64) error {
|
2023-07-17 13:35:05 +02:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
|
|
|
route, err := hsdb.getRoute(id)
|
2023-03-06 09:05:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
var routes types.Routes
|
|
|
|
node := route.Node
|
|
|
|
|
2023-05-07 12:17:16 +02:00
|
|
|
// Tailscale requires both IPv4 and IPv6 exit routes to
|
|
|
|
// be enabled at the same time, as per
|
|
|
|
// https://github.com/juanfont/headscale/issues/804#issuecomment-1399314002
|
2023-05-21 18:37:59 +02:00
|
|
|
if !route.IsExitRoute() {
|
2023-12-09 18:09:24 +01:00
|
|
|
err := hsdb.failoverRouteWithNotify(route)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&route).Error; err != nil {
|
2023-05-07 12:17:16 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-12-09 18:09:24 +01:00
|
|
|
} else {
|
|
|
|
routes, err := hsdb.getNodeRoutes(&node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-07 12:17:16 +02:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
routesToDelete := types.Routes{}
|
|
|
|
for _, r := range routes {
|
|
|
|
if r.IsExitRoute() {
|
|
|
|
routesToDelete = append(routesToDelete, r)
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 12:17:16 +02:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&routesToDelete).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-07 12:17:16 +02:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
if routes == nil {
|
|
|
|
routes, err = hsdb.getNodeRoutes(&node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-07 12:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
node.Routes = routes
|
|
|
|
|
|
|
|
stateUpdate := types.StateUpdate{
|
|
|
|
Type: types.StatePeerChanged,
|
|
|
|
ChangeNodes: types.Nodes{&node},
|
|
|
|
Message: "called from db.DeleteRoute",
|
|
|
|
}
|
|
|
|
if stateUpdate.Valid() {
|
|
|
|
hsdb.notifier.NotifyAll(stateUpdate)
|
2023-03-06 09:05:40 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
return nil
|
2023-03-06 09:05:40 +01:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
func (hsdb *HSDatabase) deleteNodeRoutes(node *types.Node) error {
|
|
|
|
routes, err := hsdb.getNodeRoutes(node)
|
2023-03-06 09:05:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range routes {
|
2023-05-11 09:09:18 +02:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&routes[i]).Error; err != nil {
|
2023-03-06 09:05:40 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-12-09 18:09:24 +01:00
|
|
|
|
|
|
|
// TODO(kradalby): This is a bit too aggressive, we could probably
|
|
|
|
// figure out which routes needs to be failed over rather than all.
|
|
|
|
hsdb.failoverRouteWithNotify(&routes[i])
|
2023-03-06 09:05:40 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
return nil
|
2023-03-06 09:05:40 +01:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
// isUniquePrefix returns if there is another node providing the same route already.
|
2023-05-21 18:37:59 +02:00
|
|
|
func (hsdb *HSDatabase) isUniquePrefix(route types.Route) bool {
|
2022-11-24 17:00:40 +01:00
|
|
|
var count int64
|
2023-05-11 09:09:18 +02:00
|
|
|
hsdb.db.
|
2023-05-21 18:37:59 +02:00
|
|
|
Model(&types.Route{}).
|
2023-09-24 13:42:05 +02:00
|
|
|
Where("prefix = ? AND node_id != ? AND advertised = ? AND enabled = ?",
|
2022-11-24 17:00:40 +01:00
|
|
|
route.Prefix,
|
2023-09-24 13:42:05 +02:00
|
|
|
route.NodeID,
|
2022-11-24 17:00:40 +01:00
|
|
|
true, true).Count(&count)
|
2022-11-25 16:29:45 +01:00
|
|
|
|
2022-11-24 17:00:40 +01:00
|
|
|
return count == 0
|
|
|
|
}
|
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
func (hsdb *HSDatabase) getPrimaryRoute(prefix netip.Prefix) (*types.Route, error) {
|
|
|
|
var route types.Route
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.
|
2023-09-24 13:42:05 +02:00
|
|
|
Preload("Node").
|
2023-05-21 18:37:59 +02:00
|
|
|
Where("prefix = ? AND advertised = ? AND enabled = ? AND is_primary = ?", types.IPPrefix(prefix), true, true, true).
|
2022-11-24 23:41:11 +01:00
|
|
|
First(&route).Error
|
2022-11-25 16:29:45 +01:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 23:41:11 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-25 16:29:45 +01:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 23:41:11 +01:00
|
|
|
return nil, gorm.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return &route, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
// getNodePrimaryRoutes returns the routes that are enabled and marked as primary (for subnet failover)
|
2022-11-25 16:29:45 +01:00
|
|
|
// Exit nodes are not considered for this, as they are never marked as Primary.
|
2023-09-24 13:42:05 +02:00
|
|
|
func (hsdb *HSDatabase) GetNodePrimaryRoutes(node *types.Node) (types.Routes, error) {
|
2023-07-17 13:35:05 +02:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
var routes types.Routes
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.
|
2023-09-24 13:42:05 +02:00
|
|
|
Preload("Node").
|
|
|
|
Where("node_id = ? AND advertised = ? AND enabled = ? AND is_primary = ?", node.ID, true, true, true).
|
2022-11-23 19:50:30 +01:00
|
|
|
Find(&routes).Error
|
2021-08-21 15:49:46 +02:00
|
|
|
if err != nil {
|
2022-11-23 19:50:30 +01:00
|
|
|
return nil, err
|
2021-08-21 15:49:46 +02:00
|
|
|
}
|
|
|
|
|
2022-11-23 19:50:30 +01:00
|
|
|
return routes, nil
|
|
|
|
}
|
2021-08-21 15:49:46 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
// SaveNodeRoutes takes a node and updates the database with
|
2023-08-09 22:20:05 +02:00
|
|
|
// the new routes.
|
2024-01-16 16:04:03 +01:00
|
|
|
// It returns a bool whether an update should be sent as the
|
2023-12-09 18:09:24 +01:00
|
|
|
// saved route impacts nodes.
|
|
|
|
func (hsdb *HSDatabase) SaveNodeRoutes(node *types.Node) (bool, error) {
|
2023-07-17 13:35:05 +02:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
return hsdb.saveNodeRoutes(node)
|
2023-07-17 13:35:05 +02:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
func (hsdb *HSDatabase) saveNodeRoutes(node *types.Node) (bool, error) {
|
|
|
|
sendUpdate := false
|
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
currentRoutes := types.Routes{}
|
2023-09-24 13:42:05 +02:00
|
|
|
err := hsdb.db.Where("node_id = ?", node.ID).Find(¤tRoutes).Error
|
2021-08-21 15:49:46 +02:00
|
|
|
if err != nil {
|
2023-12-09 18:09:24 +01:00
|
|
|
return sendUpdate, err
|
2021-08-21 15:49:46 +02:00
|
|
|
}
|
2021-04-28 16:55:29 +02:00
|
|
|
|
2022-11-23 19:50:30 +01:00
|
|
|
advertisedRoutes := map[netip.Prefix]bool{}
|
2023-11-21 18:20:06 +01:00
|
|
|
for _, prefix := range node.Hostinfo.RoutableIPs {
|
2022-11-23 19:50:30 +01:00
|
|
|
advertisedRoutes[prefix] = false
|
2021-08-21 15:49:46 +02:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:45:46 +02:00
|
|
|
log.Trace().
|
2023-09-24 13:42:05 +02:00
|
|
|
Str("node", node.Hostname).
|
2023-09-11 18:45:46 +02:00
|
|
|
Interface("advertisedRoutes", advertisedRoutes).
|
|
|
|
Interface("currentRoutes", currentRoutes).
|
|
|
|
Msg("updating routes")
|
|
|
|
|
2022-11-25 16:29:45 +01:00
|
|
|
for pos, route := range currentRoutes {
|
2022-11-23 19:50:30 +01:00
|
|
|
if _, ok := advertisedRoutes[netip.Prefix(route.Prefix)]; ok {
|
|
|
|
if !route.Advertised {
|
2022-11-25 16:29:45 +01:00
|
|
|
currentRoutes[pos].Advertised = true
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.Save(¤tRoutes[pos]).Error
|
2022-11-23 19:50:30 +01:00
|
|
|
if err != nil {
|
2023-12-09 18:09:24 +01:00
|
|
|
return sendUpdate, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a route that is newly "saved" is already
|
|
|
|
// enabled, set sendUpdate to true as it is now
|
|
|
|
// available.
|
|
|
|
if route.Enabled {
|
|
|
|
sendUpdate = true
|
2022-11-23 19:50:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
advertisedRoutes[netip.Prefix(route.Prefix)] = true
|
2022-11-25 16:29:45 +01:00
|
|
|
} else if route.Advertised {
|
|
|
|
currentRoutes[pos].Advertised = false
|
|
|
|
currentRoutes[pos].Enabled = false
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.Save(¤tRoutes[pos]).Error
|
2022-11-25 16:29:45 +01:00
|
|
|
if err != nil {
|
2023-12-09 18:09:24 +01:00
|
|
|
return sendUpdate, err
|
2021-04-28 16:55:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 15:49:46 +02:00
|
|
|
|
2022-11-23 19:50:30 +01:00
|
|
|
for prefix, exists := range advertisedRoutes {
|
|
|
|
if !exists {
|
2023-05-21 18:37:59 +02:00
|
|
|
route := types.Route{
|
2023-09-24 13:42:05 +02:00
|
|
|
NodeID: node.ID,
|
2023-05-21 18:37:59 +02:00
|
|
|
Prefix: types.IPPrefix(prefix),
|
2022-11-23 19:50:30 +01:00
|
|
|
Advertised: true,
|
|
|
|
Enabled: false,
|
|
|
|
}
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.Create(&route).Error
|
2022-11-23 19:50:30 +01:00
|
|
|
if err != nil {
|
2023-12-09 18:09:24 +01:00
|
|
|
return sendUpdate, err
|
2022-11-23 19:50:30 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-30 15:31:06 +02:00
|
|
|
}
|
2021-08-21 15:49:46 +02:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
return sendUpdate, nil
|
2021-08-21 15:49:46 +02:00
|
|
|
}
|
2022-11-24 23:41:11 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
// EnsureFailoverRouteIsAvailable takes a node and checks if the node's route
|
|
|
|
// currently have a functioning host that exposes the network.
|
|
|
|
func (hsdb *HSDatabase) EnsureFailoverRouteIsAvailable(node *types.Node) error {
|
|
|
|
nodeRoutes, err := hsdb.getNodeRoutes(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2022-11-24 23:41:11 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
for _, nodeRoute := range nodeRoutes {
|
|
|
|
routes, err := hsdb.getRoutesByPrefix(netip.Prefix(nodeRoute.Prefix))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-24 23:41:11 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
for _, route := range routes {
|
|
|
|
if route.IsPrimary {
|
|
|
|
// if we have a primary route, and the node is connected
|
|
|
|
// nothing needs to be done.
|
|
|
|
if hsdb.notifier.IsConnected(route.Node.MachineKey) {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-24 23:41:11 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
// if not, we need to failover the route
|
|
|
|
err := hsdb.failoverRouteWithNotify(&route)
|
|
|
|
if err != nil {
|
2022-11-24 23:41:11 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-12-09 18:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 16:29:45 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-20 22:03:15 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
func (hsdb *HSDatabase) FailoverNodeRoutesWithNotify(node *types.Node) error {
|
|
|
|
routes, err := hsdb.getNodeRoutes(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var changedKeys []key.MachinePublic
|
|
|
|
|
|
|
|
for _, route := range routes {
|
|
|
|
changed, err := hsdb.failoverRoute(&route)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-24 23:41:11 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
changedKeys = append(changedKeys, changed...)
|
|
|
|
}
|
2022-11-24 23:41:11 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
changedKeys = lo.Uniq(changedKeys)
|
2022-11-24 23:41:11 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
var nodes types.Nodes
|
2022-11-25 16:29:45 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
for _, key := range changedKeys {
|
|
|
|
node, err := hsdb.GetNodeByMachineKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-24 23:41:11 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
2022-11-25 16:29:45 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
if nodes != nil {
|
|
|
|
stateUpdate := types.StateUpdate{
|
|
|
|
Type: types.StatePeerChanged,
|
|
|
|
ChangeNodes: nodes,
|
|
|
|
Message: "called from db.FailoverNodeRoutesWithNotify",
|
|
|
|
}
|
|
|
|
if stateUpdate.Valid() {
|
|
|
|
hsdb.notifier.NotifyAll(stateUpdate)
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 23:41:11 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-24 23:41:11 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
func (hsdb *HSDatabase) failoverRouteWithNotify(r *types.Route) error {
|
|
|
|
changedKeys, err := hsdb.failoverRoute(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-24 23:41:11 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
if len(changedKeys) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-24 23:41:11 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
var nodes types.Nodes
|
2022-11-24 23:41:11 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
log.Trace().
|
|
|
|
Str("hostname", r.Node.Hostname).
|
|
|
|
Msg("loading machines with new primary routes from db")
|
2022-12-20 10:08:32 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
for _, key := range changedKeys {
|
|
|
|
node, err := hsdb.getNodeByMachineKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-24 23:41:11 +01:00
|
|
|
}
|
2023-12-09 18:09:24 +01:00
|
|
|
|
|
|
|
nodes = append(nodes, node)
|
2022-11-24 23:41:11 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
log.Trace().
|
|
|
|
Str("hostname", r.Node.Hostname).
|
|
|
|
Msg("notifying peers about primary route change")
|
|
|
|
|
|
|
|
if nodes != nil {
|
|
|
|
stateUpdate := types.StateUpdate{
|
|
|
|
Type: types.StatePeerChanged,
|
|
|
|
ChangeNodes: nodes,
|
|
|
|
Message: "called from db.failoverRouteWithNotify",
|
|
|
|
}
|
|
|
|
if stateUpdate.Valid() {
|
|
|
|
hsdb.notifier.NotifyAll(stateUpdate)
|
|
|
|
}
|
2022-12-20 10:08:32 +01:00
|
|
|
}
|
2022-12-20 22:03:15 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
log.Trace().
|
|
|
|
Str("hostname", r.Node.Hostname).
|
|
|
|
Msg("notified peers about primary route change")
|
|
|
|
|
2022-11-24 23:41:11 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-25 18:50:12 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
// failoverRoute takes a route that is no longer available,
|
|
|
|
// this can be either from:
|
|
|
|
// - being disabled
|
|
|
|
// - being deleted
|
|
|
|
// - host going offline
|
|
|
|
//
|
|
|
|
// and tries to find a new route to take over its place.
|
|
|
|
// If the given route was not primary, it returns early.
|
|
|
|
func (hsdb *HSDatabase) failoverRoute(r *types.Route) ([]key.MachinePublic, error) {
|
|
|
|
if r == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This route is not a primary route, and it isnt
|
|
|
|
// being served to nodes.
|
|
|
|
if !r.IsPrimary {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not have to failover exit nodes
|
|
|
|
if r.IsExitRoute() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err := hsdb.getRoutesByPrefix(netip.Prefix(r.Prefix))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var newPrimary *types.Route
|
|
|
|
|
|
|
|
// Find a new suitable route
|
|
|
|
for idx, route := range routes {
|
|
|
|
if r.ID == route.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if hsdb.notifier.IsConnected(route.Node.MachineKey) {
|
|
|
|
newPrimary = &routes[idx]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a new route was not found/available,
|
|
|
|
// return with an error.
|
|
|
|
// We do not want to update the database as
|
|
|
|
// the one currently marked as primary is the
|
|
|
|
// best we got.
|
|
|
|
if newPrimary == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Str("hostname", newPrimary.Node.Hostname).
|
|
|
|
Msg("found new primary, updating db")
|
|
|
|
|
|
|
|
// Remove primary from the old route
|
|
|
|
r.IsPrimary = false
|
|
|
|
err = hsdb.db.Save(&r).Error
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error disabling new primary route")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Str("hostname", newPrimary.Node.Hostname).
|
|
|
|
Msg("removed primary from old route")
|
|
|
|
|
|
|
|
// Set primary for the new primary
|
|
|
|
newPrimary.IsPrimary = true
|
|
|
|
err = hsdb.db.Save(&newPrimary).Error
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error enabling new primary route")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Str("hostname", newPrimary.Node.Hostname).
|
|
|
|
Msg("set primary to new route")
|
|
|
|
|
|
|
|
// Return a list of the machinekeys of the changed nodes.
|
|
|
|
return []key.MachinePublic{r.Node.MachineKey, newPrimary.Node.MachineKey}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
// EnableAutoApprovedRoutes enables any routes advertised by a node that match the ACL autoApprovers policy.
|
2023-05-21 18:37:59 +02:00
|
|
|
func (hsdb *HSDatabase) EnableAutoApprovedRoutes(
|
|
|
|
aclPolicy *policy.ACLPolicy,
|
2023-09-24 13:42:05 +02:00
|
|
|
node *types.Node,
|
2023-05-21 18:37:59 +02:00
|
|
|
) error {
|
2024-01-18 16:36:47 +01:00
|
|
|
if len(aclPolicy.AutoApprovers.ExitNode) == 0 && len(aclPolicy.AutoApprovers.Routes) == 0 {
|
|
|
|
// No autoapprovers configured
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-17 13:35:05 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
if len(node.IPAddresses) == 0 {
|
2024-01-18 16:36:47 +01:00
|
|
|
// This node has no IPAddresses, so can't possibly match any autoApprovers ACLs
|
|
|
|
return nil
|
2023-05-21 18:37:59 +02:00
|
|
|
}
|
|
|
|
|
2024-01-18 16:36:47 +01:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
routes, err := hsdb.getNodeAdvertisedRoutes(node)
|
2023-05-21 18:37:59 +02:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
2023-09-24 13:42:05 +02:00
|
|
|
Str("node", node.Hostname).
|
|
|
|
Msg("Could not get advertised routes for node")
|
2023-05-21 18:37:59 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-18 16:36:47 +01:00
|
|
|
log.Trace().Interface("routes", routes).Msg("routes for autoapproving")
|
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
approvedRoutes := types.Routes{}
|
|
|
|
|
|
|
|
for _, advertisedRoute := range routes {
|
|
|
|
if advertisedRoute.Enabled {
|
|
|
|
continue
|
2022-11-25 18:50:12 +01:00
|
|
|
}
|
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
routeApprovers, err := aclPolicy.AutoApprovers.GetRouteApprovers(
|
|
|
|
netip.Prefix(advertisedRoute.Prefix),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).
|
|
|
|
Str("advertisedRoute", advertisedRoute.String()).
|
2023-09-24 13:42:05 +02:00
|
|
|
Uint64("nodeId", node.ID).
|
2023-05-21 18:37:59 +02:00
|
|
|
Msg("Failed to resolve autoApprovers for advertised route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-18 16:36:47 +01:00
|
|
|
log.Trace().
|
|
|
|
Str("node", node.Hostname).
|
|
|
|
Str("user", node.User.Name).
|
|
|
|
Strs("routeApprovers", routeApprovers).
|
|
|
|
Str("prefix", netip.Prefix(advertisedRoute.Prefix).String()).
|
|
|
|
Msg("looking up route for autoapproving")
|
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
for _, approvedAlias := range routeApprovers {
|
2023-09-24 13:42:05 +02:00
|
|
|
if approvedAlias == node.User.Name {
|
2023-05-21 18:37:59 +02:00
|
|
|
approvedRoutes = append(approvedRoutes, advertisedRoute)
|
|
|
|
} else {
|
|
|
|
// TODO(kradalby): figure out how to get this to depend on less stuff
|
2023-09-24 13:42:05 +02:00
|
|
|
approvedIps, err := aclPolicy.ExpandAlias(types.Nodes{node}, approvedAlias)
|
2023-05-21 18:37:59 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).
|
|
|
|
Str("alias", approvedAlias).
|
|
|
|
Msg("Failed to expand alias when processing autoApprovers policy")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
// approvedIPs should contain all of node's IPs if it matches the rule, so check for first
|
|
|
|
if approvedIps.Contains(node.IPAddresses[0]) {
|
2023-05-21 18:37:59 +02:00
|
|
|
approvedRoutes = append(approvedRoutes, advertisedRoute)
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 18:50:12 +01:00
|
|
|
}
|
2023-05-21 18:37:59 +02:00
|
|
|
}
|
2022-11-25 18:50:12 +01:00
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
for _, approvedRoute := range approvedRoutes {
|
2023-07-17 13:35:05 +02:00
|
|
|
err := hsdb.enableRoute(uint64(approvedRoute.ID))
|
2023-05-21 18:37:59 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).
|
|
|
|
Str("approvedRoute", approvedRoute.String()).
|
2023-09-24 13:42:05 +02:00
|
|
|
Uint64("nodeId", node.ID).
|
2023-05-21 18:37:59 +02:00
|
|
|
Msg("Failed to enable approved route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-11-25 18:50:12 +01:00
|
|
|
}
|
|
|
|
|
2023-05-21 18:37:59 +02:00
|
|
|
return nil
|
2022-11-25 18:50:12 +01:00
|
|
|
}
|