From 12bee2b322d2b0ca76762fdf8b8676fac4cec6b7 Mon Sep 17 00:00:00 2001 From: Erik Kristensen Date: Mon, 11 Nov 2024 11:22:12 -0700 Subject: [PATCH] fix(inspector2): disable only enabled services --- resources/inspector2.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/resources/inspector2.go b/resources/inspector2.go index 89818b88..b66299ba 100644 --- a/resources/inspector2.go +++ b/resources/inspector2.go @@ -8,6 +8,7 @@ import ( "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" + "github.com/ekristen/libnuke/pkg/types" "github.com/ekristen/aws-nuke/v3/pkg/nuke" ) @@ -39,7 +40,13 @@ func (l *Inspector2Lister) List(_ context.Context, o interface{}) ([]resource.Re if *a.State.Status != inspector2.StatusDisabled { resources = append(resources, &Inspector2{ svc: svc, - accountID: a.AccountId, + AccountID: a.AccountId, + ResourceState: map[string]string{ + inspector2.ResourceScanTypeEc2: *a.ResourceState.Ec2.Status, + inspector2.ResourceScanTypeEcr: *a.ResourceState.Ecr.Status, + inspector2.ResourceScanTypeLambda: *a.ResourceState.Lambda.Status, + inspector2.ResourceScanTypeLambdaCode: *a.ResourceState.LambdaCode.Status, + }, }) } } @@ -48,14 +55,25 @@ func (l *Inspector2Lister) List(_ context.Context, o interface{}) ([]resource.Re } type Inspector2 struct { - svc *inspector2.Inspector2 - accountID *string + svc *inspector2.Inspector2 + AccountID *string + ResourceState map[string]string `property:"tagPrefix=resourceType"` +} + +func (e *Inspector2) GetEnabledResources() []string { + var resources = make([]string, 0) + for k, v := range e.ResourceState { + if v == inspector2.StatusEnabled { + resources = append(resources, k) + } + } + return resources } func (e *Inspector2) Remove(_ context.Context) error { _, err := e.svc.Disable(&inspector2.DisableInput{ - AccountIds: []*string{e.accountID}, - ResourceTypes: aws.StringSlice(inspector2.ResourceScanType_Values()), + AccountIds: []*string{e.AccountID}, + ResourceTypes: aws.StringSlice(e.GetEnabledResources()), }) if err != nil { return err @@ -65,5 +83,9 @@ func (e *Inspector2) Remove(_ context.Context) error { } func (e *Inspector2) String() string { - return *e.accountID + return *e.AccountID +} + +func (e *Inspector2) Properties() types.Properties { + return types.NewPropertiesFromStruct(e) }