/
keys.go
277 lines (272 loc) · 8.97 KB
/
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package kms
import (
"context"
"encoding/json"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/aws/aws-sdk-go-v2/service/kms/types"
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)
//go:generate cq-gen -config=keys.hcl -domain=kms -resource=keys
func Keys() *schema.Table {
return &schema.Table{
Name: "aws_kms_keys",
Description: "Contains metadata about a KMS key",
Resolver: fetchKmsKeys,
Multiplex: client.ServiceAccountRegionMultiplexer("kms"),
IgnoreError: client.IgnoreCommonErrors,
DeleteFilter: client.DeleteAccountRegionFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"arn"}},
Columns: []schema.Column{
{
Name: "account_id",
Description: "The AWS Account ID of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSAccount,
},
{
Name: "region",
Description: "The AWS Region of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSRegion,
},
{
Name: "rotation_enabled",
Description: "Specifies whether key rotation is enabled.",
Type: schema.TypeBool,
Resolver: resolveKeysRotationEnabled,
},
{
Name: "tags",
Description: "Key tags.",
Type: schema.TypeJSON,
Resolver: resolveKeysTags,
},
{
Name: "id",
Description: "The globally unique identifier for the KMS key.",
Type: schema.TypeString,
Resolver: schema.PathResolver("KeyId"),
},
{
Name: "aws_account_id",
Description: "The twelve-digit account ID of the Amazon Web Services account that owns the KMS key.",
Type: schema.TypeString,
Resolver: schema.PathResolver("AWSAccountId"),
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the KMS key",
Type: schema.TypeString,
},
{
Name: "cloud_hsm_cluster_id",
Description: "The cluster ID of the CloudHSM cluster that contains the key material for the KMS key",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "creation_date",
Description: "The date and time when the KMS key was created.",
Type: schema.TypeTimestamp,
},
{
Name: "custom_key_store_id",
Description: "A unique identifier for the custom key store that contains the KMS key",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "deletion_date",
Description: "The date and time after which KMS deletes this KMS key",
Type: schema.TypeTimestamp,
IgnoreInTests: true,
},
{
Name: "description",
Description: "The description of the KMS key.",
Type: schema.TypeString,
},
{
Name: "enabled",
Description: "Specifies whether the KMS key is enabled",
Type: schema.TypeBool,
},
{
Name: "encryption_algorithms",
Description: "The encryption algorithms that the KMS key supports",
Type: schema.TypeStringArray,
},
{
Name: "expiration_model",
Description: "Specifies whether the KMS key's key material expires",
Type: schema.TypeString,
},
{
Name: "manager",
Description: "The manager of the KMS key",
Type: schema.TypeString,
Resolver: schema.PathResolver("KeyManager"),
},
{
Name: "key_spec",
Description: "Describes the type of key material in the KMS key.",
Type: schema.TypeString,
},
{
Name: "key_state",
Description: "The current status of the KMS key",
Type: schema.TypeString,
},
{
Name: "key_usage",
Description: "The cryptographic operations for which you can use the KMS key.",
Type: schema.TypeString,
},
{
Name: "mac_algorithms",
Description: "The message authentication code (MAC) algorithm that the HMAC KMS key supports. This value is present only when the KeyUsage of the KMS key is GENERATE_VERIFY_MAC.",
Type: schema.TypeStringArray,
IgnoreInTests: true,
},
{
Name: "multi_region",
Description: "Indicates whether the KMS key is a multi-Region (True) or regional (False) key. This value is True for multi-Region primary and replica keys and False for regional KMS keys",
Type: schema.TypeBool,
},
{
Name: "multi_region_key_type",
Description: "Indicates whether the KMS key is a PRIMARY or REPLICA key.",
Type: schema.TypeString,
Resolver: schema.PathResolver("MultiRegionConfiguration.MultiRegionKeyType"),
},
{
Name: "primary_key_arn",
Description: "Displays the key ARN of a primary or replica key of a multi-Region key.",
Type: schema.TypeString,
Resolver: schema.PathResolver("MultiRegionConfiguration.PrimaryKey.Arn"),
IgnoreInTests: true,
},
{
Name: "primary_key_region",
Description: "Displays the Amazon Web Services Region of a primary or replica key in a multi-Region key.",
Type: schema.TypeString,
Resolver: schema.PathResolver("MultiRegionConfiguration.PrimaryKey.Region"),
IgnoreInTests: true,
},
{
Name: "replica_keys",
Description: "displays the key ARNs and Regions of all replica keys",
Type: schema.TypeJSON,
Resolver: resolveKeysReplicaKeys,
IgnoreInTests: true,
},
{
Name: "origin",
Description: "The source of the key material for the KMS key",
Type: schema.TypeString,
},
{
Name: "pending_deletion_window_in_days",
Description: "The waiting period before the primary key in a multi-Region key is deleted",
Type: schema.TypeInt,
IgnoreInTests: true,
},
{
Name: "signing_algorithms",
Description: "The signing algorithms that the KMS key supports",
Type: schema.TypeStringArray,
IgnoreInTests: true,
},
{
Name: "valid_to",
Description: "The time at which the imported key material expires",
Type: schema.TypeTimestamp,
IgnoreInTests: true,
},
},
}
}
// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================
func fetchKmsKeys(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
var input kms.ListKeysInput
c := meta.(*client.Client)
svc := c.Services().KMS
for {
response, err := svc.ListKeys(ctx, &input)
if err != nil {
return diag.WrapError(err)
}
for _, item := range response.Keys {
d, err := svc.DescribeKey(ctx, &kms.DescribeKeyInput{KeyId: item.KeyId}, func(options *kms.Options) {
options.Region = c.Region
})
if err != nil {
if c.IsNotFoundError(err) {
continue
}
return diag.WrapError(err)
}
if d.KeyMetadata != nil {
res <- *d.KeyMetadata
}
}
if aws.ToString(response.NextMarker) == "" {
break
}
input.Marker = response.NextMarker
}
return nil
}
func resolveKeysReplicaKeys(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
key := resource.Item.(types.KeyMetadata)
if key.MultiRegionConfiguration == nil {
return nil
}
b, err := json.Marshal(key.MultiRegionConfiguration.ReplicaKeys)
if err != nil {
return diag.WrapError(err)
}
return diag.WrapError(resource.Set(c.Name, b))
}
func resolveKeysTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cl := meta.(*client.Client)
svc := cl.Services().KMS
key := resource.Item.(types.KeyMetadata)
if key.Origin == "EXTERNAL" || key.KeyManager == "AWS" {
return nil
}
params := kms.ListResourceTagsInput{KeyId: key.KeyId}
tags := make(map[string]string)
for {
result, err := svc.ListResourceTags(ctx, ¶ms)
if err != nil {
return diag.WrapError(err)
}
for _, v := range result.Tags {
tags[aws.ToString(v.TagKey)] = aws.ToString(v.TagValue)
}
if aws.ToString(result.NextMarker) == "" {
break
}
params.Marker = result.NextMarker
}
return diag.WrapError(resource.Set(c.Name, tags))
}
func resolveKeysRotationEnabled(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cl := meta.(*client.Client)
svc := cl.Services().KMS
key := resource.Item.(types.KeyMetadata)
if key.Origin == "EXTERNAL" || key.KeyManager == "AWS" {
return nil
}
result, err := svc.GetKeyRotationStatus(ctx, &kms.GetKeyRotationStatusInput{KeyId: key.KeyId})
if err != nil {
return diag.WrapError(err)
}
return diag.WrapError(resource.Set(c.Name, result.KeyRotationEnabled))
}