This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 725
/
kms-keys.go
122 lines (97 loc) · 2.27 KB
/
kms-keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package resources
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/rebuy-de/aws-nuke/v2/pkg/types"
)
type KMSKey struct {
svc *kms.KMS
id string
state string
manager *string
tags []*kms.Tag
}
func init() {
register("KMSKey", ListKMSKeys)
}
func ListKMSKeys(sess *session.Session) ([]Resource, error) {
svc := kms.New(sess)
resources := make([]Resource, 0)
var innerErr error
err := svc.ListKeysPages(nil, func(resp *kms.ListKeysOutput, lastPage bool) bool {
for _, key := range resp.Keys {
resp, err := svc.DescribeKey(&kms.DescribeKeyInput{
KeyId: key.KeyId,
})
if err != nil {
innerErr = err
return false
}
if *resp.KeyMetadata.KeyManager == kms.KeyManagerTypeAws {
continue
}
if *resp.KeyMetadata.KeyState == kms.KeyStatePendingDeletion {
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 {
innerErr = err
return false
}
kmsKey.tags = tags.Tags
resources = append(resources, kmsKey)
}
if lastPage {
return false
}
return true
})
if err != nil {
return nil, err
}
if innerErr != nil {
return nil, err
}
return resources, nil
}
func (e *KMSKey) Filter() error {
if e.state == "PendingDeletion" {
return fmt.Errorf("is already in PendingDeletion state")
}
if e.state == "PendingReplicaDeletion" {
return fmt.Errorf("is already in PendingReplicaDeletion state")
}
if e.manager != nil && *e.manager == kms.KeyManagerTypeAws {
return fmt.Errorf("cannot delete AWS managed key")
}
return nil
}
func (e *KMSKey) Remove() error {
_, err := e.svc.ScheduleKeyDeletion(&kms.ScheduleKeyDeletionInput{
KeyId: &e.id,
PendingWindowInDays: aws.Int64(7),
})
return err
}
func (e *KMSKey) String() string {
return e.id
}
func (i *KMSKey) Properties() types.Properties {
properties := types.NewProperties()
properties.
Set("ID", i.id)
for _, tag := range i.tags {
properties.SetTag(tag.TagKey, tag.TagValue)
}
return properties
}