2023-05-21 19:37:59 +03:00
|
|
|
package db
|
2021-04-28 16:55:29 +02:00
|
|
|
|
|
|
|
import (
|
2022-11-25 15:29:45 +00:00
|
|
|
"errors"
|
2022-09-02 00:06:19 +02:00
|
|
|
"net/netip"
|
2022-11-23 18:50:30 +00:00
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2022-11-24 22:41:11 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-11-23 18:50:30 +00:00
|
|
|
"gorm.io/gorm"
|
2021-04-28 16:55:29 +02:00
|
|
|
)
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
var ErrRouteIsNotAvailable = errors.New("route is not available")
|
2021-08-21 14:49:46 +01:00
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
func (hsdb *HSDatabase) GetRoutes() (types.Routes, error) {
|
|
|
|
var routes types.Routes
|
|
|
|
err := hsdb.db.Preload("Machine").Find(&routes).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-21 14:49:46 +01:00
|
|
|
}
|
2022-11-25 15:29:45 +00:00
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
return routes, nil
|
2021-08-21 14:49:46 +01:00
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
func (hsdb *HSDatabase) GetMachineAdvertisedRoutes(machine *types.Machine) (types.Routes, error) {
|
|
|
|
var routes types.Routes
|
|
|
|
err := hsdb.db.
|
|
|
|
Preload("Machine").
|
|
|
|
Where("machine_id = ? AND advertised = true", machine.ID).
|
|
|
|
Find(&routes).Error
|
2022-11-25 17:50:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
func (hsdb *HSDatabase) GetMachineRoutes(m *types.Machine) (types.Routes, error) {
|
|
|
|
var routes types.Routes
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.
|
2022-11-25 17:50:12 +00: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 19:37:59 +03:00
|
|
|
func (hsdb *HSDatabase) GetRoute(id uint64) (*types.Route, error) {
|
|
|
|
var route types.Route
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.Preload("Machine").First(&route, id).Error
|
2022-11-25 17:50:12 +00: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 {
|
|
|
|
route, err := hsdb.GetRoute(id)
|
2022-11-25 17:50:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 19:37:59 +03:00
|
|
|
if route.IsExitRoute() {
|
|
|
|
return hsdb.enableRoutes(
|
|
|
|
&route.Machine,
|
|
|
|
types.ExitRouteV4.String(),
|
|
|
|
types.ExitRouteV6.String(),
|
|
|
|
)
|
2023-01-22 22:39:42 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
return hsdb.enableRoutes(&route.Machine, netip.Prefix(route.Prefix).String())
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
func (hsdb *HSDatabase) DisableRoute(id uint64) error {
|
|
|
|
route, err := hsdb.GetRoute(id)
|
2022-11-25 17:50:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
2023-05-21 19:37:59 +03:00
|
|
|
if !route.IsExitRoute() {
|
2023-05-07 10:17:16 +00:00
|
|
|
route.Enabled = false
|
|
|
|
route.IsPrimary = false
|
2023-05-11 09:09:18 +02:00
|
|
|
err = hsdb.db.Save(route).Error
|
2023-05-07 10:17:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
return hsdb.HandlePrimarySubnetFailover()
|
2023-05-07 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
routes, err := hsdb.GetMachineRoutes(&route.Machine)
|
2022-11-25 17:50:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-07 10:17:16 +00:00
|
|
|
for i := range routes {
|
2023-05-21 19:37:59 +03:00
|
|
|
if routes[i].IsExitRoute() {
|
2023-05-07 10:17:16 +00:00
|
|
|
routes[i].Enabled = false
|
|
|
|
routes[i].IsPrimary = false
|
2023-05-11 09:09:18 +02:00
|
|
|
err = hsdb.db.Save(&routes[i]).Error
|
2023-05-07 10:17:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
return hsdb.HandlePrimarySubnetFailover()
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
func (hsdb *HSDatabase) DeleteRoute(id uint64) error {
|
|
|
|
route, err := hsdb.GetRoute(id)
|
2023-03-06 09:05:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
2023-05-21 19:37:59 +03:00
|
|
|
if !route.IsExitRoute() {
|
2023-05-11 09:09:18 +02:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&route).Error; err != nil {
|
2023-05-07 10:17:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
return hsdb.HandlePrimarySubnetFailover()
|
2023-05-07 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
routes, err := hsdb.GetMachineRoutes(&route.Machine)
|
2023-05-07 10:17:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
routesToDelete := types.Routes{}
|
2023-05-07 10:17:16 +00:00
|
|
|
for _, r := range routes {
|
2023-05-21 19:37:59 +03:00
|
|
|
if r.IsExitRoute() {
|
2023-05-07 10:17:16 +00:00
|
|
|
routesToDelete = append(routesToDelete, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&routesToDelete).Error; err != nil {
|
2023-03-06 09:05:40 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
return hsdb.HandlePrimarySubnetFailover()
|
2023-03-06 09:05:40 +01:00
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
func (hsdb *HSDatabase) DeleteMachineRoutes(m *types.Machine) error {
|
2023-05-11 09:09:18 +02:00
|
|
|
routes, err := hsdb.GetMachineRoutes(m)
|
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-05-21 19:37:59 +03:00
|
|
|
return hsdb.HandlePrimarySubnetFailover()
|
2023-03-06 09:05:40 +01:00
|
|
|
}
|
|
|
|
|
2022-11-25 15:29:45 +00:00
|
|
|
// isUniquePrefix returns if there is another machine providing the same route already.
|
2023-05-21 19:37:59 +03:00
|
|
|
func (hsdb *HSDatabase) isUniquePrefix(route types.Route) bool {
|
2022-11-24 16:00:40 +00:00
|
|
|
var count int64
|
2023-05-11 09:09:18 +02:00
|
|
|
hsdb.db.
|
2023-05-21 19:37:59 +03:00
|
|
|
Model(&types.Route{}).
|
2022-11-24 16:00:40 +00:00
|
|
|
Where("prefix = ? AND machine_id != ? AND advertised = ? AND enabled = ?",
|
|
|
|
route.Prefix,
|
|
|
|
route.MachineID,
|
|
|
|
true, true).Count(&count)
|
2022-11-25 15:29:45 +00:00
|
|
|
|
2022-11-24 16:00:40 +00:00
|
|
|
return count == 0
|
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03: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.
|
2022-11-24 22:41:11 +00:00
|
|
|
Preload("Machine").
|
2023-05-21 19:37:59 +03:00
|
|
|
Where("prefix = ? AND advertised = ? AND enabled = ? AND is_primary = ?", types.IPPrefix(prefix), 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
|
|
|
|
}
|
|
|
|
|
2022-11-23 18:50:30 +00:00
|
|
|
// getMachinePrimaryRoutes returns the routes that are enabled and marked as primary (for subnet failover)
|
2022-11-25 15:29:45 +00:00
|
|
|
// Exit nodes are not considered for this, as they are never marked as Primary.
|
2023-05-21 19:37:59 +03:00
|
|
|
func (hsdb *HSDatabase) GetMachinePrimaryRoutes(m *types.Machine) (types.Routes, error) {
|
|
|
|
var routes types.Routes
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.
|
2022-11-23 18:50:30 +00:00
|
|
|
Preload("Machine").
|
|
|
|
Where("machine_id = ? AND advertised = ? AND enabled = ? AND is_primary = ?", m.ID, true, true, true).
|
|
|
|
Find(&routes).Error
|
2021-08-21 14:49:46 +01:00
|
|
|
if err != nil {
|
2022-11-23 18:50:30 +00:00
|
|
|
return nil, err
|
2021-08-21 14:49:46 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 18:50:30 +00:00
|
|
|
return routes, nil
|
|
|
|
}
|
2021-08-21 14:49:46 +01:00
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
func (hsdb *HSDatabase) ProcessMachineRoutes(machine *types.Machine) error {
|
|
|
|
currentRoutes := types.Routes{}
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.Where("machine_id = ?", machine.ID).Find(¤tRoutes).Error
|
2021-08-21 14:49:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-28 16:55:29 +02:00
|
|
|
|
2022-11-23 18:50:30 +00:00
|
|
|
advertisedRoutes := map[netip.Prefix]bool{}
|
|
|
|
for _, prefix := range machine.HostInfo.RoutableIPs {
|
|
|
|
advertisedRoutes[prefix] = false
|
2021-08-21 14:49:46 +01:00
|
|
|
}
|
|
|
|
|
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
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.Save(¤tRoutes[pos]).Error
|
2022-11-23 18:50:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.Save(¤tRoutes[pos]).Error
|
2022-11-25 15:29:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-28 16:55:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 14:49:46 +01:00
|
|
|
|
2022-11-23 18:50:30 +00:00
|
|
|
for prefix, exists := range advertisedRoutes {
|
|
|
|
if !exists {
|
2023-05-21 19:37:59 +03:00
|
|
|
route := types.Route{
|
2022-11-23 18:50:30 +00:00
|
|
|
MachineID: machine.ID,
|
2023-05-21 19:37:59 +03:00
|
|
|
Prefix: types.IPPrefix(prefix),
|
2022-11-23 18:50:30 +00:00
|
|
|
Advertised: true,
|
|
|
|
Enabled: false,
|
|
|
|
}
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.Create(&route).Error
|
2022-11-23 18:50:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 15:31:06 +02:00
|
|
|
}
|
2021-08-21 14:49:46 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
func (hsdb *HSDatabase) HandlePrimarySubnetFailover() error {
|
2022-11-24 22:41:11 +00:00
|
|
|
// first, get all the enabled routes
|
2023-05-21 19:37:59 +03:00
|
|
|
var routes types.Routes
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.
|
2022-11-24 22:41:11 +00:00
|
|
|
Preload("Machine").
|
|
|
|
Where("advertised = ? AND enabled = ?", true, true).
|
|
|
|
Find(&routes).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
|
|
|
log.Error().Err(err).Msg("error getting routes")
|
|
|
|
}
|
|
|
|
|
2022-12-20 10:08:32 +01:00
|
|
|
routesChanged := false
|
2022-11-25 15:29:45 +00:00
|
|
|
for pos, route := range routes {
|
2023-05-21 19:37:59 +03:00
|
|
|
if route.IsExitRoute() {
|
2022-11-24 22:41:11 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !route.IsPrimary {
|
2023-05-11 09:09:18 +02:00
|
|
|
_, err := hsdb.getPrimaryRoute(netip.Prefix(route.Prefix))
|
|
|
|
if hsdb.isUniquePrefix(route) || errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-25 17:50:12 +00:00
|
|
|
log.Info().
|
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
|
|
|
Str("machine", route.Machine.GivenName).
|
|
|
|
Msg("Setting primary route")
|
2022-11-25 15:29:45 +00:00
|
|
|
routes[pos].IsPrimary = true
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.Save(&routes[pos]).Error
|
2022-11-24 22:41:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error marking route as primary")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-11-25 15:29:45 +00:00
|
|
|
|
2022-12-20 10:08:32 +01:00
|
|
|
routesChanged = true
|
2022-12-20 21:03:15 +00:00
|
|
|
|
2022-11-24 22:41:11 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if route.IsPrimary {
|
2023-05-21 19:37:59 +03:00
|
|
|
if route.Machine.IsOnline() {
|
2022-11-24 22:41:11 +00: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 19:37:59 +03:00
|
|
|
var newPrimaryRoutes types.Routes
|
2023-05-11 09:09:18 +02:00
|
|
|
err := hsdb.db.
|
2022-11-24 22:41:11 +00:00
|
|
|
Preload("Machine").
|
|
|
|
Where("prefix = ? AND machine_id != ? AND advertised = ? AND enabled = ?",
|
|
|
|
route.Prefix,
|
|
|
|
route.MachineID,
|
|
|
|
true, true).
|
|
|
|
Find(&newPrimaryRoutes).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
|
|
|
log.Error().Err(err).Msg("error finding new primary route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
var newPrimaryRoute *types.Route
|
2022-11-25 15:29:45 +00:00
|
|
|
for pos, r := range newPrimaryRoutes {
|
2023-05-21 19:37:59 +03:00
|
|
|
if r.Machine.IsOnline() {
|
2022-11-25 15:29:45 +00:00
|
|
|
newPrimaryRoute = &newPrimaryRoutes[pos]
|
|
|
|
|
2022-11-24 22:41:11 +00: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 15:29:45 +00:00
|
|
|
|
2022-11-24 22:41:11 +00: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 15:29:45 +00:00
|
|
|
routes[pos].IsPrimary = false
|
2023-05-11 09:09:18 +02:00
|
|
|
err = hsdb.db.Save(&routes[pos]).Error
|
2022-11-24 22:41:11 +00: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 09:09:18 +02:00
|
|
|
err = hsdb.db.Save(&newPrimaryRoute).Error
|
2022-11-24 22:41:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error enabling new primary route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-12-20 10:08:32 +01:00
|
|
|
|
|
|
|
routesChanged = true
|
2022-11-24 22:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 10:08:32 +01:00
|
|
|
if routesChanged {
|
2023-05-11 09:09:18 +02:00
|
|
|
hsdb.notifyStateChange()
|
2022-12-20 10:08:32 +01:00
|
|
|
}
|
2022-12-20 21:03:15 +00:00
|
|
|
|
2022-11-24 22:41:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-25 17:50:12 +00:00
|
|
|
|
2023-05-21 19:37:59 +03: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 {
|
|
|
|
if len(machine.IPAddresses) == 0 {
|
|
|
|
return nil // This machine has no IPAddresses, so can't possibly match any autoApprovers ACLs
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err := hsdb.GetMachineAdvertisedRoutes(machine)
|
|
|
|
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 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03: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
|
|
|
|
approvedIps, err := aclPolicy.ExpandAlias(types.Machines{*machine}, approvedAlias, hsdb.stripEmailDomain)
|
|
|
|
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 17:50:12 +00:00
|
|
|
}
|
2023-05-21 19:37:59 +03:00
|
|
|
}
|
2022-11-25 17:50:12 +00:00
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
for _, approvedRoute := range approvedRoutes {
|
|
|
|
err := hsdb.EnableRoute(uint64(approvedRoute.ID))
|
|
|
|
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 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
return nil
|
2022-11-25 17:50:12 +00:00
|
|
|
}
|