driftctl/pkg/remote/aws/repository/kms_repository.go

85 lines
2.0 KiB
Go
Raw Normal View History

2021-02-24 17:49:02 +00:00
package repository
import (
2021-02-25 13:19:49 +00:00
"strings"
2021-02-24 17:49:02 +00:00
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/kms/kmsiface"
)
type KMSRepository interface {
ListAllKeys() ([]*kms.KeyListEntry, error)
2021-02-25 13:19:49 +00:00
ListAllAliases() ([]*kms.AliasListEntry, error)
2021-02-24 17:49:02 +00:00
}
type kmsRepository struct {
client kmsiface.KMSAPI
}
func NewKMSRepository(session *session.Session) *kmsRepository {
return &kmsRepository{
kms.New(session),
}
}
func (r *kmsRepository) ListAllKeys() ([]*kms.KeyListEntry, error) {
var keys []*kms.KeyListEntry
input := kms.ListKeysInput{}
err := r.client.ListKeysPages(&input,
func(resp *kms.ListKeysOutput, lastPage bool) bool {
keys = append(keys, resp.Keys...)
return !lastPage
},
)
if err != nil {
return nil, err
}
customerKeys, err := r.filterKeys(keys)
if err != nil {
return nil, err
}
return customerKeys, nil
}
2021-02-25 13:19:49 +00:00
func (r *kmsRepository) ListAllAliases() ([]*kms.AliasListEntry, error) {
var aliases []*kms.AliasListEntry
input := kms.ListAliasesInput{}
err := r.client.ListAliasesPages(&input,
func(resp *kms.ListAliasesOutput, lastPage bool) bool {
aliases = append(aliases, resp.Aliases...)
return !lastPage
},
)
if err != nil {
return nil, err
}
return r.filterAliases(aliases), nil
}
2021-02-24 17:49:02 +00:00
func (r *kmsRepository) filterKeys(keys []*kms.KeyListEntry) ([]*kms.KeyListEntry, error) {
var customerKeys []*kms.KeyListEntry
for _, key := range keys {
k, err := r.client.DescribeKey(&kms.DescribeKeyInput{
KeyId: key.KeyId,
})
if err != nil {
return nil, err
}
if k.KeyMetadata.KeyManager != nil && *k.KeyMetadata.KeyManager != "AWS" {
customerKeys = append(customerKeys, key)
}
}
return customerKeys, nil
}
2021-02-25 13:19:49 +00:00
func (r *kmsRepository) filterAliases(aliases []*kms.AliasListEntry) []*kms.AliasListEntry {
var customerAliases []*kms.AliasListEntry
for _, alias := range aliases {
if alias.AliasName != nil && !strings.HasPrefix(*alias.AliasName, "alias/aws/") {
customerAliases = append(customerAliases, alias)
}
}
return customerAliases
}