Map route into machine

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2023-05-31 09:59:15 +02:00 committed by Kristoffer Dalby
parent f7f472ae07
commit bce8427423
2 changed files with 61 additions and 13 deletions

View file

@ -47,9 +47,13 @@ func (hsdb *HSDatabase) ListPeers(machine *types.Machine) (types.Machines, error
Msg("Finding direct peers") Msg("Finding direct peers")
machines := types.Machines{} machines := types.Machines{}
if err := hsdb.db.Preload("AuthKey").Preload("AuthKey.User").Preload("User").Where("node_key <> ?", if err := hsdb.db.
Preload("AuthKey").
Preload("AuthKey.User").
Preload("User").
Preload("Routes").
Where("node_key <> ?",
machine.NodeKey).Find(&machines).Error; err != nil { machine.NodeKey).Find(&machines).Error; err != nil {
log.Error().Err(err).Msg("Error accessing db")
return types.Machines{}, err return types.Machines{}, err
} }
@ -66,7 +70,13 @@ func (hsdb *HSDatabase) ListPeers(machine *types.Machine) (types.Machines, error
func (hsdb *HSDatabase) ListMachines() ([]types.Machine, error) { func (hsdb *HSDatabase) ListMachines() ([]types.Machine, error) {
machines := []types.Machine{} machines := []types.Machine{}
if err := hsdb.db.Preload("AuthKey").Preload("AuthKey.User").Preload("User").Find(&machines).Error; err != nil { if err := hsdb.db.
Preload("AuthKey").
Preload("AuthKey.User").
Preload("User").
Preload("Routes").
Find(&machines).Error; err != nil {
return nil, err return nil, err
} }
@ -75,7 +85,13 @@ func (hsdb *HSDatabase) ListMachines() ([]types.Machine, error) {
func (hsdb *HSDatabase) ListMachinesByGivenName(givenName string) (types.Machines, error) { func (hsdb *HSDatabase) ListMachinesByGivenName(givenName string) (types.Machines, error) {
machines := types.Machines{} machines := types.Machines{}
if err := hsdb.db.Preload("AuthKey").Preload("AuthKey.User").Preload("User").Where("given_name = ?", givenName).Find(&machines).Error; err != nil { if err := hsdb.db.
Preload("AuthKey").
Preload("AuthKey.User").
Preload("User").
Preload("Routes").
Where("given_name = ?", givenName).Find(&machines).Error; err != nil {
return nil, err return nil, err
} }
@ -120,7 +136,12 @@ func (hsdb *HSDatabase) GetMachineByGivenName(
// GetMachineByID finds a Machine by ID and returns the Machine struct. // GetMachineByID finds a Machine by ID and returns the Machine struct.
func (hsdb *HSDatabase) GetMachineByID(id uint64) (*types.Machine, error) { func (hsdb *HSDatabase) GetMachineByID(id uint64) (*types.Machine, error) {
m := types.Machine{} m := types.Machine{}
if result := hsdb.db.Preload("AuthKey").Preload("User").Find(&types.Machine{ID: id}).First(&m); result.Error != nil { if result := hsdb.db.
Preload("AuthKey").
Preload("AuthKey.User").
Preload("User").
Preload("Routes").
Find(&types.Machine{ID: id}).First(&m); result.Error != nil {
return nil, result.Error return nil, result.Error
} }
@ -132,7 +153,12 @@ func (hsdb *HSDatabase) GetMachineByMachineKey(
machineKey key.MachinePublic, machineKey key.MachinePublic,
) (*types.Machine, error) { ) (*types.Machine, error) {
m := types.Machine{} m := types.Machine{}
if result := hsdb.db.Preload("AuthKey").Preload("User").First(&m, "machine_key = ?", util.MachinePublicKeyStripPrefix(machineKey)); result.Error != nil { if result := hsdb.db.
Preload("AuthKey").
Preload("AuthKey.User").
Preload("User").
Preload("Routes").
First(&m, "machine_key = ?", util.MachinePublicKeyStripPrefix(machineKey)); result.Error != nil {
return nil, result.Error return nil, result.Error
} }
@ -144,7 +170,12 @@ func (hsdb *HSDatabase) GetMachineByNodeKey(
nodeKey key.NodePublic, nodeKey key.NodePublic,
) (*types.Machine, error) { ) (*types.Machine, error) {
machine := types.Machine{} machine := types.Machine{}
if result := hsdb.db.Preload("AuthKey").Preload("User").First(&machine, "node_key = ?", if result := hsdb.db.
Preload("AuthKey").
Preload("AuthKey.User").
Preload("User").
Preload("Routes").
First(&machine, "node_key = ?",
util.NodePublicKeyStripPrefix(nodeKey)); result.Error != nil { util.NodePublicKeyStripPrefix(nodeKey)); result.Error != nil {
return nil, result.Error return nil, result.Error
} }
@ -157,7 +188,12 @@ func (hsdb *HSDatabase) GetMachineByAnyKey(
machineKey key.MachinePublic, nodeKey key.NodePublic, oldNodeKey key.NodePublic, machineKey key.MachinePublic, nodeKey key.NodePublic, oldNodeKey key.NodePublic,
) (*types.Machine, error) { ) (*types.Machine, error) {
machine := types.Machine{} machine := types.Machine{}
if result := hsdb.db.Preload("AuthKey").Preload("User").First(&machine, "machine_key = ? OR node_key = ? OR node_key = ?", if result := hsdb.db.
Preload("AuthKey").
Preload("AuthKey.User").
Preload("User").
Preload("Routes").
First(&machine, "machine_key = ? OR node_key = ? OR node_key = ?",
util.MachinePublicKeyStripPrefix(machineKey), util.MachinePublicKeyStripPrefix(machineKey),
util.NodePublicKeyStripPrefix(nodeKey), util.NodePublicKeyStripPrefix(nodeKey),
util.NodePublicKeyStripPrefix(oldNodeKey)); result.Error != nil { util.NodePublicKeyStripPrefix(oldNodeKey)); result.Error != nil {

View file

@ -59,6 +59,8 @@ type Machine struct {
HostInfo HostInfo HostInfo HostInfo
Endpoints StringList Endpoints StringList
Routes []Route
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
DeletedAt *time.Time DeletedAt *time.Time
@ -80,6 +82,16 @@ func (ma MachineAddresses) ToStringSlice() []string {
return strSlice return strSlice
} }
func (ma MachineAddresses) Prefixes() []netip.Prefix {
addrs := []netip.Prefix{}
for _, machineAddress := range ma {
ip := netip.PrefixFrom(machineAddress, machineAddress.BitLen())
addrs = append(addrs, ip)
}
return addrs
}
// AppendToIPSet adds the individual ips in MachineAddresses to a // AppendToIPSet adds the individual ips in MachineAddresses to a
// given netipx.IPSetBuilder. // given netipx.IPSetBuilder.
func (ma MachineAddresses) AppendToIPSet(build *netipx.IPSetBuilder) { func (ma MachineAddresses) AppendToIPSet(build *netipx.IPSetBuilder) {