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
|
|
|
|
err := hsdb.db.Preload("Machine").Find(&routes).Error
|
|
|
|
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-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) GetMachineAdvertisedRoutes(machine *types.Machine) (types.Routes, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
|
|
|
return hsdb.getMachineAdvertisedRoutes(machine)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) getMachineAdvertisedRoutes(machine *types.Machine) (types.Routes, error) {
|
2023-05-21 12:37:59 -04:00
|
|
|
var routes types.Routes
|
|
|
|
err := hsdb.db.
|
|
|
|
Preload("Machine").
|
|
|
|
Where("machine_id = ? AND advertised = true", machine.ID).
|
|
|
|
Find(&routes).Error
|
2022-11-25 12:50:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
func (hsdb *HSDatabase) GetMachineRoutes(m *types.Machine) (types.Routes, error) {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.RLock()
|
|
|
|
defer hsdb.mu.RUnlock()
|
|
|
|
|
|
|
|
return hsdb.getMachineRoutes(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hsdb *HSDatabase) getMachineRoutes(m *types.Machine) (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.
|
2022-11-25 12:50:12 -05:00
|
|
|
Preload("Machine").
|
|
|
|
Where("machine_id = ?", m.ID).
|
|
|
|
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-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.Preload("Machine").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(
|
|
|
|
&route.Machine,
|
|
|
|
types.ExitRouteV4.String(),
|
|
|
|
types.ExitRouteV6.String(),
|
|
|
|
)
|
2023-01-22 17:39:42 -05:00
|
|
|
}
|
|
|
|
|
2023-05-11 03:09:18 -04:00
|
|
|
return hsdb.enableRoutes(&route.Machine, 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-07-17 07:35:05 -04:00
|
|
|
routes, err := hsdb.getMachineRoutes(&route.Machine)
|
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-07-17 07:35:05 -04:00
|
|
|
routes, err := hsdb.getMachineRoutes(&route.Machine)
|
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-07-17 07:35:05 -04:00
|
|
|
func (hsdb *HSDatabase) deleteMachineRoutes(m *types.Machine) error {
|
|
|
|
routes, err := hsdb.getMachineRoutes(m)
|
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
|
|
|
}
|
|
|
|
|
2022-11-25 10:29:45 -05:00
|
|
|
// isUniquePrefix returns if there is another machine 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{}).
|
2022-11-24 11:00:40 -05:00
|
|
|
Where("prefix = ? AND machine_id != ? AND advertised = ? AND enabled = ?",
|
|
|
|
route.Prefix,
|
|
|
|
route.MachineID,
|
|
|
|
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.
|
2022-11-24 17:41:11 -05:00
|
|
|
Preload("Machine").
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-23 13:50:30 -05:00
|
|
|
// getMachinePrimaryRoutes 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-07-26 05:53:42 -04:00
|
|
|
func (hsdb *HSDatabase) GetMachinePrimaryRoutes(machine *types.Machine) (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.
|
2022-11-23 13:50:30 -05:00
|
|
|
Preload("Machine").
|
2023-07-26 05:53:42 -04:00
|
|
|
Where("machine_id = ? AND advertised = ? AND enabled = ? AND is_primary = ?", machine.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-08-09 16:20:05 -04:00
|
|
|
// SaveMachineRoutes takes a machine and updates the database with
|
|
|
|
// the new routes.
|
|
|
|
func (hsdb *HSDatabase) SaveMachineRoutes(machine *types.Machine) error {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-08-09 16:20:05 -04:00
|
|
|
return hsdb.saveMachineRoutes(machine)
|
2023-07-17 07:35:05 -04:00
|
|
|
}
|
|
|
|
|
2023-08-09 16:20:05 -04:00
|
|
|
func (hsdb *HSDatabase) saveMachineRoutes(machine *types.Machine) error {
|
2023-05-21 12:37:59 -04:00
|
|
|
currentRoutes := types.Routes{}
|
2023-05-11 03:09:18 -04:00
|
|
|
err := hsdb.db.Where("machine_id = ?", machine.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{}
|
|
|
|
for _, prefix := range machine.HostInfo.RoutableIPs {
|
|
|
|
advertisedRoutes[prefix] = false
|
2021-08-21 09:49:46 -04:00
|
|
|
}
|
|
|
|
|
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{
|
2022-11-23 13:50:30 -05:00
|
|
|
MachineID: machine.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.
|
2022-11-24 17:41:11 -05:00
|
|
|
Preload("Machine").
|
|
|
|
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-08-09 16:20:05 -04:00
|
|
|
changedMachines := make(types.Machines, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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()).
|
|
|
|
Str("machine", route.Machine.GivenName).
|
|
|
|
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-08-09 16:20:05 -04:00
|
|
|
changedMachines = append(changedMachines, &route.Machine)
|
2022-12-20 16:03:15 -05:00
|
|
|
|
2022-11-24 17:41:11 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if route.IsPrimary {
|
2023-05-21 12:37:59 -04:00
|
|
|
if route.Machine.IsOnline() {
|
2022-11-24 17:41:11 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// machine offline, find a new primary
|
|
|
|
log.Info().
|
|
|
|
Str("machine", route.Machine.Hostname).
|
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
|
|
|
Msgf("machine offline, finding a new primary subnet")
|
|
|
|
|
|
|
|
// 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.
|
2022-11-24 17:41:11 -05:00
|
|
|
Preload("Machine").
|
|
|
|
Where("prefix = ? AND machine_id != ? AND advertised = ? AND enabled = ?",
|
|
|
|
route.Prefix,
|
|
|
|
route.MachineID,
|
|
|
|
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-05-21 12:37:59 -04:00
|
|
|
if r.Machine.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().
|
|
|
|
Str("machine", route.Machine.Hostname).
|
|
|
|
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().
|
|
|
|
Str("old_machine", route.Machine.Hostname).
|
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
|
|
|
Str("new_machine", newPrimaryRoute.Machine.Hostname).
|
|
|
|
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-08-09 16:20:05 -04:00
|
|
|
changedMachines = append(changedMachines, &route.Machine)
|
2022-11-24 17:41:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 06:20:22 -04:00
|
|
|
if len(changedMachines) > 0 {
|
|
|
|
hsdb.notifier.NotifyAll(types.StateUpdate{
|
|
|
|
Type: types.StatePeerChanged,
|
|
|
|
Changed: changedMachines,
|
|
|
|
})
|
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-05-21 12:37:59 -04:00
|
|
|
// EnableAutoApprovedRoutes enables any routes advertised by a machine that match the ACL autoApprovers policy.
|
|
|
|
func (hsdb *HSDatabase) EnableAutoApprovedRoutes(
|
|
|
|
aclPolicy *policy.ACLPolicy,
|
|
|
|
machine *types.Machine,
|
|
|
|
) error {
|
2023-07-17 07:35:05 -04:00
|
|
|
hsdb.mu.Lock()
|
|
|
|
defer hsdb.mu.Unlock()
|
|
|
|
|
2023-05-21 12:37:59 -04:00
|
|
|
if len(machine.IPAddresses) == 0 {
|
|
|
|
return nil // This machine has no IPAddresses, so can't possibly match any autoApprovers ACLs
|
|
|
|
}
|
|
|
|
|
2023-07-17 07:35:05 -04:00
|
|
|
routes, err := hsdb.getMachineAdvertisedRoutes(machine)
|
2023-05-21 12:37:59 -04:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Str("machine", machine.Hostname).
|
|
|
|
Msg("Could not get advertised routes for machine")
|
|
|
|
|
|
|
|
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()).
|
|
|
|
Uint64("machineId", machine.ID).
|
|
|
|
Msg("Failed to resolve autoApprovers for advertised route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, approvedAlias := range routeApprovers {
|
|
|
|
if approvedAlias == machine.User.Name {
|
|
|
|
approvedRoutes = append(approvedRoutes, advertisedRoute)
|
|
|
|
} else {
|
|
|
|
// TODO(kradalby): figure out how to get this to depend on less stuff
|
2023-08-09 16:20:05 -04:00
|
|
|
approvedIps, err := aclPolicy.ExpandAlias(types.Machines{machine}, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// approvedIPs should contain all of machine's IPs if it matches the rule, so check for first
|
|
|
|
if approvedIps.Contains(machine.IPAddresses[0]) {
|
|
|
|
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()).
|
|
|
|
Uint64("machineId", machine.ID).
|
|
|
|
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
|
|
|
}
|