Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow disabling of dynamodb deletion protection #23

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading