Skip to content

Commit

Permalink
feat: add feature flag for SerialNumber id detector
Browse files Browse the repository at this point in the history
  • Loading branch information
slntopp committed Sep 18, 2024
1 parent dfc5c5a commit 1d319fd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
5 changes: 3 additions & 2 deletions feature_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions featureflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion providers-sdk/v1/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion providers/os/connection/device/device_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions providers/os/id/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions providers/os/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1d319fd

Please sign in to comment.