-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from ekristen/kms-tweaks
feat(kms): continue processing keys if error encountered
- Loading branch information
Showing
10 changed files
with
3,471 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/gotidy/ptr" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/kms" | ||
|
||
"github.com/ekristen/aws-nuke/v3/mocks/mock_kmsiface" | ||
"github.com/ekristen/aws-nuke/v3/pkg/nuke" | ||
) | ||
|
||
func Test_Mock_KMSAlias_List(t *testing.T) { | ||
a := assert.New(t) | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockKMS := mock_kmsiface.NewMockKMSAPI(ctrl) | ||
|
||
mockKMS.EXPECT().ListAliasesPages(gomock.Any(), gomock.Any()).DoAndReturn( | ||
func(input *kms.ListAliasesInput, fn func(*kms.ListAliasesOutput, bool) bool) error { | ||
fn(&kms.ListAliasesOutput{ | ||
Aliases: []*kms.AliasListEntry{ | ||
{AliasName: aws.String("alias/test-alias-1")}, | ||
{AliasName: aws.String("alias/test-alias-2")}, | ||
}, | ||
}, true) | ||
return nil | ||
}, | ||
) | ||
|
||
lister := KMSAliasLister{ | ||
mockSvc: mockKMS, | ||
} | ||
|
||
resources, err := lister.List(context.TODO(), &nuke.ListerOpts{ | ||
Region: &nuke.Region{ | ||
Name: "us-east-2", | ||
}, | ||
Session: session.Must(session.NewSession()), | ||
}) | ||
a.NoError(err) | ||
a.Len(resources, 2) | ||
} | ||
|
||
func Test_Mock_KMSAlias_List_Error(t *testing.T) { | ||
a := assert.New(t) | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockKMS := mock_kmsiface.NewMockKMSAPI(ctrl) | ||
|
||
mockKMS.EXPECT(). | ||
ListAliasesPages(gomock.Any(), gomock.Any()). | ||
Return(awserr.New("BadRequest", "400 Bad Request", nil)) | ||
|
||
lister := KMSAliasLister{ | ||
mockSvc: mockKMS, | ||
} | ||
|
||
resources, err := lister.List(context.TODO(), &nuke.ListerOpts{ | ||
Region: &nuke.Region{ | ||
Name: "us-east-2", | ||
}, | ||
Session: session.Must(session.NewSession()), | ||
}) | ||
a.Error(err) | ||
a.Nil(resources) | ||
a.EqualError(err, "BadRequest: 400 Bad Request") | ||
} | ||
|
||
func Test_KMSAlias_Filter(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
alias := &KMSAlias{ | ||
Name: ptr.String("alias/aws/test-alias"), | ||
} | ||
|
||
err := alias.Filter() | ||
a.Error(err) | ||
a.EqualError(err, "cannot delete AWS alias") | ||
|
||
alias.Name = ptr.String("alias/custom/test-alias") | ||
err = alias.Filter() | ||
a.NoError(err) | ||
} | ||
|
||
func Test_KMSAlias_Properties(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
alias := &KMSAlias{ | ||
Name: ptr.String("alias/custom/test-alias"), | ||
} | ||
|
||
a.Equal("alias/custom/test-alias", alias.String()) | ||
a.Equal("alias/custom/test-alias", alias.Properties().Get("Name")) | ||
} | ||
|
||
func Test_Mock_KMSAlias_Remove(t *testing.T) { | ||
a := assert.New(t) | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockKMS := mock_kmsiface.NewMockKMSAPI(ctrl) | ||
|
||
// Mock the DeleteAlias method | ||
mockKMS.EXPECT().DeleteAlias(&kms.DeleteAliasInput{ | ||
AliasName: ptr.String("alias/test-alias-1"), | ||
}).Return(&kms.DeleteAliasOutput{}, nil) | ||
|
||
alias := &KMSAlias{ | ||
svc: mockKMS, | ||
Name: ptr.String("alias/test-alias-1"), | ||
} | ||
|
||
err := alias.Remove(context.TODO()) | ||
a.NoError(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/gotidy/ptr" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/kms" | ||
"github.com/aws/aws-sdk-go/service/kms/kmsiface" | ||
|
||
"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" | ||
) | ||
|
||
const KMSKeyResource = "KMSKey" | ||
|
||
func init() { | ||
registry.Register(®istry.Registration{ | ||
Name: KMSKeyResource, | ||
Scope: nuke.Account, | ||
Lister: &KMSKeyLister{}, | ||
DependsOn: []string{ | ||
KMSAliasResource, | ||
}, | ||
}) | ||
} | ||
|
||
type KMSKeyLister struct { | ||
mockSvc kmsiface.KMSAPI | ||
} | ||
|
||
func (l *KMSKeyLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) { | ||
opts := o.(*nuke.ListerOpts) | ||
resources := make([]resource.Resource, 0) | ||
|
||
var svc kmsiface.KMSAPI | ||
if l.mockSvc != nil { | ||
svc = l.mockSvc | ||
} else { | ||
svc = kms.New(opts.Session) | ||
} | ||
|
||
inaccessibleKeys := false | ||
|
||
if err := svc.ListKeysPages(nil, func(keysOut *kms.ListKeysOutput, lastPage bool) bool { | ||
for _, key := range keysOut.Keys { | ||
resp, err := svc.DescribeKey(&kms.DescribeKeyInput{ | ||
KeyId: key.KeyId, | ||
}) | ||
if err != nil { | ||
var awsError awserr.Error | ||
if errors.As(err, &awsError) { | ||
if awsError.Code() == "AccessDeniedException" { | ||
inaccessibleKeys = true | ||
logrus.WithError(err).Debug("unable to describe key") | ||
continue | ||
} | ||
} | ||
|
||
logrus.WithError(err).Error("unable to describe key") | ||
continue | ||
} | ||
|
||
kmsKey := &KMSKey{ | ||
svc: svc, | ||
ID: resp.KeyMetadata.KeyId, | ||
State: resp.KeyMetadata.KeyState, | ||
Manager: resp.KeyMetadata.KeyManager, | ||
} | ||
|
||
tags, err := svc.ListResourceTags(&kms.ListResourceTagsInput{ | ||
KeyId: key.KeyId, | ||
}) | ||
if err != nil { | ||
logrus.WithError(err).Error("unable to list tags") | ||
} else { | ||
kmsKey.Tags = tags.Tags | ||
} | ||
|
||
resources = append(resources, kmsKey) | ||
} | ||
|
||
return !lastPage | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
if inaccessibleKeys { | ||
logrus.Warn("one or more KMS keys were inaccessible, debug logging will contain more information") | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
type KMSKey struct { | ||
svc kmsiface.KMSAPI | ||
ID *string | ||
State *string | ||
Manager *string | ||
Tags []*kms.Tag | ||
} | ||
|
||
func (r *KMSKey) Filter() error { | ||
if ptr.ToString(r.State) == kms.KeyStatePendingDeletion { | ||
return fmt.Errorf("is already in PendingDeletion state") | ||
} | ||
|
||
if ptr.ToString(r.Manager) == kms.KeyManagerTypeAws { | ||
return fmt.Errorf("cannot delete AWS managed key") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *KMSKey) Remove(_ context.Context) error { | ||
_, err := r.svc.ScheduleKeyDeletion(&kms.ScheduleKeyDeletionInput{ | ||
KeyId: r.ID, | ||
PendingWindowInDays: aws.Int64(7), | ||
}) | ||
return err | ||
} | ||
|
||
func (r *KMSKey) String() string { | ||
return *r.ID | ||
} | ||
|
||
func (r *KMSKey) Properties() types.Properties { | ||
return types.NewPropertiesFromStruct(r) | ||
} |
Oops, something went wrong.