Skip to content

Commit 7734d20

Browse files
🐛 Fix ebs metadata throwing panics if the connection is not found. (#5573)
Signed-off-by: Preslav <preslav@mondoo.com>
1 parent d6ba06c commit 7734d20

3 files changed

Lines changed: 74 additions & 2 deletions

File tree

providers/os/connection/fs/filesystem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var (
2121
)
2222

2323
func NewFileSystemConnectionWithClose(id uint32, conf *inventory.Config, asset *inventory.Asset, closeFN func()) (*FileSystemConnection, error) {
24-
path, ok := conf.Options["path"]
24+
path, ok := conf.GetOptions()["path"]
2525
if !ok {
2626
// fallback to host + path option
2727
path = conf.Host + conf.Path

providers/os/id/awsebs/awsebs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (m *ebsMetadata) extractInjectedPlatformID() (string, *awsec2.MondooInstanc
148148
return false
149149
})
150150

151-
if index > 0 {
151+
if index < 0 {
152152
log.Debug().Msgf("awsebs.metadata> no connection found of type %s", shared.Type_FileSystem.String())
153153
return "", nil, false
154154
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Mondoo, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package awsebs
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
11+
"go.mondoo.com/cnquery/v11/providers/os/connection/fs"
12+
"go.mondoo.com/cnquery/v11/providers/os/connection/local"
13+
"go.mondoo.com/cnquery/v11/providers/os/connection/shared"
14+
)
15+
16+
func TestExtractInjectedPlatformId(t *testing.T) {
17+
t.Run("without fs connection", func(t *testing.T) {
18+
a := &inventory.Asset{Mrn: "test"}
19+
osConn := local.NewConnection(1, nil, a)
20+
m := &ebsMetadata{conn: osConn}
21+
platformId, instanceId, exists := m.extractInjectedPlatformID()
22+
require.Empty(t, platformId)
23+
require.Empty(t, instanceId)
24+
require.False(t, exists)
25+
})
26+
27+
t.Run("with fs connection, but asset has no connections", func(t *testing.T) {
28+
platformId := "//platformid.api.mondoo.app/runtime/aws/ec2/v1/accounts/185972265011/regions/us-east-1/instances/i-07f67838ada5879af"
29+
conn := &inventory.Config{
30+
Type: shared.Type_FileSystem.String(),
31+
Options: map[string]string{
32+
"inject-platform-ids": platformId,
33+
// required to init the fs conn
34+
"path": "test",
35+
},
36+
}
37+
a := &inventory.Asset{Mrn: "test"}
38+
fsConn, err := fs.NewConnection(1, conn, a)
39+
require.NoError(t, err)
40+
41+
m := &ebsMetadata{conn: fsConn}
42+
platformId, instanceId, exists := m.extractInjectedPlatformID()
43+
require.Empty(t, platformId)
44+
require.Empty(t, instanceId)
45+
require.False(t, exists)
46+
})
47+
48+
t.Run("with fs connection and asset has connections", func(t *testing.T) {
49+
platformId := "//platformid.api.mondoo.app/runtime/aws/ec2/v1/accounts/185972265011/regions/us-east-1/instances/i-07f67838ada5879af"
50+
assetConns := []*inventory.Config{
51+
{
52+
Type: shared.Type_FileSystem.String(),
53+
Options: map[string]string{
54+
"inject-platform-ids": platformId,
55+
// required to init the fs conn
56+
"path": "test",
57+
},
58+
},
59+
}
60+
a := &inventory.Asset{Mrn: "test", Connections: assetConns}
61+
fsConn, err := fs.NewConnection(1, assetConns[0], a)
62+
require.NoError(t, err)
63+
64+
m := &ebsMetadata{conn: fsConn}
65+
actualPlatformId, instanceId, exists := m.extractInjectedPlatformID()
66+
require.Equal(t, platformId, actualPlatformId)
67+
require.Equal(t, "i-07f67838ada5879af", instanceId.Id)
68+
require.Equal(t, "185972265011", instanceId.Account)
69+
require.Equal(t, "us-east-1", instanceId.Region)
70+
require.True(t, exists)
71+
})
72+
}

0 commit comments

Comments
 (0)