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

⭐️ delayed asset discovery #3496

Merged
merged 8 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 3 additions & 1 deletion explorer/scan/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ func prepareAsset(a *inventory.Asset, rootAsset *inventory.Asset, runtimeLabels
a.AddMondooLabels(rootAsset)
a.AddAnnotations(rootAsset.GetAnnotations())
a.ManagedBy = rootAsset.ManagedBy
a.KindString = a.GetPlatform().Kind
if platform := a.GetPlatform(); platform != nil {
a.KindString = a.GetPlatform().Kind
imilchev marked this conversation as resolved.
Show resolved Hide resolved
}
if a.Labels == nil {
a.Labels = map[string]string{}
}
Expand Down
78 changes: 63 additions & 15 deletions explorer/scan/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"os"
"slices"
"strings"
sync "sync"
"time"
Expand All @@ -27,6 +28,7 @@ import (
"go.mondoo.com/cnquery/v10/mrn"
"go.mondoo.com/cnquery/v10/providers"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/upstream"
"go.mondoo.com/cnquery/v10/utils/multierr"
"go.mondoo.com/cnquery/v10/utils/slicesx"
Expand Down Expand Up @@ -180,6 +182,7 @@ func CreateProgressBar(discoveredAssets *DiscoveredAssets, disableProgressBar bo
asset := discoveredAssets.Assets[i].Asset
// this shouldn't happen, but might
// it normally indicates a bug in the provider
slices.Sort(asset.PlatformIds)
if presentAsset, present := progressBarElements[asset.PlatformIds[0]]; present {
return nil, fmt.Errorf("asset %s and %s have the same platform id %s", presentAsset, asset.Name, asset.PlatformIds[0])
}
Expand Down Expand Up @@ -243,30 +246,39 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
}
}()

spaceMrn := ""
var services *explorer.Services
if upstream != nil && upstream.ApiEndpoint != "" && !upstream.Incognito {
client, err := upstream.InitClient()
if err != nil {
return nil, err
}
spaceMrn = client.SpaceMrn

services, err = explorer.NewRemoteServices(client.ApiEndpoint, client.Plugins, client.HttpClient)
if err != nil {
return nil, err
}
}

assetBatches := slicesx.Batch(discoveredAssets.Assets, 100)
for i := range assetBatches {
log.Info().Msg("synchronize assets")
imilchev marked this conversation as resolved.
Show resolved Hide resolved
batch := assetBatches[i]

// sync assets
if upstream != nil && upstream.ApiEndpoint != "" && !upstream.Incognito {
log.Info().Msg("synchronize assets")
client, err := upstream.InitClient()
if err != nil {
return nil, err
}

services, err := explorer.NewRemoteServices(client.ApiEndpoint, client.Plugins, client.HttpClient)
if err != nil {
return nil, err
}

if services != nil {
assetsToSync := make([]*inventory.Asset, 0, len(batch))
for i := range batch {
// If discovery has been skipped, then we don't sync that asset just yet. We will do that during the scan
if batch[i].Asset.Connections[0].DelayDiscovery {
continue
}
assetsToSync = append(assetsToSync, batch[i].Asset)
}

resp, err := services.SynchronizeAssets(ctx, &explorer.SynchronizeAssetsReq{
SpaceMrn: client.SpaceMrn,
SpaceMrn: spaceMrn,
List: assetsToSync,
})
if err != nil {
Expand All @@ -284,8 +296,10 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
asset := batch[i].Asset
log.Debug().Str("asset", asset.Name).Strs("platform-ids", asset.PlatformIds).Msg("update asset")
platformMrn := asset.PlatformIds[0]
asset.Mrn = platformAssetMapping[platformMrn].AssetMrn
asset.Url = platformAssetMapping[platformMrn].Url
if details, ok := platformAssetMapping[platformMrn]; ok {
asset.Mrn = details.AssetMrn
asset.Url = details.Url
}
}
} else {
// ensure we have non-empty asset MRNs
Expand Down Expand Up @@ -327,6 +341,16 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
default:
}

if asset.Connections[0].DelayDiscovery {
discoveredAsset, err := handleDelayedDiscovery(ctx, asset, runtime, services, spaceMrn)
if err != nil {
reporter.AddScanError(asset, err)
multiprogress.Errored(asset.PlatformIds[0])
continue
}
asset = discoveredAsset
}

p := &progress.MultiProgressAdapter{Key: asset.PlatformIds[0], Multi: multiprogress}
s.RunAssetJob(&AssetJob{
DoRecord: job.DoRecord,
Expand All @@ -351,6 +375,30 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
return reporter.Reports(), nil
}

func handleDelayedDiscovery(ctx context.Context, asset *inventory.Asset, runtime *providers.Runtime, services *explorer.Services, spaceMrn string) (*inventory.Asset, error) {
asset.Connections[0].DelayDiscovery = false
if err := runtime.Connect(&plugin.ConnectReq{Asset: asset}); err != nil {
return nil, err
}
if services != nil {
resp, err := services.SynchronizeAssets(ctx, &explorer.SynchronizeAssetsReq{
SpaceMrn: spaceMrn,
List: []*inventory.Asset{asset},
})
if err != nil {
return nil, err
}

asset = runtime.Provider.Connection.Asset
slices.Sort(asset.PlatformIds)
details := resp.Details[asset.PlatformIds[0]]
asset.Mrn = details.AssetMrn
asset.Url = details.Url
asset.KindString = asset.GetPlatform().Kind
}
return asset, nil
}

func (s *LocalScanner) RunAssetJob(job *AssetJob) {
log.Debug().Msgf("connecting to asset %s", job.Asset.HumanName())
results, err := s.runMotorizedAsset(job)
Expand Down
Loading
Loading