Skip to content

Commit

Permalink
fix: remove predicates before rule delete (#704)
Browse files Browse the repository at this point in the history
Co-authored-by: curtis <curtis@chammock.dev>
  • Loading branch information
chammock and curtis authored Dec 4, 2021
1 parent f60f6ec commit f6d9952
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
47 changes: 43 additions & 4 deletions resources/waf-rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/waf"
"github.com/rebuy-de/aws-nuke/pkg/types"
)

type WAFRule struct {
svc *waf.WAF
ID *string
svc *waf.WAF
ID *string
rule *waf.Rule
}

func init() {
Expand All @@ -30,9 +32,13 @@ func ListWAFRules(sess *session.Session) ([]Resource, error) {
}

for _, rule := range resp.Rules {
ruleResp, _ := svc.GetRule(&waf.GetRuleInput{
RuleId: rule.RuleId,
})
resources = append(resources, &WAFRule{
svc: svc,
ID: rule.RuleId,
svc: svc,
ID: rule.RuleId,
rule: ruleResp.Rule,
})
}

Expand All @@ -53,6 +59,29 @@ func (f *WAFRule) Remove() error {
return err
}

ruleUpdates := []*waf.RuleUpdate{}
for _, predicate := range f.rule.Predicates {
ruleUpdates = append(ruleUpdates, &waf.RuleUpdate{
Action: aws.String(waf.ChangeActionDelete),
Predicate: predicate,
})
}

_, err = f.svc.UpdateRule(&waf.UpdateRuleInput{
ChangeToken: tokenOutput.ChangeToken,
RuleId: f.ID,
Updates: ruleUpdates,
})

if err != nil {
return err
}

tokenOutput, err = f.svc.GetChangeToken(&waf.GetChangeTokenInput{})
if err != nil {
return err
}

_, err = f.svc.DeleteRule(&waf.DeleteRuleInput{
RuleId: f.ID,
ChangeToken: tokenOutput.ChangeToken,
Expand All @@ -64,3 +93,13 @@ func (f *WAFRule) Remove() error {
func (f *WAFRule) String() string {
return *f.ID
}

func (f *WAFRule) Properties() types.Properties {
properties := types.NewProperties()

properties.
Set("ID", f.ID).
Set("Name", f.rule.Name)

return properties
}
28 changes: 28 additions & 0 deletions resources/wafregional-rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type WAFRegionalRule struct {
svc *wafregional.WAFRegional
ID *string
name *string
rule *waf.Rule
}

func init() {
Expand All @@ -33,10 +34,14 @@ func ListWAFRegionalRules(sess *session.Session) ([]Resource, error) {
}

for _, rule := range resp.Rules {
ruleResp, _ := svc.GetRule(&waf.GetRuleInput{
RuleId: rule.RuleId,
})
resources = append(resources, &WAFRegionalRule{
svc: svc,
ID: rule.RuleId,
name: rule.Name,
rule: ruleResp.Rule,
})
}

Expand All @@ -57,6 +62,29 @@ func (f *WAFRegionalRule) Remove() error {
return err
}

ruleUpdates := []*waf.RuleUpdate{}
for _, predicate := range f.rule.Predicates {
ruleUpdates = append(ruleUpdates, &waf.RuleUpdate{
Action: aws.String(waf.ChangeActionDelete),
Predicate: predicate,
})
}

_, err = f.svc.UpdateRule(&waf.UpdateRuleInput{
ChangeToken: tokenOutput.ChangeToken,
RuleId: f.ID,
Updates: ruleUpdates,
})

if err != nil {
return err
}

tokenOutput, err = f.svc.GetChangeToken(&waf.GetChangeTokenInput{})
if err != nil {
return err
}

_, err = f.svc.DeleteRule(&waf.DeleteRuleInput{
RuleId: f.ID,
ChangeToken: tokenOutput.ChangeToken,
Expand Down

0 comments on commit f6d9952

Please sign in to comment.