Skip to content

Commit

Permalink
fix: nuke failure when route53 record NS exists with subdomain (grunt…
Browse files Browse the repository at this point in the history
…work-io#759)

Co-authored-by: James Kwon <[email protected]>
  • Loading branch information
james03160927 and james03160927 authored Aug 14, 2024
1 parent c818032 commit 9f56e5a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
15 changes: 11 additions & 4 deletions aws/resources/route53_hostedzone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/aws/aws-sdk-go/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
Expand All @@ -19,11 +20,12 @@ func (r *Route53HostedZone) getAll(_ context.Context, configObj config.Config) (
return nil, err
}

for _, r := range result.HostedZones {
for _, zone := range result.HostedZones {
if configObj.Route53HostedZone.ShouldInclude(config.ResourceValue{
Name: r.Name,
Name: zone.Name,
}) {
ids = append(ids, r.Id)
ids = append(ids, zone.Id)
r.HostedZonesDomains[awsgo.StringValue(zone.Id)] = zone
}
}
return ids, nil
Expand Down Expand Up @@ -62,9 +64,14 @@ func (r *Route53HostedZone) nukeRecordSet(id *string) (err error) {
return err
}

// get the domain name
var domainName = awsgo.StringValue(r.HostedZonesDomains[awsgo.StringValue(id)].Name)

var changes []*route53.Change
for _, record := range output.ResourceRecordSets {
if aws.StringValue(record.Type) == "NS" || aws.StringValue(record.Type) == "SOA" {
// Note : We can't delete the SOA record or the NS record named ${domain-name}.
// Reference : https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/resource-record-sets-deleting.html
if (aws.StringValue(record.Type) == "NS" || aws.StringValue(record.Type) == "SOA") && awsgo.StringValue(record.Name) == domainName {
logging.Infof("[Skipping] resource record set type is : %s", aws.StringValue(record.Type))
continue
}
Expand Down
13 changes: 13 additions & 0 deletions aws/resources/route53_hostedzone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ func TestR53HostedZone_GetAll(t *testing.T) {
testName1 := "Test name 01"
testName2 := "Test name 02"
rc := Route53HostedZone{
HostedZonesDomains : map[string]*route53.HostedZone{
testId1 : &route53.HostedZone{
Name : awsgo.String(testName1),
},
testId2 : &route53.HostedZone{
Name : awsgo.String(testName2),
},
},
Client: mockedR53HostedZone{
ListHostedZonesOutput: route53.ListHostedZonesOutput{
HostedZones: []*route53.HostedZone{
Expand Down Expand Up @@ -103,6 +111,11 @@ func TestR53HostedZone_Nuke(t *testing.T) {
t.Parallel()

rc := Route53HostedZone{
HostedZonesDomains : map[string]*route53.HostedZone{
"collection-id-01" : &route53.HostedZone{
Name : awsgo.String("domain.com"),
},
},
Client: mockedR53HostedZone{
DeleteHostedZoneOutput: route53.DeleteHostedZoneOutput{},
},
Expand Down
2 changes: 2 additions & 0 deletions aws/resources/route53_hostedzone_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ type Route53HostedZone struct {
Client route53iface.Route53API
Region string
Ids []string
HostedZonesDomains map[string]*route53.HostedZone
}

func (r *Route53HostedZone) Init(session *session.Session) {
r.Client = route53.New(session)
r.HostedZonesDomains = make(map[string]*route53.HostedZone,0)
}

// ResourceName - the simple name of the aws resource
Expand Down

0 comments on commit 9f56e5a

Please sign in to comment.