-
Notifications
You must be signed in to change notification settings - Fork 1
/
rkms.go
296 lines (243 loc) · 8.71 KB
/
rkms.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"context"
"encoding/base64"
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/kms/kmsiface"
logger "github.com/sirupsen/logrus"
)
// MinimumKMSRegions is the minimum number of KMS regions needed to run a RKMS service
const MinimumKMSRegions = 3
// MaxNumberOfGetPlaintextDataKeyTries is the number of attempts to get/create data key before quitting
const MaxNumberOfGetPlaintextDataKeyTries = 3
// RKMS - Implementation of reliable KMS logic
type RKMS struct {
regions []string
keyIds map[string]*string
clients map[string]kmsiface.KMSAPI
store Store
// the length of the data encryption key in bytes
dataKeySizeInBytes int64
}
// NewRKMSWithDynamoDB creates a new RKMS instance with DynamoDB used as its key/value store
func NewRKMSWithDynamoDB(kmsConfig KMSConfig, dynamoDBConfig DynamoDBConfig) (*RKMS, error) {
store, err := NewDynamoDBStore(dynamoDBConfig)
if err != nil {
logger.Error(err)
return nil, err
}
clients, err := getKMSClientsForRegions(kmsConfig.Regions)
if err != nil {
logger.Error(err)
return nil, err
}
return &RKMS{kmsConfig.Regions, kmsConfig.KeyIds, clients, store, kmsConfig.DataKeySizeInBytes}, nil
}
func getKMSClientsForRegions(regions []string) (map[string]kmsiface.KMSAPI, error) {
clients := make(map[string]kmsiface.KMSAPI)
for _, region := range regions {
client, err := getKMSClientForRegion(region)
if err != nil {
return nil, err
}
clients[region] = client
}
return clients, nil
}
func getKMSClientForRegion(region string) (kmsiface.KMSAPI, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
})
if err != nil {
return nil, err
}
return kms.New(sess), nil
}
// GetPlaintextDataKey retrieves the key assosicated with the given id.
// If a key is not found in the store, a key is generated for the given id.
func (r *RKMS) GetPlaintextDataKey(ctx context.Context, id string) (*string, error) {
return r.getPlaintextDataKey(ctx, id, MaxNumberOfGetPlaintextDataKeyTries, nil)
}
func (r *RKMS) getPlaintextDataKey(ctx context.Context, id string, triesLeft int, lastErr error) (*string, error) {
if triesLeft == 0 {
return nil, lastErr
}
plaintextDataKey, err := r.lookInStoreForDataKey(ctx, id)
if err != nil {
logger.Error(err)
return nil, err
}
if plaintextDataKey != nil {
logger.Debugln("a data key was found in the store for the given id")
return plaintextDataKey, nil
}
plaintextDataKey, err = r.createDataKeyForID(ctx, id)
if err != nil {
if _, ok := err.(IDAlreadyExistsStoreError); ok {
//retry the whole process which will retry fetching data from store
return r.getPlaintextDataKey(ctx, id, triesLeft-1, err)
}
logger.Error(err)
return nil, err
}
//return the data key
return plaintextDataKey, nil
}
func (r *RKMS) lookInStoreForDataKey(ctx context.Context, id string) (*string, error) {
encryptedDataKeys, err := r.store.GetEncryptedDataKeys(ctx, id)
if err != nil {
logger.Error(err)
return nil, err
}
if encryptedDataKeys == nil {
logger.Debugln("no data key exists in the store for the given id")
return nil, nil
}
plaintextDataKey, err := r.decryptDataKey(ctx, encryptedDataKeys)
if err != nil {
err := fmt.Errorf("failed to decrypt data key in every region: %s", err)
logger.Error(err)
return nil, err
}
return plaintextDataKey, err
}
type encryptDataKeyResult struct {
region string
ciphertext *string
err error
}
func (r *RKMS) createDataKeyForID(ctx context.Context, id string) (*string, error) {
logger.Debugln("creating data key...")
firstRegion, plaintextDataKey, firstRegionCiphertext, err := r.createDataKey(ctx)
if err != nil {
logger.Errorf("failed to create a data key: %s", err)
return nil, err
}
encryptedDataKeys := make(map[string]string)
encryptedDataKeys[*firstRegion] = *firstRegionCiphertext
resultsChannel := make(chan encryptDataKeyResult, len(r.regions)-1)
childCtx, cancel := context.WithCancel(ctx)
defer cancel()
logger.Debugln("encrypting generated data key in every region...")
for _, region := range r.regions {
if strings.Compare(region, *firstRegion) == 0 { //we have already encrypted in this region and have the ciphertext
continue
}
go func(ctx context.Context, resultsChannel chan<- encryptDataKeyResult, plaintextDataKey string, region string) {
logger.Debugf("encrypting data key in %s region", region)
ciphertext, err := r.encryptDataKey(ctx, plaintextDataKey, region)
resultsChannel <- encryptDataKeyResult{region, ciphertext, err}
}(childCtx, resultsChannel, *plaintextDataKey, region)
}
for i := 0; i < len(r.regions)-1; i++ {
select {
case result := <-resultsChannel:
if result.err != nil {
logger.Errorf("failed to encrypt data key in %s region: %s", result.region, result.err)
return nil, result.err
}
encryptedDataKeys[result.region] = *result.ciphertext
case <-ctx.Done():
return nil, fmt.Errorf("cancelled while encrypting data key in all regions")
}
}
logger.Debugln("saving encrypted data keys in store...")
err = r.store.SetEncryptedDataKeysConditionally(ctx, id, encryptedDataKeys)
if err != nil {
logger.Errorf("failed to save encrypted data keys in key/value store: %s", err)
return nil, err
}
logger.Debugln("done creating and saving encrypted data keys")
return plaintextDataKey, nil
}
func (r *RKMS) createDataKey(ctx context.Context) (*string, *string, *string, error) {
for _, region := range r.regions {
input := &kms.GenerateDataKeyInput{
KeyId: r.keyIds[region],
NumberOfBytes: aws.Int64(r.dataKeySizeInBytes),
}
result, err := r.clients[region].GenerateDataKeyWithContext(ctx, input)
if err != nil { //failed to create data key in this region
logger.Error(err)
continue
}
plaintext := base64.StdEncoding.EncodeToString(result.Plaintext)
ciphertext := base64.StdEncoding.EncodeToString(result.CiphertextBlob)
return ®ion, &plaintext, &ciphertext, nil
}
return nil, nil, nil, fmt.Errorf("failed to create a data key in every region")
}
func (r *RKMS) encryptDataKey(ctx context.Context, dataKey string, region string) (*string, error) {
plaintext, err := base64.StdEncoding.DecodeString(dataKey)
if err != nil {
logger.Error(err)
return nil, err
}
input := &kms.EncryptInput{
KeyId: r.keyIds[region],
Plaintext: plaintext,
}
result, err := r.clients[region].EncryptWithContext(ctx, input)
if err != nil { //failed to create data key in this region
logger.Error(err)
return nil, err
}
ciphertext := base64.StdEncoding.EncodeToString(result.CiphertextBlob)
return &ciphertext, nil
}
type decryptDataKeyResult struct {
region string
plaintext *string
err error
}
func (r *RKMS) decryptDataKey(ctx context.Context, encryptedDataKeys map[string]string) (*string, error) {
resultsChannel := make(chan decryptDataKeyResult, len(r.regions))
childCtx, cancel := context.WithCancel(ctx)
defer cancel()
//TODO(enhancement): add config param to run this serially if wanted
for _, region := range r.regions {
go func(ctx context.Context, resultsChannel chan<- decryptDataKeyResult, ciphertext string, region string) {
ciphertextBlob, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
//TODO(enhancement): fix it asyncrounously
logger.Errorf("ciphertext value is corrupted in the store for %s region: %s", region, err)
resultsChannel <- decryptDataKeyResult{region, nil, err}
return
}
input := &kms.DecryptInput{
CiphertextBlob: ciphertextBlob,
}
logger.Debugf("decrypting data key in %s region", region)
result, err := r.clients[region].DecryptWithContext(ctx, input)
if err != nil { //failed to decrypt in this region
if aerr, ok := err.(awserr.Error); ok && aerr.Code() != request.CanceledErrorCode {
logger.Errorf("failed to decrypt in %s region: %s", region, err)
}
resultsChannel <- decryptDataKeyResult{region, nil, err}
return
}
dataKey := base64.StdEncoding.EncodeToString(result.Plaintext)
resultsChannel <- decryptDataKeyResult{region, &dataKey, nil}
}(childCtx, resultsChannel, encryptedDataKeys[region], region)
}
for i := 0; i < len(r.regions); i++ {
select {
case result := <-resultsChannel:
if result.err != nil {
logger.Infof("failed to decrypt data key in %s region: %s", result.region, result.err)
continue
}
logger.Debugf("successfully decrypted data key in %s region", result.region)
return result.plaintext, nil
case <-ctx.Done():
return nil, fmt.Errorf("cancelled while decrypting data key in all regions")
}
}
return nil, fmt.Errorf("failed to decrypt data key in all regions")
}