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 all 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 --filters region=us-east-2",
},
},
},
Expand Down
9 changes: 3 additions & 6 deletions providers/aws/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,15 @@ func parseOptsToFilters(opts map[string]string) DiscoveryFilters {
for k, v := range opts {
switch {
case strings.HasPrefix(k, "ec2:tag:"):
d.Ec2DiscoveryFilters.Tags[strings.TrimPrefix("ec2:tag:", k)] = v
d.Ec2DiscoveryFilters.Tags[strings.TrimPrefix(k, "ec2:tag:")] = v
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for that fix

case k == "ec2:region":
d.Ec2DiscoveryFilters.Regions = append(d.Ec2DiscoveryFilters.Regions, v)
case k == "all:region":
case k == "all:region", k == "region":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice addition

d.GeneralDiscoveryFilters.Regions = append(d.GeneralDiscoveryFilters.Regions, v)
case k == "region":
d.GeneralDiscoveryFilters.Regions = append(d.GeneralDiscoveryFilters.Regions, v)
d.Ec2DiscoveryFilters.Regions = append(d.Ec2DiscoveryFilters.Regions, v)
case k == "instance-id":
d.Ec2DiscoveryFilters.InstanceIds = append(d.Ec2DiscoveryFilters.InstanceIds, v)
case strings.HasPrefix(k, "all:tag:"):
d.GeneralDiscoveryFilters.Tags[strings.TrimPrefix("all:tag:", k)] = v
d.GeneralDiscoveryFilters.Tags[strings.TrimPrefix(k, "all:tag:")] = v
case k == "ecr:tag":
d.EcrDiscoveryFilters.Tags = append(d.EcrDiscoveryFilters.Tags, v)
}
Expand Down
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
3 changes: 3 additions & 0 deletions providers/aws/resources/aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,9 @@ func (a *mqlAwsEc2) getInstances(conn *connection.AwsConnection) []*jobpool.Job
if err != nil {
return []*jobpool.Job{{Err: err}}
}
if len(conn.Filters.Ec2DiscoveryFilters.Regions) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition!

regions = conn.Filters.Ec2DiscoveryFilters.Regions
}
for _, region := range regions {
regionVal := region
f := func() (jobpool.JobResult, error) {
Expand Down
27 changes: 15 additions & 12 deletions providers/aws/resources/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,23 @@ func containsInterfaceSlice(sl []interface{}, s string) bool {
}

func instanceMatchesFilters(instance *mqlAwsEc2Instance, filters connection.DiscoveryFilters) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method requires additional testing to ensure we cover all edge cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, more test is always nice, if i invest more time i would definately find some of those cases!

matches := true
f := filters.Ec2DiscoveryFilters
if len(f.Regions) > 0 {
if !contains(f.Regions, instance.Region.Data) {
matches = false
}
regions := []string{}
if len(filters.GeneralDiscoveryFilters.Regions) > 0 {
regions = append(regions, filters.GeneralDiscoveryFilters.Regions...)
}
if len(filters.Ec2DiscoveryFilters.Regions) > 0 {
regions = append(regions, filters.Ec2DiscoveryFilters.Regions...)
}
if len(regions) > 0 && !contains(regions, instance.Region.Data) {
return false
}
if len(f.InstanceIds) > 0 {
if !contains(f.InstanceIds, instance.InstanceId.Data) {
matches = false
if len(filters.Ec2DiscoveryFilters.InstanceIds) > 0 {
if !contains(filters.Ec2DiscoveryFilters.InstanceIds, instance.InstanceId.Data) {
return false
}
}
if len(f.Tags) > 0 {
for k, v := range f.Tags {
if len(filters.Ec2DiscoveryFilters.Tags) > 0 {
for k, v := range filters.Ec2DiscoveryFilters.Tags {
if instance.Tags.Data[k] == nil {
return false
}
Expand All @@ -134,7 +137,7 @@ func instanceMatchesFilters(instance *mqlAwsEc2Instance, filters connection.Disc
}
}
}
return matches
return true
}

func imageMatchesFilters(image *mqlAwsEcrImage, filters connection.DiscoveryFilters) bool {
Expand Down
Loading