From 5be13150849acd622dfee778ed13c20cdb11a3d1 Mon Sep 17 00:00:00 2001 From: Vedank Pande Date: Thu, 5 Oct 2023 00:13:47 +0530 Subject: [PATCH 1/5] feat: add lambda eventsourcemapping collector --- providers/aws/aws.go | 1 + providers/aws/lambda/eventsourcemappings.go | 56 +++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 providers/aws/lambda/eventsourcemappings.go diff --git a/providers/aws/aws.go b/providers/aws/aws.go index 701d741f3..ecf1e862e 100644 --- a/providers/aws/aws.go +++ b/providers/aws/aws.go @@ -39,6 +39,7 @@ func listOfSupportedServices() []providers.FetchDataFunction { ec2.Instances, ec2.ElasticIps, lambda.Functions, + lambda.EventSourceMappings, ec2.Acls, ec2.Subnets, ec2.SecurityGroups, diff --git a/providers/aws/lambda/eventsourcemappings.go b/providers/aws/lambda/eventsourcemappings.go new file mode 100644 index 000000000..7ad61d108 --- /dev/null +++ b/providers/aws/lambda/eventsourcemappings.go @@ -0,0 +1,56 @@ +package lambda + +import ( + "context" + "fmt" + "strings" + "time" + + "github.com/aws/aws-sdk-go-v2/service/lambda" + log "github.com/sirupsen/logrus" + "github.com/tailwarden/komiser/models" + "github.com/tailwarden/komiser/providers" +) + +func EventSourceMappings(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { + var config lambda.ListEventSourceMappingsInput + resources := make([]models.Resource, 0) + lambdaClient := lambda.NewFromConfig(*client.AWSClient) + + result, err := lambdaClient.ListEventSourceMappings(context.Background(), &config) + if err != nil { + log.Errorf("ERROR: Failed to fetch EventSourceMappings: %v", err) + return resources, err + } + + for _, mapping := range result.EventSourceMappings { + + lambdaNameSplit := strings.Split(*mapping.FunctionArn, ":") + lambdaName := lambdaNameSplit[len(lambdaNameSplit)-1] + + resources = append(resources, models.Resource{ + Provider: "AWS", + Account: client.Name, + Service: "EventSourceMapping", + ResourceId: *mapping.UUID, + Region: client.AWSClient.Region, + Name: *mapping.UUID, + Cost: 0.0, + Metadata: map[string]string{ + "lambda": *mapping.EventSourceArn, + "source": *mapping.EventSourceArn, + }, + FetchedAt: time.Now(), + Link: fmt.Sprintf("https://%s.console.aws.amazon.com/lambda/home?region=%s#/functions/%s", client.AWSClient.Region, client.AWSClient.Region, lambdaName), + }) + } + + log.WithFields(log.Fields{ + "provider": "AWS", + "account": client.Name, + "region": client.AWSClient.Region, + "service": "EventSourceMapping", + "resources": len(resources), + }).Info("Fetched resources") + return resources, nil +} \ No newline at end of file From bfd699bad8d405b74a1054ec870f94dd64e733b4 Mon Sep 17 00:00:00 2001 From: Vedank Pande Date: Thu, 5 Oct 2023 01:02:36 +0530 Subject: [PATCH 2/5] fix typo in lambda metadata value --- providers/aws/lambda/eventsourcemappings.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/aws/lambda/eventsourcemappings.go b/providers/aws/lambda/eventsourcemappings.go index 7ad61d108..65f7d96d8 100644 --- a/providers/aws/lambda/eventsourcemappings.go +++ b/providers/aws/lambda/eventsourcemappings.go @@ -37,7 +37,7 @@ func EventSourceMappings(ctx context.Context, client providers.ProviderClient) ( Name: *mapping.UUID, Cost: 0.0, Metadata: map[string]string{ - "lambda": *mapping.EventSourceArn, + "lambda": *mapping.FunctionArn, "source": *mapping.EventSourceArn, }, FetchedAt: time.Now(), From 3116702edc38808212cd9e524aba1cdcea04ebd9 Mon Sep 17 00:00:00 2001 From: Vedank Pande Date: Thu, 5 Oct 2023 01:12:58 +0530 Subject: [PATCH 3/5] imports formatting --- providers/aws/lambda/eventsourcemappings.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/providers/aws/lambda/eventsourcemappings.go b/providers/aws/lambda/eventsourcemappings.go index 65f7d96d8..20a8f3b1f 100644 --- a/providers/aws/lambda/eventsourcemappings.go +++ b/providers/aws/lambda/eventsourcemappings.go @@ -5,9 +5,10 @@ import ( "fmt" "strings" "time" - - "github.com/aws/aws-sdk-go-v2/service/lambda" + log "github.com/sirupsen/logrus" + + "github.com/aws/aws-sdk-go-v2/service/lambda" "github.com/tailwarden/komiser/models" "github.com/tailwarden/komiser/providers" ) From 056216de98f7e5da643321cf1d34868ef9ed03e5 Mon Sep 17 00:00:00 2001 From: Vedank Pande Date: Thu, 5 Oct 2023 12:17:07 +0530 Subject: [PATCH 4/5] fix: handle paginated output for eventsourcemappings --- providers/aws/lambda/eventsourcemappings.go | 67 ++++++++++++--------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/providers/aws/lambda/eventsourcemappings.go b/providers/aws/lambda/eventsourcemappings.go index 20a8f3b1f..d740868cf 100644 --- a/providers/aws/lambda/eventsourcemappings.go +++ b/providers/aws/lambda/eventsourcemappings.go @@ -5,45 +5,56 @@ import ( "fmt" "strings" "time" - + log "github.com/sirupsen/logrus" - + + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/lambda" "github.com/tailwarden/komiser/models" "github.com/tailwarden/komiser/providers" ) func EventSourceMappings(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { - var config lambda.ListEventSourceMappingsInput resources := make([]models.Resource, 0) lambdaClient := lambda.NewFromConfig(*client.AWSClient) - - result, err := lambdaClient.ListEventSourceMappings(context.Background(), &config) - if err != nil { - log.Errorf("ERROR: Failed to fetch EventSourceMappings: %v", err) - return resources, err + mappingsPerPage := aws.Int32(50) + params := &lambda.ListEventSourceMappingsInput{ + MaxItems: mappingsPerPage, } - for _, mapping := range result.EventSourceMappings { - - lambdaNameSplit := strings.Split(*mapping.FunctionArn, ":") - lambdaName := lambdaNameSplit[len(lambdaNameSplit)-1] - - resources = append(resources, models.Resource{ - Provider: "AWS", - Account: client.Name, - Service: "EventSourceMapping", - ResourceId: *mapping.UUID, - Region: client.AWSClient.Region, - Name: *mapping.UUID, - Cost: 0.0, - Metadata: map[string]string{ - "lambda": *mapping.FunctionArn, - "source": *mapping.EventSourceArn, - }, - FetchedAt: time.Now(), - Link: fmt.Sprintf("https://%s.console.aws.amazon.com/lambda/home?region=%s#/functions/%s", client.AWSClient.Region, client.AWSClient.Region, lambdaName), - }) + paginator := lambda.NewListEventSourceMappingsPaginator(lambdaClient, params, func(options *lambda.ListEventSourceMappingsPaginatorOptions) { + options.Limit = *mappingsPerPage + }) + + for paginator.HasMorePages() { + + output, err := paginator.NextPage(context.Background()) + if err != nil { + log.Errorf("ERROR: Error occurred while retrieving EventSourceMappings page: %v", err) + return resources, err + } + + for _, mapping := range output.EventSourceMappings { + + lambdaNameSplit := strings.Split(*mapping.FunctionArn, ":") + lambdaName := lambdaNameSplit[len(lambdaNameSplit)-1] + + resources = append(resources, models.Resource{ + Provider: "AWS", + Account: client.Name, + Service: "EventSourceMapping", + ResourceId: *mapping.UUID, + Region: client.AWSClient.Region, + Name: *mapping.UUID, + Cost: 0.0, + Metadata: map[string]string{ + "lambda": *mapping.FunctionArn, + "source": *mapping.EventSourceArn, + }, + FetchedAt: time.Now(), + Link: fmt.Sprintf("https://%s.console.aws.amazon.com/lambda/home?region=%s#/functions/%s", client.AWSClient.Region, client.AWSClient.Region, lambdaName), + }) + } } log.WithFields(log.Fields{ From ba64d5034b5d78360c552bcfcf3897d9ad97c40b Mon Sep 17 00:00:00 2001 From: Vedank Pande Date: Thu, 5 Oct 2023 13:18:01 +0530 Subject: [PATCH 5/5] update context passed to paginator --- providers/aws/lambda/eventsourcemappings.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/aws/lambda/eventsourcemappings.go b/providers/aws/lambda/eventsourcemappings.go index d740868cf..fa961ed80 100644 --- a/providers/aws/lambda/eventsourcemappings.go +++ b/providers/aws/lambda/eventsourcemappings.go @@ -28,7 +28,7 @@ func EventSourceMappings(ctx context.Context, client providers.ProviderClient) ( for paginator.HasMorePages() { - output, err := paginator.NextPage(context.Background()) + output, err := paginator.NextPage(ctx) if err != nil { log.Errorf("ERROR: Error occurred while retrieving EventSourceMappings page: %v", err) return resources, err