[management] Compensate for a possible nil peer when returning "empty" network map (#8285)

* we somehow can get a nil peer when returning empty components; this fix compensates for the issue, but doesn't fix the root cause (which is unknown atm)

Signed-off-by: Dmitri Dolguikh <dmitri.external@netbird.io>

* fix test

Signed-off-by: Dmitri Dolguikh <dmitri.external@netbird.io>

---------

Signed-off-by: Dmitri Dolguikh <dmitri.external@netbird.io>
This commit is contained in:
dmitri-netbird
2026-10-22 20:27:53 +02:00
committed by GitHub
parent 48ecdfbcfc
commit f548a74c7c
5 changed files with 9 additions and 3 deletions
@@ -933,7 +933,7 @@ func (c *Controller) GetValidatedPeerWithComponents(ctx context.Context, isRequi
} }
// it's possible that the peer gets deleted between the call to "sendInitialSync()" and here, bail out in this case // it's possible that the peer gets deleted between the call to "sendInitialSync()" and here, bail out in this case
if _, ok := account.Peers[peer.ID]; !ok { if p, _ := account.Peers[peer.ID]; p == nil {
return nil, nil, nil, 0, fmt.Errorf("peer '%s' no longer exists", peer.ID) return nil, nil, nil, 0, fmt.Errorf("peer '%s' no longer exists", peer.ID)
} }
@@ -20,6 +20,7 @@ const wgKeyRawLen = 32
// computed alongside the components in the network_map controller and reused // computed alongside the components in the network_map controller and reused
// from the legacy proto path) and the dns_domain string. // from the legacy proto path) and the dns_domain string.
type ComponentsEnvelopeInput struct { type ComponentsEnvelopeInput struct {
Peer *nmdata.Peer // only used when encoding "empty" component maps
Components *types.NetworkMapComponents Components *types.NetworkMapComponents
PeerConfig *proto.PeerConfig PeerConfig *proto.PeerConfig
DNSDomain string DNSDomain string
@@ -59,8 +60,8 @@ func EncodeNetworkMapEnvelope(in ComponentsEnvelopeInput) *proto.NetworkMapEnvel
Serial: networkSerial(c.Network), Serial: networkSerial(c.Network),
Network: toAccountNetwork(c.Network), Network: toAccountNetwork(c.Network),
PeerConfig: in.PeerConfig, PeerConfig: in.PeerConfig,
// components.Peers always contains the target peer // components.Peers may not contain the target peer
Peers: []*proto.PeerCompact{toPeerCompact(c.Peers[c.PeerID])}, Peers: []*proto.PeerCompact{toPeerCompact(in.Peer)},
DnsDomain: in.DNSDomain, DnsDomain: in.DNSDomain,
DnsForwarderPort: in.DNSForwarderPort, DnsForwarderPort: in.DNSForwarderPort,
UserIdClaim: in.UserIDClaim, UserIdClaim: in.UserIDClaim,
@@ -717,6 +717,7 @@ func TestEncodeNetworkMapEnvelope_NilComponentsGracefulDegrade(t *testing.T) {
// nil Components → minimal envelope, no crash. Matches the legacy // nil Components → minimal envelope, no crash. Matches the legacy
// behaviour for missing/unvalidated peers. // behaviour for missing/unvalidated peers.
env := EncodeNetworkMapEnvelope(ComponentsEnvelopeInput{ env := EncodeNetworkMapEnvelope(ComponentsEnvelopeInput{
Peer: &nmdata.Peer{ID: "missing-peer", SSHKey: "missing-peer-key"},
Components: emptyNetworkMapComponents(), Components: emptyNetworkMapComponents(),
DNSDomain: "netbird.cloud", DNSDomain: "netbird.cloud",
}) })
@@ -731,6 +732,8 @@ func TestEncodeNetworkMapEnvelope_NilComponentsGracefulDegrade(t *testing.T) {
require.NotNil(t, full.Network, "client runs Calculate() over the envelope and dereferences Network unconditionally; a nil here would crash the receiver") require.NotNil(t, full.Network, "client runs Calculate() over the envelope and dereferences Network unconditionally; a nil here would crash the receiver")
assert.Equal(t, "net-empty", full.Network.Identifier) assert.Equal(t, "net-empty", full.Network.Identifier)
assert.Equal(t, uint64(9), full.Serial) assert.Equal(t, uint64(9), full.Serial)
assert.Len(t, env.GetFull().Peers, 1)
assert.Equal(t, string(env.GetFull().Peers[0].SshPubKey), "missing-peer-key")
} }
func TestEncodeNetworkMapEnvelope_AccountSettingsAlwaysEmitted(t *testing.T) { func TestEncodeNetworkMapEnvelope_AccountSettingsAlwaysEmitted(t *testing.T) {
@@ -55,6 +55,7 @@ func ToComponentSyncResponse(
} }
envelope := EncodeNetworkMapEnvelope(ComponentsEnvelopeInput{ envelope := EncodeNetworkMapEnvelope(ComponentsEnvelopeInput{
Peer: peer,
Components: components, Components: components,
PeerConfig: peerConfig, PeerConfig: peerConfig,
DNSDomain: dnsName, DNSDomain: dnsName,
@@ -244,6 +244,7 @@ func TestEnvelopeToNetworkMap_EmptyComponents(t *testing.T) {
}) })
envelope := mgmtgrpc.EncodeNetworkMapEnvelope(mgmtgrpc.ComponentsEnvelopeInput{ envelope := mgmtgrpc.EncodeNetworkMapEnvelope(mgmtgrpc.ComponentsEnvelopeInput{
Peer: &nmdata.Peer{ID: "peer-A", Key: localPeerKey, IP: netip.AddrFrom4([4]byte{100, 64, 0, 1})},
Components: c, Components: c,
DNSDomain: "netbird.cloud", DNSDomain: "netbird.cloud",
}) })