/
resource_aws_kms_grant.go
550 lines (473 loc) · 15.4 KB
/
resource_aws_kms_grant.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package aws
import (
"bytes"
"fmt"
"log"
"sort"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsKmsGrant() *schema.Resource {
return &schema.Resource{
// There is no API for updating/modifying grants, hence no Update
// Instead changes to most fields will force a new resource
Create: resourceAwsKmsGrantCreate,
Read: resourceAwsKmsGrantRead,
Delete: resourceAwsKmsGrantDelete,
Exists: resourceAwsKmsGrantExists,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateAwsKmsGrantName,
},
"key_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"grantee_principal": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"operations": {
Type: schema.TypeSet,
Set: schema.HashString,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
kms.GrantOperationCreateGrant,
kms.GrantOperationDecrypt,
kms.GrantOperationDescribeKey,
kms.GrantOperationEncrypt,
kms.GrantOperationGenerateDataKey,
kms.GrantOperationGenerateDataKeyWithoutPlaintext,
kms.GrantOperationReEncryptFrom,
kms.GrantOperationReEncryptTo,
kms.GrantOperationRetireGrant,
}, false),
},
Required: true,
ForceNew: true,
},
"constraints": {
Type: schema.TypeSet,
Set: resourceKmsGrantConstraintsHash,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"encryption_context_equals": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
// ConflictsWith encryption_context_subset handled in Create, see kmsGrantConstraintsIsValid
},
"encryption_context_subset": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
// ConflictsWith encryption_context_equals handled in Create, see kmsGrantConstraintsIsValid
},
},
},
},
"retiring_principal": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"grant_creation_tokens": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
ForceNew: true,
},
"retire_on_delete": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
},
"grant_id": {
Type: schema.TypeString,
Computed: true,
},
"grant_token": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsKmsGrantCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kmsconn
keyId := d.Get("key_id").(string)
input := kms.CreateGrantInput{
GranteePrincipal: aws.String(d.Get("grantee_principal").(string)),
KeyId: aws.String(keyId),
Operations: expandStringSet(d.Get("operations").(*schema.Set)),
}
if v, ok := d.GetOk("name"); ok {
input.Name = aws.String(v.(string))
}
if v, ok := d.GetOk("constraints"); ok {
if !kmsGrantConstraintsIsValid(v.(*schema.Set)) {
return fmt.Errorf("A grant constraint can't have both encryption_context_equals and encryption_context_subset set")
}
input.Constraints = expandKmsGrantConstraints(v.(*schema.Set))
}
if v, ok := d.GetOk("retiring_principal"); ok {
input.RetiringPrincipal = aws.String(v.(string))
}
if v, ok := d.GetOk("grant_creation_tokens"); ok {
input.GrantTokens = expandStringSet(v.(*schema.Set))
}
log.Printf("[DEBUG]: Adding new KMS Grant: %s", input)
var out *kms.CreateGrantOutput
err := resource.Retry(3*time.Minute, func() *resource.RetryError {
var err error
out, err = conn.CreateGrant(&input)
if err != nil {
// Error Codes: https://docs.aws.amazon.com/sdk-for-go/api/service/kms/#KMS.CreateGrant
// Under some circumstances a newly created IAM Role doesn't show up and causes
// an InvalidArnException to be thrown.
if isAWSErr(err, kms.ErrCodeDependencyTimeoutException, "") ||
isAWSErr(err, kms.ErrCodeInternalException, "") ||
isAWSErr(err, kms.ErrCodeInvalidArnException, "") {
return resource.RetryableError(
fmt.Errorf("Error adding new KMS Grant for key: %s, retrying %s",
*input.KeyId, err))
}
log.Printf("[ERROR] An error occurred creating new AWS KMS Grant: %s", err)
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return err
}
log.Printf("[DEBUG] Created new KMS Grant: %s", *out.GrantId)
d.SetId(fmt.Sprintf("%s:%s", keyId, *out.GrantId))
d.Set("grant_id", out.GrantId)
d.Set("grant_token", out.GrantToken)
return resourceAwsKmsGrantRead(d, meta)
}
func resourceAwsKmsGrantRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kmsconn
keyId, grantId, err := decodeKmsGrantId(d.Id())
if err != nil {
return err
}
log.Printf("[DEBUG] Looking for grant id: %s", grantId)
grant, err := findKmsGrantByIdWithRetry(conn, keyId, grantId)
if err != nil {
return err
}
if grant == nil {
log.Printf("[WARN] %s KMS grant id not found for key id %s, removing from state file", grantId, keyId)
d.SetId("")
return nil
}
// The grant sometimes contains principals that identified by their unique id: "AROAJYCVIVUZIMTXXXXX"
// instead of "arn:aws:...", in this case don't update the state file
if strings.HasPrefix(*grant.GranteePrincipal, "arn:aws") {
d.Set("grantee_principal", grant.GranteePrincipal)
} else {
log.Printf(
"[WARN] Unable to update grantee principal state %s for grant id %s for key id %s.",
*grant.GranteePrincipal, grantId, keyId)
}
if grant.RetiringPrincipal != nil {
if strings.HasPrefix(*grant.RetiringPrincipal, "arn:aws") {
d.Set("retiring_principal", grant.RetiringPrincipal)
} else {
log.Printf(
"[WARN] Unable to update retiring principal state %s for grant id %s for key id %s",
*grant.RetiringPrincipal, grantId, keyId)
}
}
if err := d.Set("operations", aws.StringValueSlice(grant.Operations)); err != nil {
log.Printf("[DEBUG] Error setting operations for grant %s with error %s", grantId, err)
}
if *grant.Name != "" {
d.Set("name", grant.Name)
}
if grant.Constraints != nil {
if err := d.Set("constraints", flattenKmsGrantConstraints(grant.Constraints)); err != nil {
log.Printf("[DEBUG] Error setting constraints for grant %s with error %s", grantId, err)
}
}
return nil
}
func resourceAwsKmsGrantDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kmsconn
keyId, grantId, decodeErr := decodeKmsGrantId(d.Id())
if decodeErr != nil {
return decodeErr
}
doRetire := d.Get("retire_on_delete").(bool)
var err error
if doRetire {
retireInput := kms.RetireGrantInput{
GrantId: aws.String(grantId),
KeyId: aws.String(keyId),
}
log.Printf("[DEBUG] Retiring KMS grant: %s", grantId)
_, err = conn.RetireGrant(&retireInput)
} else {
revokeInput := kms.RevokeGrantInput{
GrantId: aws.String(grantId),
KeyId: aws.String(keyId),
}
log.Printf("[DEBUG] Revoking KMS grant: %s", grantId)
_, err = conn.RevokeGrant(&revokeInput)
}
if err != nil {
if isAWSErr(err, kms.ErrCodeNotFoundException, "") {
return nil
}
return err
}
log.Printf("[DEBUG] Checking if grant is revoked: %s", grantId)
err = waitForKmsGrantToBeRevoked(conn, keyId, grantId)
return err
}
func resourceAwsKmsGrantExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*AWSClient).kmsconn
keyId, grantId, err := decodeKmsGrantId(d.Id())
if err != nil {
return false, err
}
log.Printf("[DEBUG] Looking for Grant: %s", grantId)
grant, err := findKmsGrantByIdWithRetry(conn, keyId, grantId)
if err != nil {
return true, err
}
if grant != nil {
return true, err
}
return false, nil
}
func getKmsGrantById(grants []*kms.GrantListEntry, grantIdentifier string) *kms.GrantListEntry {
for _, grant := range grants {
if *grant.GrantId == grantIdentifier {
return grant
}
}
return nil
}
/*
In the functions below it is not possible to use retryOnAwsCodes function, as there
is no describe grants call, so an error has to be created if the grant is or isn't returned
by the list grants call when expected.
*/
// NB: This function only retries the grant not being returned and some edge cases, while AWS Errors
// are handled by the findKmsGrantById function
func findKmsGrantByIdWithRetry(conn *kms.KMS, keyId string, grantId string) (*kms.GrantListEntry, error) {
var grant *kms.GrantListEntry
err := resource.Retry(3*time.Minute, func() *resource.RetryError {
var err error
grant, err = findKmsGrantById(conn, keyId, grantId, nil)
if err != nil {
if serr, ok := err.(KmsGrantMissingError); ok {
// Force a retry if the grant should exist
return resource.RetryableError(serr)
}
return resource.NonRetryableError(err)
}
return nil
})
return grant, err
}
// Used by the tests as well
func waitForKmsGrantToBeRevoked(conn *kms.KMS, keyId string, grantId string) error {
err := resource.Retry(3*time.Minute, func() *resource.RetryError {
grant, err := findKmsGrantById(conn, keyId, grantId, nil)
if err != nil {
if _, ok := err.(KmsGrantMissingError); ok {
return nil
}
}
if grant != nil {
// Force a retry if the grant still exists
return resource.RetryableError(
fmt.Errorf("Grant still exists while expected to be revoked, retyring revocation check: %s", *grant.GrantId))
}
return resource.NonRetryableError(err)
})
return err
}
// The ListGrants API defaults to listing only 50 grants
// Use a marker to iterate over all grants in "pages"
// NB: This function only retries on AWS Errors
func findKmsGrantById(conn *kms.KMS, keyId string, grantId string, marker *string) (*kms.GrantListEntry, error) {
input := kms.ListGrantsInput{
KeyId: aws.String(keyId),
Limit: aws.Int64(int64(100)),
Marker: marker,
}
var out *kms.ListGrantsResponse
var err error
var grant *kms.GrantListEntry
err = resource.Retry(3*time.Minute, func() *resource.RetryError {
out, err = conn.ListGrants(&input)
if err != nil {
if isAWSErr(err, kms.ErrCodeDependencyTimeoutException, "") ||
isAWSErr(err, kms.ErrCodeInternalException, "") ||
isAWSErr(err, kms.ErrCodeInvalidArnException, "") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error listing KMS Grants: %s", err)
}
grant = getKmsGrantById(out.Grants, grantId)
if grant != nil {
return grant, nil
}
if aws.BoolValue(out.Truncated) {
log.Printf("[DEBUG] KMS Grant list truncated, getting next page via marker: %s", aws.StringValue(out.NextMarker))
return findKmsGrantById(conn, keyId, grantId, out.NextMarker)
}
return nil, NewKmsGrantMissingError(fmt.Sprintf("[DEBUG] Grant %s not found for key id: %s", grantId, keyId))
}
// Can't have both constraint options set:
// ValidationException: More than one constraint supplied
// NB: set.List() returns an empty map if the constraint is not set, filter those out
// using len(v) > 0
func kmsGrantConstraintsIsValid(constraints *schema.Set) bool {
constraintCount := 0
for _, raw := range constraints.List() {
data := raw.(map[string]interface{})
if v, ok := data["encryption_context_equals"].(map[string]interface{}); ok {
if len(v) > 0 {
constraintCount += 1
}
}
if v, ok := data["encryption_context_subset"].(map[string]interface{}); ok {
if len(v) > 0 {
constraintCount += 1
}
}
}
return constraintCount <= 1
}
func expandKmsGrantConstraints(configured *schema.Set) *kms.GrantConstraints {
if len(configured.List()) < 1 {
return nil
}
var constraint kms.GrantConstraints
for _, raw := range configured.List() {
data := raw.(map[string]interface{})
if contextEq, ok := data["encryption_context_equals"]; ok {
constraint.SetEncryptionContextEquals(stringMapToPointers(contextEq.(map[string]interface{})))
}
if contextSub, ok := data["encryption_context_subset"]; ok {
constraint.SetEncryptionContextSubset(stringMapToPointers(contextSub.(map[string]interface{})))
}
}
return &constraint
}
func sortStringMapKeys(m map[string]*string) []string {
keys := make([]string, 0)
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
// NB: For the constraint hash to be deterministic the order in which
// print the keys and values of the encryption context maps needs to be
// determistic, so sort them.
func sortedConcatStringMap(m map[string]*string) string {
var strList []string
mapKeys := sortStringMapKeys(m)
for _, key := range mapKeys {
strList = append(strList, key, *m[key])
}
return strings.Join(strList, "-")
}
// The hash needs to encapsulate what type of constraint it is
// as well as the keys and values of the constraint.
func resourceKmsGrantConstraintsHash(v interface{}) int {
var buf bytes.Buffer
m, castOk := v.(map[string]interface{})
if !castOk {
return 0
}
if v, ok := m["encryption_context_equals"]; ok {
if len(v.(map[string]interface{})) > 0 {
buf.WriteString(fmt.Sprintf("encryption_context_equals-%s-", sortedConcatStringMap(stringMapToPointers(v.(map[string]interface{})))))
}
}
if v, ok := m["encryption_context_subset"]; ok {
if len(v.(map[string]interface{})) > 0 {
buf.WriteString(fmt.Sprintf("encryption_context_subset-%s-", sortedConcatStringMap(stringMapToPointers(v.(map[string]interface{})))))
}
}
return hashcode.String(buf.String())
}
func flattenKmsGrantConstraints(constraint *kms.GrantConstraints) *schema.Set {
constraints := schema.NewSet(resourceKmsGrantConstraintsHash, []interface{}{})
if constraint == nil {
return constraints
}
m := make(map[string]interface{})
if constraint.EncryptionContextEquals != nil {
if len(constraint.EncryptionContextEquals) > 0 {
m["encryption_context_equals"] = pointersMapToStringList(constraint.EncryptionContextEquals)
}
}
if constraint.EncryptionContextSubset != nil {
if len(constraint.EncryptionContextSubset) > 0 {
m["encryption_context_subset"] = pointersMapToStringList(constraint.EncryptionContextSubset)
}
}
constraints.Add(m)
return constraints
}
func decodeKmsGrantId(id string) (string, string, error) {
if strings.HasPrefix(id, "arn:aws") {
arn_parts := strings.Split(id, "/")
if len(arn_parts) != 2 {
return "", "", fmt.Errorf("unexpected format of ARN (%q), expected KeyID:GrantID", id)
}
arn_prefix := arn_parts[0]
parts := strings.Split(arn_parts[1], ":")
if len(parts) != 2 {
return "", "", fmt.Errorf("unexpected format of ID (%q), expected KeyID:GrantID", id)
}
return fmt.Sprintf("%s/%s", arn_prefix, parts[0]), parts[1], nil
} else {
parts := strings.Split(id, ":")
if len(parts) != 2 {
return "", "", fmt.Errorf("unexpected format of ID (%q), expected KeyID:GrantID", id)
}
return parts[0], parts[1], nil
}
}
// Custom error, so we don't have to rely on
// the content of an error message
type KmsGrantMissingError string
func (e KmsGrantMissingError) Error() string {
return e.Error()
}
func NewKmsGrantMissingError(msg string) KmsGrantMissingError {
return KmsGrantMissingError(msg)
}