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

🐛 Properly target the region filter. #3225

Merged
merged 8 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion providers/aws/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var Config = plugin.Provider{
Long: "filters",
Type: plugin.FlagType_KeyValue,
Default: "",
Desc: "Filter options",
Desc: "Filter options e.g cnquery shell aws --filters region=us-east-2",
montera82 marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
Expand Down
10 changes: 9 additions & 1 deletion providers/aws/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewAwsConnection(id uint32, asset *inventory.Asset, conf *inventory.Config)
c.connectionOptions = asset.Options
if conf.Discover != nil {
c.Filters = parseOptsToFilters(conf.Discover.Filter)
c.RegionLimits = c.Filters.GeneralDiscoveryFilters.Regions
c.RegionLimits = firstNonEmptyRegion(c.Filters.GeneralDiscoveryFilters.Regions, c.Filters.Ec2DiscoveryFilters.Regions)
montera82 marked this conversation as resolved.
Show resolved Hide resolved
}
return c, nil
}
Expand Down Expand Up @@ -288,6 +288,14 @@ func CheckRegion(cfg aws.Config) error {
return nil
}

// firstNonEmptyRegion determines and return the first non-empty region.
func firstNonEmptyRegion(generalRegion, ec2Region []string) []string {
if len(ec2Region) > 0 {
return ec2Region
}
return generalRegion
}

func CheckIam(cfg aws.Config) (*sts.GetCallerIdentityOutput, error) {
ctx := context.Background()
svc := sts.NewFromConfig(cfg)
Expand Down
42 changes: 42 additions & 0 deletions providers/aws/connection/connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package connection

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestFirstNonEmptyRegion(t *testing.T) {
tests := []struct {
montera82 marked this conversation as resolved.
Show resolved Hide resolved
name string
generalRegion []string
ec2Region []string
expected []string
}{
{
name: "EC2Region Non-Empty, GeneralRegion Empty",
generalRegion: []string{},
ec2Region: []string{"us-west-2", "us-east-1"},
expected: []string{"us-west-2", "us-east-1"},
},
{
name: "EC2Region Empty, GeneralRegion Non-Empty",
generalRegion: []string{"eu-central-1", "eu-west-3"},
ec2Region: []string{},
expected: []string{"eu-central-1", "eu-west-3"},
},
{
name: "Both Empty",
generalRegion: []string{},
ec2Region: []string{},
expected: []string{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := firstNonEmptyRegion(tt.generalRegion, tt.ec2Region)
require.Equal(t, tt.expected, result, "firstNonEmptyRegion did not return the expected result")
})
}
}
2 changes: 1 addition & 1 deletion providers/aws/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func parseFlagsToFiltersOpts(m map[string]*llx.Primitive) map[string]string {
if k == "instance-id" {
o[k] = string(v.Value)
}
if strings.Contains(k, ":region") {
if strings.Contains(k, "region") {
montera82 marked this conversation as resolved.
Show resolved Hide resolved
o[k] = string(v.Value)
}
}
Expand Down
Loading