github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/aws/kms.go (about) 1 package aws 2 3 import ( 4 "github.com/aws/aws-sdk-go/aws" 5 "github.com/aws/aws-sdk-go/service/kms" 6 "github.com/gruntwork-io/terratest/modules/testing" 7 ) 8 9 // 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 10 // as "alias/my-cmk". 11 func GetCmkArn(t testing.TestingT, region string, cmkID string) string { 12 out, err := GetCmkArnE(t, region, cmkID) 13 if err != nil { 14 t.Fatal(err) 15 } 16 return out 17 } 18 19 // 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 20 // as "alias/my-cmk". 21 func GetCmkArnE(t testing.TestingT, region string, cmkID string) (string, error) { 22 kmsClient, err := NewKmsClientE(t, region) 23 if err != nil { 24 return "", err 25 } 26 27 result, err := kmsClient.DescribeKey(&kms.DescribeKeyInput{ 28 KeyId: aws.String(cmkID), 29 }) 30 31 if err != nil { 32 return "", err 33 } 34 35 return *result.KeyMetadata.Arn, nil 36 } 37 38 // NewKmsClient creates a KMS client. 39 func NewKmsClient(t testing.TestingT, region string) *kms.KMS { 40 client, err := NewKmsClientE(t, region) 41 if err != nil { 42 t.Fatal(err) 43 } 44 return client 45 } 46 47 // NewKmsClientE creates a KMS client. 48 func NewKmsClientE(t testing.TestingT, region string) (*kms.KMS, error) { 49 sess, err := NewAuthenticatedSession(region) 50 if err != nil { 51 return nil, err 52 } 53 54 return kms.New(sess), nil 55 }