wgengine/magicsock,control/controlclient: do not overwrite discokey with old key (#18606)
When a client starts up without being able to connect to control, it sends its discoKey to other nodes it wants to communicate with over TSMP. This disco key will be a newer key than the one control knows about. If the client that can connect to control gets a full netmap, ensure that the disco key for the node not connected to control is not overwritten with the stale key control knows about. This is implemented through keeping track of mapSession and use that for the discokey injection if it is available. This ensures that we are not constantly resetting the wireguard connection when getting the wrong keys from control. This is implemented as: - If the key is received via TSMP: - Set lastSeen for the peer to now() - Set online for the peer to false - When processing new keys, only accept keys where either: - Peer is online - lastSeen is newer than existing last seen If mapSession is not available, as in we are not yet connected to control, punt down the disco key injection to magicsock. Ideally, we will want to have mapSession be long lived at some point in the near future so we only need to inject keys in one location and then also use that for testing and loading the cache, but that is a yak for another PR. Updates #12639 Signed-off-by: Claus Lensbøl <claus@tailscale.com>
This commit is contained in:
@@ -623,6 +623,90 @@ func TestNetmapForResponse(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdateDiscoForNode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
initialOnline bool
|
||||
initialLastSeen time.Time
|
||||
updateOnline bool
|
||||
updateLastSeen time.Time
|
||||
wantUpdate bool
|
||||
}{
|
||||
{
|
||||
name: "newer_key_not_online",
|
||||
initialOnline: true,
|
||||
initialLastSeen: time.Unix(1, 0),
|
||||
updateOnline: false,
|
||||
updateLastSeen: time.Now(),
|
||||
wantUpdate: true,
|
||||
},
|
||||
{
|
||||
name: "newer_key_online",
|
||||
initialOnline: true,
|
||||
initialLastSeen: time.Unix(1, 0),
|
||||
updateOnline: true,
|
||||
updateLastSeen: time.Now(),
|
||||
wantUpdate: true,
|
||||
},
|
||||
{
|
||||
name: "older_key_not_online",
|
||||
initialOnline: false,
|
||||
initialLastSeen: time.Now(),
|
||||
updateOnline: false,
|
||||
updateLastSeen: time.Unix(1, 0),
|
||||
wantUpdate: false,
|
||||
},
|
||||
{
|
||||
name: "older_key_online",
|
||||
initialOnline: false,
|
||||
initialLastSeen: time.Now(),
|
||||
updateOnline: true,
|
||||
updateLastSeen: time.Unix(1, 0),
|
||||
wantUpdate: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
nu := &rememberLastNetmapUpdater{
|
||||
done: make(chan any),
|
||||
}
|
||||
ms := newTestMapSession(t, nu)
|
||||
|
||||
oldKey := key.NewDisco()
|
||||
|
||||
// Insert existing node
|
||||
node := tailcfg.Node{
|
||||
ID: 1,
|
||||
DiscoKey: oldKey.Public(),
|
||||
Online: &tt.initialOnline,
|
||||
LastSeen: &tt.initialLastSeen,
|
||||
}
|
||||
|
||||
if nm := ms.netmapForResponse(&tailcfg.MapResponse{
|
||||
Peers: []*tailcfg.Node{&node},
|
||||
}); len(nm.Peers) != 1 {
|
||||
t.Fatalf("node not inserted")
|
||||
}
|
||||
|
||||
newKey := key.NewDisco()
|
||||
ms.updateDiscoForNode(node.ID, newKey.Public(), tt.updateLastSeen, tt.updateOnline)
|
||||
<-nu.done
|
||||
|
||||
nm := ms.netmap()
|
||||
peerIdx := nm.PeerIndexByNodeID(node.ID)
|
||||
if peerIdx == -1 {
|
||||
t.Fatal("node not found")
|
||||
}
|
||||
|
||||
updated := nm.Peers[peerIdx].DiscoKey().Compare(newKey.Public()) == 0
|
||||
if updated != tt.wantUpdate {
|
||||
t.Fatalf("Disco key update: %t, wanted update: %t", updated, tt.wantUpdate)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func first[T any](s []T) T {
|
||||
if len(s) == 0 {
|
||||
var zero T
|
||||
@@ -1098,6 +1182,8 @@ func BenchmarkMapSessionDelta(b *testing.B) {
|
||||
ctx := context.Background()
|
||||
nu := &countingNetmapUpdater{}
|
||||
ms := newTestMapSession(b, nu)
|
||||
// Disable log output for benchmarks to avoid races
|
||||
ms.logf = func(string, ...any) {}
|
||||
res := &tailcfg.MapResponse{
|
||||
Node: &tailcfg.Node{
|
||||
ID: 1,
|
||||
|
||||
Reference in New Issue
Block a user