From e71283be2a03cd23c3c84f39ac72f1200c813349 Mon Sep 17 00:00:00 2001 From: Erik Kristensen Date: Tue, 11 Jul 2023 09:37:49 -0600 Subject: [PATCH] add: remove custom iam account setting password policy (#14) --- .../iam-account-setting-password-policy.go | 57 +++++++++++++++++++ resources/interface.go | 7 ++- 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 resources/iam-account-setting-password-policy.go diff --git a/resources/iam-account-setting-password-policy.go b/resources/iam-account-setting-password-policy.go new file mode 100644 index 00000000..69de3fee --- /dev/null +++ b/resources/iam-account-setting-password-policy.go @@ -0,0 +1,57 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/iam" + "github.com/aws/aws-sdk-go/service/iam/iamiface" +) + +type IAMAccountSettingPasswordPolicy struct { + svc iamiface.IAMAPI + policy *iam.PasswordPolicy +} + +func init() { + register("IAMAccountSettingPasswordPolicy", ListIAMAccountSettingPasswordPolicy) +} + +func ListIAMAccountSettingPasswordPolicy(sess *session.Session) ([]Resource, error) { + resources := make([]Resource, 0) + + svc := iam.New(sess) + + resp, err := svc.GetAccountPasswordPolicy(&iam.GetAccountPasswordPolicyInput{}) + + if err != nil { + if aerr, ok := err.(awserr.Error); ok { + switch aerr.Code() { + case iam.ErrCodeNoSuchEntityException: + return nil, nil + case iam.ErrCodeServiceFailureException: + return nil, aerr + default: + return nil, aerr + } + } + } + + resources = append(resources, &IAMAccountSettingPasswordPolicy{ + svc: svc, + policy: resp.PasswordPolicy, + }) + + return resources, nil +} + +func (e *IAMAccountSettingPasswordPolicy) Remove() error { + _, err := e.svc.DeleteAccountPasswordPolicy(&iam.DeleteAccountPasswordPolicyInput{}) + if err != nil { + return err + } + return nil +} + +func (e *IAMAccountSettingPasswordPolicy) String() string { + return "custom" +} diff --git a/resources/interface.go b/resources/interface.go index fd1c97a2..9edbcab0 100644 --- a/resources/interface.go +++ b/resources/interface.go @@ -2,7 +2,6 @@ package resources import ( "fmt" - "github.com/rebuy-de/aws-nuke/resources" "strings" "github.com/aws/aws-sdk-go/aws/session" @@ -81,7 +80,7 @@ func GetLister(name string) ResourceLister { func GetListerNames() []string { names := []string{} - for resourceType := range resources.GetListers() { + for resourceType := range GetListers() { names = append(names, resourceType) } @@ -91,3 +90,7 @@ func GetListerNames() []string { func registerCloudControl(typeName string) { register(typeName, NewListCloudControlResource(typeName), mapCloudControl(typeName)) } + +func GetListers() ResourceListers { + return resourceListers +}