-
Notifications
You must be signed in to change notification settings - Fork 14
/
storage.go
364 lines (305 loc) · 9.23 KB
/
storage.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package dynamodbstorage
import (
"context"
"encoding/base64"
"errors"
"fmt"
"io/fs"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
caddy "github.com/caddyserver/caddy/v2"
"github.com/caddyserver/certmagic"
)
const (
contentsAttribute = "Contents"
primaryKeyAttribute = "PrimaryKey"
lastUpdatedAttribute = "LastUpdated"
lockTimeoutMinutes = caddy.Duration(5 * time.Minute)
lockPollingInterval = caddy.Duration(5 * time.Second)
)
// Item holds structure of domain, certificate data,
// and last updated for marshaling with DynamoDb
type Item struct {
PrimaryKey string `json:"PrimaryKey"`
Contents string `json:"Contents"`
LastUpdated time.Time `json:"LastUpdated"`
}
// Storage implements certmagic.Storage to facilitate
// storage of certificates in DynamoDB for a clustered environment.
// Also implements certmagic.Locker to facilitate locking
// and unlocking of cert data during storage
type Storage struct {
// Table - [required] DynamoDB table name
Table string `json:"table,omitempty"`
AwsSession *session.Session `json:"-"`
// AwsEndpoint - [optional] provide an override for DynamoDB service.
// By default it'll use the standard production DynamoDB endpoints.
// Useful for testing with a local DynamoDB instance.
AwsEndpoint string `json:"aws_endpoint,omitempty"`
// AwsRegion - [optional] region using DynamoDB in.
// Useful for testing with a local DynamoDB instance.
AwsRegion string `json:"aws_region,omitempty"`
// AwsDisableSSL - [optional] disable SSL for DynamoDB connections. Default: false
// Only useful for local testing, do not use outside of local testing.
AwsDisableSSL bool `json:"aws_disable_ssl,omitempty"`
// LockTimeout - [optional] how long to wait for a lock to be created. Default: 5 minutes
LockTimeout caddy.Duration `json:"lock_timeout,omitempty"`
// LockPollingInterval - [optional] how often to check for lock released. Default: 5 seconds
LockPollingInterval caddy.Duration `json:"lock_polling_interval,omitempty"`
}
// initConfig initializes configuration for table name and AWS session
func (s *Storage) initConfig() error {
if s.Table == "" {
return errors.New("config error: table name is required")
}
if s.LockTimeout == 0 {
s.LockTimeout = lockTimeoutMinutes
}
if s.LockPollingInterval == 0 {
s.LockPollingInterval = lockPollingInterval
}
// Initialize AWS Session if needed
if s.AwsSession == nil {
var err error
s.AwsSession, err = session.NewSession(&aws.Config{
Endpoint: &s.AwsEndpoint,
Region: &s.AwsRegion,
DisableSSL: &s.AwsDisableSSL,
})
if err != nil {
return err
}
}
return nil
}
// Store puts value at key.
func (s *Storage) Store(_ context.Context, key string, value []byte) error {
if err := s.initConfig(); err != nil {
return err
}
encVal := base64.StdEncoding.EncodeToString(value)
if key == "" {
return errors.New("key must not be empty")
}
svc := dynamodb.New(s.AwsSession)
input := &dynamodb.PutItemInput{
Item: map[string]*dynamodb.AttributeValue{
primaryKeyAttribute: {
S: aws.String(key),
},
contentsAttribute: {
S: aws.String(encVal),
},
lastUpdatedAttribute: {
S: aws.String(time.Now().Format(time.RFC3339)),
},
},
TableName: aws.String(s.Table),
}
_, err := svc.PutItem(input)
return err
}
// Load retrieves the value at key.
func (s *Storage) Load(_ context.Context, key string) ([]byte, error) {
if err := s.initConfig(); err != nil {
return []byte{}, err
}
if key == "" {
return []byte{}, errors.New("key must not be empty")
}
domainItem, err := s.getItem(key)
return []byte(domainItem.Contents), err
}
// Delete deletes key.
func (s *Storage) Delete(_ context.Context, key string) error {
if err := s.initConfig(); err != nil {
return err
}
if key == "" {
return errors.New("key must not be empty")
}
svc := dynamodb.New(s.AwsSession)
input := &dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
primaryKeyAttribute: {
S: aws.String(key),
},
},
TableName: aws.String(s.Table),
}
_, err := svc.DeleteItem(input)
if err != nil {
return err
}
return nil
}
// Exists returns true if the key exists
// and there was no error checking.
func (s *Storage) Exists(ctx context.Context, key string) bool {
cert, err := s.Load(ctx, key)
if string(cert) != "" && err == nil {
return true
}
return false
}
// List returns all keys that match prefix.
// If recursive is true, non-terminal keys
// will be enumerated (i.e. "directories"
// should be walked); otherwise, only keys
// prefixed exactly by prefix will be listed.
func (s *Storage) List(_ context.Context, prefix string, recursive bool) ([]string, error) {
if err := s.initConfig(); err != nil {
return []string{}, err
}
if prefix == "" {
return []string{}, errors.New("key prefix must not be empty")
}
svc := dynamodb.New(s.AwsSession)
input := &dynamodb.ScanInput{
ExpressionAttributeNames: map[string]*string{
"#D": aws.String(primaryKeyAttribute),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":p": {
S: aws.String(prefix),
},
},
FilterExpression: aws.String("begins_with(#D, :p)"),
TableName: aws.String(s.Table),
ConsistentRead: aws.Bool(true),
}
var matchingKeys []string
pageNum := 0
err := svc.ScanPages(input,
func(page *dynamodb.ScanOutput, lastPage bool) bool {
pageNum++
var items []Item
err := dynamodbattribute.UnmarshalListOfMaps(page.Items, &items)
if err != nil {
log.Printf("error unmarshaling page of items: %s", err.Error())
return false
}
for _, i := range items {
matchingKeys = append(matchingKeys, i.PrimaryKey)
}
return !lastPage
})
if err != nil {
return []string{}, err
}
return matchingKeys, nil
}
// Stat returns information about key.
func (s *Storage) Stat(_ context.Context, key string) (certmagic.KeyInfo, error) {
domainItem, err := s.getItem(key)
if err != nil {
return certmagic.KeyInfo{}, err
}
return certmagic.KeyInfo{
Key: key,
Modified: domainItem.LastUpdated,
Size: int64(len(domainItem.Contents)),
IsTerminal: true,
}, nil
}
// Lock acquires the lock for key, blocking until the lock
// can be obtained or an error is returned. Note that, even
// after acquiring a lock, an idempotent operation may have
// already been performed by another process that acquired
// the lock before - so always check to make sure idempotent
// operations still need to be performed after acquiring the
// lock.
//
// The actual implementation of obtaining of a lock must be
// an atomic operation so that multiple Lock calls at the
// same time always results in only one caller receiving the
// lock at any given time.
//
// To prevent deadlocks, all implementations (where this concern
// is relevant) should put a reasonable expiration on the lock in
// case Unlock is unable to be called due to some sort of network
// failure or system crash.
func (s *Storage) Lock(ctx context.Context, key string) error {
if err := s.initConfig(); err != nil {
return err
}
lockKey := fmt.Sprintf("LOCK-%s", key)
// Check for existing lock
for {
existing, err := s.getItem(lockKey)
isErrNotExists := errors.Is(err, fs.ErrNotExist)
if err != nil && !isErrNotExists {
return err
}
// if lock doesn't exist or is empty, break to create a new one
if isErrNotExists || existing.Contents == "" {
break
}
// Lock exists, check if expired or sleep 5 seconds and check again
expires, err := time.Parse(time.RFC3339, existing.Contents)
if err != nil {
return err
}
if time.Now().After(expires) {
if err := s.Unlock(ctx, key); err != nil {
return err
}
break
}
select {
case <-time.After(time.Duration(s.LockPollingInterval)):
case <-ctx.Done():
return ctx.Err()
}
}
// lock doesn't exist, create it
contents := []byte(time.Now().Add(time.Duration(s.LockTimeout)).Format(time.RFC3339))
return s.Store(ctx, lockKey, contents)
}
// Unlock releases the lock for key. This method must ONLY be
// called after a successful call to Lock, and only after the
// critical section is finished, even if it errored or timed
// out. Unlock cleans up any resources allocated during Lock.
func (s *Storage) Unlock(ctx context.Context, key string) error {
if err := s.initConfig(); err != nil {
return err
}
lockKey := fmt.Sprintf("LOCK-%s", key)
return s.Delete(ctx, lockKey)
}
func (s *Storage) getItem(key string) (Item, error) {
svc := dynamodb.New(s.AwsSession)
input := &dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
primaryKeyAttribute: {
S: aws.String(key),
},
},
TableName: aws.String(s.Table),
ConsistentRead: aws.Bool(true),
}
result, err := svc.GetItem(input)
if err != nil {
return Item{}, err
}
var domainItem Item
err = dynamodbattribute.UnmarshalMap(result.Item, &domainItem)
if err != nil {
return Item{}, err
}
if domainItem.Contents == "" {
return Item{}, fs.ErrNotExist
}
dec, err := base64.StdEncoding.DecodeString(domainItem.Contents)
if err != nil {
return Item{}, err
}
domainItem.Contents = string(dec)
return domainItem, nil
}
// Interface guard
var _ certmagic.Storage = (*Storage)(nil)