Skip to content

Commit

Permalink
🐛 set runtime labels for scannable root assets (#3215)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Feb 6, 2024
1 parent 94a3930 commit c4adb50
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 4 additions & 3 deletions explorer/scan/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func DiscoverAssets(ctx context.Context, inv *inventory.Inventory, upstream *ups

// If the root asset has platform IDs, then it is a scannable asset, so we need to add it
if len(resolvedRootAsset.PlatformIds) > 0 {
prepareAsset(resolvedRootAsset, resolvedRootAsset, runtimeLabels)
if !discoveredAssets.Add(rootAssetWithRuntime.Asset, rootAssetWithRuntime.Runtime) {
rootAssetWithRuntime.Runtime.Close()
}
Expand Down Expand Up @@ -188,10 +189,10 @@ func prepareAsset(a *inventory.Asset, rootAsset *inventory.Asset, runtimeLabels
a.AddAnnotations(rootAsset.GetAnnotations())
a.ManagedBy = rootAsset.ManagedBy
a.KindString = a.GetPlatform().Kind
if a.Labels == nil {
a.Labels = map[string]string{}
}
for k, v := range runtimeLabels {
if a.Labels == nil {
a.Labels = map[string]string{}
}
a.Labels[k] = v
}
}
23 changes: 23 additions & 0 deletions explorer/scan/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,29 @@ func TestDiscoverAssets(t *testing.T) {
}
})

t.Run("set ci/cd labels for scannable root assets", func(t *testing.T) {
inv := getInventory()
inv.Spec.Assets[0].Connections[0].Type = "local"

val, isSet := os.LookupEnv("GITHUB_ACTION")
defer func() {
if isSet {
require.NoError(t, os.Setenv("GITHUB_ACTION", val))
} else {
require.NoError(t, os.Unsetenv("GITHUB_ACTION"))
}
}()
inv.Spec.Assets[0].Category = inventory.AssetCategory_CATEGORY_CICD
require.NoError(t, os.Setenv("GITHUB_ACTION", "go-test"))
discoveredAssets, err := DiscoverAssets(context.Background(), inv, nil, providers.NullRecording{})
require.NoError(t, err)

for _, asset := range discoveredAssets.Assets {
require.Contains(t, asset.Asset.Labels, "mondoo.com/exec-environment")
assert.Equal(t, "actions.github.com", asset.Asset.Labels["mondoo.com/exec-environment"])
}
})

t.Run("scannable root asset", func(t *testing.T) {
inv := getInventory()
inv.Spec.Assets[0].Connections[0].Type = "local"
Expand Down

0 comments on commit c4adb50

Please sign in to comment.