forked from evalphobia/aws-sdk-go-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
237 lines (200 loc) · 6.26 KB
/
client.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
package kms
import (
"encoding/base64"
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws/awserr"
SDK "github.com/aws/aws-sdk-go/service/kms"
"github.com/evalphobia/aws-sdk-go-wrapper/config"
"github.com/evalphobia/aws-sdk-go-wrapper/log"
"github.com/evalphobia/aws-sdk-go-wrapper/private/pointers"
)
const (
serviceName = "KMS"
aliasPrefix = "alias/"
)
// KMS has KMS client.
type KMS struct {
client *SDK.KMS
logger log.Logger
}
// New returns initialized *Rekognition.
func New(conf config.Config) (*KMS, error) {
sess, err := conf.Session()
if err != nil {
return nil, err
}
svc := &KMS{
client: SDK.New(sess),
logger: log.DefaultLogger,
}
return svc, nil
}
// SetLogger sets logger.
func (svc *KMS) SetLogger(logger log.Logger) {
svc.logger = logger
}
// CreateAlias executes CreateAlias operation.
func (svc *KMS) CreateAlias(keyID, aliasName string) error {
if !strings.HasPrefix(aliasName, aliasPrefix) {
aliasName = aliasPrefix + aliasName
}
_, err := svc.client.CreateAlias(&SDK.CreateAliasInput{
TargetKeyId: pointers.String(keyID),
AliasName: pointers.String(aliasName),
})
if err != nil {
svc.Errorf("error on `CreateAlias` operation; keyID=%s; aliasName=%s; error=%s;", keyID, aliasName, err.Error())
return err
}
svc.Infof("success on `CreateAlias` operation; keyID=%s; aliasName=%s;", keyID, aliasName)
return nil
}
// CreateKey executes CreateKey operation.
func (svc *KMS) CreateKey(tags ...Tag) (*SDK.KeyMetadata, error) {
var kmsTags []*SDK.Tag
for _, tag := range tags {
kmsTags = append(kmsTags, tag.Tag())
}
output, err := svc.client.CreateKey(&SDK.CreateKeyInput{
Tags: kmsTags,
})
if err != nil {
svc.Errorf("error on `CreateKey` operation; error=%s;", err.Error())
return nil, err
}
metaData := output.KeyMetadata
svc.Infof("success on `CreateKey` operation; keyID=%s; arn=%s;", *metaData.KeyId, *metaData.Arn)
return metaData, nil
}
// CreateKeyWithAlias creates a key and sets alias name.
func (svc *KMS) CreateKeyWithAlias(aliasName string, tags ...Tag) (*SDK.KeyMetadata, error) {
if !strings.HasPrefix(aliasName, aliasPrefix) {
aliasName = aliasPrefix + aliasName
}
_, err := svc.DescribeKey(aliasName)
if err == nil {
return nil, fmt.Errorf("error: aliasName=[%s] is already exists", aliasName)
}
aerr, ok := err.(awserr.Error)
if !ok {
return nil, err
}
switch aerr.Code() {
case SDK.ErrCodeNotFoundException:
// error must be NotFoundException.
default:
return nil, err
}
metaData, err := svc.CreateKey(tags...)
if err != nil {
return nil, err
}
err = svc.CreateAlias(*metaData.KeyId, aliasName)
return metaData, err
}
// DescribeKey executes DescribeKey operation.
func (svc *KMS) DescribeKey(keyName string) (metaData *SDK.KeyMetadata, err error) {
output, err := svc.client.DescribeKey(&SDK.DescribeKeyInput{
KeyId: pointers.String(keyName),
})
if err != nil {
svc.Errorf("error on `DescribeKey` operation; keyName=%s; error=%s;", keyName, err.Error())
return nil, err
}
return output.KeyMetadata, nil
}
// DeleteKey executes ScheduleKeyDeletion operation from Key name(key id, arn or alias).
func (svc *KMS) DeleteKey(keyName string, day int) error {
metaData, err := svc.DescribeKey(keyName)
if err != nil {
return err
}
return svc.ScheduleKeyDeletion(*metaData.KeyId, day)
}
// ScheduleKeyDeletion executes ScheduleKeyDeletion operation.
func (svc *KMS) ScheduleKeyDeletion(keyID string, day int) error {
_, err := svc.client.ScheduleKeyDeletion(&SDK.ScheduleKeyDeletionInput{
KeyId: pointers.String(keyID),
PendingWindowInDays: pointers.Long(day),
})
if err != nil {
svc.Errorf("error on `ScheduleKeyDeletion` operation; keyID=%s; error=%s;", keyID, err.Error())
}
return err
}
// Encrypt executes Encrypt operation.
func (svc *KMS) Encrypt(keyName string, plainData []byte) (encryptedData []byte, err error) {
output, err := svc.client.Encrypt(&SDK.EncryptInput{
KeyId: pointers.String(keyName),
Plaintext: plainData,
})
if err != nil {
svc.Errorf("error on `Encrypt` operation; keyName=%s; error=%s;", keyName, err.Error())
return nil, err
}
return output.CiphertextBlob, nil
}
// EncryptString executes Encrypt operation with base64 string.
func (svc *KMS) EncryptString(keyName, plainText string) (base64Text string, err error) {
encryptedData, err := svc.Encrypt(keyName, []byte(plainText))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(encryptedData), nil
}
// Decrypt executes Decrypt operation.
func (svc *KMS) Decrypt(encryptedData []byte) (plainData []byte, err error) {
output, err := svc.client.Decrypt(&SDK.DecryptInput{
CiphertextBlob: encryptedData,
})
if err != nil {
svc.Errorf("error on `Decrypt` operation; error=%s;", err.Error())
return nil, err
}
return output.Plaintext, nil
}
// DecryptString executes Decrypt operation with base64 string.
func (svc *KMS) DecryptString(base64Text string) (plainText string, err error) {
byt, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return "", err
}
plainData, err := svc.Decrypt(byt)
if err != nil {
return "", err
}
return string(plainData), nil
}
// ReEncrypt executes ReEncrypt operation.
func (svc *KMS) ReEncrypt(destinationKey string, encryptedData []byte) (resultEncryptedData []byte, err error) {
output, err := svc.client.ReEncrypt(&SDK.ReEncryptInput{
DestinationKeyId: pointers.String(destinationKey),
CiphertextBlob: encryptedData,
})
if err != nil {
svc.Errorf("error on `ReEncrypt` operation; destinationKey=%s; error=%s;", destinationKey, err.Error())
return nil, err
}
return output.CiphertextBlob, nil
}
// ReEncryptString executes ReEncrypt operation with base64 string.
func (svc *KMS) ReEncryptString(destinationKey, base64Text string) (resultBase64Text string, err error) {
byt, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return "", err
}
encryptedData, err := svc.ReEncrypt(destinationKey, byt)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(encryptedData), nil
}
// Infof logging information.
func (svc *KMS) Infof(format string, v ...interface{}) {
svc.logger.Infof(serviceName, format, v...)
}
// Errorf logging error information.
func (svc *KMS) Errorf(format string, v ...interface{}) {
svc.logger.Errorf(serviceName, format, v...)
}