Skip to content

Commit

Permalink
🧹 cache windows smbios response (#4896)
Browse files Browse the repository at this point in the history
* 🧹 cache windows smbios response

Signed-off-by: Ivan Milchev <[email protected]>

* fix build

Signed-off-by: Ivan Milchev <[email protected]>

---------

Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Nov 20, 2024
1 parent c36f241 commit d46d864
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 30 deletions.
8 changes: 2 additions & 6 deletions providers/os/id/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func readValue(conn shared.Connection, fPath string) string {
return string(content)
}

func Detect(conn shared.Connection, p *inventory.Platform) (string, string, []string) {
func Detect(conn shared.Connection, p *inventory.Platform, smbiosMgr smbios.SmBiosManager) (string, string, []string) {
var values []string
if p.IsFamily("linux") {
// Fetching the data from the smbios manager is slow for some transports
Expand All @@ -38,11 +38,7 @@ func Detect(conn shared.Connection, p *inventory.Platform) (string, string, []st
readValue(conn, "/sys/class/dmi/id/bios_vendor"),
}
} else {
mgr, err := smbios.ResolveManager(conn, p)
if err != nil {
return "", "", nil
}
info, err := mgr.Info()
info, err := smbiosMgr.Info()
if err != nil {
log.Debug().Err(err).Msg("failed to query smbios")
return "", "", nil
Expand Down
21 changes: 17 additions & 4 deletions providers/os/id/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers/os/connection/mock"
"go.mondoo.com/cnquery/v11/providers/os/detector"
"go.mondoo.com/cnquery/v11/providers/os/resources/smbios"
)

func TestDetectInstance(t *testing.T) {
Expand All @@ -19,7 +20,10 @@ func TestDetectInstance(t *testing.T) {
platform, ok := detector.DetectOS(conn)
require.True(t, ok)

identifier, name, related := Detect(conn, platform)
mgr, err := smbios.ResolveManager(conn, platform)
require.NoError(t, err)

identifier, name, related := Detect(conn, platform, mgr)

assert.Equal(t, "//platformid.api.mondoo.app/runtime/aws/ec2/v1/accounts/123456789012/regions/us-west-2/instances/i-1234567890abcdef0", identifier)
assert.Equal(t, "ec2-name", name)
Expand All @@ -33,7 +37,10 @@ func TestDetectInstanceArm(t *testing.T) {
platform, ok := detector.DetectOS(conn)
require.True(t, ok)

identifier, name, related := Detect(conn, platform)
mgr, err := smbios.ResolveManager(conn, platform)
require.NoError(t, err)

identifier, name, related := Detect(conn, platform, mgr)

assert.Equal(t, "//platformid.api.mondoo.app/runtime/aws/ec2/v1/accounts/123456789012/regions/us-west-2/instances/i-1234567890abcdef0", identifier)
assert.Equal(t, "ec2-name", name)
Expand All @@ -47,7 +54,10 @@ func TestDetectNotInstance(t *testing.T) {
platform, ok := detector.DetectOS(conn)
require.True(t, ok)

identifier, name, related := Detect(conn, platform)
mgr, err := smbios.ResolveManager(conn, platform)
require.NoError(t, err)

identifier, name, related := Detect(conn, platform, mgr)

assert.Equal(t, "", identifier)
assert.Equal(t, "", name)
Expand All @@ -61,7 +71,10 @@ func TestDetectContainer(t *testing.T) {
platform, ok := detector.DetectOS(conn)
require.True(t, ok)

identifier, name, related := Detect(conn, platform)
mgr, err := smbios.ResolveManager(conn, platform)
require.NoError(t, err)

identifier, name, related := Detect(conn, platform, mgr)

assert.Equal(t, "//platformid.api.mondoo.app/runtime/aws/ecs/v1/accounts/172746783610/regions/us-east-1/container/vjtest/f088b38d61ac45d6a946b5aebbe7197a/314e35e0-2d0a-4408-b37e-16063461d73a", identifier)
assert.Equal(t, "fargate-app", name)
Expand Down
8 changes: 2 additions & 6 deletions providers/os/id/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
azureIdentifierFileLinux = "/sys/class/dmi/id/sys_vendor"
)

func Detect(conn shared.Connection, pf *inventory.Platform) (string, string, []string) {
func Detect(conn shared.Connection, pf *inventory.Platform, smbiosMgr smbios.SmBiosManager) (string, string, []string) {
sysVendor := ""
if pf.IsFamily("linux") {
// Fetching the product version from the smbios manager is slow
Expand All @@ -33,11 +33,7 @@ func Detect(conn shared.Connection, pf *inventory.Platform) (string, string, []s
}
sysVendor = string(content)
} else {
mgr, err := smbios.ResolveManager(conn, pf)
if err != nil {
return "", "", nil
}
info, err := mgr.Info()
info, err := smbiosMgr.Info()
if err != nil {
log.Debug().Err(err).Msg("failed to query smbios")
return "", "", nil
Expand Down
10 changes: 8 additions & 2 deletions providers/os/id/clouddetect/clouddetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.mondoo.com/cnquery/v11/providers/os/id/aws"
"go.mondoo.com/cnquery/v11/providers/os/id/azure"
"go.mondoo.com/cnquery/v11/providers/os/id/gcp"
"go.mondoo.com/cnquery/v11/providers/os/resources/smbios"
)

type (
Expand All @@ -20,7 +21,7 @@ type (
PlatformID = string
)

type detectorFunc func(conn shared.Connection, p *inventory.Platform) (PlatformID, PlatformName, []RelatedPlatformID)
type detectorFunc func(conn shared.Connection, p *inventory.Platform, smbiosMgr smbios.SmBiosManager) (PlatformID, PlatformName, []RelatedPlatformID)

var detectors = []detectorFunc{
aws.Detect,
Expand All @@ -35,6 +36,11 @@ type detectResult struct {
}

func Detect(conn shared.Connection, p *inventory.Platform) (PlatformID, PlatformName, []RelatedPlatformID) {
mgr, err := smbios.ResolveManager(conn, p)
if err != nil {
return "", "", nil
}

wg := sync.WaitGroup{}
wg.Add(len(detectors))

Expand All @@ -43,7 +49,7 @@ func Detect(conn shared.Connection, p *inventory.Platform) (PlatformID, Platform
go func(f detectorFunc) {
defer wg.Done()

v, name, related := f(conn, p)
v, name, related := f(conn, p, mgr)
if v != "" {
valChan <- detectResult{
platformName: name,
Expand Down
8 changes: 2 additions & 6 deletions providers/os/id/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
gceIdentifierFileLinux = "/sys/class/dmi/id/product_name"
)

func Detect(conn shared.Connection, p *inventory.Platform) (string, string, []string) {
func Detect(conn shared.Connection, p *inventory.Platform, smbiosMgr smbios.SmBiosManager) (string, string, []string) {
productName := ""
if p.IsFamily("linux") {
// Fetching the product version from the smbios manager is slow
Expand All @@ -33,11 +33,7 @@ func Detect(conn shared.Connection, p *inventory.Platform) (string, string, []st
}
productName = string(content)
} else {
mgr, err := smbios.ResolveManager(conn, p)
if err != nil {
return "", "", nil
}
info, err := mgr.Info()
info, err := smbiosMgr.Info()
if err != nil {
log.Debug().Err(err).Msg("failed to query smbios")
return "", "", nil
Expand Down
16 changes: 13 additions & 3 deletions providers/os/id/gcp/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers/os/connection/mock"
"go.mondoo.com/cnquery/v11/providers/os/detector"
"go.mondoo.com/cnquery/v11/providers/os/resources/smbios"
)

func TestDetectLinuxInstance(t *testing.T) {
Expand All @@ -19,7 +20,10 @@ func TestDetectLinuxInstance(t *testing.T) {
platform, ok := detector.DetectOS(conn)
require.True(t, ok)

identifier, name, relatedIdentifiers := Detect(conn, platform)
mgr, err := smbios.ResolveManager(conn, platform)
require.NoError(t, err)

identifier, name, relatedIdentifiers := Detect(conn, platform, mgr)

assert.Equal(t, "//platformid.api.mondoo.app/runtime/gcp/compute/v1/projects/mondoo-dev-262313/zones/us-central1-a/instances/6001244637815193808", identifier)
assert.Equal(t, "", name)
Expand All @@ -33,7 +37,10 @@ func TestDetectWindowsInstance(t *testing.T) {
platform, ok := detector.DetectOS(conn)
require.True(t, ok)

identifier, name, relatedIdentifiers := Detect(conn, platform)
mgr, err := smbios.ResolveManager(conn, platform)
require.NoError(t, err)

identifier, name, relatedIdentifiers := Detect(conn, platform, mgr)

assert.Equal(t, "//platformid.api.mondoo.app/runtime/gcp/compute/v1/projects/mondoo-dev-262313/zones/us-central1-a/instances/5275377306317132843", identifier)
assert.Equal(t, "", name)
Expand All @@ -47,7 +54,10 @@ func TestNoMatch(t *testing.T) {
platform, ok := detector.DetectOS(conn)
require.True(t, ok)

identifier, name, relatedIdentifiers := Detect(conn, platform)
mgr, err := smbios.ResolveManager(conn, platform)
require.NoError(t, err)

identifier, name, relatedIdentifiers := Detect(conn, platform, mgr)

assert.Empty(t, identifier)
assert.Empty(t, name)
Expand Down
12 changes: 9 additions & 3 deletions providers/os/provider/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"go.mondoo.com/cnquery/v11/providers/os/id/ids"
"go.mondoo.com/cnquery/v11/providers/os/id/machineid"
"go.mondoo.com/cnquery/v11/providers/os/id/sshhostkey"
"go.mondoo.com/cnquery/v11/providers/os/resources/smbios"
)

// default id detectors
Expand Down Expand Up @@ -75,8 +76,13 @@ func (s *Service) detect(asset *inventory.Asset, conn shared.Connection) error {
}

if hasDetector(detectors, ids.IdDetector_CloudDetect) {
mgr, err := smbios.ResolveManager(conn, asset.Platform)
if err != nil {
return err
}

log.Debug().Msg("run cloud platform detector")
if id, name, related := aws.Detect(conn, asset.Platform); id != "" {
if id, name, related := aws.Detect(conn, asset.Platform, mgr); id != "" {
asset.PlatformIds = append(asset.PlatformIds, id)
if name != "" {
// if we weren't able to detect a name for this asset, don't update to an empty value
Expand All @@ -85,12 +91,12 @@ func (s *Service) detect(asset *inventory.Asset, conn shared.Connection) error {
asset.RelatedAssets = append(asset.RelatedAssets, relatedIds2assets(related)...)
}

if id, _, related := azure.Detect(conn, asset.Platform); id != "" {
if id, _, related := azure.Detect(conn, asset.Platform, mgr); id != "" {
asset.PlatformIds = append(asset.PlatformIds, id)
asset.RelatedAssets = append(asset.RelatedAssets, relatedIds2assets(related)...)
}

if id, _, related := gcp.Detect(conn, asset.Platform); id != "" {
if id, _, related := gcp.Detect(conn, asset.Platform, mgr); id != "" {
asset.PlatformIds = append(asset.PlatformIds, id)
asset.RelatedAssets = append(asset.RelatedAssets, relatedIds2assets(related)...)
}
Expand Down
10 changes: 10 additions & 0 deletions providers/os/resources/smbios/win.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"io"
"strconv"
"sync"

"go.mondoo.com/cnquery/v11/providers/os/connection/shared"
"go.mondoo.com/cnquery/v11/providers/os/resources/powershell"
Expand Down Expand Up @@ -128,13 +129,21 @@ type smbiosSystemProduct struct {
// https://docs.microsoft.com/en-us/windows-hardware/drivers/bringup/smbios
type WindowsSmbiosManager struct {
provider shared.Connection
smInfo *SmBiosInfo
lock sync.Mutex
}

func (s *WindowsSmbiosManager) Name() string {
return "Windows Smbios Manager"
}

func (s *WindowsSmbiosManager) Info() (*SmBiosInfo, error) {
s.lock.Lock()
defer s.lock.Unlock()
if s.smInfo != nil {
return s.smInfo, nil
}

c, err := s.provider.RunCommand(powershell.Encode(smbiosWindowsScript))
if err != nil {
return nil, err
Expand Down Expand Up @@ -184,6 +193,7 @@ func (s *WindowsSmbiosManager) Info() (*SmBiosInfo, error) {
Type: winBios.Chassis[0].GetChassisTypes().Value()[0],
},
}
s.smInfo = &smInfo

return &smInfo, nil
}
Expand Down

0 comments on commit d46d864

Please sign in to comment.