From 8b966f1ec0ab6a85dfcad23abbf09dcb9b0bae4c Mon Sep 17 00:00:00 2001 From: Mikita Iwanowski Date: Wed, 8 Jan 2025 23:15:14 +0100 Subject: [PATCH 1/4] feat: keep mounted flag --- providers/os/config/config.go | 6 ++++++ providers/os/connection/device/device_connection.go | 7 +++++-- providers/os/provider/provider.go | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/providers/os/config/config.go b/providers/os/config/config.go index b2e1f4c43e..d6c53017c6 100644 --- a/providers/os/config/config.go +++ b/providers/os/config/config.go @@ -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, diff --git a/providers/os/connection/device/device_connection.go b/providers/os/connection/device/device_connection.go index 7057ffceef..163fa0a6ce 100644 --- a/providers/os/connection/device/device_connection.go +++ b/providers/os/connection/device/device_connection.go @@ -24,7 +24,10 @@ import ( "go.mondoo.com/cnquery/v11/providers/os/id/ids" ) -const PlatformIdInject = "inject-platform-ids" +const ( + PlatformIdInject = "inject-platform-ids" + KeepMounted = "keep-mounted" +) type DeviceConnection struct { *fs.FileSystemConnection @@ -139,7 +142,7 @@ func (c *DeviceConnection) Close() { return } - if c.deviceManager != nil { + if c.deviceManager != nil && c.Conf().Options[KeepMounted] != "true" { c.deviceManager.UnmountAndClose() } } diff --git a/providers/os/provider/provider.go b/providers/os/provider/provider.go index 481f5e43b6..5ce28538eb 100644 --- a/providers/os/provider/provider.go +++ b/providers/os/provider/provider.go @@ -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 From 28fb94afe3e49af14207d7e24277db92a6734014 Mon Sep 17 00:00:00 2001 From: Mikita Iwanowski Date: Wed, 8 Jan 2025 23:18:27 +0100 Subject: [PATCH 2/4] feat: skip asset detection context option --- providers/os/connection/device/device_connection.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/providers/os/connection/device/device_connection.go b/providers/os/connection/device/device_connection.go index 163fa0a6ce..43ff60fbf1 100644 --- a/providers/os/connection/device/device_connection.go +++ b/providers/os/connection/device/device_connection.go @@ -25,8 +25,9 @@ import ( ) const ( - PlatformIdInject = "inject-platform-ids" - KeepMounted = "keep-mounted" + PlatformIdInject = "inject-platform-ids" + KeepMounted = "keep-mounted" + SkipAssetDetection = "skip-asset-detection" ) type DeviceConnection struct { @@ -95,6 +96,8 @@ func NewDeviceConnection(connId uint32, conf *inventory.Config, asset *inventory 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 { @@ -120,6 +123,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 { From 300a2407b14ffda38e0a3f9fb05d44462bc8b63f Mon Sep 17 00:00:00 2001 From: Mikita Iwanowski Date: Thu, 9 Jan 2025 10:59:32 +0100 Subject: [PATCH 3/4] fix: access conn options --- providers/os/connection/device/device_connection.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/providers/os/connection/device/device_connection.go b/providers/os/connection/device/device_connection.go index 43ff60fbf1..44697fac7e 100644 --- a/providers/os/connection/device/device_connection.go +++ b/providers/os/connection/device/device_connection.go @@ -39,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) { @@ -89,6 +92,7 @@ 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} @@ -150,7 +154,7 @@ func (c *DeviceConnection) Close() { return } - if c.deviceManager != nil && c.Conf().Options[KeepMounted] != "true" { + if c.deviceManager != nil && !c.keepMounted { c.deviceManager.UnmountAndClose() } } From b9936cea431bf0189034792d4e2ba63c768042a5 Mon Sep 17 00:00:00 2001 From: Mikita Iwanowski Date: Thu, 9 Jan 2025 13:34:06 +0100 Subject: [PATCH 4/4] fix: prevent error when skipAssetDetection is enabled --- providers/os/connection/device/device_connection.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/os/connection/device/device_connection.go b/providers/os/connection/device/device_connection.go index 44697fac7e..428ae466b4 100644 --- a/providers/os/connection/device/device_connection.go +++ b/providers/os/connection/device/device_connection.go @@ -140,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") }