elastic / elastic/cloudbeat

AWS SDK: Use paginators

Open
#1,192 0 comments 1 reaction 0 assignees View on GitHub
cloudbeat Team:Cloud Security technical debt
Dominant language
Go
Stars
58
Forks
55
Avg merge
9h 9m
Merged PRs (30d)
424

Description

**Motivation**
The AWS go SDK provides paginators for list operation of its API: https://aws.github.io/aws-sdk-go-v2/docs/making-requests/#using-paginators

Instead, in our codebase, we are using the manual way of paginating which involves checking the `NextToken` in the output and copying to the input if not empty. This method is more error-prone (higher chance the developer forgets something) and more verbose.

I propose to switch to using the SDK paginators where applicable, for example:

```diff
diff --git a/resources/providers/awslib/iam/access_analyzer.go b/resources/providers/awslib/iam/access_analyzer.go
index b498cabf..fdfc28a7 100644
--- a/resources/providers/awslib/iam/access_analyzer.go
+++ b/resources/providers/awslib/iam/access_analyzer.go
@@ -85,17 +85,13 @@ func (p Provider) GetAccessAnalyzers(ctx context.Context) (awslib.AwsResource, e
func getAccessAnalyzersForRegion(ctx context.Context, region string, c AccessAnalyzerClient) (analyzersForRegion, error) {
analyzers := make([]types.AnalyzerSummary, 0)

- input := &accessanalyzer.ListAnalyzersInput{}
- for {
- out, err := c.ListAnalyzers(ctx, input)
+ paginator := accessanalyzer.NewListAnalyzersPaginator(c, &accessanalyzer.ListAnalyzersInput{})
+ for paginator.HasMorePages() {
+ out, err := paginator.NextPage(ctx)
if err != nil {
return analyzersForRegion{}, err
}
analyzers = append(analyzers, out.Analyzers...)
- if out.NextToken == nil {
- break
- }
- input.NextToken = out.NextToken
}

return analyzersForRegion{
```

**Definition of done**
What needs to be completed at the end of this task
- [ ] Use paginators throughout the codebase instead of `NextToken`
- [ ] Tests do not change (no functional difference)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.