Skip to content

Commit

Permalink
delayed asset discovery
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev committed Mar 5, 2024
1 parent 449d063 commit d5261b3
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 225 deletions.
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
}
if a.Labels == nil {
a.Labels = map[string]string{}
}
Expand Down
68 changes: 53 additions & 15 deletions explorer/scan/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 @@ -243,30 +244,39 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
}
}()

spaceMrn := ""
var services *explorer.Services
if upstream != nil && upstream.ApiEndpoint != "" && !upstream.Incognito {
log.Info().Msg("synchronize assets")
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 {
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].SkipDiscovery {
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 +294,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 +339,32 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
default:
}

if asset.Connections[0].SkipDiscovery {
asset.Connections[0].SkipDiscovery = false
if err := runtime.Connect(&plugin.ConnectReq{Asset: asset}); err != nil {
log.Error().Err(err).Str("asset", asset.Name).Msg("failed to connect to asset")
reporter.AddScanError(asset, err)
multiprogress.Errored(asset.PlatformIds[0])
continue
}
if services != nil {
resp, err := services.SynchronizeAssets(ctx, &explorer.SynchronizeAssetsReq{
SpaceMrn: spaceMrn,
List: []*inventory.Asset{asset},
})
if err != nil {
reporter.AddScanError(asset, err)
multiprogress.Errored(asset.PlatformIds[0])
continue
}

// platformMrn := asset.PlatformIds[0]
details := resp.Details[asset.PlatformIds[0]]
asset.Mrn = details.AssetMrn
asset.Url = details.Url
}
}

p := &progress.MultiProgressAdapter{Key: asset.PlatformIds[0], Multi: multiprogress}
s.RunAssetJob(&AssetJob{
DoRecord: job.DoRecord,
Expand Down
Loading

0 comments on commit d5261b3

Please sign in to comment.