Skip to content

Commit

Permalink
fix: Allow disabling of dynamodb deletion protection (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish25 committed Jun 24, 2024
1 parent 123fe34 commit b5f96b3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type DisableDeletionProtection struct {
CloudformationStack bool `yaml:"CloudformationStack"`
ELBv2 bool `yaml:"ELBv2"`
QLDBLedger bool `yaml:"QLDBLedger"`
DynamoDBTable bool `yaml:"DynamoDBTable"`
}

type PresetDefinitions struct {
Expand Down
39 changes: 28 additions & 11 deletions resources/dynamodb-tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/rebuy-de/aws-nuke/v2/pkg/config"
"github.com/rebuy-de/aws-nuke/v2/pkg/types"
)

type DynamoDBTable struct {
svc *dynamodb.DynamoDB
id string
tags []*dynamodb.Tag
svc *dynamodb.DynamoDB
id string
deletionProtection bool
tags []*dynamodb.Tag

featureFlags config.FeatureFlags
}

func init() {
Expand All @@ -27,23 +31,35 @@ func ListDynamoDBTables(sess *session.Session) ([]Resource, error) {

resources := make([]Resource, 0)
for _, tableName := range resp.TableNames {
tags, err := GetTableTags(svc, tableName)
table, tags, err := GetDynamoDBTable(svc, tableName)

if err != nil {
continue
}

resources = append(resources, &DynamoDBTable{
svc: svc,
id: *tableName,
tags: tags,
svc: svc,
id: *tableName,
deletionProtection: *table.DeletionProtectionEnabled,
tags: tags,
})
}

return resources, nil
}

func (i *DynamoDBTable) Remove() error {
if i.deletionProtection && i.featureFlags.DisableDeletionProtection.DynamoDBTable {
modifyParams := &dynamodb.UpdateTableInput{
TableName: aws.String(i.id),
DeletionProtectionEnabled: aws.Bool(false),
}
_, err := i.svc.UpdateTable(modifyParams)
if err != nil {
return err
}
}

params := &dynamodb.DeleteTableInput{
TableName: aws.String(i.id),
}
Expand All @@ -56,29 +72,30 @@ func (i *DynamoDBTable) Remove() error {
return nil
}

func GetTableTags(svc *dynamodb.DynamoDB, tableName *string) ([]*dynamodb.Tag, error) {
func GetDynamoDBTable(svc *dynamodb.DynamoDB, tableName *string) (*dynamodb.TableDescription, []*dynamodb.Tag, error) {
result, err := svc.DescribeTable(&dynamodb.DescribeTableInput{
TableName: aws.String(*tableName),
})

if err != nil {
return make([]*dynamodb.Tag, 0), err
return nil, make([]*dynamodb.Tag, 0), err
}

tags, err := svc.ListTagsOfResource(&dynamodb.ListTagsOfResourceInput{
ResourceArn: result.Table.TableArn,
})

if err != nil {
return make([]*dynamodb.Tag, 0), err
return nil, make([]*dynamodb.Tag, 0), err
}

return tags.Tags, nil
return result.Table, tags.Tags, nil
}

func (i *DynamoDBTable) Properties() types.Properties {
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)
Expand Down

0 comments on commit b5f96b3

Please sign in to comment.