forked from rebuy-de/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for Athena WorkGroups and NamedQueries (rebuy-de#464) * Upgrade to aws sdk version 1.28.12 (rebuy-de#466) * Upgrade to aws sdk version 1.28.12 * added eks fargate profile resource (rebuy-de#469) * added eks fargate profile resource * added eks nodegroup as resource (rebuy-de#468) * added eks nodegroup as resource * add route53 health check resource (rebuy-de#471) * Delete all record sets of the type "NS" except for the default "NS" settings (rebuy-de#473) * fixed rrs behaviour on NS deletion * Handle pagination 101+ route53 healthchecks (rebuy-de#472) * Handle 101+ route53 * add cloudformation stackset support (rebuy-de#475) * feat: Add OwnerId Prop to EC2TGW (rebuy-de#483) * rebuy-de#430 - cloudformation support for disabling termination protection (rebuy-de#481) * rebuy-de#430 - cloudformation support for disabling termination protection * Disable Security Hub (rebuy-de#484) * Disable Security Hub See rebuy-de#452 Add the ability to detect regions with Security Hub enabled and disable it if a "hub" resource is returned. * Rename property of SecurityHub hub Arn * Replace Regex in S3Object sample to recursive nuke (rebuy-de#486) Replace the wildcard to ** to allow recurside sublevel directories in the nuke process. Co-authored-by: Bruno Paiuca <bruno.paiuca@wildlifestudios.com> Co-authored-by: Jan Michalowsky <sejamich@googlemail.com> Co-authored-by: matthagenbuch <7414485+matthagenbuch@users.noreply.github.com> Co-authored-by: Brian <Brian-Williams@users.noreply.github.com> Co-authored-by: Tyler Southwick <tyler.southwick@nike.com> Co-authored-by: Ryan Whelan <4249368+rwhelan@users.noreply.github.com> Co-authored-by: Jeff <jscarter3@gmail.com> Co-authored-by: Greg Bailey <gbailey@lxpro.com> Co-authored-by: Bruno Paiuca <bn.paiuca@gmail.com> Co-authored-by: Bruno Paiuca <bruno.paiuca@wildlifestudios.com>
- Loading branch information
1 parent
a2fe279
commit 3378684
Showing
13 changed files
with
716 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
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/cloudformation" | ||
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface" | ||
"github.com/rebuy-de/aws-nuke/pkg/types" | ||
"github.com/sirupsen/logrus" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func init() { | ||
register("CloudFormationStackSet", ListCloudFormationStackSets) | ||
} | ||
|
||
func ListCloudFormationStackSets(sess *session.Session) ([]Resource, error) { | ||
svc := cloudformation.New(sess) | ||
|
||
params := &cloudformation.ListStackSetsInput{} | ||
resources := make([]Resource, 0) | ||
|
||
for { | ||
resp, err := svc.ListStackSets(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, stackSetSummary := range resp.Summaries { | ||
resources = append(resources, &CloudFormationStackSet{ | ||
svc: svc, | ||
stackSetSummary: stackSetSummary, | ||
sleepDuration: 10 * time.Second, | ||
}) | ||
} | ||
|
||
if resp.NextToken == nil { | ||
break | ||
} | ||
|
||
params.NextToken = resp.NextToken | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
type CloudFormationStackSet struct { | ||
svc cloudformationiface.CloudFormationAPI | ||
stackSetSummary *cloudformation.StackSetSummary | ||
sleepDuration time.Duration | ||
} | ||
|
||
func (cfs *CloudFormationStackSet) findStackInstances() (map[string][]string, error) { | ||
accounts := make(map[string][]string) | ||
|
||
input := &cloudformation.ListStackInstancesInput{ | ||
StackSetName: cfs.stackSetSummary.StackSetName, | ||
} | ||
|
||
for { | ||
resp, err := cfs.svc.ListStackInstances(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, stackInstanceSummary := range resp.Summaries { | ||
if regions, ok := accounts[*stackInstanceSummary.Account]; !ok { | ||
accounts[*stackInstanceSummary.Account] = []string{*stackInstanceSummary.Region} | ||
} else { | ||
accounts[*stackInstanceSummary.Account] = append(regions, *stackInstanceSummary.Region) | ||
} | ||
} | ||
|
||
if resp.NextToken == nil { | ||
break | ||
} | ||
|
||
input.NextToken = resp.NextToken | ||
} | ||
|
||
return accounts, nil | ||
} | ||
|
||
func (cfs *CloudFormationStackSet) waitForStackSetOperation(operationId string) error { | ||
for { | ||
result, err := cfs.svc.DescribeStackSetOperation(&cloudformation.DescribeStackSetOperationInput{ | ||
StackSetName: cfs.stackSetSummary.StackSetName, | ||
OperationId: &operationId, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
logrus.Infof("Got stackInstance operation status on stackSet=%s operationId=%s status=%s", *cfs.stackSetSummary.StackSetName, operationId, *result.StackSetOperation.Status) | ||
if *result.StackSetOperation.Status == cloudformation.StackSetOperationResultStatusSucceeded { | ||
return nil | ||
} else if *result.StackSetOperation.Status == cloudformation.StackSetOperationResultStatusFailed || *result.StackSetOperation.Status == cloudformation.StackSetOperationResultStatusCancelled { | ||
return fmt.Errorf("unable to delete stackSet=%s operationId=%s status=%s", *cfs.stackSetSummary.StackSetName, operationId, *result.StackSetOperation.Status) | ||
} else { | ||
logrus.Infof("Waiting on stackSet=%s operationId=%s status=%s", *cfs.stackSetSummary.StackSetName, operationId, *result.StackSetOperation.Status) | ||
time.Sleep(cfs.sleepDuration) | ||
} | ||
} | ||
} | ||
|
||
func (cfs *CloudFormationStackSet) deleteStackInstances(accountId string, regions []string) error { | ||
logrus.Infof("Deleting stack instance accountId=%s regions=%s", accountId, strings.Join(regions, ",")) | ||
regionsInput := make([]*string, len(regions)) | ||
for i, region := range regions { | ||
regionsInput[i] = aws.String(region) | ||
fmt.Printf("region=%s i=%d\n", region, i) | ||
} | ||
result, err := cfs.svc.DeleteStackInstances(&cloudformation.DeleteStackInstancesInput{ | ||
StackSetName: cfs.stackSetSummary.StackSetName, | ||
Accounts: []*string{&accountId}, | ||
Regions: regionsInput, | ||
RetainStacks: aws.Bool(true), //this will remove the stack set instance from the stackset, but will leave the stack in the account/region it was deployed to | ||
}) | ||
|
||
fmt.Printf("got result=%v err=%v\n", result, err) | ||
|
||
if result == nil { | ||
return fmt.Errorf("got null result") | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return cfs.waitForStackSetOperation(*result.OperationId) | ||
} | ||
|
||
func (cfs *CloudFormationStackSet) Remove() error { | ||
accounts, err := cfs.findStackInstances() | ||
if err != nil { | ||
return err | ||
} | ||
for accountId, regions := range accounts { | ||
err := cfs.deleteStackInstances(accountId, regions) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
_, err = cfs.svc.DeleteStackSet(&cloudformation.DeleteStackSetInput{ | ||
StackSetName: cfs.stackSetSummary.StackSetName, | ||
}) | ||
return err | ||
} | ||
|
||
func (cfs *CloudFormationStackSet) Properties() types.Properties { | ||
properties := types.NewProperties() | ||
properties.Set("Name", cfs.stackSetSummary.StackSetName) | ||
properties.Set("StackSetId", cfs.stackSetSummary.StackSetId) | ||
|
||
return properties | ||
} | ||
|
||
func (cfs *CloudFormationStackSet) String() string { | ||
return *cfs.stackSetSummary.StackSetName | ||
} |
Oops, something went wrong.