Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

skip managed KMS keys #72

Merged
merged 1 commit into from
Jan 17, 2018
Merged
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
31 changes: 9 additions & 22 deletions resources/kms-keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package resources

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
)

type KMSKey struct {
svc *kms.KMS
id string
state string
alias string
svc *kms.KMS
id string
state string
manager *string
}

func init() {
Expand All @@ -23,18 +22,6 @@ func init() {
func ListKMSKeys(sess *session.Session) ([]Resource, error) {
svc := kms.New(sess)

respAlias, err := svc.ListAliases(nil)
if err != nil {
return nil, err
}

aliasMap := map[string]string{}
for _, alias := range respAlias.Aliases {
if alias.TargetKeyId != nil {
aliasMap[*alias.TargetKeyId] = *alias.AliasName
}
}

resp, err := svc.ListKeys(nil)
if err != nil {
return nil, err
Expand All @@ -50,10 +37,10 @@ func ListKMSKeys(sess *session.Session) ([]Resource, error) {
}

resources = append(resources, &KMSKey{
svc: svc,
id: *resp.KeyMetadata.KeyId,
state: *resp.KeyMetadata.KeyState,
alias: aliasMap[*resp.KeyMetadata.KeyId],
svc: svc,
id: *resp.KeyMetadata.KeyId,
state: *resp.KeyMetadata.KeyState,
manager: resp.KeyMetadata.KeyManager,
})
}

Expand All @@ -65,7 +52,7 @@ func (e *KMSKey) Filter() error {
return fmt.Errorf("is already in PendingDeletion state")
}

if strings.HasPrefix(e.alias, "alias/aws/") {
if e.manager != nil && *e.manager == kms.KeyManagerTypeAws {
return fmt.Errorf("cannot delete AWS managed key")
}

Expand Down