forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kms.go
56 lines (46 loc) · 1.28 KB
/
kms.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
package aws
import (
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
)
// GetCmkArn gets the ARN of a KMS Customer Master Key (CMK) in the given region with the given ID. The ID can be an alias, such
// as "alias/my-cmk".
func GetCmkArn(t *testing.T, region string, cmkID string) string {
out, err := GetCmkArnE(t, region, cmkID)
if err != nil {
t.Fatal(err)
}
return out
}
// GetCmkArnE gets the ARN of a KMS Customer Master Key (CMK) in the given region with the given ID. The ID can be an alias, such
// as "alias/my-cmk".
func GetCmkArnE(t *testing.T, region string, cmkID string) (string, error) {
kmsClient, err := NewKmsClientE(t, region)
if err != nil {
return "", err
}
result, err := kmsClient.DescribeKey(&kms.DescribeKeyInput{
KeyId: aws.String(cmkID),
})
if err != nil {
return "", err
}
return *result.KeyMetadata.Arn, nil
}
// NewKmsClient creates a KMS client.
func NewKmsClient(t *testing.T, region string) *kms.KMS {
client, err := NewKmsClientE(t, region)
if err != nil {
t.Fatal(err)
}
return client
}
// NewKmsClientE creates a KMS client.
func NewKmsClientE(t *testing.T, region string) (*kms.KMS, error) {
sess, err := NewAuthenticatedSession(region)
if err != nil {
return nil, err
}
return kms.New(sess), nil
}