From d0bfb717a0c8e4ec434d6d938e7a041a529b4b8a Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Fri, 29 Nov 2024 17:55:34 +0100 Subject: [PATCH 1/9] Get static system info once Get static system info once for Windows, Darwin and Linux nodes This should improve startup and peer authentication times --- client/system/info.go | 8 ++++ client/system/info_darwin.go | 52 +++++++++++++++++---- client/system/info_linux.go | 53 +++++++++++++++++----- client/system/info_windows.go | 85 ++++++++++++++++++++++++++--------- 4 files changed, 157 insertions(+), 41 deletions(-) diff --git a/client/system/info.go b/client/system/info.go index 2af2e637b92..200d835df31 100644 --- a/client/system/info.go +++ b/client/system/info.go @@ -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) diff --git a/client/system/info_darwin.go b/client/system/info_darwin.go index 6f4ed173b4b..c3854308c4c 100644 --- a/client/system/info_darwin.go +++ b/client/system/info_darwin.go @@ -10,6 +10,8 @@ import ( "os/exec" "runtime" "strings" + "sync" + "time" "golang.org/x/sys/unix" @@ -20,6 +22,39 @@ import ( "github.com/netbirdio/netbird/version" ) +var ( + staticInfo StaticInfo + once sync.Once +) + +func init() { + go func() { + _ = updateStaticInfo() + }() +} + +func updateStaticInfo() StaticInfo { + once.Do(func() { + ctx := context.Background() + wg := sync.WaitGroup{} + wg.Add(3) + go func() { + staticInfo.SystemSerialNumber, staticInfo.SystemProductName, staticInfo.SystemManufacturer = sysInfo() + wg.Done() + }() + go func() { + staticInfo.Environment.Cloud = detect_cloud.Detect(ctx) + wg.Done() + }() + go func() { + staticInfo.Environment.Platform = detect_platform.Detect(ctx) + wg.Done() + }() + wg.Wait() + }) + return staticInfo +} + // GetInfo retrieves and parses the system information func GetInfo(ctx context.Context) *Info { utsname := unix.Utsname{} @@ -41,11 +76,10 @@ 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), + start := time.Now() + si := updateStaticInfo() + if time.Since(start) > 1*time.Second { + log.Infof("updateStaticInfo took %s", time.Since(start)) } gio := &Info{ @@ -57,10 +91,10 @@ func GetInfo(ctx context.Context) *Info { CPUs: runtime.NumCPU(), KernelVersion: release, NetworkAddresses: addrs, - SystemSerialNumber: serialNum, - SystemProductName: prodName, - SystemManufacturer: manufacturer, - Environment: env, + SystemSerialNumber: si.SystemSerialNumber, + SystemProductName: si.SystemProductName, + SystemManufacturer: si.SystemManufacturer, + Environment: si.Environment, } systemHostname, _ := os.Hostname() diff --git a/client/system/info_linux.go b/client/system/info_linux.go index b6a142bce28..1fa5d96d6de 100644 --- a/client/system/info_linux.go +++ b/client/system/info_linux.go @@ -11,6 +11,7 @@ import ( "regexp" "runtime" "strings" + "sync" "time" log "github.com/sirupsen/logrus" @@ -21,6 +22,40 @@ import ( "github.com/netbirdio/netbird/version" ) +var ( + staticInfo StaticInfo + once sync.Once +) + +func init() { + go func() { + _ = updateStaticInfo() + }() +} + +func updateStaticInfo() StaticInfo { + once.Do(func() { + ctx := context.Background() + wg := sync.WaitGroup{} + wg.Add(3) + go func() { + wrapper := SysInfoWrapper{} + staticInfo.SystemSerialNumber, staticInfo.SystemProductName, staticInfo.SystemManufacturer = sysInfo(wrapper.GetSysInfo()) + wg.Done() + }() + go func() { + staticInfo.Environment.Cloud = detect_cloud.Detect(ctx) + wg.Done() + }() + go func() { + staticInfo.Environment.Platform = detect_platform.Detect(ctx) + wg.Done() + }() + wg.Wait() + }) + return staticInfo +} + type SysInfoGetter interface { GetSysInfo() SysInfo } @@ -65,12 +100,10 @@ func GetInfo(ctx context.Context) *Info { log.Warnf("failed to discover network addresses: %s", err) } - si := SysInfoWrapper{} - serialNum, prodName, manufacturer := sysInfo(si.GetSysInfo()) - - env := Environment{ - Cloud: detect_cloud.Detect(ctx), - Platform: detect_platform.Detect(ctx), + start := time.Now() + si := updateStaticInfo() + if time.Since(start) > 1*time.Second { + log.Infof("updateStaticInfo took %s", time.Since(start)) } gio := &Info{ @@ -85,10 +118,10 @@ func GetInfo(ctx context.Context) *Info { UIVersion: extractUserAgent(ctx), KernelVersion: osInfo[1], NetworkAddresses: addrs, - SystemSerialNumber: serialNum, - SystemProductName: prodName, - SystemManufacturer: manufacturer, - Environment: env, + SystemSerialNumber: si.SystemSerialNumber, + SystemProductName: si.SystemProductName, + SystemManufacturer: si.SystemManufacturer, + Environment: si.Environment, } return gio diff --git a/client/system/info_windows.go b/client/system/info_windows.go index 68631fe164d..29aa16a2e1a 100644 --- a/client/system/info_windows.go +++ b/client/system/info_windows.go @@ -6,6 +6,8 @@ import ( "os" "runtime" "strings" + "sync" + "time" log "github.com/sirupsen/logrus" "github.com/yusufpapurcu/wmi" @@ -32,6 +34,39 @@ type Win32_BIOS struct { SerialNumber string } +var ( + staticInfo StaticInfo + once sync.Once +) + +func init() { + go func() { + _ = updateStaticInfo() + }() +} + +func updateStaticInfo() StaticInfo { + once.Do(func() { + ctx := context.Background() + wg := sync.WaitGroup{} + wg.Add(3) + go func() { + staticInfo.SystemSerialNumber, staticInfo.SystemProductName, staticInfo.SystemManufacturer = sysInfo() + wg.Done() + }() + go func() { + staticInfo.Environment.Cloud = detect_cloud.Detect(ctx) + wg.Done() + }() + go func() { + staticInfo.Environment.Platform = detect_platform.Detect(ctx) + wg.Done() + }() + wg.Wait() + }) + return staticInfo +} + // GetInfo retrieves and parses the system information func GetInfo(ctx context.Context) *Info { osName, osVersion := getOSNameAndVersion() @@ -42,24 +77,10 @@ func GetInfo(ctx context.Context) *Info { log.Warnf("failed to discover network addresses: %s", err) } - serialNum, err := sysNumber() - if err != nil { - log.Warnf("failed to get system serial number: %s", err) - } - - prodName, err := sysProductName() - if err != nil { - log.Warnf("failed to get system product name: %s", err) - } - - manufacturer, err := sysManufacturer() - if err != nil { - log.Warnf("failed to get system manufacturer: %s", err) - } - - env := Environment{ - Cloud: detect_cloud.Detect(ctx), - Platform: detect_platform.Detect(ctx), + start := time.Now() + si := updateStaticInfo() + if time.Since(start) > 1*time.Second { + log.Infof("updateStaticInfo took %s", time.Since(start)) } gio := &Info{ @@ -71,10 +92,10 @@ func GetInfo(ctx context.Context) *Info { CPUs: runtime.NumCPU(), KernelVersion: buildVersion, NetworkAddresses: addrs, - SystemSerialNumber: serialNum, - SystemProductName: prodName, - SystemManufacturer: manufacturer, - Environment: env, + SystemSerialNumber: si.SystemSerialNumber, + SystemProductName: si.SystemProductName, + SystemManufacturer: si.SystemManufacturer, + Environment: si.Environment, } systemHostname, _ := os.Hostname() @@ -85,6 +106,26 @@ func GetInfo(ctx context.Context) *Info { return gio } +func sysInfo() (serialNumber string, productName string, manufacturer string) { + var err error + serialNumber, err = sysNumber() + if err != nil { + log.Warnf("failed to get system serial number: %s", err) + } + + productName, err = sysProductName() + if err != nil { + log.Warnf("failed to get system product name: %s", err) + } + + manufacturer, err = sysManufacturer() + if err != nil { + log.Warnf("failed to get system manufacturer: %s", err) + } + + return serialNumber, productName, manufacturer +} + func getOSNameAndVersion() (string, string) { var dst []Win32_OperatingSystem query := wmi.CreateQuery(&dst, "") From ca8d47737c9496e125032f38dbdf175c07f451a5 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Fri, 29 Nov 2024 18:27:27 +0100 Subject: [PATCH 2/9] decrease code duplication --- client/system/info_darwin.go | 36 -------------------- client/system/info_linux.go | 51 +++++------------------------ client/system/info_windows.go | 37 --------------------- client/system/static_info.go | 46 ++++++++++++++++++++++++++ client/system/sysinfo_linux_test.go | 3 +- 5 files changed, 56 insertions(+), 117 deletions(-) create mode 100644 client/system/static_info.go diff --git a/client/system/info_darwin.go b/client/system/info_darwin.go index c3854308c4c..d9f07fb6dae 100644 --- a/client/system/info_darwin.go +++ b/client/system/info_darwin.go @@ -10,51 +10,15 @@ import ( "os/exec" "runtime" "strings" - "sync" "time" "golang.org/x/sys/unix" log "github.com/sirupsen/logrus" - "github.com/netbirdio/netbird/client/system/detect_cloud" - "github.com/netbirdio/netbird/client/system/detect_platform" "github.com/netbirdio/netbird/version" ) -var ( - staticInfo StaticInfo - once sync.Once -) - -func init() { - go func() { - _ = updateStaticInfo() - }() -} - -func updateStaticInfo() StaticInfo { - once.Do(func() { - ctx := context.Background() - wg := sync.WaitGroup{} - wg.Add(3) - go func() { - staticInfo.SystemSerialNumber, staticInfo.SystemProductName, staticInfo.SystemManufacturer = sysInfo() - wg.Done() - }() - go func() { - staticInfo.Environment.Cloud = detect_cloud.Detect(ctx) - wg.Done() - }() - go func() { - staticInfo.Environment.Platform = detect_platform.Detect(ctx) - wg.Done() - }() - wg.Wait() - }) - return staticInfo -} - // GetInfo retrieves and parses the system information func GetInfo(ctx context.Context) *Info { utsname := unix.Utsname{} diff --git a/client/system/info_linux.go b/client/system/info_linux.go index 1fa5d96d6de..7b185dcff48 100644 --- a/client/system/info_linux.go +++ b/client/system/info_linux.go @@ -11,50 +11,15 @@ import ( "regexp" "runtime" "strings" - "sync" "time" log "github.com/sirupsen/logrus" "github.com/zcalusic/sysinfo" - "github.com/netbirdio/netbird/client/system/detect_cloud" - "github.com/netbirdio/netbird/client/system/detect_platform" "github.com/netbirdio/netbird/version" ) -var ( - staticInfo StaticInfo - once sync.Once -) - -func init() { - go func() { - _ = updateStaticInfo() - }() -} - -func updateStaticInfo() StaticInfo { - once.Do(func() { - ctx := context.Background() - wg := sync.WaitGroup{} - wg.Add(3) - go func() { - wrapper := SysInfoWrapper{} - staticInfo.SystemSerialNumber, staticInfo.SystemProductName, staticInfo.SystemManufacturer = sysInfo(wrapper.GetSysInfo()) - wg.Done() - }() - go func() { - staticInfo.Environment.Cloud = detect_cloud.Detect(ctx) - wg.Done() - }() - go func() { - staticInfo.Environment.Platform = detect_platform.Detect(ctx) - wg.Done() - }() - wg.Wait() - }) - return staticInfo -} +var sisInfoWrapper SysInfo type SysInfoGetter interface { GetSysInfo() SysInfo @@ -141,10 +106,10 @@ func _getInfo() string { return out.String() } -func sysInfo(si SysInfo) (string, string, string) { +func sysInfo() (string, string, string) { isascii := regexp.MustCompile("^[[:ascii:]]+$") - serials := []string{si.ChassisSerial, si.ProductSerial} + serials := []string{sisInfoWrapper.ChassisSerial, sisInfoWrapper.ProductSerial} serial := "" for _, s := range serials { @@ -156,12 +121,12 @@ func sysInfo(si SysInfo) (string, string, string) { } } - if serial == "" && isascii.MatchString(si.BoardSerial) { - serial = si.BoardSerial + if serial == "" && isascii.MatchString(sisInfoWrapper.BoardSerial) { + serial = sisInfoWrapper.BoardSerial } var name string - for _, n := range []string{si.ProductName, si.BoardName} { + for _, n := range []string{sisInfoWrapper.ProductName, sisInfoWrapper.BoardName} { if isascii.MatchString(n) { name = n break @@ -169,8 +134,8 @@ func sysInfo(si SysInfo) (string, string, string) { } var manufacturer string - if isascii.MatchString(si.ProductVendor) { - manufacturer = si.ProductVendor + if isascii.MatchString(sisInfoWrapper.ProductVendor) { + manufacturer = sisInfoWrapper.ProductVendor } return serial, name, manufacturer } diff --git a/client/system/info_windows.go b/client/system/info_windows.go index 29aa16a2e1a..943b162b68d 100644 --- a/client/system/info_windows.go +++ b/client/system/info_windows.go @@ -6,15 +6,11 @@ import ( "os" "runtime" "strings" - "sync" "time" log "github.com/sirupsen/logrus" - "github.com/yusufpapurcu/wmi" "golang.org/x/sys/windows/registry" - "github.com/netbirdio/netbird/client/system/detect_cloud" - "github.com/netbirdio/netbird/client/system/detect_platform" "github.com/netbirdio/netbird/version" ) @@ -34,39 +30,6 @@ type Win32_BIOS struct { SerialNumber string } -var ( - staticInfo StaticInfo - once sync.Once -) - -func init() { - go func() { - _ = updateStaticInfo() - }() -} - -func updateStaticInfo() StaticInfo { - once.Do(func() { - ctx := context.Background() - wg := sync.WaitGroup{} - wg.Add(3) - go func() { - staticInfo.SystemSerialNumber, staticInfo.SystemProductName, staticInfo.SystemManufacturer = sysInfo() - wg.Done() - }() - go func() { - staticInfo.Environment.Cloud = detect_cloud.Detect(ctx) - wg.Done() - }() - go func() { - staticInfo.Environment.Platform = detect_platform.Detect(ctx) - wg.Done() - }() - wg.Wait() - }) - return staticInfo -} - // GetInfo retrieves and parses the system information func GetInfo(ctx context.Context) *Info { osName, osVersion := getOSNameAndVersion() diff --git a/client/system/static_info.go b/client/system/static_info.go new file mode 100644 index 00000000000..c7b2ca84357 --- /dev/null +++ b/client/system/static_info.go @@ -0,0 +1,46 @@ +//go:build (linux && !android) || windows || darwin + +package system + +import ( + "context" + "sync" + "time" + + "github.com/netbirdio/netbird/client/system/detect_cloud" + "github.com/netbirdio/netbird/client/system/detect_platform" +) + +var ( + staticInfo StaticInfo + once sync.Once +) + +func init() { + go func() { + _ = updateStaticInfo() + }() +} + +func updateStaticInfo() StaticInfo { + once.Do(func() { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + wg := sync.WaitGroup{} + wg.Add(3) + go func() { + staticInfo.SystemSerialNumber, staticInfo.SystemProductName, staticInfo.SystemManufacturer = sysInfo() + wg.Done() + }() + go func() { + staticInfo.Environment.Cloud = detect_cloud.Detect(ctx) + wg.Done() + }() + go func() { + staticInfo.Environment.Platform = detect_platform.Detect(ctx) + wg.Done() + }() + wg.Wait() + }) + return staticInfo +} diff --git a/client/system/sysinfo_linux_test.go b/client/system/sysinfo_linux_test.go index f6a0b70587b..ee1a6ff44ab 100644 --- a/client/system/sysinfo_linux_test.go +++ b/client/system/sysinfo_linux_test.go @@ -183,7 +183,8 @@ func Test_sysInfo(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotSerialNum, gotProdName, gotManufacturer := sysInfo(tt.sysInfo) + sisInfoWrapper = tt.sysInfo + gotSerialNum, gotProdName, gotManufacturer := sysInfo() if gotSerialNum != tt.wantSerialNum { t.Errorf("sysInfo() gotSerialNum = %v, want %v", gotSerialNum, tt.wantSerialNum) } From 94c72f4f03cf443bebf2b14c0d69440e56a35305 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Fri, 29 Nov 2024 18:31:36 +0100 Subject: [PATCH 3/9] add wmi package back --- client/system/info_windows.go | 1 + 1 file changed, 1 insertion(+) diff --git a/client/system/info_windows.go b/client/system/info_windows.go index 943b162b68d..933f3aed5a1 100644 --- a/client/system/info_windows.go +++ b/client/system/info_windows.go @@ -9,6 +9,7 @@ import ( "time" log "github.com/sirupsen/logrus" + "github.com/yusufpapurcu/wmi" "golang.org/x/sys/windows/registry" "github.com/netbirdio/netbird/version" From a76c2a6fd7a41b0834d18f2148de39f3d6f5a8fa Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Fri, 29 Nov 2024 19:38:19 +0100 Subject: [PATCH 4/9] skip ios --- client/system/static_info.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/system/static_info.go b/client/system/static_info.go index c7b2ca84357..fabe65a6806 100644 --- a/client/system/static_info.go +++ b/client/system/static_info.go @@ -1,4 +1,4 @@ -//go:build (linux && !android) || windows || darwin +//go:build (linux && !android) || windows || (darwin && !ios) package system From d9ca4dfabf31e0b20cf3ba8a50c6b2019a98e19c Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Mon, 2 Dec 2024 18:13:07 +0100 Subject: [PATCH 5/9] fix SysInfo wrapper --- client/system/info_linux.go | 22 +++++++++++++--------- client/system/sysinfo_linux_test.go | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/client/system/info_linux.go b/client/system/info_linux.go index 7b185dcff48..b2363519fdb 100644 --- a/client/system/info_linux.go +++ b/client/system/info_linux.go @@ -19,17 +19,21 @@ import ( "github.com/netbirdio/netbird/version" ) -var sisInfoWrapper SysInfo +var sisInfoWrapper SysInfoWrapper type SysInfoGetter interface { GetSysInfo() SysInfo } type SysInfoWrapper struct { - si sysinfo.SysInfo + providedInfo *SysInfo + si sysinfo.SysInfo } func (s SysInfoWrapper) GetSysInfo() SysInfo { + if s.providedInfo != nil { + return *s.providedInfo + } s.si.GetSysInfo() return SysInfo{ ChassisSerial: s.si.Chassis.Serial, @@ -108,8 +112,8 @@ func _getInfo() string { func sysInfo() (string, string, string) { isascii := regexp.MustCompile("^[[:ascii:]]+$") - - serials := []string{sisInfoWrapper.ChassisSerial, sisInfoWrapper.ProductSerial} + si := sisInfoWrapper.GetSysInfo() + serials := []string{si.ChassisSerial, si.ProductSerial} serial := "" for _, s := range serials { @@ -121,12 +125,12 @@ func sysInfo() (string, string, string) { } } - if serial == "" && isascii.MatchString(sisInfoWrapper.BoardSerial) { - serial = sisInfoWrapper.BoardSerial + if serial == "" && isascii.MatchString(si.BoardSerial) { + serial = si.BoardSerial } var name string - for _, n := range []string{sisInfoWrapper.ProductName, sisInfoWrapper.BoardName} { + for _, n := range []string{si.ProductName, si.BoardName} { if isascii.MatchString(n) { name = n break @@ -134,8 +138,8 @@ func sysInfo() (string, string, string) { } var manufacturer string - if isascii.MatchString(sisInfoWrapper.ProductVendor) { - manufacturer = sisInfoWrapper.ProductVendor + if isascii.MatchString(si.ProductVendor) { + manufacturer = si.ProductVendor } return serial, name, manufacturer } diff --git a/client/system/sysinfo_linux_test.go b/client/system/sysinfo_linux_test.go index ee1a6ff44ab..9878ab3b453 100644 --- a/client/system/sysinfo_linux_test.go +++ b/client/system/sysinfo_linux_test.go @@ -183,7 +183,7 @@ func Test_sysInfo(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - sisInfoWrapper = tt.sysInfo + sisInfoWrapper.providedInfo = &tt.sysInfo gotSerialNum, gotProdName, gotManufacturer := sysInfo() if gotSerialNum != tt.wantSerialNum { t.Errorf("sysInfo() gotSerialNum = %v, want %v", gotSerialNum, tt.wantSerialNum) From d6288c3d6dfdf90e4a713b5010a330f0c148b680 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Mon, 2 Dec 2024 18:13:55 +0100 Subject: [PATCH 6/9] adjust log level --- client/system/info_darwin.go | 2 +- client/system/info_linux.go | 2 +- client/system/info_windows.go | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/client/system/info_darwin.go b/client/system/info_darwin.go index d9f07fb6dae..13b0a446bd3 100644 --- a/client/system/info_darwin.go +++ b/client/system/info_darwin.go @@ -43,7 +43,7 @@ func GetInfo(ctx context.Context) *Info { start := time.Now() si := updateStaticInfo() if time.Since(start) > 1*time.Second { - log.Infof("updateStaticInfo took %s", time.Since(start)) + log.Warnf("updateStaticInfo took %s", time.Since(start)) } gio := &Info{ diff --git a/client/system/info_linux.go b/client/system/info_linux.go index b2363519fdb..5f52e92e524 100644 --- a/client/system/info_linux.go +++ b/client/system/info_linux.go @@ -72,7 +72,7 @@ func GetInfo(ctx context.Context) *Info { start := time.Now() si := updateStaticInfo() if time.Since(start) > 1*time.Second { - log.Infof("updateStaticInfo took %s", time.Since(start)) + log.Warnf("updateStaticInfo took %s", time.Since(start)) } gio := &Info{ diff --git a/client/system/info_windows.go b/client/system/info_windows.go index 933f3aed5a1..eae9ac81df8 100644 --- a/client/system/info_windows.go +++ b/client/system/info_windows.go @@ -9,7 +9,6 @@ import ( "time" log "github.com/sirupsen/logrus" - "github.com/yusufpapurcu/wmi" "golang.org/x/sys/windows/registry" "github.com/netbirdio/netbird/version" @@ -44,7 +43,7 @@ func GetInfo(ctx context.Context) *Info { start := time.Now() si := updateStaticInfo() if time.Since(start) > 1*time.Second { - log.Infof("updateStaticInfo took %s", time.Since(start)) + log.Warnf("updateStaticInfo took %s", time.Since(start)) } gio := &Info{ From 2a58e5d1891dbcf5c22d648eace8ac31edabb58b Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Mon, 2 Dec 2024 18:30:07 +0100 Subject: [PATCH 7/9] add wmi import back --- client/system/info_windows.go | 1 + 1 file changed, 1 insertion(+) diff --git a/client/system/info_windows.go b/client/system/info_windows.go index eae9ac81df8..28bd3d3007c 100644 --- a/client/system/info_windows.go +++ b/client/system/info_windows.go @@ -9,6 +9,7 @@ import ( "time" log "github.com/sirupsen/logrus" + "github.com/yusufpapurcu/wmi" "golang.org/x/sys/windows/registry" "github.com/netbirdio/netbird/version" From 3ddeff1e5c54ebcdc21701447f010fe85bcb94f4 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Mon, 2 Dec 2024 19:00:48 +0100 Subject: [PATCH 8/9] remove unused interface --- client/system/info_linux.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/client/system/info_linux.go b/client/system/info_linux.go index 5f52e92e524..f0673b05739 100644 --- a/client/system/info_linux.go +++ b/client/system/info_linux.go @@ -21,10 +21,6 @@ import ( var sisInfoWrapper SysInfoWrapper -type SysInfoGetter interface { - GetSysInfo() SysInfo -} - type SysInfoWrapper struct { providedInfo *SysInfo si sysinfo.SysInfo From 6d536109191d2e6c2c9fb1894442d9d1df7ff203 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Mon, 2 Dec 2024 20:21:31 +0100 Subject: [PATCH 9/9] refactor getSystemInfo --- client/system/info_linux.go | 41 +++++++++++++---------------- client/system/sysinfo_linux_test.go | 4 ++- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/client/system/info_linux.go b/client/system/info_linux.go index f0673b05739..bfc77be1915 100644 --- a/client/system/info_linux.go +++ b/client/system/info_linux.go @@ -1,5 +1,4 @@ //go:build !android -// +build !android package system @@ -19,27 +18,10 @@ import ( "github.com/netbirdio/netbird/version" ) -var sisInfoWrapper SysInfoWrapper - -type SysInfoWrapper struct { - providedInfo *SysInfo - si sysinfo.SysInfo -} - -func (s SysInfoWrapper) GetSysInfo() SysInfo { - if s.providedInfo != nil { - return *s.providedInfo - } - s.si.GetSysInfo() - return SysInfo{ - ChassisSerial: s.si.Chassis.Serial, - ProductSerial: s.si.Product.Serial, - BoardSerial: s.si.Board.Serial, - ProductName: s.si.Product.Name, - BoardName: s.si.Board.Name, - ProductVendor: s.si.Product.Vendor, - } -} +var ( + // it is override in tests + getSystemInfo = defaultSysInfoImplementation +) // GetInfo retrieves and parses the system information func GetInfo(ctx context.Context) *Info { @@ -108,7 +90,7 @@ func _getInfo() string { func sysInfo() (string, string, string) { isascii := regexp.MustCompile("^[[:ascii:]]+$") - si := sisInfoWrapper.GetSysInfo() + si := getSystemInfo() serials := []string{si.ChassisSerial, si.ProductSerial} serial := "" @@ -139,3 +121,16 @@ func sysInfo() (string, string, string) { } return serial, name, manufacturer } + +func defaultSysInfoImplementation() SysInfo { + si := sysinfo.SysInfo{} + si.GetSysInfo() + return SysInfo{ + ChassisSerial: si.Chassis.Serial, + ProductSerial: si.Product.Serial, + BoardSerial: si.Board.Serial, + ProductName: si.Product.Name, + BoardName: si.Board.Name, + ProductVendor: si.Product.Vendor, + } +} diff --git a/client/system/sysinfo_linux_test.go b/client/system/sysinfo_linux_test.go index 9878ab3b453..ae89bfcf974 100644 --- a/client/system/sysinfo_linux_test.go +++ b/client/system/sysinfo_linux_test.go @@ -183,7 +183,9 @@ func Test_sysInfo(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - sisInfoWrapper.providedInfo = &tt.sysInfo + getSystemInfo = func() SysInfo { + return tt.sysInfo + } gotSerialNum, gotProdName, gotManufacturer := sysInfo() if gotSerialNum != tt.wantSerialNum { t.Errorf("sysInfo() gotSerialNum = %v, want %v", gotSerialNum, tt.wantSerialNum)