Skip to content

Commit

Permalink
✨ asset kinds inside inventory package
Browse files Browse the repository at this point in the history
Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Feb 27, 2025
1 parent 6eeddea commit eff89a2
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 14 deletions.
11 changes: 11 additions & 0 deletions providers-sdk/v1/inventory/asset_kinds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package inventory

// Use this file to define kinds of assets that are used widely.

const (
AssetKindCloudVM = "virtualmachine"
AssetKindBaremetal = "baremetal"
)
3 changes: 1 addition & 2 deletions providers/aws/connection/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package connection

import (
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers/os/id/clouddetect"
)

func (a *AwsConnection) PlatformInfo() *inventory.Platform {
Expand Down Expand Up @@ -39,7 +38,7 @@ func getPlatformKind(platformName string) string {
// "aws-ecs-container",
"aws-ec2-instance",
"aws-ssm-instance":
return clouddetect.AssetKind
return inventory.AssetKindCloudVM
case "aws":
return "api"
default:
Expand Down
5 changes: 2 additions & 3 deletions providers/aws/resources/discovery_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"go.mondoo.com/cnquery/v11/providers/aws/connection/awsec2ebsconn"
awsec2ebstypes "go.mondoo.com/cnquery/v11/providers/aws/connection/awsec2ebsconn/types"
"go.mondoo.com/cnquery/v11/providers/os/id/awsec2"
"go.mondoo.com/cnquery/v11/providers/os/id/clouddetect"
"go.mondoo.com/cnquery/v11/providers/os/id/containerid"
"go.mondoo.com/cnquery/v11/providers/os/id/ids"
)
Expand Down Expand Up @@ -286,7 +285,7 @@ func addConnectionInfoToEc2Asset(instance *mqlAwsEc2Instance, accountId string,
asset.PlatformIds = []string{awsec2.MondooInstanceID(accountId, instance.Region.Data, instance.InstanceId.Data)}
asset.IdDetector = []string{ids.IdDetector_Hostname, ids.IdDetector_CloudDetect, ids.IdDetector_SshHostkey}
asset.Platform = &inventory.Platform{
Kind: clouddetect.AssetKind,
Kind: inventory.AssetKindCloudVM,
Runtime: "aws-ec2-instance",
Family: getPlatformFamily(instance.PlatformDetails.Data),
}
Expand Down Expand Up @@ -433,7 +432,7 @@ func addConnectionInfoToSSMAsset(instance *mqlAwsSsmInstance, accountId string,
asset.Options = conn.ConnectionOptions()
asset.PlatformIds = []string{awsec2.MondooInstanceID(accountId, instance.Region.Data, instance.InstanceId.Data)}
asset.Platform = &inventory.Platform{
Kind: clouddetect.AssetKind,
Kind: inventory.AssetKindCloudVM,
Runtime: "aws-ssm-instance",
Family: getPlatformFamily(instance.PlatformName.Data),
}
Expand Down
32 changes: 32 additions & 0 deletions providers/builtin_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ import (
_ "embed"
// osconf "go.mondoo.com/cnquery/v11/providers/os/config"
// os "go.mondoo.com/cnquery/v11/providers/os/provider"
awsconf "go.mondoo.com/cnquery/v11/providers/aws/config"

Check failure on line 13 in providers/builtin_dev.go

View workflow job for this annotation

GitHub Actions / go-bench

no required module provides package go.mondoo.com/cnquery/v11/providers/aws/config; to add it:

Check failure on line 13 in providers/builtin_dev.go

View workflow job for this annotation

GitHub Actions / go-race

no required module provides package go.mondoo.com/cnquery/v11/providers/aws/config; to add it:
aws "go.mondoo.com/cnquery/v11/providers/aws/provider"

Check failure on line 14 in providers/builtin_dev.go

View workflow job for this annotation

GitHub Actions / go-race

no required module provides package go.mondoo.com/cnquery/v11/providers/aws/provider; to add it:
osconf "go.mondoo.com/cnquery/v11/providers/os/config"
os "go.mondoo.com/cnquery/v11/providers/os/provider"
)

// //go:embed os/resources/os.resources.json
// var osInfo []byte

//go:embed os.resources.json
var osInfo []byte

//go:embed aws.resources.json

Check failure on line 25 in providers/builtin_dev.go

View workflow job for this annotation

GitHub Actions / go-race

pattern aws.resources.json: no matching files found

Check failure on line 25 in providers/builtin_dev.go

View workflow job for this annotation

GitHub Actions / go-test-integration

pattern aws.resources.json: no matching files found
var awsInfo []byte

func init() {
// builtinProviders[osconf.Config.ID] = &builtinProvider{
// Runtime: &RunningProvider{
Expand All @@ -27,4 +37,26 @@ func init() {
// Config: &osconf.Config,
// }

builtinProviders[osconf.Config.ID] = &builtinProvider{
Runtime: &RunningProvider{
Name: osconf.Config.Name,
ID: osconf.Config.ID,
Plugin: os.Init(),
Schema: MustLoadSchema("os", osInfo),
isClosed: false,
},
Config: &osconf.Config,
}

builtinProviders[awsconf.Config.ID] = &builtinProvider{
Runtime: &RunningProvider{
Name: awsconf.Config.Name,
ID: awsconf.Config.ID,
Plugin: aws.Init(),
Schema: MustLoadSchema("aws", awsInfo),
isClosed: false,
},
Config: &awsconf.Config,
}

}
15 changes: 8 additions & 7 deletions providers/os/id/clouddetect/clouddetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,26 @@ var detectors = []detectorFunc{
gcp.Detect,
}

const AssetKind = "virtualmachine"

type detectResult struct {
platformId string
platformName string
relatedPlatformIds []string
}

// PlatformInfo contains platform information gathered from one of our cloud detectors.
type PlatformInfo struct {
ID string
Name string
Kind string
RelatedPlatformIDs []string
}

func Detect(conn shared.Connection, p *inventory.Platform) PlatformInfo {
// Detect tried to detect if we are running on a cloud asset, and if so, it returns
// the platform information, otherwise it returns a `nil` pointer.
func Detect(conn shared.Connection, p *inventory.Platform) *PlatformInfo {
mgr, err := smbios.ResolveManager(conn, p)
if err != nil {
return PlatformInfo{"", "", "", nil}
return nil
}

wg := sync.WaitGroup{}
Expand Down Expand Up @@ -82,11 +83,11 @@ func Detect(conn shared.Connection, p *inventory.Platform) PlatformInfo {
}

if len(platformIds) == 0 {
return PlatformInfo{"", "", "", nil}
return nil
} else if len(platformIds) > 1 {
log.Error().Strs("detected", platformIds).Msg("multiple cloud platform ids detected")
return PlatformInfo{"", "", "", nil}
return nil
}

return PlatformInfo{platformIds[0], name, AssetKind, relatedPlatformIds}
return &PlatformInfo{platformIds[0], name, inventory.AssetKindCloudVM, relatedPlatformIds}
}
2 changes: 1 addition & 1 deletion providers/os/id/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func gatherPlatformInfo(conn shared.Connection, pf *inventory.Platform, idDetect
return &platformInfo{}, nil
case idDetector == ids.IdDetector_CloudDetect:
cloudPlatformInfo := clouddetect.Detect(conn, pf)
if cloudPlatformInfo.ID != "" {
if cloudPlatformInfo != nil {
return &platformInfo{
IDs: []string{cloudPlatformInfo.ID},
Name: cloudPlatformInfo.Name,
Expand Down
2 changes: 1 addition & 1 deletion providers/os/provider/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s *Service) detect(asset *inventory.Asset, conn shared.Connection) error {
if hasDetector(detectors, ids.IdDetector_CloudDetect) {
log.Debug().Msg("run cloud platform detector")
cloudPlatformInfo := clouddetect.Detect(conn, asset.Platform)
if cloudPlatformInfo.ID != "" {
if cloudPlatformInfo != nil {
log.Debug().Interface("info", cloudPlatformInfo).Msg("cloud platform detected")
asset.PlatformIds = append(asset.PlatformIds, cloudPlatformInfo.ID)
if cloudPlatformInfo.Name != "" {
Expand Down

0 comments on commit eff89a2

Please sign in to comment.