Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add serial number to asset ids #4539

Merged
merged 7 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 6 additions & 5 deletions providers/os/id/ids/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
package ids

const (
IdDetector_Hostname = "hostname"
IdDetector_MachineID = "machine-id"
IdDetector_CloudDetect = "cloud-detect"
IdDetector_SshHostkey = "ssh-host-key"
IdDetector_AwsEcs = "aws-ecs"
IdDetector_Hostname = "hostname"
IdDetector_MachineID = "machine-id"
IdDetector_SerialNumber = "serialnumber"
IdDetector_CloudDetect = "cloud-detect"
IdDetector_SshHostkey = "ssh-host-key"
IdDetector_AwsEcs = "aws-ecs"

// IdDetector_PlatformID = "transport-platform-id" // TODO: how does this work?
)
19 changes: 18 additions & 1 deletion 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 All @@ -17,6 +19,7 @@ import (
"go.mondoo.com/cnquery/v11/providers/os/id/hostname"
"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/serialnumber"
"go.mondoo.com/cnquery/v11/providers/os/id/sshhostkey"
)

Expand All @@ -35,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,6 +56,9 @@ func IdentifyPlatform(conn shared.Connection, p *inventory.Platform, idDetectors
switch conn.Type() {
case shared.Type_Local:
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 Expand Up @@ -161,6 +167,17 @@ func GatherPlatformInfo(conn shared.Connection, pf *inventory.Platform, idDetect
}, hostErr
}
return &PlatformInfo{}, nil
case idDetector == ids.IdDetector_SerialNumber:
serial, err := serialnumber.SerialNumber(conn, pf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we tested that for macos, windows and linux?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is taken from the existing resource - machine.system.serial - which works on all aforementioned OS's
My tests so far show that the on the systems which don't provide that data (for example CI containers) serial evaluates to empty string, so it's omitted in the asset.ids

if err == nil && len(serial) > 0 {
identifier = "//platformid.api.mondoo.app/serialnumber/" + serial
return &PlatformInfo{
IDs: []string{identifier},
Name: "",
RelatedPlatformIDs: []string{},
}, nil
}
return &PlatformInfo{}, nil
case idDetector == ids.IdDetector_AwsEcs:
metadata, err := awsecs.Resolve(conn, pf)
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions providers/os/id/serialnumber/serial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package serialnumber

import (
"github.com/pkg/errors"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers/os/connection/shared"
"go.mondoo.com/cnquery/v11/providers/os/resources/smbios"
)

func SerialNumber(conn shared.Connection, p *inventory.Platform) (string, error) {
mgr, err := smbios.ResolveManager(conn, p)
if err != nil {
return "", errors.Wrap(err, "cannot determine platform serial number")
}
if mgr == nil {
return "", errors.New("cannot determine platform serial number")
}

info, err := mgr.Info()
if err != nil {
return "", errors.New("cannot determine platform serial number")
}

return info.SysInfo.SerialNumber, nil
}
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 {
if asset.Name == "" {
asset.Name = fingerprint.Name
Expand All @@ -368,7 +368,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 @@ -385,7 +385,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 @@ -400,7 +400,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 @@ -415,7 +415,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 @@ -467,7 +467,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
Loading