Skip to content

Commit e4d8f9b

Browse files
🐛 Bring back resources discovery in AWS. (#5997)
1 parent 7173d37 commit e4d8f9b

File tree

2 files changed

+76
-39
lines changed

2 files changed

+76
-39
lines changed

providers/aws/resources/discovery.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package resources
55

66
import (
7-
"slices"
8-
97
"github.com/aws/aws-sdk-go-v2/aws/arn"
108
"github.com/rs/zerolog/log"
119
"go.mondoo.com/cnquery/v12/llx"
@@ -157,25 +155,30 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
157155

158156
func getDiscoveryTargets(config *inventory.Config) []string {
159157
targets := config.Discover.Targets
158+
160159
if len(targets) == 0 {
161160
return Auto
162161
}
163-
if stringx.ContainsAnyOf(targets, DiscoveryAll) {
162+
163+
if stringx.Contains(targets, DiscoveryAll) {
164164
// return the All list + All Api Resources list
165165
return allDiscovery()
166166
}
167-
if stringx.ContainsAnyOf(targets, DiscoveryAuto) {
168-
for i, target := range targets {
169-
if target == DiscoveryAuto {
170-
// remove the auto keyword
171-
targets = slices.Delete(targets, i, i+1)
172-
}
167+
168+
// the targets we return.
169+
res := []string{}
170+
for _, target := range targets {
171+
switch target {
172+
case DiscoveryAuto:
173+
res = append(res, Auto...)
174+
case DiscoveryResources:
175+
res = append(res, AllAPIResources...)
176+
default:
177+
res = append(res, target)
173178
}
174-
// add in the required discovery targets
175-
return append(targets, Auto...)
176179
}
177-
// random assortment of targets
178-
return targets
180+
181+
return stringx.DedupStringArray(res)
179182
}
180183

181184
// for now we have to post process the filters

providers/aws/resources/discovery_test.go

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package resources
55

66
import (
7-
"sort"
87
"testing"
98
"time"
109

@@ -75,32 +74,67 @@ func TestAddConnInfoToEc2Instances(t *testing.T) {
7574
}
7675

7776
func TestGetDiscoveryTargets(t *testing.T) {
78-
config := &inventory.Config{
79-
Discover: &inventory.Discovery{
80-
Targets: []string{},
77+
cases := []struct {
78+
name string
79+
targets []string
80+
want []string
81+
}{
82+
{
83+
name: "empty",
84+
targets: []string{},
85+
want: Auto,
86+
},
87+
{
88+
name: "all",
89+
targets: []string{"all"},
90+
want: allDiscovery(),
91+
},
92+
{
93+
name: "auto",
94+
targets: []string{"auto"},
95+
want: Auto,
96+
},
97+
{
98+
name: "resources",
99+
targets: []string{"resources"},
100+
want: AllAPIResources,
101+
},
102+
{
103+
name: "auto and resources",
104+
targets: []string{"auto", "resources"},
105+
want: append(Auto, AllAPIResources...),
106+
},
107+
{
108+
name: "all and resources",
109+
targets: []string{"all", "resources"},
110+
want: allDiscovery(),
111+
},
112+
{
113+
name: "all, auto and resources",
114+
targets: []string{"all", "resources"},
115+
want: allDiscovery(),
116+
},
117+
{
118+
name: "random",
119+
targets: []string{"s3-buckets", "iam-users", "instances"},
120+
want: []string{DiscoveryS3Buckets, DiscoveryIAMUsers, DiscoveryInstances},
121+
},
122+
{
123+
name: "duplicates",
124+
targets: []string{"auto", "s3-buckets", "iam-users", "s3-buckets", "auto"},
125+
want: append(Auto, []string{DiscoveryS3Buckets, DiscoveryIAMUsers}...),
81126
},
82127
}
83-
// test all with other stuff
84-
config.Discover.Targets = []string{"all", "projects", "instances"}
85-
require.Equal(t, allDiscovery(), getDiscoveryTargets(config))
86-
87-
// test just all
88-
config.Discover.Targets = []string{"all"}
89-
require.Equal(t, allDiscovery(), getDiscoveryTargets(config))
90128

91-
// test auto with other stuff
92-
config.Discover.Targets = []string{"auto", "s3-buckets", "iam-users"}
93-
res := append(Auto, []string{DiscoveryS3Buckets, DiscoveryIAMUsers}...)
94-
sort.Strings(res)
95-
targets := getDiscoveryTargets(config)
96-
sort.Strings(targets)
97-
require.Equal(t, res, targets)
98-
99-
// test just auto
100-
config.Discover.Targets = []string{"auto"}
101-
require.Equal(t, Auto, getDiscoveryTargets(config))
102-
103-
// test random
104-
config.Discover.Targets = []string{"s3-buckets", "iam-users", "instances"}
105-
require.Equal(t, []string{DiscoveryS3Buckets, DiscoveryIAMUsers, DiscoveryInstances}, getDiscoveryTargets(config))
129+
for _, tc := range cases {
130+
t.Run(tc.name, func(t *testing.T) {
131+
config := &inventory.Config{
132+
Discover: &inventory.Discovery{
133+
Targets: tc.targets,
134+
},
135+
}
136+
got := getDiscoveryTargets(config)
137+
require.ElementsMatch(t, tc.want, got)
138+
})
139+
}
106140
}

0 commit comments

Comments
 (0)