diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6d3a80cb8..345cd2f9e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -22,6 +22,13 @@ jobs: go install golang.org/x/lint/golint@latest - name: Checkout code uses: actions/checkout@v3 + - name: Check Formatting + run: | + if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then + echo "### Go formatting is off, please execute 'gofmt -w -s .' - see following diff: ###" + gofmt -s -d . + exit 1 + fi - name: Test Project run: | make test diff --git a/cmd/root.go b/cmd/root.go index 627722ac3..c39b1949b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -69,6 +69,8 @@ func NewRootCommand() *cobra.Command { awsutil.DefaultAWSPartitionID = endpoints.AwsPartitionID case endpoints.UsGovEast1RegionID, endpoints.UsGovWest1RegionID: awsutil.DefaultAWSPartitionID = endpoints.AwsUsGovPartitionID + case endpoints.CnNorth1RegionID, endpoints.CnNorthwest1RegionID: + awsutil.DefaultAWSPartitionID = endpoints.AwsCnPartitionID default: if config.CustomEndpoints.GetRegion(defaultRegion) == nil { err = fmt.Errorf("The custom region '%s' must be specified in the configuration 'endpoints'", defaultRegion) diff --git a/go.mod b/go.mod index 1034d3403..6d85b7fc7 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/rebuy-de/aws-nuke/v2 go 1.19 require ( - github.com/aws/aws-sdk-go v1.44.307 + github.com/aws/aws-sdk-go v1.44.313 github.com/fatih/color v1.15.0 github.com/golang/mock v1.6.0 github.com/google/uuid v1.3.0 diff --git a/go.sum b/go.sum index 7f3829b60..27e140388 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/aws/aws-sdk-go v1.44.307 h1:2R0/EPgpZcFSUwZhYImq/srjaOrOfLv5MNRzrFyAM38= -github.com/aws/aws-sdk-go v1.44.307/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= +github.com/aws/aws-sdk-go v1.44.313 h1:u6EuNQqgAmi09GEZ5g/XGHLF0XV31WcdU5rnHyIBHBc= +github.com/aws/aws-sdk-go v1.44.313/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/pkg/config/filter.go b/pkg/config/filter.go index 61fcbef00..019ed8ebf 100644 --- a/pkg/config/filter.go +++ b/pkg/config/filter.go @@ -84,11 +84,12 @@ func parseDate(input string) (time.Time, error) { return t, nil } - formats := []string{"2006-01-02", + formats := []string{ + "2006-01-02", "2006/01/02", "2006-01-02T15:04:05Z", - "2006-01-02 15:04:05.000 -0700 MST", // Date format used by AWS for CreateTime on ASGs - time.RFC3339Nano, // Format of t.MarshalText() and t.MarshalJSON() + "2006-01-02 15:04:05 -0700 MST", // Date format used by AWS for CreateTime on ASGs + time.RFC3339Nano, // Format of t.MarshalText() and t.MarshalJSON() time.RFC3339, } for _, f := range formats { diff --git a/pkg/config/filter_test.go b/pkg/config/filter_test.go index a26df88c7..25f0d236f 100644 --- a/pkg/config/filter_test.go +++ b/pkg/config/filter_test.go @@ -48,18 +48,22 @@ func TestUnmarshalFilter(t *testing.T) { }, { yaml: `{"type":"dateOlderThan","value":"0"}`, - match: []string{strconv.Itoa(int(future.Unix())), + match: []string{ + strconv.Itoa(int(future.Unix())), future.Format("2006-01-02"), future.Format("2006/01/02"), future.Format("2006-01-02T15:04:05Z"), + future.Format("2006-01-02 15:04:05.000 +0000 UTC"), future.Format(time.RFC3339Nano), future.Format(time.RFC3339), }, - mismatch: []string{"", + mismatch: []string{ + "", strconv.Itoa(int(past.Unix())), past.Format("2006-01-02"), past.Format("2006/01/02"), past.Format("2006-01-02T15:04:05Z"), + past.Format("2006-01-02 15:04:05.14 -0700 MST"), past.Format(time.RFC3339Nano), past.Format(time.RFC3339), }, @@ -98,5 +102,4 @@ func TestUnmarshalFilter(t *testing.T) { } }) } - } diff --git a/resources/appconfig-applications.go b/resources/appconfig-applications.go new file mode 100644 index 000000000..cb419decc --- /dev/null +++ b/resources/appconfig-applications.go @@ -0,0 +1,53 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/appconfig" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type AppConfigApplication struct { + svc *appconfig.AppConfig + id *string + name *string +} + +func init() { + register("AppConfigApplication", ListAppConfigApplications) +} + +func ListAppConfigApplications(sess *session.Session) ([]Resource, error) { + svc := appconfig.New(sess) + resources := []Resource{} + params := &appconfig.ListApplicationsInput{ + MaxResults: aws.Int64(100), + } + err := svc.ListApplicationsPages(params, func(page *appconfig.ListApplicationsOutput, lastPage bool) bool { + for _, item := range page.Items { + resources = append(resources, &AppConfigApplication{ + svc: svc, + id: item.Id, + name: item.Name, + }) + } + return true + }) + if err != nil { + return nil, err + } + return resources, nil +} + +func (f *AppConfigApplication) Remove() error { + _, err := f.svc.DeleteApplication(&appconfig.DeleteApplicationInput{ + ApplicationId: f.id, + }) + return err +} + +func (f *AppConfigApplication) Properties() types.Properties { + return types.NewProperties(). + Set("ID", f.id). + Set("Name", f.name) +} diff --git a/resources/appconfig-configurationprofiles.go b/resources/appconfig-configurationprofiles.go new file mode 100644 index 000000000..79efd01c6 --- /dev/null +++ b/resources/appconfig-configurationprofiles.go @@ -0,0 +1,70 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/appconfig" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" + "github.com/sirupsen/logrus" +) + +type AppConfigConfigurationProfile struct { + svc *appconfig.AppConfig + applicationId *string + id *string + name *string +} + +func init() { + register("AppConfigConfigurationProfile", ListAppConfigConfigurationProfiles) +} + +func ListAppConfigConfigurationProfiles(sess *session.Session) ([]Resource, error) { + svc := appconfig.New(sess) + resources := []Resource{} + applications, err := ListAppConfigApplications(sess) + if err != nil { + return nil, err + } + for _, applicationResource := range applications { + application, ok := applicationResource.(*AppConfigApplication) + if !ok { + logrus.Errorf("Unable to cast AppConfigApplication.") + continue + } + params := &appconfig.ListConfigurationProfilesInput{ + ApplicationId: application.id, + MaxResults: aws.Int64(100), + } + err := svc.ListConfigurationProfilesPages(params, func(page *appconfig.ListConfigurationProfilesOutput, lastPage bool) bool { + for _, item := range page.Items { + resources = append(resources, &AppConfigConfigurationProfile{ + svc: svc, + applicationId: application.id, + id: item.Id, + name: item.Name, + }) + } + return true + }) + if err != nil { + return nil, err + } + } + return resources, nil +} + +func (f *AppConfigConfigurationProfile) Remove() error { + _, err := f.svc.DeleteConfigurationProfile(&appconfig.DeleteConfigurationProfileInput{ + ApplicationId: f.applicationId, + ConfigurationProfileId: f.id, + }) + return err +} + +func (f *AppConfigConfigurationProfile) Properties() types.Properties { + return types.NewProperties(). + Set("ApplicationID", f.applicationId). + Set("ID", f.id). + Set("Name", f.name) +} diff --git a/resources/appconfig-deploymentstrategies.go b/resources/appconfig-deploymentstrategies.go new file mode 100644 index 000000000..670cb09d6 --- /dev/null +++ b/resources/appconfig-deploymentstrategies.go @@ -0,0 +1,53 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/appconfig" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type AppConfigDeploymentStrategy struct { + svc *appconfig.AppConfig + id *string + name *string +} + +func init() { + register("AppConfigDeploymentStrategy", ListAppConfigDeploymentStrategies) +} + +func ListAppConfigDeploymentStrategies(sess *session.Session) ([]Resource, error) { + svc := appconfig.New(sess) + resources := []Resource{} + params := &appconfig.ListDeploymentStrategiesInput{ + MaxResults: aws.Int64(100), + } + err := svc.ListDeploymentStrategiesPages(params, func(page *appconfig.ListDeploymentStrategiesOutput, lastPage bool) bool { + for _, item := range page.Items { + resources = append(resources, &AppConfigDeploymentStrategy{ + svc: svc, + id: item.Id, + name: item.Name, + }) + } + return true + }) + if err != nil { + return nil, err + } + return resources, nil +} + +func (f *AppConfigDeploymentStrategy) Remove() error { + _, err := f.svc.DeleteDeploymentStrategy(&appconfig.DeleteDeploymentStrategyInput{ + DeploymentStrategyId: f.id, + }) + return err +} + +func (f *AppConfigDeploymentStrategy) Properties() types.Properties { + return types.NewProperties(). + Set("ID", f.id). + Set("Name", f.name) +} diff --git a/resources/appconfig-environments.go b/resources/appconfig-environments.go new file mode 100644 index 000000000..d12015a73 --- /dev/null +++ b/resources/appconfig-environments.go @@ -0,0 +1,70 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/appconfig" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" + "github.com/sirupsen/logrus" +) + +type AppConfigEnvironment struct { + svc *appconfig.AppConfig + applicationId *string + id *string + name *string +} + +func init() { + register("AppConfigEnvironment", ListAppConfigEnvironments) +} + +func ListAppConfigEnvironments(sess *session.Session) ([]Resource, error) { + svc := appconfig.New(sess) + resources := []Resource{} + applications, err := ListAppConfigApplications(sess) + if err != nil { + return nil, err + } + for _, applicationResource := range applications { + application, ok := applicationResource.(*AppConfigApplication) + if !ok { + logrus.Errorf("Unable to cast AppConfigApplication.") + continue + } + params := &appconfig.ListEnvironmentsInput{ + ApplicationId: application.id, + MaxResults: aws.Int64(100), + } + err := svc.ListEnvironmentsPages(params, func(page *appconfig.ListEnvironmentsOutput, lastPage bool) bool { + for _, item := range page.Items { + resources = append(resources, &AppConfigEnvironment{ + svc: svc, + applicationId: application.id, + id: item.Id, + name: item.Name, + }) + } + return true + }) + if err != nil { + return nil, err + } + } + return resources, nil +} + +func (f *AppConfigEnvironment) Remove() error { + _, err := f.svc.DeleteEnvironment(&appconfig.DeleteEnvironmentInput{ + ApplicationId: f.applicationId, + EnvironmentId: f.id, + }) + return err +} + +func (f *AppConfigEnvironment) Properties() types.Properties { + return types.NewProperties(). + Set("ApplicationID", f.applicationId). + Set("ID", f.id). + Set("Name", f.name) +} diff --git a/resources/appconfig-hostedconfigurationversions.go b/resources/appconfig-hostedconfigurationversions.go new file mode 100644 index 000000000..6fff16c1b --- /dev/null +++ b/resources/appconfig-hostedconfigurationversions.go @@ -0,0 +1,72 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/appconfig" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" + "github.com/sirupsen/logrus" +) + +type AppConfigHostedConfigurationVersion struct { + svc *appconfig.AppConfig + applicationId *string + configurationProfileId *string + versionNumber *int64 +} + +func init() { + register("AppConfigHostedConfigurationVersion", ListAppConfigHostedConfigurationVersions) +} + +func ListAppConfigHostedConfigurationVersions(sess *session.Session) ([]Resource, error) { + svc := appconfig.New(sess) + resources := []Resource{} + configurationProfiles, err := ListAppConfigConfigurationProfiles(sess) + if err != nil { + return nil, err + } + for _, configurationProfileResource := range configurationProfiles { + configurationProfile, ok := configurationProfileResource.(*AppConfigConfigurationProfile) + if !ok { + logrus.Errorf("Unable to cast AppConfigConfigurationProfile.") + continue + } + params := &appconfig.ListHostedConfigurationVersionsInput{ + ApplicationId: configurationProfile.applicationId, + ConfigurationProfileId: configurationProfile.id, + MaxResults: aws.Int64(100), + } + err := svc.ListHostedConfigurationVersionsPages(params, func(page *appconfig.ListHostedConfigurationVersionsOutput, lastPage bool) bool { + for _, item := range page.Items { + resources = append(resources, &AppConfigHostedConfigurationVersion{ + svc: svc, + applicationId: configurationProfile.applicationId, + configurationProfileId: configurationProfile.id, + versionNumber: item.VersionNumber, + }) + } + return true + }) + if err != nil { + return nil, err + } + } + return resources, nil +} + +func (f *AppConfigHostedConfigurationVersion) Remove() error { + _, err := f.svc.DeleteHostedConfigurationVersion(&appconfig.DeleteHostedConfigurationVersionInput{ + ApplicationId: f.applicationId, + ConfigurationProfileId: f.configurationProfileId, + VersionNumber: f.versionNumber, + }) + return err +} + +func (f *AppConfigHostedConfigurationVersion) Properties() types.Properties { + return types.NewProperties(). + Set("ApplicationID", f.applicationId). + Set("ConfigurationProfileID", f.configurationProfileId). + Set("VersionNumber", f.versionNumber) +} diff --git a/resources/autoscaling-groups.go b/resources/autoscaling-groups.go index 5f41ec7d9..94aff114f 100644 --- a/resources/autoscaling-groups.go +++ b/resources/autoscaling-groups.go @@ -1,6 +1,8 @@ package resources import ( + "time" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/autoscaling" @@ -27,7 +29,6 @@ func ListAutoscalingGroups(s *session.Session) ([]Resource, error) { } return !lastPage }) - if err != nil { return nil, err } @@ -65,7 +66,7 @@ func (asg *AutoScalingGroup) Properties() types.Properties { properties.SetTag(tag.Key, tag.Value) } - properties.Set("CreatedTime", asg.group.CreatedTime) + properties.Set("CreatedTime", asg.group.CreatedTime.Format(time.RFC3339)) properties.Set("Name", asg.group.AutoScalingGroupName) return properties diff --git a/resources/batch-computeenvironments.go b/resources/batch-computeenvironments.go index b9908d3e8..166574c2f 100644 --- a/resources/batch-computeenvironments.go +++ b/resources/batch-computeenvironments.go @@ -31,7 +31,7 @@ func ListBatchComputeEnvironments(sess *session.Session) ([]Resource, error) { for _, computeEnvironment := range output.ComputeEnvironments { resources = append(resources, &BatchComputeEnvironment{ - svc: svc, + svc: svc, computeEnvironmentName: computeEnvironment.ComputeEnvironmentName, }) } diff --git a/resources/batch-computeenvironmentstates.go b/resources/batch-computeenvironmentstates.go index cf867b16c..866092e74 100644 --- a/resources/batch-computeenvironmentstates.go +++ b/resources/batch-computeenvironmentstates.go @@ -35,9 +35,9 @@ func ListBatchComputeEnvironmentStates(sess *session.Session) ([]Resource, error for _, computeEnvironment := range output.ComputeEnvironments { resources = append(resources, &BatchComputeEnvironmentState{ - svc: svc, + svc: svc, computeEnvironmentName: computeEnvironment.ComputeEnvironmentName, - state: computeEnvironment.State, + state: computeEnvironment.State, }) } diff --git a/resources/billing-costandusagereports.go b/resources/billing-costandusagereports.go index b0148cbdf..2a81fe26b 100644 --- a/resources/billing-costandusagereports.go +++ b/resources/billing-costandusagereports.go @@ -39,11 +39,11 @@ func ListBillingCostandUsageReports(sess *session.Session) ([]Resource, error) { resources := []Resource{} for _, report := range reports { resources = append(resources, &BillingCostandUsageReport{ - svc: svc, - reportName: report.ReportName, - s3Bucket: report.S3Bucket, - s3Prefix: report.S3Prefix, - s3Region: report.S3Region, + svc: svc, + reportName: report.ReportName, + s3Bucket: report.S3Bucket, + s3Prefix: report.S3Prefix, + s3Region: report.S3Region, }) } diff --git a/resources/cloudfront-origin-access-identities.go b/resources/cloudfront-origin-access-identities.go index 375121f60..943886ad6 100644 --- a/resources/cloudfront-origin-access-identities.go +++ b/resources/cloudfront-origin-access-identities.go @@ -1,58 +1,58 @@ package resources import ( - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/cloudfront" "github.com/rebuy-de/aws-nuke/v2/pkg/types" ) type CloudFrontOriginAccessIdentity struct { - svc *cloudfront.CloudFront - ID *string + svc *cloudfront.CloudFront + ID *string } func init() { - register("CloudFrontOriginAccessIdentity", ListCloudFrontOriginAccessIdentities) + register("CloudFrontOriginAccessIdentity", ListCloudFrontOriginAccessIdentities) } func ListCloudFrontOriginAccessIdentities(sess *session.Session) ([]Resource, error) { - svc := cloudfront.New(sess) - resources := []Resource{} - - for { - resp, err := svc.ListCloudFrontOriginAccessIdentities(nil) - if err != nil { - return nil, err - } - - for _, item := range resp.CloudFrontOriginAccessIdentityList.Items { - resources = append(resources,&CloudFrontOriginAccessIdentity{ - svc: svc, - ID: item.Id, - }) - } - return resources, nil - } + svc := cloudfront.New(sess) + resources := []Resource{} + + for { + resp, err := svc.ListCloudFrontOriginAccessIdentities(nil) + if err != nil { + return nil, err + } + + for _, item := range resp.CloudFrontOriginAccessIdentityList.Items { + resources = append(resources, &CloudFrontOriginAccessIdentity{ + svc: svc, + ID: item.Id, + }) + } + return resources, nil + } } func (f *CloudFrontOriginAccessIdentity) Remove() error { - resp, err := f.svc.GetCloudFrontOriginAccessIdentity(&cloudfront.GetCloudFrontOriginAccessIdentityInput{ - Id: f.ID, - }) - if err != nil { - return err - } - - _, err = f.svc.DeleteCloudFrontOriginAccessIdentity(&cloudfront.DeleteCloudFrontOriginAccessIdentityInput{ - Id: f.ID, - IfMatch: resp.ETag, - }) - - return err + resp, err := f.svc.GetCloudFrontOriginAccessIdentity(&cloudfront.GetCloudFrontOriginAccessIdentityInput{ + Id: f.ID, + }) + if err != nil { + return err + } + + _, err = f.svc.DeleteCloudFrontOriginAccessIdentity(&cloudfront.DeleteCloudFrontOriginAccessIdentityInput{ + Id: f.ID, + IfMatch: resp.ETag, + }) + + return err } func (f *CloudFrontOriginAccessIdentity) Properties() types.Properties { - properties := types.NewProperties() - properties.Set("ID", f.ID) - return properties + properties := types.NewProperties() + properties.Set("ID", f.ID) + return properties } diff --git a/resources/codestar-connections.go b/resources/codestar-connections.go index 1d49201cd..b3d454d13 100644 --- a/resources/codestar-connections.go +++ b/resources/codestar-connections.go @@ -8,10 +8,10 @@ import ( ) type CodeStarConnection struct { - svc *codestarconnections.CodeStarConnections - connectionARN *string - connectionName *string - providerType *string + svc *codestarconnections.CodeStarConnections + connectionARN *string + connectionName *string + providerType *string } func init() { @@ -34,10 +34,10 @@ func ListCodeStarConnections(sess *session.Session) ([]Resource, error) { for _, connection := range output.Connections { resources = append(resources, &CodeStarConnection{ - svc: svc, - connectionARN: connection.ConnectionArn, - connectionName: connection.ConnectionName, - providerType: connection.ProviderType, + svc: svc, + connectionARN: connection.ConnectionArn, + connectionName: connection.ConnectionName, + providerType: connection.ProviderType, }) } @@ -68,7 +68,6 @@ func (f *CodeStarConnection) Properties() types.Properties { return properties } - func (f *CodeStarConnection) String() string { return *f.connectionName } diff --git a/resources/configservice-configurationrecorders.go b/resources/configservice-configurationrecorders.go index 669bbfe31..c0d958098 100644 --- a/resources/configservice-configurationrecorders.go +++ b/resources/configservice-configurationrecorders.go @@ -26,7 +26,7 @@ func ListConfigServiceConfigurationRecorders(sess *session.Session) ([]Resource, resources := make([]Resource, 0) for _, configurationRecorder := range resp.ConfigurationRecorders { resources = append(resources, &ConfigServiceConfigurationRecorder{ - svc: svc, + svc: svc, configurationRecorderName: configurationRecorder.Name, }) } diff --git a/resources/dax-subnetgroups.go b/resources/dax-subnetgroups.go index c451f6e8e..81fc63c59 100644 --- a/resources/dax-subnetgroups.go +++ b/resources/dax-subnetgroups.go @@ -1,10 +1,10 @@ package resources import ( + "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dax" - "fmt" ) type DAXSubnetGroup struct { diff --git a/resources/dynamodb-tables.go b/resources/dynamodb-tables.go index 04a57fa17..cb191e610 100644 --- a/resources/dynamodb-tables.go +++ b/resources/dynamodb-tables.go @@ -34,8 +34,8 @@ func ListDynamoDBTables(sess *session.Session) ([]Resource, error) { } resources = append(resources, &DynamoDBTable{ - svc: svc, - id: *tableName, + svc: svc, + id: *tableName, tags: tags, }) } @@ -65,7 +65,7 @@ func GetTableTags(svc *dynamodb.DynamoDB, tableName *string) ([]*dynamodb.Tag, e return make([]*dynamodb.Tag, 0), err } - tags, err := svc.ListTagsOfResource(&dynamodb.ListTagsOfResourceInput{ + tags, err := svc.ListTagsOfResource(&dynamodb.ListTagsOfResourceInput{ ResourceArn: result.Table.TableArn, }) @@ -77,17 +77,16 @@ func GetTableTags(svc *dynamodb.DynamoDB, tableName *string) ([]*dynamodb.Tag, e } func (i *DynamoDBTable) Properties() types.Properties { - properties := types.NewProperties() - properties.Set("Identifier", i.id) + properties := types.NewProperties() + properties.Set("Identifier", i.id) - for _, tag := range i.tags { - properties.SetTag(tag.Key, tag.Value) - } + for _, tag := range i.tags { + properties.SetTag(tag.Key, tag.Value) + } - return properties + return properties } - func (i *DynamoDBTable) String() string { return i.id } diff --git a/resources/ec2-default-security-group-rules.go b/resources/ec2-default-security-group-rules.go index 71903cc08..c3fea3aa0 100644 --- a/resources/ec2-default-security-group-rules.go +++ b/resources/ec2-default-security-group-rules.go @@ -12,6 +12,7 @@ type EC2DefaultSecurityGroupRule struct { id *string groupId *string isEgress *bool + tags []*ec2.Tag } func init() { @@ -62,6 +63,7 @@ func ListEC2SecurityGroupRules(sess *session.Session) ([]Resource, error) { id: rule.SecurityGroupRuleId, groupId: rule.GroupId, isEgress: rule.IsEgress, + tags: rule.Tags, }) } return !lastPage @@ -103,6 +105,9 @@ func (r *EC2DefaultSecurityGroupRule) Remove() error { func (r *EC2DefaultSecurityGroupRule) Properties() types.Properties { properties := types.NewProperties() + for _, tagValue := range r.tags { + properties.SetTag(tagValue.Key, tagValue.Value) + } properties.Set("SecurityGroupId", r.groupId) return properties } diff --git a/resources/ec2-tgw-attachments.go b/resources/ec2-tgw-attachments.go index ca7dc3696..84cae7559 100644 --- a/resources/ec2-tgw-attachments.go +++ b/resources/ec2-tgw-attachments.go @@ -52,6 +52,21 @@ func (e *EC2TGWAttachment) Remove() error { // as part of TGW to delete VPN attachments. return fmt.Errorf("VPN attachment") } + + // Execute different API calls depending on the resource type. + if *e.tgwa.ResourceType == "peering" { + params := &ec2.DeleteTransitGatewayPeeringAttachmentInput{ + TransitGatewayAttachmentId: e.tgwa.TransitGatewayAttachmentId, + } + + _, err := e.svc.DeleteTransitGatewayPeeringAttachment(params) + if err != nil { + return err + } + + return nil + } + params := &ec2.DeleteTransitGatewayVpcAttachmentInput{ TransitGatewayAttachmentId: e.tgwa.TransitGatewayAttachmentId, } diff --git a/resources/ec2-vpn-connections.go b/resources/ec2-vpn-connections.go index be78228b5..2ca1054b9 100644 --- a/resources/ec2-vpn-connections.go +++ b/resources/ec2-vpn-connections.go @@ -9,8 +9,8 @@ import ( ) type EC2VPNConnection struct { - svc *ec2.EC2 - conn *ec2.VpnConnection + svc *ec2.EC2 + conn *ec2.VpnConnection } func init() { @@ -29,8 +29,8 @@ func ListEC2VPNConnections(sess *session.Session) ([]Resource, error) { resources := make([]Resource, 0) for _, out := range resp.VpnConnections { resources = append(resources, &EC2VPNConnection{ - svc: svc, - conn: out, + svc: svc, + conn: out, }) } diff --git a/resources/ecs-services.go b/resources/ecs-services.go index d5a3792e6..d53a669d9 100644 --- a/resources/ecs-services.go +++ b/resources/ecs-services.go @@ -52,24 +52,19 @@ func ListECSServices(sess *session.Session) ([]Resource, error) { Cluster: clusterArn, MaxResults: aws.Int64(10), } - output, err := svc.ListServices(serviceParams) + err := svc.ListServicesPages(serviceParams, func(page *ecs.ListServicesOutput, lastPage bool) bool { + for _, serviceArn := range page.ServiceArns { + resources = append(resources, &ECSService{ + svc: svc, + serviceARN: serviceArn, + clusterARN: clusterArn, + }) + } + return true + }) if err != nil { return nil, err } - - for _, serviceArn := range output.ServiceArns { - resources = append(resources, &ECSService{ - svc: svc, - serviceARN: serviceArn, - clusterARN: clusterArn, - }) - } - - if output.NextToken == nil { - continue - } - - serviceParams.NextToken = output.NextToken } return resources, nil diff --git a/resources/iam-policies.go b/resources/iam-policies.go index d4db7bec7..ece41af34 100644 --- a/resources/iam-policies.go +++ b/resources/iam-policies.go @@ -1,11 +1,11 @@ package resources import ( - "github.com/sirupsen/logrus" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/iam" "github.com/rebuy-de/aws-nuke/v2/pkg/types" + "github.com/sirupsen/logrus" ) type IAMPolicy struct { diff --git a/resources/kms-keys.go b/resources/kms-keys.go index cfb1193ed..c7a67f682 100644 --- a/resources/kms-keys.go +++ b/resources/kms-keys.go @@ -86,6 +86,10 @@ func (e *KMSKey) Filter() error { return fmt.Errorf("is already in PendingDeletion state") } + if e.state == "PendingReplicaDeletion" { + return fmt.Errorf("is already in PendingReplicaDeletion state") + } + if e.manager != nil && *e.manager == kms.KeyManagerTypeAws { return fmt.Errorf("cannot delete AWS managed key") } diff --git a/resources/rds-clusters.go b/resources/rds-clusters.go index 882712517..686a7961c 100644 --- a/resources/rds-clusters.go +++ b/resources/rds-clusters.go @@ -30,12 +30,12 @@ func ListRDSClusters(sess *session.Session) ([]Resource, error) { resources := make([]Resource, 0) for _, instance := range resp.DBClusters { tags, err := svc.ListTagsForResource(&rds.ListTagsForResourceInput{ - ResourceName: instance.DBClusterArn, - }) + ResourceName: instance.DBClusterArn, + }) - if err != nil { - continue - } + if err != nil { + continue + } resources = append(resources, &RDSDBCluster{ svc: svc, @@ -49,7 +49,7 @@ func ListRDSClusters(sess *session.Session) ([]Resource, error) { } func (i *RDSDBCluster) Remove() error { - if (i.deletionProtection) { + if i.deletionProtection { modifyParams := &rds.ModifyDBClusterInput{ DBClusterIdentifier: &i.id, DeletionProtection: aws.Bool(false), @@ -78,13 +78,13 @@ func (i *RDSDBCluster) String() string { } func (i *RDSDBCluster) Properties() types.Properties { - properties := types.NewProperties() - properties.Set("Identifier", i.id) + properties := types.NewProperties() + properties.Set("Identifier", i.id) properties.Set("Deletion Protection", i.deletionProtection) - for _, tag := range i.tags { - properties.SetTag(tag.Key, tag.Value) - } + for _, tag := range i.tags { + properties.SetTag(tag.Key, tag.Value) + } - return properties + return properties } diff --git a/resources/rds-dbclusterparametergroups.go b/resources/rds-dbclusterparametergroups.go index 9466d8c52..04a53af3a 100644 --- a/resources/rds-dbclusterparametergroups.go +++ b/resources/rds-dbclusterparametergroups.go @@ -31,12 +31,12 @@ func ListRDSClusterParameterGroups(sess *session.Session) ([]Resource, error) { var resources []Resource for _, parametergroup := range resp.DBClusterParameterGroups { tags, err := svc.ListTagsForResource(&rds.ListTagsForResourceInput{ - ResourceName: parametergroup.DBClusterParameterGroupArn, - }) + ResourceName: parametergroup.DBClusterParameterGroupArn, + }) - if err != nil { - continue - } + if err != nil { + continue + } resources = append(resources, &RDSDBClusterParameterGroup{ svc: svc, diff --git a/resources/rds-subnets.go b/resources/rds-subnets.go index 17978a195..017a2c297 100644 --- a/resources/rds-subnets.go +++ b/resources/rds-subnets.go @@ -28,12 +28,12 @@ func ListRDSSubnetGroups(sess *session.Session) ([]Resource, error) { var resources []Resource for _, subnetGroup := range resp.DBSubnetGroups { tags, err := svc.ListTagsForResource(&rds.ListTagsForResourceInput{ - ResourceName: subnetGroup.DBSubnetGroupArn, - }) + ResourceName: subnetGroup.DBSubnetGroupArn, + }) - if err != nil { - continue - } + if err != nil { + continue + } resources = append(resources, &RDSDBSubnetGroup{ svc: svc, @@ -64,12 +64,12 @@ func (i *RDSDBSubnetGroup) String() string { } func (i *RDSDBSubnetGroup) Properties() types.Properties { - properties := types.NewProperties() - properties.Set("Name", i.name) + properties := types.NewProperties() + properties.Set("Name", i.name) - for _, tag := range i.tags { - properties.SetTag(tag.Key, tag.Value) - } + for _, tag := range i.tags { + properties.SetTag(tag.Key, tag.Value) + } - return properties + return properties } diff --git a/resources/redshift-subnetgroups.go b/resources/redshift-subnetgroups.go index a94600c13..767eb0f64 100644 --- a/resources/redshift-subnetgroups.go +++ b/resources/redshift-subnetgroups.go @@ -31,7 +31,7 @@ func ListRedshiftSubnetGroups(sess *session.Session) ([]Resource, error) { for _, subnetGroup := range output.ClusterSubnetGroups { resources = append(resources, &RedshiftSubnetGroup{ - svc: svc, + svc: svc, clusterSubnetGroupName: subnetGroup.ClusterSubnetGroupName, }) } diff --git a/resources/route53-resource-records.go b/resources/route53-resource-records.go index 35f3bc792..c21cfd3f7 100644 --- a/resources/route53-resource-records.go +++ b/resources/route53-resource-records.go @@ -95,7 +95,7 @@ func (r *Route53ResourceRecordSet) Remove() error { HostedZoneId: r.hostedZoneId, ChangeBatch: &route53.ChangeBatch{ Changes: []*route53.Change{ - &route53.Change{ + { Action: aws.String("DELETE"), ResourceRecordSet: r.data, }, diff --git a/resources/wafregional-byte-match-set-tuples.go b/resources/wafregional-byte-match-set-tuples.go index ddf7966f7..45ba62251 100644 --- a/resources/wafregional-byte-match-set-tuples.go +++ b/resources/wafregional-byte-match-set-tuples.go @@ -70,7 +70,7 @@ func (r *WAFRegionalByteMatchSetIP) Remove() error { ChangeToken: tokenOutput.ChangeToken, ByteMatchSetId: r.matchSetid, Updates: []*waf.ByteMatchSetUpdate{ - &waf.ByteMatchSetUpdate{ + { Action: aws.String("DELETE"), ByteMatchTuple: r.tuple, }, diff --git a/resources/wafregional-ip-set-ips.go b/resources/wafregional-ip-set-ips.go index 6ad754cc2..875668a21 100644 --- a/resources/wafregional-ip-set-ips.go +++ b/resources/wafregional-ip-set-ips.go @@ -70,7 +70,7 @@ func (r *WAFRegionalIPSetIP) Remove() error { ChangeToken: tokenOutput.ChangeToken, IPSetId: r.ipSetid, Updates: []*waf.IPSetUpdate{ - &waf.IPSetUpdate{ + { Action: aws.String("DELETE"), IPSetDescriptor: r.descriptor, }, diff --git a/resources/wafregional-rate-based-rule-predicates.go b/resources/wafregional-rate-based-rule-predicates.go index 67319a56d..630387d6f 100644 --- a/resources/wafregional-rate-based-rule-predicates.go +++ b/resources/wafregional-rate-based-rule-predicates.go @@ -72,7 +72,7 @@ func (r *WAFRegionalRateBasedRulePredicate) Remove() error { RuleId: r.ruleID, RateLimit: r.rateLimit, Updates: []*waf.RuleUpdate{ - &waf.RuleUpdate{ + { Action: aws.String("DELETE"), Predicate: r.predicate, }, diff --git a/resources/wafregional-regex-match-tuples.go b/resources/wafregional-regex-match-tuples.go index f2a9ebafa..5095bfdaa 100644 --- a/resources/wafregional-regex-match-tuples.go +++ b/resources/wafregional-regex-match-tuples.go @@ -69,7 +69,7 @@ func (r *WAFRegionalRegexMatchTuple) Remove() error { ChangeToken: tokenOutput.ChangeToken, RegexMatchSetId: r.matchSetid, Updates: []*waf.RegexMatchSetUpdate{ - &waf.RegexMatchSetUpdate{ + { Action: aws.String("DELETE"), RegexMatchTuple: r.tuple, }, diff --git a/resources/wafregional-regex-pattern-tuples.go b/resources/wafregional-regex-pattern-tuples.go index 9c7d8dea3..3a0c84223 100644 --- a/resources/wafregional-regex-pattern-tuples.go +++ b/resources/wafregional-regex-pattern-tuples.go @@ -69,7 +69,7 @@ func (r *WAFRegionalRegexPatternString) Remove() error { ChangeToken: tokenOutput.ChangeToken, RegexPatternSetId: r.patternSetid, Updates: []*waf.RegexPatternSetUpdate{ - &waf.RegexPatternSetUpdate{ + { Action: aws.String("DELETE"), RegexPatternString: r.patternString, }, diff --git a/resources/wafregional-rule-predicates.go b/resources/wafregional-rule-predicates.go index d7713c200..6c9a9adf9 100644 --- a/resources/wafregional-rule-predicates.go +++ b/resources/wafregional-rule-predicates.go @@ -69,7 +69,7 @@ func (r *WAFRegionalRulePredicate) Remove() error { ChangeToken: tokenOutput.ChangeToken, RuleId: r.ruleID, Updates: []*waf.RuleUpdate{ - &waf.RuleUpdate{ + { Action: aws.String("DELETE"), Predicate: r.predicate, }, diff --git a/tools/tools.go b/tools/tools.go index 6f89b957f..356e89829 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -1,4 +1,4 @@ -// +build tools +//go:build tools package main