🐛 handle asset explorer discovery targets properly#7089
Merged
Conversation
There was a problem hiding this comment.
Discovery target filtering for k8s namespaces works correctly; documentation and skill guide updated accordingly.
Additional findings (file/line not in diff):
- 🟡
providers/k8s/resources/discovery.go:192— Pre-existing issue (not introduced by this PR): ifinvConfig.Discover == nilandconn.Asset() == nil, the early return is skipped and the function will panic at line 217 (invConfig.Discover.Targets).discoverNamespaceStage(line 273) handles this correctly by returning unconditionally whenDiscoveris nil. Consider aligning the guard here, e.g.:
if invConfig.Discover == nil || len(invConfig.Discover.Targets) == 0 {
if conn.Asset() != nil {
in.Spec.Assets = append(in.Spec.Assets, conn.Asset())
}
return in, nil
}
This comment has been minimized.
This comment has been minimized.
Signed-off-by: Ivan Milchev <ivan@mondoo.com>
f4aab07 to
a9e2d77
Compare
Signed-off-by: Ivan Milchev <ivan@mondoo.com>
a9e2d77 to
9730353
Compare
| @@ -1,4 +1,4 @@ | |||
| // Copyright (c) Mondoo, Inc. | |||
| // Copyright Mondoo, Inc. 2024, 2026 | |||
There was a problem hiding this comment.
🟡 warning — This is a generated file (Code generated by resources. DO NOT EDIT.) with an unrelated copyright header change. It shouldn't be part of this PR — was it accidentally included? If the generator changed the copyright format, that change should be in a separate commit/PR.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes discovery target filtering for staged discovery. Previously,
--discover podswould still scan intermediate hierarchy levels (namespaces) because all discovered assets with platform IDs were treated as scannable.Problem
With staged discovery (
AssetExplorer), the K8s provider emits namespace assets in Stage 1 so thatAssetExplorercan connect to them and trigger Stage 2 (workload discovery). But the scanner treats every asset with a platform ID as scannable — so namespaces were added to the progress bar and scanned even when the user only asked for pods.Solution
Strip platform IDs from intermediate assets that don't match discovery targets. The existing "no platform IDs → skip" logic in
AssetExplorerand the scanner already handles these correctly:This requires zero changes to
AssetExploreror the scanner — it's purely a provider-side convention.Changes
providers/k8s/resources/discovery.go— IndiscoverClusterStage, check if namespaces are a discovery target. If not, setns.PlatformIds = nilbefore emitting. The namespace is still emitted with its connection config (triggering Stage 2), but skipped by the scanner.docs/adr/002-staged-discovery.md— New "Traversal-Only Assets" section documenting the pattern with code examples and a generalization table for K8s/GCP/AWS..claude/skills/staged-discovery/SKILL.md— New step for stripping platform IDs, updated verification and checklist.Example
Test plan
cnspec scan k8s --discover pods— only pods appear in the progress bar, namespaces are traversed but not scannedcnspec scan k8s --discover namespaces— namespaces are scanned (platform IDs preserved)cnspec scan k8s --discover auto— everything is scanned (backward compatible)cnspec scan k8s— default discovery unchangedgo build ./providers/k8s/...compiles🤖 Generated with Claude Code