Add static system info

This commit is contained in:
Maycon Santos
2024-11-28 23:46:29 +01:00
parent c6641be94b
commit 07f0f9fdbd
13 changed files with 108 additions and 65 deletions

View File

@@ -61,6 +61,14 @@ type Info struct {
Files []File // for posture checks
}
// StaticInfo is an object that contains machine information that does not change
type StaticInfo struct {
SystemSerialNumber string
SystemProductName string
SystemManufacturer string
Environment Environment
}
// extractUserAgent extracts Netbird's agent (client) name and version from the outgoing context
func extractUserAgent(ctx context.Context) string {
md, hasMeta := metadata.FromOutgoingContext(ctx)
@@ -142,7 +150,7 @@ func isDuplicated(addresses []NetworkAddress, addr NetworkAddress) bool {
}
// GetInfoWithChecks retrieves and parses the system information with applied checks.
func GetInfoWithChecks(ctx context.Context, checks []*proto.Checks) (*Info, error) {
func GetInfoWithChecks(ctx context.Context, checks []*proto.Checks, staticInfo *StaticInfo) (*Info, error) {
processCheckPaths := make([]string, 0)
for _, check := range checks {
processCheckPaths = append(processCheckPaths, check.GetFiles()...)
@@ -153,8 +161,17 @@ func GetInfoWithChecks(ctx context.Context, checks []*proto.Checks) (*Info, erro
return nil, err
}
info := GetInfo(ctx)
info := GetInfo(ctx, staticInfo)
info.Files = files
return info, nil
}
// GetStaticInfoInBackground retrieves and parses the system information in the background
func GetStaticInfoInBackground(ctx context.Context) <-chan *StaticInfo {
ch := make(chan *StaticInfo)
go func() {
ch <- getStaticInfo(ctx)
}()
return ch
}

View File

@@ -21,7 +21,7 @@ import (
)
// GetInfo retrieves and parses the system information
func GetInfo(ctx context.Context) *Info {
func GetInfo(ctx context.Context, staticInfo *StaticInfo) *Info {
utsname := unix.Utsname{}
err := unix.Uname(&utsname)
if err != nil {
@@ -41,26 +41,22 @@ func GetInfo(ctx context.Context) *Info {
log.Warnf("failed to discover network addresses: %s", err)
}
serialNum, prodName, manufacturer := sysInfo()
env := Environment{
Cloud: detect_cloud.Detect(ctx),
Platform: detect_platform.Detect(ctx),
gio := &Info{
Kernel: sysName,
OSVersion: strings.TrimSpace(string(swVersion)),
Platform: machine,
OS: sysName,
GoOS: runtime.GOOS,
CPUs: runtime.NumCPU(),
KernelVersion: release,
NetworkAddresses: addrs,
}
gio := &Info{
Kernel: sysName,
OSVersion: strings.TrimSpace(string(swVersion)),
Platform: machine,
OS: sysName,
GoOS: runtime.GOOS,
CPUs: runtime.NumCPU(),
KernelVersion: release,
NetworkAddresses: addrs,
SystemSerialNumber: serialNum,
SystemProductName: prodName,
SystemManufacturer: manufacturer,
Environment: env,
if staticInfo != nil {
gio.SystemSerialNumber = staticInfo.SystemSerialNumber
gio.SystemProductName = staticInfo.SystemProductName
gio.SystemManufacturer = staticInfo.SystemManufacturer
gio.Environment = staticInfo.Environment
}
systemHostname, _ := os.Hostname()
@@ -71,6 +67,21 @@ func GetInfo(ctx context.Context) *Info {
return gio
}
func getStaticInfo(ctx context.Context) *StaticInfo {
serialNum, prodName, manufacturer := sysInfo()
env := Environment{
Cloud: detect_cloud.Detect(ctx),
Platform: detect_platform.Detect(ctx),
}
return &StaticInfo{
SystemSerialNumber: serialNum,
SystemProductName: prodName,
SystemManufacturer: manufacturer,
Environment: env,
}
}
func sysInfo() (serialNumber string, productName string, manufacturer string) {
out, _ := exec.Command("/usr/sbin/ioreg", "-l").Output() // err ignored for brevity
for _, l := range strings.Split(string(out), "\n") {

View File

@@ -9,7 +9,7 @@ import (
)
func Test_LocalWTVersion(t *testing.T) {
got := GetInfo(context.TODO())
got := GetInfo(context.TODO(), nil)
want := "development"
assert.Equal(t, want, got.WiretrusteeVersion)
}
@@ -21,7 +21,7 @@ func Test_UIVersion(t *testing.T) {
"user-agent": {want},
})
got := GetInfo(ctx)
got := GetInfo(ctx, nil)
assert.Equal(t, want, got.UIVersion)
}
@@ -30,7 +30,7 @@ func Test_CustomHostname(t *testing.T) {
ctx := context.WithValue(context.Background(), DeviceNameCtxKey, "custom-host")
want := "custom-host"
got := GetInfo(ctx)
got := GetInfo(ctx, nil)
assert.Equal(t, want, got.Hostname)
}