From 1d319fdc926b89b1163f5cd4dbcf84f6a2b4f8e4 Mon Sep 17 00:00:00 2001 From: Mikita Iwanowski Date: Wed, 18 Sep 2024 15:55:06 +0200 Subject: [PATCH] feat: add feature flag for SerialNumber id detector --- feature_string.go | 5 +++-- featureflags.go | 7 +++++++ providers-sdk/v1/sysinfo/sysinfo.go | 3 ++- providers/os/connection/device/device_connection.go | 2 +- providers/os/id/platform.go | 9 +++++++-- providers/os/provider/provider.go | 12 ++++++------ 6 files changed, 26 insertions(+), 12 deletions(-) diff --git a/feature_string.go b/feature_string.go index 16b3aec562..c2e0695fdf 100644 --- a/feature_string.go +++ b/feature_string.go @@ -16,11 +16,12 @@ func _() { _ = x[ErrorsAsFailures-6] _ = x[StoreResourcesData-7] _ = x[FineGrainedAssets-8] + _ = x[SerialNumberAsID-9] } -const _Feature_name = "MassQueriesPiperCodeBoolAssertionsK8sNodeDiscoveryMQLAssetContextErrorsAsFailuresStoreResourcesDataFineGrainedAssets" +const _Feature_name = "MassQueriesPiperCodeBoolAssertionsK8sNodeDiscoveryMQLAssetContextErrorsAsFailuresStoreResourcesDataFineGrainedAssetsSerialNumberAsID" -var _Feature_index = [...]uint8{0, 11, 20, 34, 50, 65, 81, 99, 116} +var _Feature_index = [...]uint8{0, 11, 20, 34, 50, 65, 81, 99, 116, 132} func (i Feature) String() string { i -= 1 diff --git a/featureflags.go b/featureflags.go index 947ad629fb..0595c3cc6d 100644 --- a/featureflags.go +++ b/featureflags.go @@ -88,6 +88,12 @@ const ( // start: v11.x // end: tbd (candidate: v12.0) FineGrainedAssets + + // SerialNumberAsID feature flag + // desc: Use serial number as the asset ID + // start: v11.x + // end: tbd (candidate: v12.0) + SerialNumberAsID ) // FeaturesValue is a map from feature name to feature flag @@ -99,6 +105,7 @@ var FeaturesValue = map[string]Feature{ ErrorsAsFailures.String(): ErrorsAsFailures, StoreResourcesData.String(): StoreResourcesData, FineGrainedAssets.String(): FineGrainedAssets, + SerialNumberAsID.String(): SerialNumberAsID, } // DefaultFeatures are a set of default flags that are active diff --git a/providers-sdk/v1/sysinfo/sysinfo.go b/providers-sdk/v1/sysinfo/sysinfo.go index 4e5aa72fe3..0ae78eeb84 100644 --- a/providers-sdk/v1/sysinfo/sysinfo.go +++ b/providers-sdk/v1/sysinfo/sysinfo.go @@ -11,6 +11,7 @@ import ( "go.mondoo.com/cnquery/v11" "go.mondoo.com/cnquery/v11/cli/execruntime" "go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory" + "go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin" "go.mondoo.com/cnquery/v11/providers/os/connection/local" "go.mondoo.com/cnquery/v11/providers/os/id" "go.mondoo.com/cnquery/v11/providers/os/id/hostname" @@ -46,7 +47,7 @@ func Get() (*SystemInfo, error) { Type: "local", }, &asset) - fingerprint, platform, _ := id.IdentifyPlatform(conn, asset.Platform, asset.IdDetector) + fingerprint, platform, _ := id.IdentifyPlatform(conn, &plugin.ConnectReq{}, asset.Platform, asset.IdDetector) if fingerprint != nil { if len(fingerprint.PlatformIDs) > 0 { sysInfo.PlatformId = fingerprint.PlatformIDs[0] diff --git a/providers/os/connection/device/device_connection.go b/providers/os/connection/device/device_connection.go index da236f4a51..e484124964 100644 --- a/providers/os/connection/device/device_connection.go +++ b/providers/os/connection/device/device_connection.go @@ -111,7 +111,7 @@ func NewDeviceConnection(connId uint32, conf *inventory.Config, asset *inventory } asset.Platform = p asset.IdDetector = []string{ids.IdDetector_Hostname} - fingerprint, p, err := id.IdentifyPlatform(res, asset.Platform, asset.IdDetector) + fingerprint, p, err := id.IdentifyPlatform(res, &plugin.ConnectReq{}, asset.Platform, asset.IdDetector) if err == nil { if asset.Name == "" { asset.Name = fingerprint.Name diff --git a/providers/os/id/platform.go b/providers/os/id/platform.go index be24c06f6a..a52ea6ab7a 100644 --- a/providers/os/id/platform.go +++ b/providers/os/id/platform.go @@ -8,7 +8,9 @@ import ( "fmt" "github.com/rs/zerolog/log" + "go.mondoo.com/cnquery/v11" "go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory" + "go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin" "go.mondoo.com/cnquery/v11/providers/os/connection/shared" "go.mondoo.com/cnquery/v11/providers/os/detector" "go.mondoo.com/cnquery/v11/providers/os/id/awsec2" @@ -36,7 +38,7 @@ type PlatformInfo struct { RelatedPlatformIDs []string } -func IdentifyPlatform(conn shared.Connection, p *inventory.Platform, idDetectors []string) (*PlatformFingerprint, *inventory.Platform, error) { +func IdentifyPlatform(conn shared.Connection, req *plugin.ConnectReq, p *inventory.Platform, idDetectors []string) (*PlatformFingerprint, *inventory.Platform, error) { var ok bool if p == nil { p, ok = detector.DetectOS(conn) @@ -53,7 +55,10 @@ func IdentifyPlatform(conn shared.Connection, p *inventory.Platform, idDetectors // fallback to default id detectors switch conn.Type() { case shared.Type_Local: - idDetectors = []string{ids.IdDetector_Hostname, ids.IdDetector_SerialNumber, ids.IdDetector_CloudDetect} + idDetectors = []string{ids.IdDetector_Hostname, ids.IdDetector_CloudDetect} + if cnquery.Features(req.Features).IsActive(cnquery.SerialNumberAsID) { + idDetectors = append(idDetectors, ids.IdDetector_SerialNumber) + } case shared.Type_SSH: idDetectors = []string{ids.IdDetector_Hostname, ids.IdDetector_CloudDetect, ids.IdDetector_SshHostkey} case shared.Type_Tar, shared.Type_FileSystem, shared.Type_DockerSnapshot: diff --git a/providers/os/provider/provider.go b/providers/os/provider/provider.go index 27c044241c..e973dc05d1 100644 --- a/providers/os/provider/provider.go +++ b/providers/os/provider/provider.go @@ -350,7 +350,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba case shared.Type_Local.String(), "k8s": // FIXME: k8s is a temp workaround for cross-provider resources conn = local.NewConnection(connId, conf, asset) - fingerprint, p, err := id.IdentifyPlatform(conn, asset.Platform, asset.IdDetector) + fingerprint, p, err := id.IdentifyPlatform(conn, req, asset.Platform, asset.IdDetector) if err == nil { asset.Name = fingerprint.Name asset.PlatformIds = fingerprint.PlatformIDs @@ -366,7 +366,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba return nil, err } - fingerprint, p, err := id.IdentifyPlatform(conn, asset.Platform, asset.IdDetector) + fingerprint, p, err := id.IdentifyPlatform(conn, req, asset.Platform, asset.IdDetector) if err == nil { if conn.Asset().Connections[0].Runtime != "vagrant" { asset.Name = fingerprint.Name @@ -383,7 +383,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba return nil, err } - fingerprint, p, err := id.IdentifyPlatform(conn, asset.Platform, asset.IdDetector) + fingerprint, p, err := id.IdentifyPlatform(conn, req, asset.Platform, asset.IdDetector) if err == nil { asset.Name = fingerprint.Name asset.PlatformIds = fingerprint.PlatformIDs @@ -398,7 +398,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba return nil, err } - fingerprint, p, err := id.IdentifyPlatform(conn, asset.Platform, asset.IdDetector) + fingerprint, p, err := id.IdentifyPlatform(conn, req, asset.Platform, asset.IdDetector) if err == nil { asset.Name = fingerprint.Name asset.PlatformIds = fingerprint.PlatformIDs @@ -413,7 +413,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba return nil, err } - fingerprint, p, err := id.IdentifyPlatform(conn, asset.Platform, asset.IdDetector) + fingerprint, p, err := id.IdentifyPlatform(conn, req, asset.Platform, asset.IdDetector) if err == nil { asset.Name = fingerprint.Name asset.PlatformIds = fingerprint.PlatformIDs @@ -465,7 +465,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba // This is a workaround to set Google COS platform IDs when scanned from inside k8s pID, err := conn.(*fs.FileSystemConnection).Identifier() if err != nil { - fingerprint, p, err := id.IdentifyPlatform(conn, asset.Platform, asset.IdDetector) + fingerprint, p, err := id.IdentifyPlatform(conn, req, asset.Platform, asset.IdDetector) if err == nil { asset.Name = fingerprint.Name asset.PlatformIds = fingerprint.PlatformIDs