2023-05-21 16:37:59 +00:00
|
|
|
package db
|
2021-04-28 14:55:29 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-25 15:29:45 +00:00
|
|
|
"errors"
|
2024-04-10 12:49:34 +00:00
|
|
|
"fmt"
|
2022-09-01 22:06:19 +00:00
|
|
|
"net/netip"
|
2024-04-10 13:35:09 +00:00
|
|
|
"sort"
|
2022-11-23 18:50:30 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2024-04-21 16:28:17 +00:00
|
|
|
"github.com/puzpuzpuz/xsync/v3"
|
2022-11-24 22:41:11 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-11-23 18:50:30 +00:00
|
|
|
"gorm.io/gorm"
|
2024-10-02 07:06:09 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2024-04-10 13:35:09 +00:00
|
|
|
"tailscale.com/util/set"
|
2021-04-28 14:55:29 +00:00
|
|
|
)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
var ErrRouteIsNotAvailable = errors.New("route is not available")
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func GetRoutes(tx *gorm.DB) (types.Routes, error) {
|
2023-05-21 16:37:59 +00:00
|
|
|
var routes types.Routes
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.
|
2023-12-09 17:09:24 +00:00
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func getAdvertisedAndEnabledRoutes(tx *gorm.DB) (types.Routes, error) {
|
2023-12-09 17:09:24 +00:00
|
|
|
var routes types.Routes
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.
|
2023-12-09 17:09:24 +00:00
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
Where("advertised = ? AND enabled = ?", true, true).
|
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func getRoutesByPrefix(tx *gorm.DB, pref netip.Prefix) (types.Routes, error) {
|
2023-12-09 17:09:24 +00:00
|
|
|
var routes types.Routes
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.
|
2023-12-09 17:09:24 +00:00
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
2024-10-02 09:41:58 +00:00
|
|
|
Where("prefix = ?", pref.String()).
|
2023-12-09 17:09:24 +00:00
|
|
|
Find(&routes).Error
|
2023-05-21 16:37:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
2022-11-25 15:29:45 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
return routes, nil
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func GetNodeAdvertisedRoutes(tx *gorm.DB, node *types.Node) (types.Routes, error) {
|
2023-05-21 16:37:59 +00:00
|
|
|
var routes types.Routes
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.
|
2023-09-24 11:42:05 +00:00
|
|
|
Preload("Node").
|
2023-12-09 17:09:24 +00:00
|
|
|
Preload("Node.User").
|
2023-09-24 11:42:05 +00:00
|
|
|
Where("node_id = ? AND advertised = true", node.ID).
|
2023-05-21 16:37:59 +00:00
|
|
|
Find(&routes).Error
|
2022-11-25 17:50:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
func (hsdb *HSDatabase) GetNodeRoutes(node *types.Node) (types.Routes, error) {
|
2024-02-08 16:28:19 +00:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (types.Routes, error) {
|
|
|
|
return GetNodeRoutes(rx, node)
|
|
|
|
})
|
2023-07-17 11:35:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func GetNodeRoutes(tx *gorm.DB, node *types.Node) (types.Routes, error) {
|
2023-05-21 16:37:59 +00:00
|
|
|
var routes types.Routes
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.
|
2023-09-24 11:42:05 +00:00
|
|
|
Preload("Node").
|
2023-12-09 17:09:24 +00:00
|
|
|
Preload("Node.User").
|
2023-09-24 11:42:05 +00:00
|
|
|
Where("node_id = ?", node.ID).
|
2022-11-25 17:50:12 +00:00
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func GetRoute(tx *gorm.DB, id uint64) (*types.Route, error) {
|
2023-05-21 16:37:59 +00:00
|
|
|
var route types.Route
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.
|
2023-12-09 17:09:24 +00:00
|
|
|
Preload("Node").
|
|
|
|
Preload("Node.User").
|
|
|
|
First(&route, id).Error
|
2022-11-25 17:50:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &route, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func EnableRoute(tx *gorm.DB, id uint64) (*types.StateUpdate, error) {
|
|
|
|
route, err := GetRoute(tx, id)
|
2022-11-25 17:50:12 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, err
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 22:39:42 +00: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 16:37:59 +00:00
|
|
|
if route.IsExitRoute() {
|
2024-02-08 16:28:19 +00:00
|
|
|
return enableRoutes(
|
|
|
|
tx,
|
2023-09-24 11:42:05 +00:00
|
|
|
&route.Node,
|
2024-10-02 07:06:09 +00:00
|
|
|
tsaddr.AllIPv4(),
|
|
|
|
tsaddr.AllIPv6(),
|
2023-05-21 16:37:59 +00:00
|
|
|
)
|
2023-01-22 22:39:42 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
return enableRoutes(tx, &route.Node, netip.Prefix(route.Prefix))
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func DisableRoute(tx *gorm.DB,
|
|
|
|
id uint64,
|
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-02-08 16:28:19 +00:00
|
|
|
route, err := GetRoute(tx, id)
|
2022-11-25 17:50:12 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, err
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
var routes types.Routes
|
|
|
|
node := route.Node
|
|
|
|
|
2023-05-07 10:17:16 +00: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
|
2024-02-23 09:59:24 +00:00
|
|
|
var update []types.NodeID
|
2023-05-21 16:37:59 +00:00
|
|
|
if !route.IsExitRoute() {
|
2024-02-23 09:59:24 +00:00
|
|
|
route.Enabled = false
|
|
|
|
err = tx.Save(route).Error
|
2023-12-09 17:09:24 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, err
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 16:28:17 +00:00
|
|
|
update, err = failoverRouteTx(tx, isLikelyConnected, route)
|
2023-05-07 10:17:16 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, err
|
2023-05-07 10:17:16 +00:00
|
|
|
}
|
2023-12-09 17:09:24 +00:00
|
|
|
} else {
|
2024-02-08 16:28:19 +00:00
|
|
|
routes, 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, err
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
2023-05-07 10:17:16 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
for i := range routes {
|
|
|
|
if routes[i].IsExitRoute() {
|
|
|
|
routes[i].Enabled = false
|
|
|
|
routes[i].IsPrimary = false
|
2024-02-23 09:59:24 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
err = tx.Save(&routes[i]).Error
|
2023-12-09 17:09:24 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, err
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// If update is empty, it means that one was not created
|
|
|
|
// by failover (as a failover was not necessary), create
|
|
|
|
// one and return to the caller.
|
|
|
|
if update == nil {
|
2024-02-23 09:59:24 +00:00
|
|
|
update = []types.NodeID{node.ID}
|
2023-05-07 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
return update, nil
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (hsdb *HSDatabase) DeleteRoute(
|
|
|
|
id uint64,
|
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) {
|
|
|
|
return Write(hsdb.DB, func(tx *gorm.DB) ([]types.NodeID, error) {
|
2024-04-21 16:28:17 +00:00
|
|
|
return DeleteRoute(tx, id, isLikelyConnected)
|
2024-02-08 16:28:19 +00:00
|
|
|
})
|
|
|
|
}
|
2023-07-17 11:35:05 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func DeleteRoute(
|
|
|
|
tx *gorm.DB,
|
|
|
|
id uint64,
|
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-02-08 16:28:19 +00:00
|
|
|
route, err := GetRoute(tx, id)
|
2023-03-06 08:05:40 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, err
|
2023-03-06 08:05:40 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
var routes types.Routes
|
|
|
|
node := route.Node
|
|
|
|
|
2023-05-07 10:17:16 +00: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
|
2024-02-23 09:59:24 +00:00
|
|
|
var update []types.NodeID
|
2023-05-21 16:37:59 +00:00
|
|
|
if !route.IsExitRoute() {
|
2024-04-21 16:28:17 +00:00
|
|
|
update, err = failoverRouteTx(tx, isLikelyConnected, route)
|
2023-12-09 17:09:24 +00:00
|
|
|
if err != nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, nil
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
if err := tx.Unscoped().Delete(&route).Error; err != nil {
|
|
|
|
return nil, err
|
2023-05-07 10:17:16 +00:00
|
|
|
}
|
2023-12-09 17:09:24 +00:00
|
|
|
} else {
|
2024-02-23 09:59:24 +00:00
|
|
|
routes, 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, err
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
2023-05-07 10:17:16 +00:00
|
|
|
|
2024-06-23 20:06:50 +00:00
|
|
|
var routesToDelete types.Routes
|
2023-12-09 17:09:24 +00:00
|
|
|
for _, r := range routes {
|
|
|
|
if r.IsExitRoute() {
|
|
|
|
routesToDelete = append(routesToDelete, r)
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 10:17:16 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
if err := tx.Unscoped().Delete(&routesToDelete).Error; err != nil {
|
|
|
|
return nil, err
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
2023-05-07 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// If update is empty, it means that one was not created
|
|
|
|
// by failover (as a failover was not necessary), create
|
|
|
|
// one and return to the caller.
|
2023-12-09 17:09:24 +00:00
|
|
|
if routes == nil {
|
2024-02-08 16:28:19 +00:00
|
|
|
routes, 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, err
|
2023-05-07 10:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
node.Routes = routes
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
if update == nil {
|
2024-02-23 09:59:24 +00:00
|
|
|
update = []types.NodeID{node.ID}
|
2023-03-06 08:05:40 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
return update, nil
|
2023-03-06 08:05:40 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 16:28:17 +00:00
|
|
|
func deleteNodeRoutes(tx *gorm.DB, node *types.Node, isLikelyConnected *xsync.MapOf[types.NodeID, bool]) ([]types.NodeID, error) {
|
2024-02-08 16:28:19 +00:00
|
|
|
routes, err := GetNodeRoutes(tx, node)
|
2023-03-06 08:05:40 +00:00
|
|
|
if err != nil {
|
2024-04-10 12:49:34 +00:00
|
|
|
return nil, fmt.Errorf("getting node routes: %w", err)
|
2023-03-06 08:05:40 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
var changed []types.NodeID
|
2023-03-06 08:05:40 +00:00
|
|
|
for i := range routes {
|
2024-02-08 16:28:19 +00:00
|
|
|
if err := tx.Unscoped().Delete(&routes[i]).Error; err != nil {
|
2024-04-10 12:49:34 +00:00
|
|
|
return nil, fmt.Errorf("deleting route(%d): %w", &routes[i].ID, err)
|
2023-03-06 08:05:40 +00:00
|
|
|
}
|
2023-12-09 17:09:24 +00:00
|
|
|
|
|
|
|
// TODO(kradalby): This is a bit too aggressive, we could probably
|
|
|
|
// figure out which routes needs to be failed over rather than all.
|
2024-04-21 16:28:17 +00:00
|
|
|
chn, err := failoverRouteTx(tx, isLikelyConnected, &routes[i])
|
2024-02-23 09:59:24 +00:00
|
|
|
if err != nil {
|
2024-04-10 12:49:34 +00:00
|
|
|
return changed, fmt.Errorf("failing over route after delete: %w", err)
|
2024-02-23 09:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if chn != nil {
|
|
|
|
changed = append(changed, chn...)
|
|
|
|
}
|
2023-03-06 08:05:40 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
return changed, nil
|
2023-03-06 08:05:40 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
// isUniquePrefix returns if there is another node providing the same route already.
|
2024-02-08 16:28:19 +00:00
|
|
|
func isUniquePrefix(tx *gorm.DB, route types.Route) bool {
|
2022-11-24 16:00:40 +00:00
|
|
|
var count int64
|
2024-02-08 16:28:19 +00:00
|
|
|
tx.Model(&types.Route{}).
|
2023-09-24 11:42:05 +00:00
|
|
|
Where("prefix = ? AND node_id != ? AND advertised = ? AND enabled = ?",
|
2024-10-02 09:41:58 +00:00
|
|
|
route.Prefix.String(),
|
2023-09-24 11:42:05 +00:00
|
|
|
route.NodeID,
|
2022-11-24 16:00:40 +00:00
|
|
|
true, true).Count(&count)
|
2022-11-25 15:29:45 +00:00
|
|
|
|
2022-11-24 16:00:40 +00:00
|
|
|
return count == 0
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func getPrimaryRoute(tx *gorm.DB, prefix netip.Prefix) (*types.Route, error) {
|
2023-05-21 16:37:59 +00:00
|
|
|
var route types.Route
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.
|
2023-09-24 11:42:05 +00:00
|
|
|
Preload("Node").
|
2024-10-02 09:41:58 +00:00
|
|
|
Where("prefix = ? AND advertised = ? AND enabled = ? AND is_primary = ?", prefix.String(), true, true, true).
|
2022-11-24 22:41:11 +00:00
|
|
|
First(&route).Error
|
2022-11-25 15:29:45 +00:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 22:41:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-25 15:29:45 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 22:41:11 +00:00
|
|
|
return nil, gorm.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return &route, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
func (hsdb *HSDatabase) GetNodePrimaryRoutes(node *types.Node) (types.Routes, error) {
|
2024-02-08 16:28:19 +00:00
|
|
|
return Read(hsdb.DB, func(rx *gorm.DB) (types.Routes, error) {
|
|
|
|
return GetNodePrimaryRoutes(rx, node)
|
|
|
|
})
|
|
|
|
}
|
2023-07-17 11:35:05 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// 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 GetNodePrimaryRoutes(tx *gorm.DB, node *types.Node) (types.Routes, error) {
|
2023-05-21 16:37:59 +00:00
|
|
|
var 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 = ? AND is_primary = ?", node.ID, true, true, true).
|
2022-11-23 18:50:30 +00:00
|
|
|
Find(&routes).Error
|
2021-08-21 13:49:46 +00:00
|
|
|
if err != nil {
|
2022-11-23 18:50:30 +00:00
|
|
|
return nil, err
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 18:50:30 +00:00
|
|
|
return routes, nil
|
|
|
|
}
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func (hsdb *HSDatabase) SaveNodeRoutes(node *types.Node) (bool, error) {
|
|
|
|
return Write(hsdb.DB, func(tx *gorm.DB) (bool, error) {
|
|
|
|
return SaveNodeRoutes(tx, node)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
// SaveNodeRoutes takes a node and updates the database with
|
2023-08-09 20:20:05 +00:00
|
|
|
// the new routes.
|
2024-01-16 15:04:03 +00:00
|
|
|
// It returns a bool whether an update should be sent as the
|
2023-12-09 17:09:24 +00:00
|
|
|
// saved route impacts nodes.
|
2024-02-08 16:28:19 +00:00
|
|
|
func SaveNodeRoutes(tx *gorm.DB, node *types.Node) (bool, error) {
|
2023-12-09 17:09:24 +00:00
|
|
|
sendUpdate := false
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
currentRoutes := types.Routes{}
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.Where("node_id = ?", node.ID).Find(¤tRoutes).Error
|
2021-08-21 13:49:46 +00:00
|
|
|
if err != nil {
|
2023-12-09 17:09:24 +00:00
|
|
|
return sendUpdate, err
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
2021-04-28 14:55:29 +00:00
|
|
|
|
2022-11-23 18:50:30 +00:00
|
|
|
advertisedRoutes := map[netip.Prefix]bool{}
|
2023-11-21 17:20:06 +00:00
|
|
|
for _, prefix := range node.Hostinfo.RoutableIPs {
|
2022-11-23 18:50:30 +00:00
|
|
|
advertisedRoutes[prefix] = false
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 16:45:46 +00:00
|
|
|
log.Trace().
|
2023-09-24 11:42:05 +00:00
|
|
|
Str("node", node.Hostname).
|
2023-09-11 16:45:46 +00:00
|
|
|
Interface("advertisedRoutes", advertisedRoutes).
|
|
|
|
Interface("currentRoutes", currentRoutes).
|
|
|
|
Msg("updating routes")
|
|
|
|
|
2022-11-25 15:29:45 +00:00
|
|
|
for pos, route := range currentRoutes {
|
2022-11-23 18:50:30 +00:00
|
|
|
if _, ok := advertisedRoutes[netip.Prefix(route.Prefix)]; ok {
|
|
|
|
if !route.Advertised {
|
2022-11-25 15:29:45 +00:00
|
|
|
currentRoutes[pos].Advertised = true
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.Save(¤tRoutes[pos]).Error
|
2022-11-23 18:50:30 +00:00
|
|
|
if err != nil {
|
2023-12-09 17:09:24 +00: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 18:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
advertisedRoutes[netip.Prefix(route.Prefix)] = true
|
2022-11-25 15:29:45 +00:00
|
|
|
} else if route.Advertised {
|
|
|
|
currentRoutes[pos].Advertised = false
|
|
|
|
currentRoutes[pos].Enabled = false
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.Save(¤tRoutes[pos]).Error
|
2022-11-25 15:29:45 +00:00
|
|
|
if err != nil {
|
2023-12-09 17:09:24 +00:00
|
|
|
return sendUpdate, err
|
2021-04-28 14:55:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2022-11-23 18:50:30 +00:00
|
|
|
for prefix, exists := range advertisedRoutes {
|
|
|
|
if !exists {
|
2023-05-21 16:37:59 +00:00
|
|
|
route := types.Route{
|
2024-02-23 09:59:24 +00:00
|
|
|
NodeID: node.ID.Uint64(),
|
2024-10-02 09:41:58 +00:00
|
|
|
Prefix: prefix,
|
2022-11-23 18:50:30 +00:00
|
|
|
Advertised: true,
|
|
|
|
Enabled: false,
|
|
|
|
}
|
2024-02-08 16:28:19 +00:00
|
|
|
err := tx.Create(&route).Error
|
2022-11-23 18:50:30 +00:00
|
|
|
if err != nil {
|
2023-12-09 17:09:24 +00:00
|
|
|
return sendUpdate, err
|
2022-11-23 18:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-30 13:31:06 +00:00
|
|
|
}
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
return sendUpdate, nil
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2024-04-10 13:35:09 +00:00
|
|
|
// FailoverNodeRoutesIfNeccessary takes a node and checks if the node's route
|
|
|
|
// need to be failed over to another host.
|
|
|
|
// If needed, the failover will be attempted.
|
|
|
|
func FailoverNodeRoutesIfNeccessary(
|
2024-02-08 16:28:19 +00:00
|
|
|
tx *gorm.DB,
|
2024-04-21 16:28:17 +00:00
|
|
|
isLikelyConnected *xsync.MapOf[types.NodeID, bool],
|
2024-02-08 16:28:19 +00:00
|
|
|
node *types.Node,
|
|
|
|
) (*types.StateUpdate, error) {
|
|
|
|
nodeRoutes, 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, nil
|
2022-11-24 22:41:11 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 13:35:09 +00:00
|
|
|
changedNodes := make(set.Set[types.NodeID])
|
|
|
|
|
|
|
|
nodeRouteLoop:
|
2023-12-09 17:09:24 +00:00
|
|
|
for _, nodeRoute := range nodeRoutes {
|
2024-02-08 16:28:19 +00:00
|
|
|
routes, err := getRoutesByPrefix(tx, netip.Prefix(nodeRoute.Prefix))
|
2023-12-09 17:09:24 +00:00
|
|
|
if err != nil {
|
2024-04-10 12:49:34 +00:00
|
|
|
return nil, fmt.Errorf("getting routes by prefix: %w", err)
|
2022-11-24 22:41:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
for _, route := range routes {
|
|
|
|
if route.IsPrimary {
|
|
|
|
// if we have a primary route, and the node is connected
|
|
|
|
// nothing needs to be done.
|
2024-04-21 16:28:17 +00:00
|
|
|
if val, ok := isLikelyConnected.Load(route.Node.ID); ok && val {
|
2024-04-10 13:35:09 +00:00
|
|
|
continue nodeRouteLoop
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
// if not, we need to failover the route
|
2024-04-21 16:28:17 +00:00
|
|
|
failover := failoverRoute(isLikelyConnected, &route, routes)
|
2024-04-10 12:49:34 +00:00
|
|
|
if failover != nil {
|
2024-04-10 13:35:09 +00:00
|
|
|
err := failover.save(tx)
|
2024-04-10 12:49:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("saving failover routes: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-04-10 13:35:09 +00:00
|
|
|
changedNodes.Add(failover.old.Node.ID)
|
|
|
|
changedNodes.Add(failover.new.Node.ID)
|
|
|
|
|
|
|
|
continue nodeRouteLoop
|
2024-02-08 16:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-25 15:29:45 +00:00
|
|
|
|
2024-04-10 13:35:09 +00:00
|
|
|
chng := changedNodes.Slice()
|
|
|
|
sort.SliceStable(chng, func(i, j int) bool {
|
|
|
|
return chng[i] < chng[j]
|
|
|
|
})
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
if len(changedNodes) != 0 {
|
|
|
|
return &types.StateUpdate{
|
2023-12-09 17:09:24 +00:00
|
|
|
Type: types.StatePeerChanged,
|
2024-04-10 13:35:09 +00:00
|
|
|
ChangeNodes: chng,
|
|
|
|
Message: "called from db.FailoverNodeRoutesIfNeccessary",
|
2024-02-08 16:28:19 +00:00
|
|
|
}, nil
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
return nil, nil
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
// failoverRouteTx takes a route that is no longer available,
|
2023-12-09 17:09:24 +00:00
|
|
|
// 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.
|
2024-02-23 09:59:24 +00:00
|
|
|
func failoverRouteTx(
|
2024-02-08 16:28:19 +00:00
|
|
|
tx *gorm.DB,
|
2024-04-21 16:28:17 +00:00
|
|
|
isLikelyConnected *xsync.MapOf[types.NodeID, bool],
|
2024-02-08 16:28:19 +00:00
|
|
|
r *types.Route,
|
2024-02-23 09:59:24 +00:00
|
|
|
) ([]types.NodeID, error) {
|
2023-12-09 17:09:24 +00:00
|
|
|
if r == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// This route is not a primary route, and it is not
|
2023-12-09 17:09:24 +00:00
|
|
|
// being served to nodes.
|
|
|
|
if !r.IsPrimary {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not have to failover exit nodes
|
|
|
|
if r.IsExitRoute() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
routes, err := getRoutesByPrefix(tx, netip.Prefix(r.Prefix))
|
2023-12-09 17:09:24 +00:00
|
|
|
if err != nil {
|
2024-04-10 12:49:34 +00:00
|
|
|
return nil, fmt.Errorf("getting routes by prefix: %w", err)
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 16:28:17 +00:00
|
|
|
fo := failoverRoute(isLikelyConnected, r, routes)
|
2024-02-23 09:59:24 +00:00
|
|
|
if fo == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-04-10 12:49:34 +00:00
|
|
|
err = fo.save(tx)
|
2024-02-23 09:59:24 +00:00
|
|
|
if err != nil {
|
2024-04-10 12:49:34 +00:00
|
|
|
return nil, fmt.Errorf("saving failover route: %w", err)
|
2024-02-23 09:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Str("hostname", fo.new.Node.Hostname).
|
|
|
|
Msgf("set primary to new route, was: id(%d), host(%s), now: id(%d), host(%s)", fo.old.ID, fo.old.Node.Hostname, fo.new.ID, fo.new.Node.Hostname)
|
|
|
|
|
|
|
|
// Return a list of the machinekeys of the changed nodes.
|
|
|
|
return []types.NodeID{fo.old.Node.ID, fo.new.Node.ID}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type failover struct {
|
|
|
|
old *types.Route
|
|
|
|
new *types.Route
|
|
|
|
}
|
|
|
|
|
2024-04-10 12:49:34 +00:00
|
|
|
func (f *failover) save(tx *gorm.DB) error {
|
|
|
|
err := tx.Save(f.old).Error
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("saving old primary: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Save(f.new).Error
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("saving new primary: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
func failoverRoute(
|
2024-04-21 16:28:17 +00:00
|
|
|
isLikelyConnected *xsync.MapOf[types.NodeID, bool],
|
2024-02-23 09:59:24 +00:00
|
|
|
routeToReplace *types.Route,
|
|
|
|
altRoutes types.Routes,
|
|
|
|
) *failover {
|
|
|
|
if routeToReplace == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This route is not a primary route, and it is not
|
|
|
|
// being served to nodes.
|
|
|
|
if !routeToReplace.IsPrimary {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not have to failover exit nodes
|
|
|
|
if routeToReplace.IsExitRoute() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
var newPrimary *types.Route
|
|
|
|
|
|
|
|
// Find a new suitable route
|
2024-02-23 09:59:24 +00:00
|
|
|
for idx, route := range altRoutes {
|
|
|
|
if routeToReplace.ID == route.ID {
|
2023-12-09 17:09:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-03 14:30:15 +00:00
|
|
|
if !route.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-21 16:28:17 +00:00
|
|
|
if isLikelyConnected != nil {
|
|
|
|
if val, ok := isLikelyConnected.Load(route.Node.ID); ok && val {
|
|
|
|
newPrimary = &altRoutes[idx]
|
|
|
|
break
|
|
|
|
}
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a new route was not found/available,
|
2024-02-08 16:28:19 +00:00
|
|
|
// return without an error.
|
2023-12-09 17:09:24 +00:00
|
|
|
// We do not want to update the database as
|
|
|
|
// the one currently marked as primary is the
|
|
|
|
// best we got.
|
|
|
|
if newPrimary == nil {
|
2024-02-23 09:59:24 +00:00
|
|
|
return nil
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
routeToReplace.IsPrimary = false
|
2023-12-09 17:09:24 +00:00
|
|
|
newPrimary.IsPrimary = true
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
return &failover{
|
|
|
|
old: routeToReplace,
|
|
|
|
new: newPrimary,
|
2023-12-09 17:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
func (hsdb *HSDatabase) EnableAutoApprovedRoutes(
|
|
|
|
aclPolicy *policy.ACLPolicy,
|
2023-09-24 11:42:05 +00:00
|
|
|
node *types.Node,
|
2024-02-23 09:59:24 +00:00
|
|
|
) error {
|
|
|
|
return hsdb.Write(func(tx *gorm.DB) error {
|
2024-02-08 16:28:19 +00:00
|
|
|
return EnableAutoApprovedRoutes(tx, aclPolicy, node)
|
|
|
|
})
|
|
|
|
}
|
2023-07-17 11:35:05 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// EnableAutoApprovedRoutes enables any routes advertised by a node that match the ACL autoApprovers policy.
|
|
|
|
func EnableAutoApprovedRoutes(
|
|
|
|
tx *gorm.DB,
|
|
|
|
aclPolicy *policy.ACLPolicy,
|
|
|
|
node *types.Node,
|
2024-02-23 09:59:24 +00:00
|
|
|
) error {
|
2024-04-17 05:03:06 +00:00
|
|
|
if node.IPv4 == nil && node.IPv6 == nil {
|
2024-02-23 09:59:24 +00:00
|
|
|
return nil // This node has no IPAddresses, so can't possibly match any autoApprovers ACLs
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
routes, err := GetNodeAdvertisedRoutes(tx, node)
|
2023-05-21 16:37:59 +00:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2024-04-10 12:49:34 +00:00
|
|
|
return fmt.Errorf("getting advertised routes for node(%s %d): %w", node.Hostname, node.ID, err)
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 15:36:47 +00:00
|
|
|
log.Trace().Interface("routes", routes).Msg("routes for autoapproving")
|
|
|
|
|
2024-06-23 20:06:50 +00:00
|
|
|
var approvedRoutes types.Routes
|
2023-05-21 16:37:59 +00:00
|
|
|
|
|
|
|
for _, advertisedRoute := range routes {
|
|
|
|
if advertisedRoute.Enabled {
|
|
|
|
continue
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
routeApprovers, err := aclPolicy.AutoApprovers.GetRouteApprovers(
|
|
|
|
netip.Prefix(advertisedRoute.Prefix),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-04-10 12:49:34 +00:00
|
|
|
return fmt.Errorf("failed to resolve autoApprovers for route(%d) for node(%s %d): %w", advertisedRoute.ID, node.Hostname, node.ID, err)
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 15:36:47 +00:00
|
|
|
log.Trace().
|
|
|
|
Str("node", node.Hostname).
|
2024-11-18 02:49:51 +00:00
|
|
|
Uint("user.id", node.User.ID).
|
2024-01-18 15:36:47 +00:00
|
|
|
Strs("routeApprovers", routeApprovers).
|
|
|
|
Str("prefix", netip.Prefix(advertisedRoute.Prefix).String()).
|
|
|
|
Msg("looking up route for autoapproving")
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
for _, approvedAlias := range routeApprovers {
|
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 approvedAlias == node.User.Username() {
|
2023-05-21 16:37:59 +00:00
|
|
|
approvedRoutes = append(approvedRoutes, advertisedRoute)
|
|
|
|
} else {
|
|
|
|
// TODO(kradalby): figure out how to get this to depend on less stuff
|
2023-09-24 11:42:05 +00:00
|
|
|
approvedIps, err := aclPolicy.ExpandAlias(types.Nodes{node}, approvedAlias)
|
2023-05-21 16:37:59 +00:00
|
|
|
if err != nil {
|
2024-04-10 12:49:34 +00:00
|
|
|
return fmt.Errorf("expanding alias %q for autoApprovers: %w", approvedAlias, err)
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
// approvedIPs should contain all of node's IPs if it matches the rule, so check for first
|
2024-04-17 05:03:06 +00:00
|
|
|
if approvedIps.Contains(*node.IPv4) {
|
2023-05-21 16:37:59 +00:00
|
|
|
approvedRoutes = append(approvedRoutes, advertisedRoute)
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
2022-11-25 17:50:12 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
for _, approvedRoute := range approvedRoutes {
|
2024-02-23 09:59:24 +00:00
|
|
|
_, err := EnableRoute(tx, uint64(approvedRoute.ID))
|
2023-05-21 16:37:59 +00:00
|
|
|
if err != nil {
|
2024-04-10 12:49:34 +00:00
|
|
|
return fmt.Errorf("enabling approved route(%d): %w", approvedRoute.ID, err)
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
return nil
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|