mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-21 12:59:08 +02:00
[management] Persist peer flags in meta updates (#3958)
This PR adds persistence for peer feature flags when updating metadata, including equality checks, gRPC extraction, and corresponding unit tests. - Introduce a new `Flags` struct with `isEqual` and incorporate it into `PeerSystemMeta`. - Update `UpdateMetaIfNew` logic to consider flag changes. - Extend gRPC server’s `extractPeerMeta` to populate `Flags` and add tests for `Flags.isEqual`.
This commit is contained in:
@@ -392,6 +392,18 @@ func extractPeerMeta(ctx context.Context, meta *proto.PeerSystemMeta) nbpeer.Pee
|
|||||||
Cloud: meta.GetEnvironment().GetCloud(),
|
Cloud: meta.GetEnvironment().GetCloud(),
|
||||||
Platform: meta.GetEnvironment().GetPlatform(),
|
Platform: meta.GetEnvironment().GetPlatform(),
|
||||||
},
|
},
|
||||||
|
Flags: nbpeer.Flags{
|
||||||
|
RosenpassEnabled: meta.GetFlags().GetRosenpassEnabled(),
|
||||||
|
RosenpassPermissive: meta.GetFlags().GetRosenpassPermissive(),
|
||||||
|
ServerSSHAllowed: meta.GetFlags().GetServerSSHAllowed(),
|
||||||
|
DisableClientRoutes: meta.GetFlags().GetDisableClientRoutes(),
|
||||||
|
DisableServerRoutes: meta.GetFlags().GetDisableServerRoutes(),
|
||||||
|
DisableDNS: meta.GetFlags().GetDisableDNS(),
|
||||||
|
DisableFirewall: meta.GetFlags().GetDisableFirewall(),
|
||||||
|
BlockLANAccess: meta.GetFlags().GetBlockLANAccess(),
|
||||||
|
BlockInbound: meta.GetFlags().GetBlockInbound(),
|
||||||
|
LazyConnectionEnabled: meta.GetFlags().GetLazyConnectionEnabled(),
|
||||||
|
},
|
||||||
Files: files,
|
Files: files,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,6 +94,22 @@ type File struct {
|
|||||||
ProcessIsRunning bool
|
ProcessIsRunning bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flags defines a set of options to control feature behavior
|
||||||
|
type Flags struct {
|
||||||
|
RosenpassEnabled bool
|
||||||
|
RosenpassPermissive bool
|
||||||
|
ServerSSHAllowed bool
|
||||||
|
|
||||||
|
DisableClientRoutes bool
|
||||||
|
DisableServerRoutes bool
|
||||||
|
DisableDNS bool
|
||||||
|
DisableFirewall bool
|
||||||
|
BlockLANAccess bool
|
||||||
|
BlockInbound bool
|
||||||
|
|
||||||
|
LazyConnectionEnabled bool
|
||||||
|
}
|
||||||
|
|
||||||
// PeerSystemMeta is a metadata of a Peer machine system
|
// PeerSystemMeta is a metadata of a Peer machine system
|
||||||
type PeerSystemMeta struct { //nolint:revive
|
type PeerSystemMeta struct { //nolint:revive
|
||||||
Hostname string
|
Hostname string
|
||||||
@@ -111,6 +127,7 @@ type PeerSystemMeta struct { //nolint:revive
|
|||||||
SystemProductName string
|
SystemProductName string
|
||||||
SystemManufacturer string
|
SystemManufacturer string
|
||||||
Environment Environment `gorm:"serializer:json"`
|
Environment Environment `gorm:"serializer:json"`
|
||||||
|
Flags Flags `gorm:"serializer:json"`
|
||||||
Files []File `gorm:"serializer:json"`
|
Files []File `gorm:"serializer:json"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,7 +172,8 @@ func (p PeerSystemMeta) isEqual(other PeerSystemMeta) bool {
|
|||||||
p.SystemProductName == other.SystemProductName &&
|
p.SystemProductName == other.SystemProductName &&
|
||||||
p.SystemManufacturer == other.SystemManufacturer &&
|
p.SystemManufacturer == other.SystemManufacturer &&
|
||||||
p.Environment.Cloud == other.Environment.Cloud &&
|
p.Environment.Cloud == other.Environment.Cloud &&
|
||||||
p.Environment.Platform == other.Environment.Platform
|
p.Environment.Platform == other.Environment.Platform &&
|
||||||
|
p.Flags.isEqual(other.Flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PeerSystemMeta) isEmpty() bool {
|
func (p PeerSystemMeta) isEmpty() bool {
|
||||||
@@ -315,3 +333,16 @@ func (p *Peer) UpdateLastLogin() *Peer {
|
|||||||
p.Status = newStatus
|
p.Status = newStatus
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f Flags) isEqual(other Flags) bool {
|
||||||
|
return f.RosenpassEnabled == other.RosenpassEnabled &&
|
||||||
|
f.RosenpassPermissive == other.RosenpassPermissive &&
|
||||||
|
f.ServerSSHAllowed == other.ServerSSHAllowed &&
|
||||||
|
f.DisableClientRoutes == other.DisableClientRoutes &&
|
||||||
|
f.DisableServerRoutes == other.DisableServerRoutes &&
|
||||||
|
f.DisableDNS == other.DisableDNS &&
|
||||||
|
f.DisableFirewall == other.DisableFirewall &&
|
||||||
|
f.BlockLANAccess == other.BlockLANAccess &&
|
||||||
|
f.BlockInbound == other.BlockInbound &&
|
||||||
|
f.LazyConnectionEnabled == other.LazyConnectionEnabled
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FQDNOld is the original implementation for benchmarking purposes
|
// FQDNOld is the original implementation for benchmarking purposes
|
||||||
@@ -83,3 +85,59 @@ func TestIsEqual(t *testing.T) {
|
|||||||
t.Error("meta1 should be equal to meta2")
|
t.Error("meta1 should be equal to meta2")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFlags_IsEqual(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
f1 Flags
|
||||||
|
f2 Flags
|
||||||
|
expect bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "should be equal when all fields are identical",
|
||||||
|
f1: Flags{
|
||||||
|
RosenpassEnabled: true, RosenpassPermissive: false, ServerSSHAllowed: true,
|
||||||
|
DisableClientRoutes: false, DisableServerRoutes: true, DisableDNS: false,
|
||||||
|
DisableFirewall: true, BlockLANAccess: false, BlockInbound: true, LazyConnectionEnabled: true,
|
||||||
|
},
|
||||||
|
f2: Flags{
|
||||||
|
RosenpassEnabled: true, RosenpassPermissive: false, ServerSSHAllowed: true,
|
||||||
|
DisableClientRoutes: false, DisableServerRoutes: true, DisableDNS: false,
|
||||||
|
DisableFirewall: true, BlockLANAccess: false, BlockInbound: true, LazyConnectionEnabled: true,
|
||||||
|
},
|
||||||
|
expect: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "shouldn't be equal when fields are different",
|
||||||
|
f1: Flags{
|
||||||
|
RosenpassEnabled: true, RosenpassPermissive: false, ServerSSHAllowed: true,
|
||||||
|
DisableClientRoutes: false, DisableServerRoutes: true, DisableDNS: false,
|
||||||
|
DisableFirewall: true, BlockLANAccess: false, BlockInbound: true, LazyConnectionEnabled: true,
|
||||||
|
},
|
||||||
|
f2: Flags{
|
||||||
|
RosenpassEnabled: false, RosenpassPermissive: true, ServerSSHAllowed: false,
|
||||||
|
DisableClientRoutes: true, DisableServerRoutes: false, DisableDNS: true,
|
||||||
|
DisableFirewall: false, BlockLANAccess: true, BlockInbound: false, LazyConnectionEnabled: false,
|
||||||
|
},
|
||||||
|
expect: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "should be equal when both are empty",
|
||||||
|
f1: Flags{},
|
||||||
|
f2: Flags{},
|
||||||
|
expect: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "shouldn't be equal when at least one field differs",
|
||||||
|
f1: Flags{RosenpassEnabled: true},
|
||||||
|
f2: Flags{RosenpassEnabled: false},
|
||||||
|
expect: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
require.Equal(t, tt.expect, tt.f1.isEqual(tt.f2))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user