From 01818c3c9ca1089941294430053c5368ac6f9cfc Mon Sep 17 00:00:00 2001 From: Edwin Date: Wed, 28 Feb 2024 11:21:14 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Properly=20target=20the=20region?= =?UTF-8?q?=20filter.=20(#3225)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Properly target the region filter. * Enhance the check for other regions. * Test firstNonEmptyRegion. * Add an example to the flag. * Update providers/aws/config/config.go * Implement PR feedbacks. * Remove unneeded * Fix the ec2 filter. --------- Co-authored-by: Christoph Hartmann --- providers/aws/config/config.go | 2 +- providers/aws/connection/connection.go | 9 +++------ providers/aws/provider/provider.go | 2 +- providers/aws/resources/aws_ec2.go | 3 +++ providers/aws/resources/discovery.go | 27 ++++++++++++++------------ 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/providers/aws/config/config.go b/providers/aws/config/config.go index 2234b6e1e5..4c6bc162fe 100644 --- a/providers/aws/config/config.go +++ b/providers/aws/config/config.go @@ -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", }, }, }, diff --git a/providers/aws/connection/connection.go b/providers/aws/connection/connection.go index 4053ad8970..ecdf1a78ad 100644 --- a/providers/aws/connection/connection.go +++ b/providers/aws/connection/connection.go @@ -126,18 +126,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 case k == "ec2:region": d.Ec2DiscoveryFilters.Regions = append(d.Ec2DiscoveryFilters.Regions, v) - case k == "all:region": + case k == "all:region", k == "region": 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) } diff --git a/providers/aws/provider/provider.go b/providers/aws/provider/provider.go index 0679ab98c1..4629704a25 100644 --- a/providers/aws/provider/provider.go +++ b/providers/aws/provider/provider.go @@ -88,7 +88,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") { o[k] = string(v.Value) } } diff --git a/providers/aws/resources/aws_ec2.go b/providers/aws/resources/aws_ec2.go index 4dd9461234..23e2a6840d 100644 --- a/providers/aws/resources/aws_ec2.go +++ b/providers/aws/resources/aws_ec2.go @@ -618,6 +618,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 { + regions = conn.Filters.Ec2DiscoveryFilters.Regions + } for _, region := range regions { regionVal := region f := func() (jobpool.JobResult, error) { diff --git a/providers/aws/resources/discovery.go b/providers/aws/resources/discovery.go index e031ee345f..d1fc2edf99 100644 --- a/providers/aws/resources/discovery.go +++ b/providers/aws/resources/discovery.go @@ -112,20 +112,23 @@ func containsInterfaceSlice(sl []interface{}, s string) bool { } func instanceMatchesFilters(instance *mqlAwsEc2Instance, filters connection.DiscoveryFilters) bool { - 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 } @@ -134,7 +137,7 @@ func instanceMatchesFilters(instance *mqlAwsEc2Instance, filters connection.Disc } } } - return matches + return true } func imageMatchesFilters(image *mqlAwsEcrImage, filters connection.DiscoveryFilters) bool {