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

Migrate to aws sdk v2 part 1 #772

Merged
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
38 changes: 20 additions & 18 deletions aws/resources/access_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@ import (
"context"
"sync"

"github.com/aws/aws-sdk-go/service/accessanalyzer"
"github.com/aws/aws-sdk-go-v2/service/accessanalyzer"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/go-commons/errors"
"github.com/hashicorp/go-multierror"
)

func (analyzer *AccessAnalyzer) getAll(c context.Context, configObj config.Config) ([]*string, error) {
allAnalyzers := []*string{}
err := analyzer.Client.ListAnalyzersPagesWithContext(
analyzer.Context,
&accessanalyzer.ListAnalyzersInput{},
func(page *accessanalyzer.ListAnalyzersOutput, lastPage bool) bool {
for _, analyzer := range page.Analyzers {
if configObj.AccessAnalyzer.ShouldInclude(config.ResourceValue{
Time: analyzer.CreatedAt,
Name: analyzer.Name,
}) {
allAnalyzers = append(allAnalyzers, analyzer.Name)
}
var allAnalyzers []*string
paginator := accessanalyzer.NewListAnalyzersPaginator(analyzer.Client, &accessanalyzer.ListAnalyzersInput{})
for paginator.HasMorePages() {
page, err := paginator.NextPage(c)
if err != nil {
return nil, errors.WithStackTrace(err)
}

for _, check := range page.Analyzers {
if configObj.AccessAnalyzer.ShouldInclude(config.ResourceValue{
Time: check.CreatedAt,
Name: check.Name,
}) {
allAnalyzers = append(allAnalyzers, check.Name)
}
return !lastPage
},
)
return allAnalyzers, errors.WithStackTrace(err)
}
}

return allAnalyzers, nil
}

func (analyzer *AccessAnalyzer) nukeAll(names []*string) error {
Expand Down Expand Up @@ -76,7 +78,7 @@ func (analyzer *AccessAnalyzer) deleteAccessAnalyzerAsync(wg *sync.WaitGroup, er
defer wg.Done()

input := &accessanalyzer.DeleteAnalyzerInput{AnalyzerName: analyzerName}
_, err := analyzer.Client.DeleteAnalyzerWithContext(
_, err := analyzer.Client.DeleteAnalyzer(
analyzer.Context, input,
)
errChan <- err
Expand Down
39 changes: 17 additions & 22 deletions aws/resources/access_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,43 @@ import (
"time"

"github.com/aws/aws-sdk-go-v2/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/accessanalyzer"
"github.com/aws/aws-sdk-go/service/accessanalyzer/accessanalyzeriface"
"github.com/aws/aws-sdk-go-v2/service/accessanalyzer"
"github.com/aws/aws-sdk-go-v2/service/accessanalyzer/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/stretchr/testify/require"
)

type mockedAccessAnalyzer struct {
accessanalyzeriface.AccessAnalyzerAPI
ListAnalyzersPagesOutput accessanalyzer.ListAnalyzersOutput
DeleteAnalyzerOutput accessanalyzer.DeleteAnalyzerOutput
AccessAnalyzerAPI
ListAnalyzersOutput accessanalyzer.ListAnalyzersOutput
DeleteAnalyzerOutput accessanalyzer.DeleteAnalyzerOutput
}

func (m mockedAccessAnalyzer) ListAnalyzersPagesWithContext(_ awsgo.Context, _ *accessanalyzer.ListAnalyzersInput, callback func(*accessanalyzer.ListAnalyzersOutput, bool) bool, _ ...request.Option) error {
callback(&m.ListAnalyzersPagesOutput, true)
return nil
func (m mockedAccessAnalyzer) ListAnalyzers(context.Context, *accessanalyzer.ListAnalyzersInput, ...func(*accessanalyzer.Options)) (*accessanalyzer.ListAnalyzersOutput, error) {
return &m.ListAnalyzersOutput, nil
}

func (m mockedAccessAnalyzer) DeleteAnalyzerWithContext(awsgo.Context, *accessanalyzer.DeleteAnalyzerInput, ...request.Option) (*accessanalyzer.DeleteAnalyzerOutput, error) {
func (m mockedAccessAnalyzer) DeleteAnalyzer(ctx context.Context, params *accessanalyzer.DeleteAnalyzerInput, optFns ...func(*accessanalyzer.Options)) (*accessanalyzer.DeleteAnalyzerOutput, error) {
return &m.DeleteAnalyzerOutput, nil
}

func TestAccessAnalyzer_GetAll(t *testing.T) {

t.Parallel()

now := time.Now()
testName1 := "test1"
testName2 := "test2"
aa := AccessAnalyzer{
Client: mockedAccessAnalyzer{
ListAnalyzersPagesOutput: accessanalyzer.ListAnalyzersOutput{
Analyzers: []*accessanalyzer.AnalyzerSummary{
ListAnalyzersOutput: accessanalyzer.ListAnalyzersOutput{
Analyzers: []types.AnalyzerSummary{
{
Name: awsgo.String(testName1),
CreatedAt: awsgo.Time(now),
Name: aws.String(testName1),
CreatedAt: aws.Time(now),
},
{
Name: awsgo.String(testName2),
CreatedAt: awsgo.Time(now.Add(1)),
Name: aws.String(testName2),
CreatedAt: aws.Time(now.Add(1)),
},
},
},
Expand Down Expand Up @@ -79,20 +75,19 @@ func TestAccessAnalyzer_GetAll(t *testing.T) {
expected: []string{testName1},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
names, err := aa.getAll(context.Background(), config.Config{
AccessAnalyzer: tc.configObj,
})
require.NoError(t, err)
require.Equal(t, tc.expected, awsgo.StringValueSlice(names))
require.Equal(t, tc.expected, aws.ToStringSlice(names))
})
}

}

func TestAccessAnalyzer_NukeAll(t *testing.T) {

t.Parallel()

aa := AccessAnalyzer{
Expand All @@ -101,6 +96,6 @@ func TestAccessAnalyzer_NukeAll(t *testing.T) {
},
}

err := aa.nukeAll([]*string{awsgo.String("test")})
err := aa.nukeAll([]*string{aws.String("test")})
require.NoError(t, err)
}
25 changes: 15 additions & 10 deletions aws/resources/access_analyzer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ package resources
import (
"context"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/accessanalyzer"
"github.com/aws/aws-sdk-go/service/accessanalyzer/accessanalyzeriface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/accessanalyzer"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/go-commons/errors"
)

type AccessAnalyzerAPI interface {
DeleteAnalyzer(ctx context.Context, params *accessanalyzer.DeleteAnalyzerInput, optFns ...func(*accessanalyzer.Options)) (*accessanalyzer.DeleteAnalyzerOutput, error)
ListAnalyzers(context.Context, *accessanalyzer.ListAnalyzersInput, ...func(*accessanalyzer.Options)) (*accessanalyzer.ListAnalyzersOutput, error)
}

// AccessAnalyzer - represents all AWS secrets manager secrets that should be deleted.
type AccessAnalyzer struct {
BaseAwsResource
Client accessanalyzeriface.AccessAnalyzerAPI
Client AccessAnalyzerAPI
Region string
AnalyzerNames []string
}

func (analyzer *AccessAnalyzer) Init(session *session.Session) {
analyzer.Client = accessanalyzer.New(session)
func (analyzer *AccessAnalyzer) InitV2(cfg aws.Config) {
analyzer.Client = accessanalyzer.NewFromConfig(cfg)
}

func (analyzer *AccessAnalyzer) IsUsingV2() bool { return true }

// ResourceName - the simple name of the aws resource
func (analyzer *AccessAnalyzer) ResourceName() string {
return "accessanalyzer"
Expand All @@ -40,7 +45,7 @@ func (analyzer *AccessAnalyzer) MaxBatchSize() int {
return 10
}

// To get the resource configuration
// GetAndSetResourceConfig To get the resource configuration
func (analyzer *AccessAnalyzer) GetAndSetResourceConfig(configObj config.Config) config.ResourceType {
return configObj.AccessAnalyzer
}
Expand All @@ -51,13 +56,13 @@ func (analyzer *AccessAnalyzer) GetAndSetIdentifiers(c context.Context, configOb
return nil, err
}

analyzer.AnalyzerNames = awsgo.StringValueSlice(identifiers)
analyzer.AnalyzerNames = aws.ToStringSlice(identifiers)
return analyzer.AnalyzerNames, nil
}

// Nuke - nuke 'em all!!!
func (analyzer *AccessAnalyzer) Nuke(identifiers []string) error {
if err := analyzer.nukeAll(awsgo.StringSlice(identifiers)); err != nil {
if err := analyzer.nukeAll(aws.StringSlice(identifiers)); err != nil {
return errors.WithStackTrace(err)
}

Expand Down
49 changes: 19 additions & 30 deletions aws/resources/acm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/service/acm"
"github.com/aws/aws-sdk-go-v2/service/acm"
"github.com/aws/aws-sdk-go-v2/service/acm/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
Expand All @@ -13,40 +14,29 @@ import (

// Returns a list of strings of ACM ARNs
func (a *ACM) getAll(c context.Context, configObj config.Config) ([]*string, error) {
var acmArns []*string
paginator := acm.NewListCertificatesPaginator(a.Client, &acm.ListCertificatesInput{})
for paginator.HasMorePages() {
page, err := paginator.NextPage(c)
if err != nil {
return nil, errors.WithStackTrace(err)
}

params := &acm.ListCertificatesInput{}

acmArns := []*string{}
err := a.Client.ListCertificatesPagesWithContext(a.Context, params,
func(page *acm.ListCertificatesOutput, lastPage bool) bool {
for i := range page.CertificateSummaryList {
logging.Debug(fmt.Sprintf("Found ACM %s with domain name %s",
*page.CertificateSummaryList[i].CertificateArn, *page.CertificateSummaryList[i].DomainName))
if a.shouldInclude(page.CertificateSummaryList[i], configObj) {
logging.Debug(fmt.Sprintf(
"Including ACM %s", *page.CertificateSummaryList[i].CertificateArn))
acmArns = append(acmArns, page.CertificateSummaryList[i].CertificateArn)
} else {
logging.Debug(fmt.Sprintf(
"Skipping ACM %s", *page.CertificateSummaryList[i].CertificateArn))
}
for _, cert := range page.CertificateSummaryList {
logging.Debug(fmt.Sprintf("Found ACM %s with domain name %s", *cert.CertificateArn, *cert.DomainName))
if a.shouldInclude(cert, configObj) {
logging.Debug(fmt.Sprintf("Including ACM %s", *cert.CertificateArn))
acmArns = append(acmArns, cert.CertificateArn)
} else {
logging.Debug(fmt.Sprintf("Skipping ACM %s", *cert.CertificateArn))
}

return !lastPage
},
)
if err != nil {
return nil, errors.WithStackTrace(err)
}
}

return acmArns, nil
}

func (a *ACM) shouldInclude(acm *acm.CertificateSummary, configObj config.Config) bool {
if acm == nil {
return false
}

func (a *ACM) shouldInclude(acm types.CertificateSummary, configObj config.Config) bool {
if acm.InUse != nil && *acm.InUse {
logging.Debug(fmt.Sprintf("ACM %s is in use", *acm.CertificateArn))
return false
Expand All @@ -69,14 +59,13 @@ func (a *ACM) nukeAll(arns []*string) error {
}

logging.Debugf("Deleting all ACMs in region %s", a.Region)

deletedCount := 0
for _, acmArn := range arns {
params := &acm.DeleteCertificateInput{
CertificateArn: acmArn,
}

_, err := a.Client.DeleteCertificateWithContext(a.Context, params)
_, err := a.Client.DeleteCertificate(a.Context, params)
if err != nil {
logging.Debugf("[Failed] %s", err)
} else {
Expand Down
Loading