mirror of
https://github.com/juanfont/headscale.git
synced 2026-07-08 00:50:20 +09:00
db: drop ambiguous machine-key getter, match precisely in test helper
The First()-by-machine-key getter returned an undefined node when a machine key mapped to several nodes. It was used only by RegisterNodeForTest; match on (machine_key, user_id) there instead and remove the getter. Updates #3312
This commit is contained in:
committed by
Kristoffer Dalby
parent
1689478485
commit
68a6d3cf17
+10
-27
@@ -143,27 +143,6 @@ func GetNodeByID(tx *gorm.DB, id types.NodeID) (*types.Node, error) {
|
|||||||
return &mach, nil
|
return &mach, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hsdb *HSDatabase) GetNodeByMachineKey(machineKey key.MachinePublic) (*types.Node, error) {
|
|
||||||
return GetNodeByMachineKey(hsdb.DB, machineKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetNodeByMachineKey finds a [types.Node] by its [key.MachinePublic] and returns the [types.Node] struct.
|
|
||||||
func GetNodeByMachineKey(
|
|
||||||
tx *gorm.DB,
|
|
||||||
machineKey key.MachinePublic,
|
|
||||||
) (*types.Node, error) {
|
|
||||||
mach := types.Node{}
|
|
||||||
if result := tx.
|
|
||||||
Preload("AuthKey").
|
|
||||||
Preload("AuthKey.User").
|
|
||||||
Preload("User").
|
|
||||||
First(&mach, "machine_key = ?", machineKey.String()); result.Error != nil {
|
|
||||||
return nil, result.Error
|
|
||||||
}
|
|
||||||
|
|
||||||
return &mach, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hsdb *HSDatabase) GetNodeByNodeKey(nodeKey key.NodePublic) (*types.Node, error) {
|
func (hsdb *HSDatabase) GetNodeByNodeKey(nodeKey key.NodePublic) (*types.Node, error) {
|
||||||
return GetNodeByNodeKey(hsdb.DB, nodeKey)
|
return GetNodeByNodeKey(hsdb.DB, nodeKey)
|
||||||
}
|
}
|
||||||
@@ -297,12 +276,16 @@ func RegisterNodeForTest(tx *gorm.DB, node types.Node, ipv4 *netip.Addr, ipv6 *n
|
|||||||
|
|
||||||
logEvent.Msg("registering test node")
|
logEvent.Msg("registering test node")
|
||||||
|
|
||||||
// If the a new node is registered with the same machine key, to the same user,
|
// Reuse the existing node's identity only when the same machine
|
||||||
// update the existing node.
|
// re-registers for the same user; a different user is a new node. Match on
|
||||||
// If the same node is registered again, but to a new user, then that is considered
|
// (machine_key, user_id) precisely - a machine key can map to several nodes
|
||||||
// a new node.
|
// (one per user), so a machine-key-only lookup would be ambiguous.
|
||||||
oldNode, _ := GetNodeByMachineKey(tx, node.MachineKey)
|
var oldNode types.Node
|
||||||
if oldNode != nil && oldNode.UserID == node.UserID {
|
|
||||||
|
err := tx.
|
||||||
|
Where("machine_key = ? AND user_id = ?", node.MachineKey.String(), node.UserID).
|
||||||
|
First(&oldNode).Error
|
||||||
|
if err == nil {
|
||||||
node.ID = oldNode.ID
|
node.ID = oldNode.ID
|
||||||
node.GivenName = oldNode.GivenName
|
node.GivenName = oldNode.GivenName
|
||||||
node.ApprovedRoutes = oldNode.ApprovedRoutes
|
node.ApprovedRoutes = oldNode.ApprovedRoutes
|
||||||
|
|||||||
Reference in New Issue
Block a user