Skip to content

Commit 079dbfb

Browse files
committed
🐛 Enhance asset deduplication logic to evict subsets when supersets are added and update related tests
1 parent 3557249 commit 079dbfb

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

discovery/discovery.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import (
1515
"go.mondoo.com/mql/v13/cli/execruntime"
1616
"go.mondoo.com/mql/v13/internal/workerpool"
1717
"go.mondoo.com/mql/v13/llx"
18-
"go.mondoo.com/mql/v13/utils/slicesx"
1918
"go.mondoo.com/mql/v13/logger"
2019
"go.mondoo.com/mql/v13/providers"
2120
inventory "go.mondoo.com/mql/v13/providers-sdk/v1/inventory"
2221
"go.mondoo.com/mql/v13/providers-sdk/v1/inventory/manager"
2322
"go.mondoo.com/mql/v13/providers-sdk/v1/plugin"
2423
"go.mondoo.com/mql/v13/providers-sdk/v1/upstream"
24+
"go.mondoo.com/mql/v13/utils/slicesx"
2525
"google.golang.org/protobuf/proto"
2626
)
2727

@@ -68,19 +68,30 @@ func (d *DiscoveredAssets) Add(asset *inventory.Asset, runtime *providers.Runtim
6868
return false
6969
}
7070

71+
// Evict existing assets whose IDs are a subset of the new asset's IDs.
72+
// This ensures deduplication is order-independent.
73+
kept := d.Assets[:0]
7174
for _, existing := range d.Assets {
7275
if slicesx.IsSubsetOf(asset.PlatformIds, existing.Asset.PlatformIds) {
7376
log.Debug().Str("asset", asset.Name).Strs("platform-ids", asset.PlatformIds).Msg("discovery> skipping duplicate asset")
7477
return false
7578
}
79+
if slicesx.IsSubsetOf(existing.Asset.PlatformIds, asset.PlatformIds) {
80+
log.Debug().Str("asset", existing.Asset.Name).Strs("platform-ids", existing.Asset.PlatformIds).Msg("discovery> evicting asset that is a subset of new asset")
81+
if existing.Runtime != nil {
82+
existing.Runtime.Close()
83+
}
84+
continue
85+
}
86+
kept = append(kept, existing)
7687
}
88+
d.Assets = kept
7789

7890
log.Debug().Str("asset", asset.Name).Strs("platform-ids", asset.PlatformIds).Int("total", len(d.Assets)+1).Msg("discovery> added asset")
7991
d.Assets = append(d.Assets, &AssetWithRuntime{Asset: asset, Runtime: runtime})
8092
return true
8193
}
8294

83-
8495
func (d *DiscoveredAssets) AddError(asset *inventory.Asset, err error) {
8596
d.assetsLock.Lock()
8697
defer d.assetsLock.Unlock()

discovery/discovery_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ func TestDiscoveredAssetsAdd(t *testing.T) {
5959
assert.Len(t, d.Assets, 1)
6060
})
6161

62-
t.Run("subset rejection is order-independent", func(t *testing.T) {
62+
t.Run("subset is evicted when superset arrives", func(t *testing.T) {
6363
d := &DiscoveredAssets{}
6464
fewer := &inventory.Asset{PlatformIds: []string{"hostname/node1", "ssh/ABC"}}
6565
more := &inventory.Asset{PlatformIds: []string{"hostname/node1", "ssh/ABC", "cloud/xyz"}}
6666

67-
// Even when the subset is added first, the superset is still added
68-
// (it has a new ID), and neither is incorrectly rejected.
67+
// When the subset is added first, adding the superset evicts the subset.
6968
assert.True(t, d.Add(fewer, nil))
7069
assert.True(t, d.Add(more, nil))
71-
assert.Len(t, d.Assets, 2)
70+
assert.Len(t, d.Assets, 1)
71+
assert.Equal(t, more.PlatformIds, d.Assets[0].Asset.PlatformIds)
7272
})
7373

7474
t.Run("no cross-asset conflation", func(t *testing.T) {

0 commit comments

Comments
 (0)