mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-19 11:57:07 +09:00
state: record the control address without holding persistMu
Write NodeStore unlocked, persist through persistNodeWith; drop the dedicated db setter and rollback. Strip IPv6 zones. Updates #2272
This commit is contained in:
1 parent
bae1d6fd84
commit
3af065bb03
7 files changed
+79
-155
No files matched your search
@@ -230,12 +230,10 @@ func TestAPIv2Device_GetPostureAttributes(t *testing.T) {
|
||||
{name: "IPv6", addr: "2001:db8::10"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
changed, err := e.app.state.SetLastControlAddress(
|
||||
require.NoError(t, e.app.state.SetLastControlAddress(
|
||||
e.nodeID,
|
||||
netip.MustParseAddr(test.addr),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, changed)
|
||||
))
|
||||
|
||||
attrs := getDevicePostureAttributes(t, e.api, e.deviceID)
|
||||
assert.Equal(t, test.addr, attrs.Attributes["ip:publicAddress"])
|
||||
|
||||
@@ -179,26 +179,6 @@ func SetLastSeen(tx *gorm.DB, nodeID types.NodeID, lastSeen time.Time) error {
|
||||
return tx.Model(&types.Node{}).Where("id = ?", nodeID).Update("last_seen", lastSeen).Error
|
||||
}
|
||||
|
||||
// SetLastControlAddress persists the source IP most recently observed on an
|
||||
// authenticated control connection from a node. It updates only this metadata
|
||||
// column and deliberately leaves UpdatedAt unchanged.
|
||||
func (hsdb *HSDatabase) SetLastControlAddress(nodeID types.NodeID, addr netip.Addr) error {
|
||||
return hsdb.Write(func(tx *gorm.DB) error {
|
||||
result := tx.Model(&types.Node{}).
|
||||
Where("id = ?", nodeID).
|
||||
UpdateColumn("last_control_address", addr.String())
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (hsdb *HSDatabase) NodeSetExpiry(nodeID types.NodeID, expiry *time.Time) error {
|
||||
return hsdb.Write(func(tx *gorm.DB) error {
|
||||
return NodeSetExpiry(tx, nodeID, expiry)
|
||||
|
||||
+5
-23
@@ -1077,35 +1077,17 @@ func (ns *noiseServer) getAndValidateNode(mapRequest tailcfg.MapRequest) (types.
|
||||
return nv, nil
|
||||
}
|
||||
|
||||
// recordLastControlAddress records the outer request's already-resolved peer
|
||||
// address after node-key and Noise machine-key validation. Failure to parse or
|
||||
// persist this auxiliary metadata must not break an otherwise valid map poll.
|
||||
// recordLastControlAddress stores the outer request's resolved client address
|
||||
// once the node's keys are validated. Failing to store it must not fail the
|
||||
// poll.
|
||||
func (ns *noiseServer) recordLastControlAddress(node types.NodeView, remoteAddr string) {
|
||||
addr, ok := parsePeerAddr(remoteAddr)
|
||||
if !ok {
|
||||
log.Warn().
|
||||
Str("remote_addr", remoteAddr).
|
||||
EmbedObject(node).
|
||||
Msg("could not parse node control connection address")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
changed, err := ns.headscale.state.SetLastControlAddress(node.ID(), addr.Unmap())
|
||||
err := ns.headscale.state.SetLastControlAddress(node.ID(), addr)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("remote_addr", remoteAddr).
|
||||
EmbedObject(node).
|
||||
Msg("could not persist node control connection address")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if changed {
|
||||
log.Debug().
|
||||
Stringer("last_control_address", addr.Unmap()).
|
||||
EmbedObject(node).
|
||||
Msg("updated node control connection address")
|
||||
log.Error().Err(err).EmbedObject(node).Msg("could not persist node control connection address")
|
||||
}
|
||||
}
|
||||
@@ -385,10 +385,9 @@ func TestPollNetMapHandler_LastControlAddress(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
before, ok := app.state.GetNodeByID(view.ID())
|
||||
require.True(t, ok)
|
||||
require.True(t, before.LastControlAddress().Valid())
|
||||
wantUnchanged := before.LastControlAddress().Get()
|
||||
// Seeded, so the cases below do not depend on the ones above.
|
||||
wantUnchanged := netip.MustParseAddr("203.0.113.50")
|
||||
require.NoError(t, app.state.SetLastControlAddress(view.ID(), wantUnchanged))
|
||||
|
||||
t.Run("invalid address is ignored", func(t *testing.T) {
|
||||
resp := pollMapRequest(t, ns, "not-an-address", request)
|
||||
|
||||
@@ -165,38 +165,6 @@ func TestSnapshotFromNodes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetLastControlAddressReusesPeerTopology(t *testing.T) {
|
||||
peerCalculations := 0
|
||||
nodes := types.Nodes{
|
||||
new(createTestNode(1, 1, "user1", "node1")),
|
||||
new(createTestNode(2, 1, "user1", "node2")),
|
||||
}
|
||||
store := NewNodeStore(nodes, func(nodes []types.NodeView) map[types.NodeID][]types.NodeID {
|
||||
peerCalculations++
|
||||
|
||||
return allowAllPeersFunc(nodes)
|
||||
}, 1, time.Millisecond)
|
||||
store.Start()
|
||||
t.Cleanup(store.Stop)
|
||||
|
||||
require.Equal(t, 1, peerCalculations)
|
||||
|
||||
addr := netip.MustParseAddr("203.0.113.10")
|
||||
updated, ok := store.UpdateNode(1, func(n *types.Node) {
|
||||
n.LastControlAddress = &addr
|
||||
})
|
||||
require.True(t, ok)
|
||||
require.True(t, updated.LastControlAddress().Valid())
|
||||
assert.Equal(t, addr, updated.LastControlAddress().Get())
|
||||
assert.Equal(t, 1, peerCalculations, "metadata must not recalculate peer membership")
|
||||
|
||||
peers := store.ListPeers(2)
|
||||
require.Equal(t, 1, peers.Len())
|
||||
require.True(t, peers.At(0).LastControlAddress().Valid())
|
||||
assert.Equal(t, addr, peers.At(0).LastControlAddress().Get(),
|
||||
"reused peer topology must contain refreshed node views")
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
|
||||
func createTestNode(nodeID types.NodeID, userID uint, username, hostname string) types.Node {
|
||||
@@ -1996,7 +1964,8 @@ var nodeFieldImpact = map[string]string{
|
||||
"IsOnline": "election", "Unhealthy": "election",
|
||||
"MachineKey": "payload", "NodeKey": "payload", "DiscoKey": "payload",
|
||||
"Endpoints": "payload", "Hostname": "payload", "GivenName": "payload",
|
||||
"RegisterMethod": "payload", "AuthKeyID": "payload", "AuthKey": "payload",
|
||||
"LastControlAddress": "payload",
|
||||
"RegisterMethod": "payload", "AuthKeyID": "payload", "AuthKey": "payload",
|
||||
"Ephemeral": "payload",
|
||||
"Expiry": "election", "LastSeen": "payload", "CreatedAt": "payload",
|
||||
"UpdatedAt": "payload", "DeletedAt": "payload",
|
||||
@@ -2053,6 +2022,7 @@ func TestUpdateChangesReportsRelationFields(t *testing.T) {
|
||||
{name: "session start", mutate: func(n *types.Node) { n.SessionStart = time.Now() }, wantElection: true},
|
||||
{name: "endpoint", mutate: func(n *types.Node) { n.Endpoints = []netip.AddrPort{netip.MustParseAddrPort("192.0.2.1:1")} }},
|
||||
{name: "lastseen", mutate: func(n *types.Node) { n.LastSeen = new(time.Now()) }},
|
||||
{name: "last control address", mutate: func(n *types.Node) { n.LastControlAddress = new(netip.MustParseAddr("203.0.113.10")) }},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
@@ -319,49 +319,26 @@ func TestPersistEmptyEndpoints(t *testing.T) {
|
||||
func TestSetLastControlAddressPersists(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
addr netip.Addr
|
||||
addr string
|
||||
want string
|
||||
}{
|
||||
{name: "IPv4", addr: netip.MustParseAddr("203.0.113.10")},
|
||||
{name: "IPv6", addr: netip.MustParseAddr("2001:db8::10")},
|
||||
{name: "IPv4", addr: "203.0.113.10", want: "203.0.113.10"},
|
||||
{name: "IPv6", addr: "2001:db8::10", want: "2001:db8::10"},
|
||||
{name: "IPv4-mapped", addr: "::ffff:203.0.113.44", want: "203.0.113.44"},
|
||||
{name: "zoned", addr: "fe80::1%eth0", want: "fe80::1"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
dbPath, s, nodeID := persistTestSetup(t)
|
||||
want := netip.MustParseAddr(test.want)
|
||||
|
||||
require.NoError(t, s.db.DB.Exec(`
|
||||
CREATE TABLE last_control_address_writes(count integer);
|
||||
CREATE TRIGGER count_last_control_address_writes
|
||||
AFTER UPDATE OF last_control_address ON nodes
|
||||
BEGIN
|
||||
INSERT INTO last_control_address_writes VALUES(1);
|
||||
END;
|
||||
`).Error)
|
||||
|
||||
changed, err := s.SetLastControlAddress(nodeID, test.addr)
|
||||
require.NoError(t, err)
|
||||
require.True(t, changed)
|
||||
|
||||
var writes int64
|
||||
require.NoError(t, s.db.DB.Model(&struct{ Count int }{}).
|
||||
Table("last_control_address_writes").Count(&writes).Error)
|
||||
require.Equal(t, int64(1), writes)
|
||||
|
||||
changed, err = s.SetLastControlAddress(nodeID, test.addr)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, changed, "the same address must be a no-op")
|
||||
|
||||
require.NoError(t, s.db.DB.Model(&struct{ Count int }{}).
|
||||
Table("last_control_address_writes").Count(&writes).Error)
|
||||
assert.Equal(t, int64(1), writes,
|
||||
"the same address must not issue another database update")
|
||||
require.NoError(t, s.db.DB.Exec(`
|
||||
DROP TRIGGER count_last_control_address_writes;
|
||||
DROP TABLE last_control_address_writes;
|
||||
`).Error)
|
||||
require.NoError(t, s.SetLastControlAddress(nodeID, netip.MustParseAddr(test.addr)))
|
||||
require.NoError(t, s.SetLastControlAddress(nodeID, netip.MustParseAddr(test.addr)))
|
||||
|
||||
nv, ok := s.GetNodeByID(nodeID)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, want, nv.LastControlAddress().Get())
|
||||
|
||||
_, err = s.UpdateNodeFromMapRequest(nodeID, tailcfg.MapRequest{
|
||||
_, err := s.UpdateNodeFromMapRequest(nodeID, tailcfg.MapRequest{
|
||||
NodeKey: nv.NodeKey(),
|
||||
Hostinfo: &tailcfg.Hostinfo{Hostname: "updated-after-control-address"},
|
||||
})
|
||||
@@ -370,7 +347,7 @@ DROP TABLE last_control_address_writes;
|
||||
stored, err := s.DB().GetNodeByID(nodeID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, stored.LastControlAddress)
|
||||
assert.Equal(t, test.addr, *stored.LastControlAddress,
|
||||
assert.Equal(t, want, *stored.LastControlAddress,
|
||||
"an unrelated node update must preserve the address")
|
||||
|
||||
require.NoError(t, s.Close())
|
||||
@@ -379,11 +356,24 @@ DROP TABLE last_control_address_writes;
|
||||
reloaded, ok := s2.GetNodeByID(nodeID)
|
||||
require.True(t, ok)
|
||||
require.True(t, reloaded.LastControlAddress().Valid())
|
||||
assert.Equal(t, test.addr, reloaded.LastControlAddress().Get())
|
||||
assert.Equal(t, want, reloaded.LastControlAddress().Get())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetLastControlAddressRejects(t *testing.T) {
|
||||
_, s, nodeID := persistTestSetup(t)
|
||||
|
||||
require.ErrorIs(t, s.SetLastControlAddress(nodeID, netip.Addr{}),
|
||||
ErrInvalidLastControlAddress)
|
||||
require.ErrorIs(t, s.SetLastControlAddress(nodeID+999, netip.MustParseAddr("192.0.2.1")),
|
||||
ErrNodeNotInNodeStore)
|
||||
|
||||
stored, err := s.DB().GetNodeByID(nodeID)
|
||||
require.NoError(t, err, "a rejected address must leave the node readable")
|
||||
assert.Nil(t, stored.LastControlAddress)
|
||||
}
|
||||
|
||||
// TestRegistrationRejectsNodeKeyClaimedByAnotherMachine proves a new
|
||||
// registration cannot claim a NodeKey already bound to a different machine.
|
||||
// NodeKeys are public (peers learn them from the netmap), so without this
|
||||
@@ -2889,3 +2879,26 @@ func TestUserNameCollisionDoesNotBlockCreationOrRestart(t *testing.T) {
|
||||
require.Len(t, users, 3)
|
||||
require.Empty(t, reopened.polMan.NodeCapMap(node.ID))
|
||||
}
|
||||
|
||||
// TestSetLastControlAddressWritesNodeStoreOutsidePersistMu holds persistMu
|
||||
// and expects the NodeStore write to land anyway: every map request calls
|
||||
// SetLastControlAddress, and waiting for a NodeStore batch under the lock
|
||||
// would serialise all nodes' persists behind one batch interval each.
|
||||
func TestSetLastControlAddressWritesNodeStoreOutsidePersistMu(t *testing.T) {
|
||||
_, s, nodeID := persistTestSetup(t)
|
||||
addr := netip.MustParseAddr("203.0.113.10")
|
||||
|
||||
s.persistMu.Lock()
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- s.SetLastControlAddress(nodeID, addr) }()
|
||||
|
||||
assert.Eventually(t, func() bool {
|
||||
nv, ok := s.GetNodeByID(nodeID)
|
||||
|
||||
return ok && nv.LastControlAddress().GetOr(netip.Addr{}) == addr
|
||||
}, 5*time.Second, 10*time.Millisecond)
|
||||
|
||||
s.persistMu.Unlock()
|
||||
require.NoError(t, <-done)
|
||||
}
|
||||
+17
-35
@@ -132,56 +132,38 @@ var nodeUpdateColumns = []string{
|
||||
"UpdatedAt",
|
||||
}
|
||||
|
||||
// SetLastControlAddress stores the source IP most recently observed on an
|
||||
// authenticated control connection. The update is deliberately metadata-only:
|
||||
// it does not rescan policy or emit a peer-map change. Repeated observations of
|
||||
// the same address do not touch either the NodeStore or database.
|
||||
func (s *State) SetLastControlAddress(id types.NodeID, addr netip.Addr) (bool, error) {
|
||||
// SetLastControlAddress stores the source IP of a node's latest authenticated
|
||||
// control connection. Every map request calls it, so it takes persistMu only
|
||||
// for the database write, and an unchanged address writes nothing.
|
||||
func (s *State) SetLastControlAddress(id types.NodeID, addr netip.Addr) error {
|
||||
// A zero address would be stored as text that no longer parses, failing
|
||||
// every later read of the node.
|
||||
if !addr.IsValid() {
|
||||
return false, ErrInvalidLastControlAddress
|
||||
return ErrInvalidLastControlAddress
|
||||
}
|
||||
|
||||
addr = addr.Unmap()
|
||||
// The zone names the server's interface, or is header text from a proxy.
|
||||
addr = addr.Unmap().WithZone("")
|
||||
|
||||
s.persistMu.Lock()
|
||||
defer s.persistMu.Unlock()
|
||||
|
||||
current, ok := s.nodeStore.GetNode(id)
|
||||
node, ok := s.nodeStore.GetNode(id)
|
||||
if !ok {
|
||||
return false, fmt.Errorf("%w: %d", ErrNodeNotInNodeStore, id)
|
||||
return fmt.Errorf("%w: %d", ErrNodeNotInNodeStore, id)
|
||||
}
|
||||
|
||||
previous := current.LastControlAddress()
|
||||
if previous.Valid() && previous.Get() == addr {
|
||||
return false, nil
|
||||
if node.LastControlAddress().GetOr(netip.Addr{}) == addr {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, ok = s.nodeStore.UpdateNode(id, func(n *types.Node) {
|
||||
node, ok = s.nodeStore.UpdateNode(id, func(n *types.Node) {
|
||||
n.LastControlAddress = &addr
|
||||
})
|
||||
if !ok {
|
||||
return false, fmt.Errorf("%w: %d", ErrNodeNotInNodeStore, id)
|
||||
return fmt.Errorf("%w: %d", ErrNodeNotInNodeStore, id)
|
||||
}
|
||||
|
||||
err := s.db.SetLastControlAddress(id, addr)
|
||||
if err != nil {
|
||||
// Keep the in-memory source of truth aligned with the failed database
|
||||
// write so a later observation of the same address retries persistence.
|
||||
var old *netip.Addr
|
||||
_, err := s.persistNodeWith(node, []string{"LastControlAddress"}, nil)
|
||||
|
||||
if previous.Valid() {
|
||||
addr := previous.Get()
|
||||
old = &addr
|
||||
}
|
||||
|
||||
s.nodeStore.UpdateNode(id, func(n *types.Node) {
|
||||
n.LastControlAddress = old
|
||||
})
|
||||
|
||||
return false, fmt.Errorf("persisting last control address: %w", err)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
return err
|
||||
}
|
||||
|
||||
// ErrRegistrationExpired is returned when a registration has expired.
|
||||
|
||||
Reference in new issue
Block a user