2023-05-21 12:37:59 -04:00
|
|
|
package db
|
2021-04-28 10:55:29 -04:00
|
|
|
|
|
|
|
import (
|
2022-11-25 10:29:45 -05:00
|
|
|
"errors"
|
2022-09-01 18:06:19 -04:00
|
|
|
"net/netip"
|
2022-11-23 13:50:30 -05:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2022-11-24 17:41:11 -05:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-11-23 13:50:30 -05:00
|
|
|
"gorm.io/gorm"
|
2021-04-28 10:55:29 -04:00
|
|
|
)
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
var ErrRouteIsNotAvailable = errors.New("route is not available")
|
2021-08-21 09:49:46 -04:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) GetRoutes() (types.Routes, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
|
|
|
return hsdb.getRoutes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) getRoutes() (types.Routes, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
var routes types.Routes
|
2023-09-24 07:42:05 -04:00
|
|
|
err := hsdb.db.Preload("Node").Find(&routes).Error
|
2023-05-21 12:37:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-21 09:49:46 -04:00
|
|
|
}
|
2022-11-25 10:29:45 -05:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
return routes, nil
|
2021-08-21 09:49:46 -04:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (hsdb *HSDatabase) GetNodeAdvertisedRoutes(node *types.Node) (types.Routes, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
return hsdb.getNodeAdvertisedRoutes(node)
|
2023-07-17 07:35:05 -04:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (hsdb *HSDatabase) getNodeAdvertisedRoutes(node *types.Node) (types.Routes, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
var routes types.Routes
|
|
|
|
err := hsdb.db.
|
2023-09-24 07:42:05 -04:00
|
|
|
Preload("Node").
|
|
|
|
Where("node_id = ? AND advertised = true", node.ID).
|
2023-05-21 12:37:59 -04:00
|
|
|
Find(&routes).Error
|
2022-11-25 12:50:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (hsdb *HSDatabase) GetNodeRoutes(node *types.Node) (types.Routes, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
return hsdb.getNodeRoutes(node)
|
2023-07-17 07:35:05 -04:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (hsdb *HSDatabase) getNodeRoutes(node *types.Node) (types.Routes, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
var routes types.Routes
|
2023-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.
|
2023-09-24 07:42:05 -04:00
|
|
|
Preload("Node").
|
|
|
|
Where("node_id = ?", node.ID).
|
2022-11-25 12:50:12 -05:00
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) GetRoute(id uint64) (*types.Route, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
|
|
|
return hsdb.getRoute(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) getRoute(id uint64) (*types.Route, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
var route types.Route
|
2023-09-24 07:42:05 -04:00
|
|
|
err := hsdb.db.Preload("Node").First(&route, id).Error
|
2022-11-25 12:50:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &route, nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
func (hsdb *HSDatabase) EnableRoute(id uint64) error {
|
2023-07-17 07:35:05 -04: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 12:50:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-22 17:39:42 -05: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 12:37:59 -04:00
|
|
|
if route.IsExitRoute() {
|
|
|
|
return hsdb.enableRoutes(
|
2023-09-24 07:42:05 -04:00
|
|
|
&route.Node,
|
2023-05-21 12:37:59 -04:00
|
|
|
types.ExitRouteV4.String(),
|
|
|
|
types.ExitRouteV6.String(),
|
|
|
|
)
|
2023-01-22 17:39:42 -05:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
return hsdb.enableRoutes(&route.Node, netip.Prefix(route.Prefix).String())
|
2022-11-25 12:50:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
func (hsdb *HSDatabase) DisableRoute(id uint64) error {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
|
|
|
route, err := hsdb.getRoute(id)
|
2022-11-25 12:50:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-07 06:17:16 -04: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 12:37:59 -04:00
|
|
|
if !route.IsExitRoute() {
|
2023-05-07 06:17:16 -04:00
|
|
|
route.Enabled = false
|
|
|
|
route.IsPrimary = false
|
2023-05-11 03:09:18 -04:00
|
|
|
err = hsdb.db.Save(route).Error
|
2023-05-07 06:17:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-17 07:35:05 -04:00
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
2023-05-07 06:17:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
routes, err := hsdb.getNodeRoutes(&route.Node)
|
2022-11-25 12:50:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-07 06:17:16 -04:00
|
|
|
for i := range routes {
|
2023-05-21 12:37:59 -04:00
|
|
|
if routes[i].IsExitRoute() {
|
2023-05-07 06:17:16 -04:00
|
|
|
routes[i].Enabled = false
|
|
|
|
routes[i].IsPrimary = false
|
2023-05-11 03:09:18 -04:00
|
|
|
err = hsdb.db.Save(&routes[i]).Error
|
2023-05-07 06:17:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 07:35:05 -04:00
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
2022-11-25 12:50:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
func (hsdb *HSDatabase) DeleteRoute(id uint64) error {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
|
|
|
route, err := hsdb.getRoute(id)
|
2023-03-06 03:05:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-07 06:17:16 -04: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 12:37:59 -04:00
|
|
|
if !route.IsExitRoute() {
|
2023-05-11 03:09:18 -04:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&route).Error; err != nil {
|
2023-05-07 06:17:16 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-17 07:35:05 -04:00
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
2023-05-07 06:17:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
routes, err := hsdb.getNodeRoutes(&route.Node)
|
2023-05-07 06:17:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
routesToDelete := types.Routes{}
|
2023-05-07 06:17:16 -04:00
|
|
|
for _, r := range routes {
|
2023-05-21 12:37:59 -04:00
|
|
|
if r.IsExitRoute() {
|
2023-05-07 06:17:16 -04:00
|
|
|
routesToDelete = append(routesToDelete, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&routesToDelete).Error; err != nil {
|
2023-03-06 03:05:40 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-17 07:35:05 -04:00
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
2023-03-06 03:05:40 -05:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (hsdb *HSDatabase) deleteNodeRoutes(node *types.Node) error {
|
|
|
|
routes, err := hsdb.getNodeRoutes(node)
|
2023-03-06 03:05:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range routes {
|
2023-05-11 03:09:18 -04:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&routes[i]).Error; err != nil {
|
2023-03-06 03:05:40 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 07:35:05 -04:00
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
2023-03-06 03:05:40 -05:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
// isUniquePrefix returns if there is another node providing the same route already.
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) isUniquePrefix(route types.Route) bool {
|
2022-11-24 11:00:40 -05:00
|
|
|
var count int64
|
2023-05-11 03:09:18 -04:00
|
|
|
hsdb.db.
|
2023-05-21 12:37:59 -04:00
|
|
|
Model(&types.Route{}).
|
2023-09-24 07:42:05 -04:00
|
|
|
Where("prefix = ? AND node_id != ? AND advertised = ? AND enabled = ?",
|
2022-11-24 11:00:40 -05:00
|
|
|
route.Prefix,
|
2023-09-24 07:42:05 -04:00
|
|
|
route.NodeID,
|
2022-11-24 11:00:40 -05:00
|
|
|
true, true).Count(&count)
|
2022-11-25 10:29:45 -05:00
|
|
|
|
2022-11-24 11:00:40 -05:00
|
|
|
return count == 0
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) getPrimaryRoute(prefix netip.Prefix) (*types.Route, error) {
|
|
|
|
var route types.Route
|
2023-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.
|
2023-09-24 07:42:05 -04:00
|
|
|
Preload("Node").
|
2023-05-21 12:37:59 -04:00
|
|
|
Where("prefix = ? AND advertised = ? AND enabled = ? AND is_primary = ?", types.IPPrefix(prefix), true, true, true).
|
2022-11-24 17:41:11 -05:00
|
|
|
First(&route).Error
|
2022-11-25 10:29:45 -05:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 17:41:11 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-25 10:29:45 -05:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 17:41:11 -05:00
|
|
|
return nil, gorm.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return &route, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
// getNodePrimaryRoutes returns the routes that are enabled and marked as primary (for subnet failover)
|
2022-11-25 10:29:45 -05:00
|
|
|
// Exit nodes are not considered for this, as they are never marked as Primary.
|
2023-09-24 07:42:05 -04:00
|
|
|
func (hsdb *HSDatabase) GetNodePrimaryRoutes(node *types.Node) (types.Routes, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
var routes types.Routes
|
2023-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.
|
2023-09-24 07:42:05 -04:00
|
|
|
Preload("Node").
|
|
|
|
Where("node_id = ? AND advertised = ? AND enabled = ? AND is_primary = ?", node.ID, true, true, true).
|
2022-11-23 13:50:30 -05:00
|
|
|
Find(&routes).Error
|
2021-08-21 09:49:46 -04:00
|
|
|
if err != nil {
|
2022-11-23 13:50:30 -05:00
|
|
|
return nil, err
|
2021-08-21 09:49:46 -04:00
|
|
|
}
|
|
|
|
|
2022-11-23 13:50:30 -05:00
|
|
|
return routes, nil
|
|
|
|
}
|
2021-08-21 09:49:46 -04:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
// SaveNodeRoutes takes a node and updates the database with
|
2023-08-09 16:20:05 -04:00
|
|
|
// the new routes.
|
2023-09-24 07:42:05 -04:00
|
|
|
func (hsdb *HSDatabase) SaveNodeRoutes(node *types.Node) error {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
return hsdb.saveNodeRoutes(node)
|
2023-07-17 07:35:05 -04:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
func (hsdb *HSDatabase) saveNodeRoutes(node *types.Node) error {
|
2023-05-21 12:37:59 -04:00
|
|
|
currentRoutes := types.Routes{}
|
2023-09-24 07:42:05 -04:00
|
|
|
err := hsdb.db.Where("node_id = ?", node.ID).Find(¤tRoutes).Error
|
2021-08-21 09:49:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-28 10:55:29 -04:00
|
|
|
|
2022-11-23 13:50:30 -05:00
|
|
|
advertisedRoutes := map[netip.Prefix]bool{}
|
2023-09-24 07:42:05 -04:00
|
|
|
for _, prefix := range node.HostInfo.RoutableIPs {
|
2022-11-23 13:50:30 -05:00
|
|
|
advertisedRoutes[prefix] = false
|
2021-08-21 09:49:46 -04:00
|
|
|
}
|
|
|
|
|
2023-09-11 12:45:46 -04:00
|
|
|
log.Trace().
|
2023-09-24 07:42:05 -04:00
|
|
|
Str("node", node.Hostname).
|
2023-09-11 12:45:46 -04:00
|
|
|
Interface("advertisedRoutes", advertisedRoutes).
|
|
|
|
Interface("currentRoutes", currentRoutes).
|
|
|
|
Msg("updating routes")
|
|
|
|
|
2022-11-25 10:29:45 -05:00
|
|
|
for pos, route := range currentRoutes {
|
2022-11-23 13:50:30 -05:00
|
|
|
if _, ok := advertisedRoutes[netip.Prefix(route.Prefix)]; ok {
|
|
|
|
if !route.Advertised {
|
2022-11-25 10:29:45 -05:00
|
|
|
currentRoutes[pos].Advertised = true
|
2023-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.Save(¤tRoutes[pos]).Error
|
2022-11-23 13:50:30 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
advertisedRoutes[netip.Prefix(route.Prefix)] = true
|
2022-11-25 10:29:45 -05:00
|
|
|
} else if route.Advertised {
|
|
|
|
currentRoutes[pos].Advertised = false
|
|
|
|
currentRoutes[pos].Enabled = false
|
2023-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.Save(¤tRoutes[pos]).Error
|
2022-11-25 10:29:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 09:49:46 -04:00
|
|
|
|
2022-11-23 13:50:30 -05:00
|
|
|
for prefix, exists := range advertisedRoutes {
|
|
|
|
if !exists {
|
2023-05-21 12:37:59 -04:00
|
|
|
route := types.Route{
|
2023-09-24 07:42:05 -04:00
|
|
|
NodeID: node.ID,
|
2023-05-21 12:37:59 -04:00
|
|
|
Prefix: types.IPPrefix(prefix),
|
2022-11-23 13:50:30 -05:00
|
|
|
Advertised: true,
|
|
|
|
Enabled: false,
|
|
|
|
}
|
2023-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.Create(&route).Error
|
2022-11-23 13:50:30 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 09:31:06 -04:00
|
|
|
}
|
2021-08-21 09:49:46 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-24 17:41:11 -05:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) HandlePrimarySubnetFailover() error {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) handlePrimarySubnetFailover() error {
|
2022-11-24 17:41:11 -05:00
|
|
|
// first, get all the enabled routes
|
2023-05-21 12:37:59 -04:00
|
|
|
var routes types.Routes
|
2023-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.
|
2023-09-24 07:42:05 -04:00
|
|
|
Preload("Node").
|
2022-11-24 17:41:11 -05:00
|
|
|
Where("advertised = ? AND enabled = ?", true, true).
|
|
|
|
Find(&routes).Error
|
2022-11-25 10:29:45 -05:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 17:41:11 -05:00
|
|
|
log.Error().Err(err).Msg("error getting routes")
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
changedNodes := make(types.Nodes, 0)
|
2022-11-25 10:29:45 -05:00
|
|
|
for pos, route := range routes {
|
2023-05-21 12:37:59 -04:00
|
|
|
if route.IsExitRoute() {
|
2022-11-24 17:41:11 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
node := &route.Node
|
2023-08-09 16:56:21 -04:00
|
|
|
|
2022-11-24 17:41:11 -05:00
|
|
|
if !route.IsPrimary {
|
2023-05-11 03:09:18 -04:00
|
|
|
_, err := hsdb.getPrimaryRoute(netip.Prefix(route.Prefix))
|
|
|
|
if hsdb.isUniquePrefix(route) || errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-25 12:50:12 -05:00
|
|
|
log.Info().
|
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
2023-09-24 07:42:05 -04:00
|
|
|
Str("node", route.Node.GivenName).
|
2022-11-25 12:50:12 -05:00
|
|
|
Msg("Setting primary route")
|
2022-11-25 10:29:45 -05:00
|
|
|
routes[pos].IsPrimary = true
|
2023-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.Save(&routes[pos]).Error
|
2022-11-24 17:41:11 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error marking route as primary")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-11-25 10:29:45 -05:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
changedNodes = append(changedNodes, node)
|
2022-12-20 16:03:15 -05:00
|
|
|
|
2022-11-24 17:41:11 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if route.IsPrimary {
|
2023-09-24 07:42:05 -04:00
|
|
|
if route.Node.IsOnline() {
|
2022-11-24 17:41:11 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
// node offline, find a new primary
|
2022-11-24 17:41:11 -05:00
|
|
|
log.Info().
|
2023-09-24 07:42:05 -04:00
|
|
|
Str("node", route.Node.Hostname).
|
2022-11-24 17:41:11 -05:00
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
2023-09-24 07:42:05 -04:00
|
|
|
Msgf("node offline, finding a new primary subnet")
|
2022-11-24 17:41:11 -05:00
|
|
|
|
|
|
|
// find a new primary route
|
2023-05-21 12:37:59 -04:00
|
|
|
var newPrimaryRoutes types.Routes
|
2023-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.
|
2023-09-24 07:42:05 -04:00
|
|
|
Preload("Node").
|
|
|
|
Where("prefix = ? AND node_id != ? AND advertised = ? AND enabled = ?",
|
2022-11-24 17:41:11 -05:00
|
|
|
route.Prefix,
|
2023-09-24 07:42:05 -04:00
|
|
|
route.NodeID,
|
2022-11-24 17:41:11 -05:00
|
|
|
true, true).
|
|
|
|
Find(&newPrimaryRoutes).Error
|
2022-11-25 10:29:45 -05:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 17:41:11 -05:00
|
|
|
log.Error().Err(err).Msg("error finding new primary route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
var newPrimaryRoute *types.Route
|
2022-11-25 10:29:45 -05:00
|
|
|
for pos, r := range newPrimaryRoutes {
|
2023-09-24 07:42:05 -04:00
|
|
|
if r.Node.IsOnline() {
|
2022-11-25 10:29:45 -05:00
|
|
|
newPrimaryRoute = &newPrimaryRoutes[pos]
|
|
|
|
|
2022-11-24 17:41:11 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if newPrimaryRoute == nil {
|
|
|
|
log.Warn().
|
2023-09-24 07:42:05 -04:00
|
|
|
Str("node", route.Node.Hostname).
|
2022-11-24 17:41:11 -05:00
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
|
|
|
Msgf("no alternative primary route found")
|
2022-11-25 10:29:45 -05:00
|
|
|
|
2022-11-24 17:41:11 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().
|
2023-09-24 07:42:05 -04:00
|
|
|
Str("old_node", route.Node.Hostname).
|
2022-11-24 17:41:11 -05:00
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
2023-09-24 07:42:05 -04:00
|
|
|
Str("new_node", newPrimaryRoute.Node.Hostname).
|
2022-11-24 17:41:11 -05:00
|
|
|
Msgf("found new primary route")
|
|
|
|
|
|
|
|
// disable the old primary route
|
2022-11-25 10:29:45 -05:00
|
|
|
routes[pos].IsPrimary = false
|
2023-05-11 03:09:18 -04:00
|
|
|
err = hsdb.db.Save(&routes[pos]).Error
|
2022-11-24 17:41:11 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error disabling old primary route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable the new primary route
|
|
|
|
newPrimaryRoute.IsPrimary = true
|
2023-05-11 03:09:18 -04:00
|
|
|
err = hsdb.db.Save(&newPrimaryRoute).Error
|
2022-11-24 17:41:11 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error enabling new primary route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-12-20 04:08:32 -05:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
changedNodes = append(changedNodes, node)
|
2022-11-24 17:41:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
if len(changedNodes) > 0 {
|
2023-06-29 06:20:22 -04:00
|
|
|
hsdb.notifier.NotifyAll(types.StateUpdate{
|
|
|
|
Type: types.StatePeerChanged,
|
2023-09-24 07:42:05 -04:00
|
|
|
Changed: changedNodes,
|
2023-06-29 06:20:22 -04:00
|
|
|
})
|
2022-12-20 04:08:32 -05:00
|
|
|
}
|
2022-12-20 16:03:15 -05:00
|
|
|
|
2022-11-24 17:41:11 -05:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-25 12:50:12 -05:00
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
// EnableAutoApprovedRoutes enables any routes advertised by a node that match the ACL autoApprovers policy.
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) EnableAutoApprovedRoutes(
|
|
|
|
aclPolicy *policy.ACLPolicy,
|
2023-09-24 07:42:05 -04:00
|
|
|
node *types.Node,
|
2023-05-21 12:37:59 -04:00
|
|
|
) error {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
if len(node.IPAddresses) == 0 {
|
|
|
|
return nil // This node has no IPAddresses, so can't possibly match any autoApprovers ACLs
|
2023-05-21 12:37:59 -04:00
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04:00
|
|
|
routes, err := hsdb.getNodeAdvertisedRoutes(node)
|
2023-05-21 12:37:59 -04:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
2023-09-24 07:42:05 -04:00
|
|
|
Str("node", node.Hostname).
|
|
|
|
Msg("Could not get advertised routes for node")
|
2023-05-21 12:37:59 -04:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
approvedRoutes := types.Routes{}
|
|
|
|
|
|
|
|
for _, advertisedRoute := range routes {
|
|
|
|
if advertisedRoute.Enabled {
|
|
|
|
continue
|
2022-11-25 12:50:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
routeApprovers, err := aclPolicy.AutoApprovers.GetRouteApprovers(
|
|
|
|
netip.Prefix(advertisedRoute.Prefix),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).
|
|
|
|
Str("advertisedRoute", advertisedRoute.String()).
|
2023-09-24 07:42:05 -04:00
|
|
|
Uint64("nodeId", node.ID).
|
2023-05-21 12:37:59 -04:00
|
|
|
Msg("Failed to resolve autoApprovers for advertised route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, approvedAlias := range routeApprovers {
|
2023-09-24 07:42:05 -04:00
|
|
|
if approvedAlias == node.User.Name {
|
2023-05-21 12:37:59 -04:00
|
|
|
approvedRoutes = append(approvedRoutes, advertisedRoute)
|
|
|
|
} else {
|
|
|
|
// TODO(kradalby): figure out how to get this to depend on less stuff
|
2023-09-24 07:42:05 -04:00
|
|
|
approvedIps, err := aclPolicy.ExpandAlias(types.Nodes{node}, approvedAlias)
|
2023-05-21 12:37:59 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).
|
|
|
|
Str("alias", approvedAlias).
|
|
|
|
Msg("Failed to expand alias when processing autoApprovers policy")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-24 07:42:05 -04: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 12:37:59 -04:00
|
|
|
approvedRoutes = append(approvedRoutes, advertisedRoute)
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 12:50:12 -05:00
|
|
|
}
|
2023-05-21 12:37:59 -04:00
|
|
|
}
|
2022-11-25 12:50:12 -05:00
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
for _, approvedRoute := range approvedRoutes {
|
2023-07-17 07:35:05 -04:00
|
|
|
err := hsdb.enableRoute(uint64(approvedRoute.ID))
|
2023-05-21 12:37:59 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).
|
|
|
|
Str("approvedRoute", approvedRoute.String()).
|
2023-09-24 07:42:05 -04:00
|
|
|
Uint64("nodeId", node.ID).
|
2023-05-21 12:37:59 -04:00
|
|
|
Msg("Failed to enable approved route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-11-25 12:50:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
return nil
|
2022-11-25 12:50:12 -05:00
|
|
|
}
|