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

feat: keep block devices mounted option #5076

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions providers/os/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ var Config = plugin.Provider{
Desc: "Include mounted block devices in the scan",
Option: plugin.FlagOption_Hidden,
},
{
Long: "keep-mounted",
Type: plugin.FlagType_Bool,
Desc: "Keep mounted block devices mounted after the scan",
Option: plugin.FlagOption_Hidden,
},
{
Long: "platform-ids",
Type: plugin.FlagType_List,
Expand Down
21 changes: 18 additions & 3 deletions providers/os/connection/device/device_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import (
"go.mondoo.com/cnquery/v11/providers/os/id/ids"
)

const PlatformIdInject = "inject-platform-ids"
const (
PlatformIdInject = "inject-platform-ids"
KeepMounted = "keep-mounted"
SkipAssetDetection = "skip-asset-detection"
)

type DeviceConnection struct {
*fs.FileSystemConnection
Expand All @@ -35,6 +39,9 @@ type DeviceConnection struct {
MountedDirs []string
// map of mountpoints to partition infos
partitions map[string]*snapshot.PartitionInfo

// whether to keep the devices mounted after the connection is closed
keepMounted bool
}

func getDeviceManager(conf *inventory.Config) (DeviceManager, error) {
Expand Down Expand Up @@ -85,13 +92,16 @@ func NewDeviceConnection(connId uint32, conf *inventory.Config, asset *inventory
if conf.Options == nil {
conf.Options = make(map[string]string)
}
res.keepMounted = conf.Options[KeepMounted] == "true"

if len(asset.IdDetector) == 0 {
asset.IdDetector = []string{ids.IdDetector_Hostname, ids.IdDetector_SshHostkey}
}

res.partitions = make(map[string]*snapshot.PartitionInfo)

skipAssetDetection := conf.Options[SkipAssetDetection] == "true"

// we iterate over all the blocks and try to run OS detection on each one of them
// we only return one asset, if we find the right block (e.g. the one with the root FS)
for _, block := range blocks {
Expand All @@ -117,6 +127,11 @@ func NewDeviceConnection(connId uint32, conf *inventory.Config, asset *inventory
continue
}

if skipAssetDetection {
log.Debug().Msg("device connection> skipping asset detection as requested")
continue
}

if fsConn, err := tryDetectAsset(connId, block, conf, asset); err != nil {
log.Error().Err(err).Msg("partition did not return an asset, continuing")
} else {
Expand All @@ -125,7 +140,7 @@ func NewDeviceConnection(connId uint32, conf *inventory.Config, asset *inventory
}

// if none of the blocks returned a platform that we could detect, we return an error
if asset.Platform == nil {
if asset.Platform == nil && !skipAssetDetection {
res.Close()
return nil, errors.New("device connection> no platform detected")
}
Expand All @@ -139,7 +154,7 @@ func (c *DeviceConnection) Close() {
return
}

if c.deviceManager != nil {
if c.deviceManager != nil && !c.keepMounted {
c.deviceManager.UnmountAndClose()
}
}
Expand Down
3 changes: 3 additions & 0 deletions providers/os/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error)
if includeMounted, ok := flags["include-mounted"]; ok {
conf.Options["include-mounted"] = strconv.FormatBool(includeMounted.RawData().Value.(bool))
}
if keepMounted, ok := flags["keep-mounted"]; ok {
conf.Options["keep-mounted"] = strconv.FormatBool(keepMounted.RawData().Value.(bool))
}

if platformIDs, ok := flags["platform-ids"]; ok {
platformIDs := platformIDs.Array
Expand Down
Loading