mirror of
https://github.com/netbirdio/netbird.git
synced 2026-09-14 18:59:08 +02:00
Merge branch 'main' into embedded-vnc
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/netbirdio/netbird/shared/management/proto"
|
||||
)
|
||||
|
||||
// InfoSource gathers the system info sent to management, keeping the posture
|
||||
// check results from the last Refresh for the cheap Current snapshots.
|
||||
type InfoSource struct {
|
||||
files atomic.Pointer[[]File]
|
||||
}
|
||||
|
||||
// Refresh gathers the info with the posture checks evaluated, bounded by timeout.
|
||||
func (s *InfoSource) Refresh(ctx context.Context, timeout time.Duration, checks []*proto.Checks, excludeIPs ...netip.Addr) (*Info, bool) {
|
||||
info, ok := GetInfoWithChecksTimeout(ctx, timeout, checks, excludeIPs...)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
files := slices.Clone(info.Files)
|
||||
s.files.Store(&files)
|
||||
return info, true
|
||||
}
|
||||
|
||||
// Current gathers the info without evaluating the checks, reusing the last Refresh results.
|
||||
func (s *InfoSource) Current(ctx context.Context, excludeIPs ...netip.Addr) *Info {
|
||||
info := GetInfo(ctx)
|
||||
info.removeAddresses(excludeIPs...)
|
||||
if files := s.files.Load(); files != nil {
|
||||
info.Files = *files
|
||||
}
|
||||
return info
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/netbirdio/netbird/shared/management/proto"
|
||||
)
|
||||
|
||||
func TestInfoSource_CurrentBeforeRefresh(t *testing.T) {
|
||||
var src InfoSource
|
||||
|
||||
info := src.Current(context.Background())
|
||||
|
||||
assert.Empty(t, info.Files)
|
||||
}
|
||||
|
||||
func TestInfoSource_CurrentReusesRefreshedFiles(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "agent")
|
||||
require.NoError(t, os.WriteFile(path, nil, 0o600))
|
||||
checks := []*proto.Checks{{Files: []string{path}}}
|
||||
|
||||
var src InfoSource
|
||||
refreshed, ok := src.Refresh(context.Background(), 15*time.Second, checks)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, []File{{Path: path, Exist: true}}, refreshed.Files)
|
||||
|
||||
info := src.Current(context.Background())
|
||||
|
||||
assert.Equal(t, refreshed.Files, info.Files)
|
||||
}
|
||||
|
||||
func TestInfoSource_CurrentExcludesAddresses(t *testing.T) {
|
||||
addrs := GetInfo(context.Background()).NetworkAddresses
|
||||
if len(addrs) == 0 {
|
||||
t.Skip("no network addresses on this host")
|
||||
}
|
||||
excluded := addrs[0].NetIP.Addr()
|
||||
matching := 0
|
||||
for _, addr := range addrs {
|
||||
if addr.NetIP.Addr() == excluded {
|
||||
matching++
|
||||
}
|
||||
}
|
||||
|
||||
var src InfoSource
|
||||
info := src.Current(context.Background(), excluded)
|
||||
|
||||
assert.Len(t, info.NetworkAddresses, len(addrs)-matching)
|
||||
for _, addr := range info.NetworkAddresses {
|
||||
assert.NotEqual(t, excluded, addr.NetIP.Addr())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user