/
kmsOperations.go
57 lines (50 loc) · 1.31 KB
/
kmsOperations.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
package aws
import (
"context"
"log/slog"
"time"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/aws/aws-sdk-go-v2/service/kms/types"
)
// KmsClient type for mocks
type KmsClient interface {
CreateKey(ctx context.Context, params *kms.CreateKeyInput, optFns ...func(*kms.Options)) (*kms.CreateKeyOutput, error)
}
// KmsOperations struct with the KmsClient
type KmsOperations struct {
Client KmsClient
}
// KmsConfig descripes our KmsConfig and they way it works
type KmsConfig struct {
Description *string
Multiregion *bool
Policy *string
}
// CreateKMSKey creates a new KMS key for multiregion deploys
func (k KmsOperations) CreateKMSKey(cfg *KmsConfig) (*types.KeyMetadata, error) {
input := &kms.CreateKeyInput{}
if cfg != nil {
input = handleKmsConfig(*cfg)
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
output, err := k.Client.CreateKey(ctx, input)
if err != nil {
slog.Warn("Error creating KMS key", "error", err)
return nil, err
}
return output.KeyMetadata, nil
}
func handleKmsConfig(k KmsConfig) *kms.CreateKeyInput {
input := kms.CreateKeyInput{}
if k.Description != nil {
input.Description = k.Description
}
if k.Multiregion != nil {
input.MultiRegion = k.Multiregion
}
if k.Policy != nil {
input.Policy = k.Policy
}
return &input
}