forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkms-key_test.go
67 lines (55 loc) · 1.36 KB
/
kms-key_test.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
//go:build integration
package resources
import (
"context"
"fmt"
"testing"
"github.com/gotidy/ptr"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/sts"
)
func Test_KMSKey_Remove(t *testing.T) {
cfg := aws.NewConfig()
cfg.Region = ptr.String("us-east-1")
sess := session.Must(session.NewSession(cfg))
svc := kms.New(sess)
stsSvc := sts.New(sess)
ident, err := stsSvc.GetCallerIdentity(&sts.GetCallerIdentityInput{})
assert.NoError(t, err)
createInput := &kms.CreateKeyInput{
Policy: aws.String(fmt.Sprintf(`{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::%s:root"
},
"Action": "kms:*",
"Resource": "*"
}
]
}`, *ident.Account)),
}
out, err := svc.CreateKey(createInput)
assert.NoError(t, err)
assert.NotNil(t, out)
kmsKey := KMSKey{
svc: svc,
ID: out.KeyMetadata.KeyId,
}
removeError := kmsKey.Remove(context.TODO())
assert.NoError(t, removeError)
_, err = svc.DescribeKey(&kms.DescribeKeyInput{
KeyId: kmsKey.ID,
})
var awsError awserr.Error
if errors.As(err, &awsError) {
assert.Equal(t, "NoSuchEntity", awsError.Code())
}
}