From 1ded821364448d23a2cf1413d6220d16904946d6 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Tue, 31 Oct 2023 10:03:20 -0500 Subject: [PATCH 1/6] cl-724 Adding support for codedeploy deployment configs Due to errors with using the cloud control resource type AWS::CodeDeploy::DeploymentConfig, creating custom nuke module for CodeDeploy Deployment Configs. --- resources/codedeploy-deployment-configs.go | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 resources/codedeploy-deployment-configs.go diff --git a/resources/codedeploy-deployment-configs.go b/resources/codedeploy-deployment-configs.go new file mode 100644 index 000000000..0c47934c6 --- /dev/null +++ b/resources/codedeploy-deployment-configs.go @@ -0,0 +1,74 @@ +package resources + +import ( + "fmt" + "strings" + + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/codedeploy" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type CodeDeployDeploymentConfig struct { + svc *codedeploy.CodeDeploy + deploymentConfigName *string +} + +func init() { + register("CodeDeployDeploymentConfig", ListCodeDeployDeploymentConfigs) +} + +func ListCodeDeployDeploymentConfigs(sess *session.Session) ([]Resource, error) { + svc := codedeploy.New(sess) + resources := []Resource{} + + params := &codedeploy.ListDeploymentConfigsInput{} + + for { + resp, err := svc.ListDeploymentConfigs(params) + if err != nil { + return nil, err + } + + for _, config := range resp.DeploymentConfigsList { + resources = append(resources, &CodeDeployDeploymentConfig{ + svc: svc, + deploymentConfigName: config, + }) + } + + if resp.NextToken == nil { + break + } + + params.NextToken = resp.NextToken + } + + return resources, nil +} + +func (f *CodeDeployDeploymentConfig) Filter() error { + if strings.HasPrefix(*f.deploymentConfigName, "CodeDeployDefault") { + return fmt.Errorf("cannot delete default codedeploy config") + } + return nil +} + +func (f *CodeDeployDeploymentConfig) Remove() error { + + _, err := f.svc.DeleteDeploymentConfig(&codedeploy.DeleteDeploymentConfigInput{ + DeploymentConfigName: f.deploymentConfigName, + }) + + return err +} + +func (f *CodeDeployDeploymentConfig) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("DeploymentConfigName", f.deploymentConfigName) + return properties +} + +func (f *CodeDeployDeploymentConfig) String() string { + return *f.deploymentConfigName +} From 31d479e18544c4cee451cdb4c1930158e0c2e22d Mon Sep 17 00:00:00 2001 From: Sherd White <106187526+swhite-oreilly@users.noreply.github.com> Date: Wed, 1 Nov 2023 11:23:44 -0500 Subject: [PATCH 2/6] Update resources/codedeploy-deployment-configs.go Co-authored-by: Gabriela Soria --- resources/codedeploy-deployment-configs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/codedeploy-deployment-configs.go b/resources/codedeploy-deployment-configs.go index 0c47934c6..5360210ed 100644 --- a/resources/codedeploy-deployment-configs.go +++ b/resources/codedeploy-deployment-configs.go @@ -15,7 +15,7 @@ type CodeDeployDeploymentConfig struct { } func init() { - register("CodeDeployDeploymentConfig", ListCodeDeployDeploymentConfigs) + register("CodeDeployDeploymentConfig", ListCodeDeployDeploymentConfigs, mapCloudControl("AWS::CodeDeploy::DeploymentConfig")) } func ListCodeDeployDeploymentConfigs(sess *session.Session) ([]Resource, error) { From 0bc09f274589a3f0c3245fc0d23451be3feb22bd Mon Sep 17 00:00:00 2001 From: Sherd White Date: Wed, 1 Nov 2023 13:17:31 -0500 Subject: [PATCH 3/6] Adding custom action type support. --- resources/codepipeline-custom-action-types.go | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 resources/codepipeline-custom-action-types.go diff --git a/resources/codepipeline-custom-action-types.go b/resources/codepipeline-custom-action-types.go new file mode 100644 index 000000000..02acd7410 --- /dev/null +++ b/resources/codepipeline-custom-action-types.go @@ -0,0 +1,79 @@ +package resources + +import ( + "fmt" + "strings" + + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/codepipeline" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type CodePipelineCustomActionType struct { + svc *codepipeline.CodePipeline + owner *string + category *string + provider *string +} + +func init() { + register("CodePipelineCustomActionType", ListCodePipelineCustomActionTypes) +} + +func ListCodePipelineCustomActionTypes(sess *session.Session) ([]Resource, error) { + svc := codepipeline.New(sess) + resources := []Resource{} + + params := &codepipeline.ListActionTypesInput{} + + for { + resp, err := svc.ListActionTypes(params) + if err != nil { + return nil, err + } + + for _, actionTypes := range resp.ActionTypes { + resources = append(resources, &CodePipelineCustomActionType{ + svc: svc, + owner: actionTypes.Id.Owner, + category: actionTypes.Id.Category, + provider: actionTypes.Id.Provider, + }) + } + + if resp.NextToken == nil { + break + } + + params.NextToken = resp.NextToken + } + + return resources, nil +} + +func (f *CodePipelineCustomActionType) Filter() error { + if !strings.HasPrefix(*f.owner, "Custom") { + return fmt.Errorf("cannot delete default codepipeline custom action type") + } + return nil +} + +func (f *CodePipelineCustomActionType) Remove() error { + _, err := f.svc.DeleteCustomActionType(&codepipeline.DeleteCustomActionTypeInput{ + Category: f.category, + }) + + return err +} + +func (f *CodePipelineCustomActionType) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("Category", f.category) + properties.Set("Owner", f.owner) + properties.Set("Provider", f.provider) + return properties +} + +func (f *CodePipelineCustomActionType) String() string { + return *f.owner +} From 96f7e1435437c65a45f476334808b7a8c67f6d48 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Wed, 1 Nov 2023 18:28:02 -0500 Subject: [PATCH 4/6] Add support for codedeploy deployment groups, adding map for custom action types. --- resources/codedeploy-deployment-configs.go | 1 - resources/codedeploy-deployment-group.go | 69 +++++++++++++++++++ resources/codepipeline-custom-action-types.go | 2 +- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 resources/codedeploy-deployment-group.go diff --git a/resources/codedeploy-deployment-configs.go b/resources/codedeploy-deployment-configs.go index 5360210ed..5682df161 100644 --- a/resources/codedeploy-deployment-configs.go +++ b/resources/codedeploy-deployment-configs.go @@ -55,7 +55,6 @@ func (f *CodeDeployDeploymentConfig) Filter() error { } func (f *CodeDeployDeploymentConfig) Remove() error { - _, err := f.svc.DeleteDeploymentConfig(&codedeploy.DeleteDeploymentConfigInput{ DeploymentConfigName: f.deploymentConfigName, }) diff --git a/resources/codedeploy-deployment-group.go b/resources/codedeploy-deployment-group.go new file mode 100644 index 000000000..67616a4de --- /dev/null +++ b/resources/codedeploy-deployment-group.go @@ -0,0 +1,69 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/codedeploy" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type CodeDeployDeploymentGroup struct { + svc *codedeploy.CodeDeploy + deploymentGroupName *string + applicationName *string +} + +func init() { + register("CodeDeployDeploymentGroup", ListCodeDeployDeploymentGroups) +} + +func ListCodeDeployDeploymentGroups(sess *session.Session) ([]Resource, error) { + svc := codedeploy.New(sess) + resources := []Resource{} + + appParams := &codedeploy.ListApplicationsInput{} + appResp, err := svc.ListApplications(appParams) + if err != nil { + return nil, err + } + + for _, appName := range appResp.Applications { + // For each application, list deployment groups + deploymentGroupParams := &codedeploy.ListDeploymentGroupsInput{ + ApplicationName: appName, + } + deploymentGroupResp, err := svc.ListDeploymentGroups(deploymentGroupParams) + if err != nil { + return nil, err + } + + for _, group := range deploymentGroupResp.DeploymentGroups { + resources = append(resources, &CodeDeployDeploymentGroup{ + svc: svc, + deploymentGroupName: group, + applicationName: appName, + }) + } + } + + return resources, nil +} + +func (f *CodeDeployDeploymentGroup) Remove() error { + _, err := f.svc.DeleteDeploymentGroup(&codedeploy.DeleteDeploymentGroupInput{ + ApplicationName: f.applicationName, + DeploymentGroupName: f.deploymentGroupName, + }) + + return err +} + +func (f *CodeDeployDeploymentGroup) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("DeploymentGroupName", f.deploymentGroupName) + properties.Set("ApplicationName", f.applicationName) + return properties +} + +func (f *CodeDeployDeploymentGroup) String() string { + return *f.deploymentGroupName +} diff --git a/resources/codepipeline-custom-action-types.go b/resources/codepipeline-custom-action-types.go index 02acd7410..fd9e2370a 100644 --- a/resources/codepipeline-custom-action-types.go +++ b/resources/codepipeline-custom-action-types.go @@ -17,7 +17,7 @@ type CodePipelineCustomActionType struct { } func init() { - register("CodePipelineCustomActionType", ListCodePipelineCustomActionTypes) + register("CodePipelineCustomActionType", ListCodePipelineCustomActionTypes, mapCloudControl("AWS::CodePipeline::CustomActionType")) } func ListCodePipelineCustomActionTypes(sess *session.Session) ([]Resource, error) { From ec3fa042dd073f111e4bf1f2a000263c2e4b0534 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Wed, 1 Nov 2023 18:38:55 -0500 Subject: [PATCH 5/6] Update codepipeline-custom-action-types.go Adding needed provider and version. --- resources/codepipeline-custom-action-types.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/resources/codepipeline-custom-action-types.go b/resources/codepipeline-custom-action-types.go index fd9e2370a..97a7903b3 100644 --- a/resources/codepipeline-custom-action-types.go +++ b/resources/codepipeline-custom-action-types.go @@ -14,6 +14,7 @@ type CodePipelineCustomActionType struct { owner *string category *string provider *string + version *string } func init() { @@ -38,6 +39,7 @@ func ListCodePipelineCustomActionTypes(sess *session.Session) ([]Resource, error owner: actionTypes.Id.Owner, category: actionTypes.Id.Category, provider: actionTypes.Id.Provider, + version: actionTypes.Id.Version, }) } @@ -61,6 +63,8 @@ func (f *CodePipelineCustomActionType) Filter() error { func (f *CodePipelineCustomActionType) Remove() error { _, err := f.svc.DeleteCustomActionType(&codepipeline.DeleteCustomActionTypeInput{ Category: f.category, + Provider: f.provider, + Version: f.version, }) return err @@ -71,6 +75,7 @@ func (f *CodePipelineCustomActionType) Properties() types.Properties { properties.Set("Category", f.category) properties.Set("Owner", f.owner) properties.Set("Provider", f.provider) + properties.Set("Version", f.version) return properties } From f34717f54889646ad598c959c2a41b57983ddadf Mon Sep 17 00:00:00 2001 From: Sherd White Date: Thu, 2 Nov 2023 10:27:25 -0500 Subject: [PATCH 6/6] Adding webhook support. --- ...oup.go => codedeploy-deployment-groups.go} | 0 resources/codepipeline-webhooks.go | 63 +++++++++++++++++++ 2 files changed, 63 insertions(+) rename resources/{codedeploy-deployment-group.go => codedeploy-deployment-groups.go} (100%) create mode 100644 resources/codepipeline-webhooks.go diff --git a/resources/codedeploy-deployment-group.go b/resources/codedeploy-deployment-groups.go similarity index 100% rename from resources/codedeploy-deployment-group.go rename to resources/codedeploy-deployment-groups.go diff --git a/resources/codepipeline-webhooks.go b/resources/codepipeline-webhooks.go new file mode 100644 index 000000000..508894fe0 --- /dev/null +++ b/resources/codepipeline-webhooks.go @@ -0,0 +1,63 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/codepipeline" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type CodePipelineWebhook struct { + svc *codepipeline.CodePipeline + name *string +} + +func init() { + register("CodePipelineWebhook", ListCodePipelineWebhooks) +} + +func ListCodePipelineWebhooks(sess *session.Session) ([]Resource, error) { + svc := codepipeline.New(sess) + resources := []Resource{} + + params := &codepipeline.ListWebhooksInput{} + + for { + resp, err := svc.ListWebhooks(params) + if err != nil { + return nil, err + } + + for _, webHooks := range resp.Webhooks { + resources = append(resources, &CodePipelineWebhook{ + svc: svc, + name: webHooks.Definition.Name, + }) + } + + if resp.NextToken == nil { + break + } + + params.NextToken = resp.NextToken + } + + return resources, nil +} + +func (f *CodePipelineWebhook) Remove() error { + _, err := f.svc.DeleteWebhook(&codepipeline.DeleteWebhookInput{ + Name: f.name, + }) + + return err +} + +func (f *CodePipelineWebhook) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("Name", f.name) + return properties +} + +func (f *CodePipelineWebhook) String() string { + return *f.name +}