From 2013216d25c78aecbc3c0273a87050c757d1347b Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Sat, 17 Feb 2024 01:25:23 +0100 Subject: [PATCH 1/4] fix shell command Signed-off-by: Ivan Milchev --- apps/cnquery/cmd/shell.go | 23 +++++++++++------------ cli/components/assetselect.go | 6 +++--- explorer/scan/discovery.go | 6 +++--- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/apps/cnquery/cmd/shell.go b/apps/cnquery/cmd/shell.go index d8faaede13..b2b228ddc1 100644 --- a/apps/cnquery/cmd/shell.go +++ b/apps/cnquery/cmd/shell.go @@ -108,11 +108,17 @@ func StartShell(runtime *providers.Runtime, conf *ShellConfig) error { connectAsset := filteredAssets[0] if len(filteredAssets) > 1 { + invAssets := make([]*inventory.Asset, 0, len(filteredAssets)) + for _, a := range filteredAssets { + invAssets = append(invAssets, a.Asset) + } + isTTY := isatty.IsTerminal(os.Stdout.Fd()) if isTTY { - connectAsset = components.AssetSelect(filteredAssets) + selectedAsset := components.AssetSelect(invAssets) + connectAsset = filteredAssets[selectedAsset] } else { - fmt.Println(components.AssetList(theme.OperatingSystemTheme, filteredAssets)) + fmt.Println(components.AssetList(theme.OperatingSystemTheme, invAssets)) log.Fatal().Msg("cannot connect to more than one asset, use --platform-id to select a specific asset") } } @@ -121,19 +127,12 @@ func StartShell(runtime *providers.Runtime, conf *ShellConfig) error { log.Fatal().Msg("no asset selected") } - err = runtime.Connect(&plugin.ConnectReq{ - Features: conf.Features, - Asset: connectAsset, - Upstream: conf.UpstreamConfig, - }) - if err != nil { - log.Fatal().Err(err).Msg("failed to connect to asset") - } - log.Info().Msgf("connected to %s", runtime.Provider.Connection.Asset.Platform.Title) + log.Info().Msgf("connected to %s", connectAsset.Runtime.Provider.Connection.Asset.Platform.Title) // when we close the shell, we need to close the backend and store the recording onCloseHandler := func() { runtime.Close() + connectAsset.Runtime.Close() providers.Coordinator.Shutdown() } @@ -142,7 +141,7 @@ func StartShell(runtime *providers.Runtime, conf *ShellConfig) error { shellOptions = append(shellOptions, shell.WithFeatures(conf.Features)) shellOptions = append(shellOptions, shell.WithUpstreamConfig(conf.UpstreamConfig)) - sh, err := shell.New(runtime, shellOptions...) + sh, err := shell.New(connectAsset.Runtime, shellOptions...) if err != nil { log.Error().Err(err).Msg("failed to initialize interactive shell") } diff --git a/cli/components/assetselect.go b/cli/components/assetselect.go index fd5180e6aa..57d36cbfec 100644 --- a/cli/components/assetselect.go +++ b/cli/components/assetselect.go @@ -12,7 +12,7 @@ import ( "go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory" ) -func AssetSelect(assetList []*inventory.Asset) *inventory.Asset { +func AssetSelect(assetList []*inventory.Asset) int { list := make([]string, len(assetList)) // map asset name to list @@ -36,9 +36,9 @@ func AssetSelect(assetList []*inventory.Asset) *inventory.Asset { } if selection == -1 { - return nil + return -1 } selected := assetList[selection] log.Info().Int("selection", selection).Str("asset", selected.Name).Msg("selected asset") - return selected + return selection } diff --git a/explorer/scan/discovery.go b/explorer/scan/discovery.go index 9794f17feb..a3711aaa08 100644 --- a/explorer/scan/discovery.go +++ b/explorer/scan/discovery.go @@ -57,12 +57,12 @@ func (d *DiscoveredAssets) AddError(asset *inventory.Asset, err error) { d.Errors = append(d.Errors, &AssetWithError{Asset: asset, Err: err}) } -func (d *DiscoveredAssets) GetAssetsByPlatformID(platformID string) []*inventory.Asset { - var assets []*inventory.Asset +func (d *DiscoveredAssets) GetAssetsByPlatformID(platformID string) []*AssetWithRuntime { + var assets []*AssetWithRuntime for _, a := range d.Assets { for _, p := range a.Asset.PlatformIds { if platformID == "" || p == platformID { - assets = append(assets, a.Asset) + assets = append(assets, a) break } } From 510783ae3b19d72b3c554c6c3d99c6ec14b1d69e Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Sat, 17 Feb 2024 01:51:14 +0100 Subject: [PATCH 2/4] fix error Signed-off-by: Ivan Milchev --- apps/cnquery/cmd/shell.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/cnquery/cmd/shell.go b/apps/cnquery/cmd/shell.go index b2b228ddc1..38833fc160 100644 --- a/apps/cnquery/cmd/shell.go +++ b/apps/cnquery/cmd/shell.go @@ -124,7 +124,8 @@ func StartShell(runtime *providers.Runtime, conf *ShellConfig) error { } if connectAsset == nil { - log.Fatal().Msg("no asset selected") + log.Error().Msg("no asset selected") + os.Exit(1) } log.Info().Msgf("connected to %s", connectAsset.Runtime.Provider.Connection.Asset.Platform.Title) From 9e04a163005ad2738d8366bbe664d139aeee6bd0 Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Sat, 17 Feb 2024 01:53:33 +0100 Subject: [PATCH 3/4] fix tests Signed-off-by: Ivan Milchev --- explorer/scan/discovery_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/explorer/scan/discovery_test.go b/explorer/scan/discovery_test.go index 1e7afd4ecf..36e4350053 100644 --- a/explorer/scan/discovery_test.go +++ b/explorer/scan/discovery_test.go @@ -82,7 +82,7 @@ func TestDiscoveredAssets_GetAssetsByPlatformID(t *testing.T) { // Make sure adding duplicates is not possible assets := d.GetAssetsByPlatformID(allPlatformIds[0]) assert.Len(t, assets, 1) - assert.Equal(t, allPlatformIds[0], assets[0].PlatformIds[0]) + assert.Equal(t, allPlatformIds[0], assets[0].Asset.PlatformIds[0]) } func TestDiscoveredAssets_GetAssetsByPlatformID_Empty(t *testing.T) { @@ -110,7 +110,7 @@ func TestDiscoveredAssets_GetAssetsByPlatformID_Empty(t *testing.T) { assert.Len(t, assets, 10) platformIds := []string{} for _, a := range assets { - platformIds = append(platformIds, a.PlatformIds[0]) + platformIds = append(platformIds, a.Asset.PlatformIds[0]) } assert.ElementsMatch(t, allPlatformIds, platformIds) } From dc1fabed2c865f51ad748dee9ba75c153bf73f62 Mon Sep 17 00:00:00 2001 From: Victoria Jeffrey Date: Fri, 16 Feb 2024 20:10:07 -0700 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=A7=B9=20fix=20aws=20connection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- providers/aws/resources/discovery.go | 5 ----- providers/aws/resources/discovery_conversion.go | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/providers/aws/resources/discovery.go b/providers/aws/resources/discovery.go index a3e2795aea..e031ee345f 100644 --- a/providers/aws/resources/discovery.go +++ b/providers/aws/resources/discovery.go @@ -186,11 +186,6 @@ func Discover(runtime *plugin.Runtime, filters connection.DiscoveryFilters) (*in Assets: []*inventory.Asset{}, }} - if (conn.Conf == nil || len(conn.Conf.Discover.Targets) == 0) && conn.Asset() != nil { - in.Spec.Assets = append(in.Spec.Assets, conn.Asset()) - return in, nil - } - res, err := NewResource(runtime, "aws.account", map[string]*llx.RawData{"id": llx.StringData("aws.account/" + conn.AccountId())}) if err != nil { return nil, err diff --git a/providers/aws/resources/discovery_conversion.go b/providers/aws/resources/discovery_conversion.go index 4de1fd99a1..95f0f1cf93 100644 --- a/providers/aws/resources/discovery_conversion.go +++ b/providers/aws/resources/discovery_conversion.go @@ -204,7 +204,7 @@ func accountAsset(conn *connection.AwsConnection, awsAccount *mqlAwsAccount) *in PlatformIds: []string{id}, Name: name, Platform: connection.GetPlatformForObject(""), - Connections: []*inventory.Config{conn.Conf}, + Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))}, Options: conn.ConnectionOptions(), } }