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

Fix config rule nuking #762

Merged
merged 2 commits into from
Aug 16, 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
24 changes: 20 additions & 4 deletions aws/resources/config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,36 @@ func (csr *ConfigServiceRule) nukeAll(configRuleNames []string) error {

for _, configRuleName := range configRuleNames {
logging.Debug(fmt.Sprintf("Start deleting config service rule: %s", configRuleName))
_, err := csr.Client.DeleteRemediationConfigurationWithContext(csr.Context, &configservice.DeleteRemediationConfigurationInput{
ConfigRuleName: aws.String(configRuleName),

res, err := csr.Client.DescribeRemediationConfigurationsWithContext(csr.Context, &configservice.DescribeRemediationConfigurationsInput{
ConfigRuleNames: []*string{aws.String(configRuleName)},
})
if err != nil {
pterm.Error.Println(fmt.Sprintf("Failed to delete remediation configuration w/ err %s", err))
pterm.Error.Println(fmt.Sprintf("Failed to describe remediation configurations w/ err %s", err))
report.Record(report.Entry{
Identifier: configRuleName,
ResourceType: "Config service rule",
Error: err,
})

continue
}

if len(res.RemediationConfigurations) > 0 {
_, err := csr.Client.DeleteRemediationConfigurationWithContext(csr.Context, &configservice.DeleteRemediationConfigurationInput{
ConfigRuleName: aws.String(configRuleName),
})
if err != nil {
pterm.Error.Println(fmt.Sprintf("Failed to delete remediation configuration w/ err %s", err))
report.Record(report.Entry{
Identifier: configRuleName,
ResourceType: "Config service rule",
Error: err,
})

continue
}
}

params := &configservice.DeleteConfigRuleInput{
ConfigRuleName: aws.String(configRuleName),
}
Expand Down
16 changes: 11 additions & 5 deletions aws/resources/config_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (

type mockedConfigServiceRule struct {
configserviceiface.ConfigServiceAPI
DescribeConfigRulesOutput configservice.DescribeConfigRulesOutput
DeleteConfigRuleOutput configservice.DeleteConfigRuleOutput
DeleteRemediationConfigurationOutput configservice.DeleteRemediationConfigurationOutput
DescribeConfigRulesOutput configservice.DescribeConfigRulesOutput
DeleteConfigRuleOutput configservice.DeleteConfigRuleOutput
DeleteRemediationConfigurationOutput configservice.DeleteRemediationConfigurationOutput
DescribeRemediationConfigurationsOutput configservice.DescribeRemediationConfigurationsOutput
}

func (m mockedConfigServiceRule) DescribeConfigRulesPagesWithContext(_ awsgo.Context, _ *configservice.DescribeConfigRulesInput, fn func(*configservice.DescribeConfigRulesOutput, bool) bool, _ ...request.Option) error {
Expand All @@ -34,6 +35,10 @@ func (m mockedConfigServiceRule) DeleteRemediationConfigurationWithContext(_ aws
return &m.DeleteRemediationConfigurationOutput, nil
}

func (m mockedConfigServiceRule) DescribeRemediationConfigurationsWithContext(_ awsgo.Context, _ *configservice.DescribeRemediationConfigurationsInput, _ ...request.Option) (*configservice.DescribeRemediationConfigurationsOutput, error) {
return &m.DescribeRemediationConfigurationsOutput, nil
}

func TestConfigServiceRule_GetAll(t *testing.T) {

t.Parallel()
Expand Down Expand Up @@ -87,8 +92,9 @@ func TestConfigServiceRule_NukeAll(t *testing.T) {

csr := ConfigServiceRule{
Client: mockedConfigServiceRule{
DeleteConfigRuleOutput: configservice.DeleteConfigRuleOutput{},
DeleteRemediationConfigurationOutput: configservice.DeleteRemediationConfigurationOutput{},
DeleteConfigRuleOutput: configservice.DeleteConfigRuleOutput{},
DeleteRemediationConfigurationOutput: configservice.DeleteRemediationConfigurationOutput{},
DescribeRemediationConfigurationsOutput: configservice.DescribeRemediationConfigurationsOutput{},
},
}

Expand Down