1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
package resources
import (
"errors"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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/config"
"github.com/rebuy-de/aws-nuke/pkg/types"
"github.com/sirupsen/logrus"
)
const CLOUDFORMATION_MAX_DELETE_ATTEMPT = 3
func init() {
register("CloudFormationStack", ListCloudFormationStacks)
}
func ListCloudFormationStacks(sess *session.Session) ([]Resource, error) {
svc := cloudformation.New(sess)
params := &cloudformation.DescribeStacksInput{}
resources := make([]Resource, 0)
for {
resp, err := svc.DescribeStacks(params)
if err != nil {
return nil, err
}
for _, stack := range resp.Stacks {
resources = append(resources, &CloudFormationStack{
svc: svc,
stack: stack,
maxDeleteAttempts: CLOUDFORMATION_MAX_DELETE_ATTEMPT,
})
}
if resp.NextToken == nil {
break
}
params.NextToken = resp.NextToken
}
return resources, nil
}
type CloudFormationStack struct {
svc cloudformationiface.CloudFormationAPI
stack *cloudformation.Stack
maxDeleteAttempts int
featureFlags config.FeatureFlags
}
func (cfs *CloudFormationStack) FeatureFlags(ff config.FeatureFlags) {
cfs.featureFlags = ff
}
func (cfs *CloudFormationStack) Remove() error {
return cfs.removeWithAttempts(0)
}
func (cfs *CloudFormationStack) removeWithAttempts(attempt int) error {
if err := cfs.doRemove(); err != nil {
logrus.Errorf("CloudFormationStack stackName=%s attempt=%d maxAttempts=%d delete failed: %s", *cfs.stack.StackName, attempt, cfs.maxDeleteAttempts, err.Error())
awsErr, ok := err.(awserr.Error)
if ok && awsErr.Code() == "ValidationError" &&
awsErr.Message() == "Stack ["+*cfs.stack.StackName+"] cannot be deleted while TerminationProtection is enabled" {
if cfs.featureFlags.DisableDeletionProtection.CloudformationStack {
logrus.Infof("CloudFormationStack stackName=%s attempt=%d maxAttempts=%d updating termination protection", *cfs.stack.StackName, attempt, cfs.maxDeleteAttempts)
_, err = cfs.svc.UpdateTerminationProtection(&cloudformation.UpdateTerminationProtectionInput{
EnableTerminationProtection: aws.Bool(false),
StackName: cfs.stack.StackName,
})
if err != nil {
return err
}
} else {
logrus.Warnf("CloudFormationStack stackName=%s attempt=%d maxAttempts=%d set feature flag to disable deletion protection", *cfs.stack.StackName, attempt, cfs.maxDeleteAttempts)
return err
}
}
if attempt >= cfs.maxDeleteAttempts {
return errors.New("CFS might not be deleted after this run.")
} else {
return cfs.removeWithAttempts(attempt + 1)
}
} else {
return nil
}
}
func (cfs *CloudFormationStack) doRemove() error {
o, err := cfs.svc.DescribeStacks(&cloudformation.DescribeStacksInput{
StackName: cfs.stack.StackName,
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "ValidationFailed" && strings.HasSuffix(awsErr.Message(), " does not exist") {
logrus.Infof("CloudFormationStack stackName=%s no longer exists", *cfs.stack.StackName)
return nil
}
}
return err
}
stack := o.Stacks[0]
if *stack.StackStatus == cloudformation.StackStatusDeleteComplete {
//stack already deleted, no need to re-delete
return nil
} else if *stack.StackStatus == cloudformation.StackStatusDeleteInProgress {
logrus.Infof("CloudFormationStack stackName=%s delete in progress. Waiting", *cfs.stack.StackName)
return cfs.svc.WaitUntilStackDeleteComplete(&cloudformation.DescribeStacksInput{
StackName: cfs.stack.StackName,
})
} else if *stack.StackStatus == cloudformation.StackStatusDeleteFailed {
logrus.Infof("CloudFormationStack stackName=%s delete failed. Attempting to retain and delete stack", *cfs.stack.StackName)
// This means the CFS has undeleteable resources.
// In order to move on with nuking, we retain them in the deletion.
retainableResources, err := cfs.svc.ListStackResources(&cloudformation.ListStackResourcesInput{
StackName: cfs.stack.StackName,
})
if err != nil {
return err
}
retain := make([]*string, 0)
for _, r := range retainableResources.StackResourceSummaries {
if *r.ResourceStatus != cloudformation.ResourceStatusDeleteComplete {
retain = append(retain, r.LogicalResourceId)
}
}
_, err = cfs.svc.DeleteStack(&cloudformation.DeleteStackInput{
StackName: cfs.stack.StackName,
RetainResources: retain,
})
if err != nil {
return err
}
return cfs.svc.WaitUntilStackDeleteComplete(&cloudformation.DescribeStacksInput{
StackName: cfs.stack.StackName,
})
} else {
if err := cfs.waitForStackToStabilize(*stack.StackStatus); err != nil {
return err
} else if _, err := cfs.svc.DeleteStack(&cloudformation.DeleteStackInput{
StackName: cfs.stack.StackName,
}); err != nil {
return err
} else if err := cfs.svc.WaitUntilStackDeleteComplete(&cloudformation.DescribeStacksInput{
StackName: cfs.stack.StackName,
}); err != nil {
return err
} else {
return nil
}
}
}
func (cfs *CloudFormationStack) waitForStackToStabilize(currentStatus string) error {
switch currentStatus {
case cloudformation.StackStatusUpdateInProgress:
fallthrough
case cloudformation.StackStatusUpdateRollbackCompleteCleanupInProgress:
fallthrough
case cloudformation.StackStatusUpdateRollbackInProgress:
logrus.Infof("CloudFormationStack stackName=%s update in progress. Waiting to stabalize", *cfs.stack.StackName)
return cfs.svc.WaitUntilStackUpdateComplete(&cloudformation.DescribeStacksInput{
StackName: cfs.stack.StackName,
})
case cloudformation.StackStatusCreateInProgress:
fallthrough
case cloudformation.StackStatusRollbackInProgress:
logrus.Infof("CloudFormationStack stackName=%s create in progress. Waiting to stabalize", *cfs.stack.StackName)
return cfs.svc.WaitUntilStackCreateComplete(&cloudformation.DescribeStacksInput{
StackName: cfs.stack.StackName,
})
default:
return nil
}
}
func (cfs *CloudFormationStack) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Name", cfs.stack.StackName)
for _, tagValue := range cfs.stack.Tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}
return properties
}
func (cfs *CloudFormationStack) String() string {
return *cfs.stack.StackName
}
|