forked from gamalan/caddy-tlsredis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorageredis.go
480 lines (389 loc) · 12.6 KB
/
storageredis.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package storageredis
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/fs"
"path"
"runtime"
"strings"
"sync"
"time"
"go.uber.org/zap"
"github.com/bsm/redislock"
"github.com/caddyserver/certmagic"
"github.com/go-redis/redis/v8"
)
const (
// LockDuration is lock time duration
LockDuration = 10 * time.Second
// LockFreshnessInterval is how often to update a lock's TTL. Locks with a TTL
// more than this duration in the past (plus a grace period for latency) can be
// considered stale.
LockFreshnessInterval = 3 * time.Second
// LockPollInterval is how frequently to check the existence of a lock
LockPollInterval = 1 * time.Second
// Maximum size for the stack trace when recovering from panics.
stackTraceBufferSize = 1024 * 128
// ScanCount is how many scan command might return
ScanCount int64 = 100
// Default Values
// DefaultAESKey needs to be 32 bytes long
DefaultAESKey = ""
// DefaultKeyPrefix defines the default prefix in KV store
DefaultKeyPrefix = "caddytls"
// DefaultValuePrefix sets a prefix to KV values to check validation
DefaultValuePrefix = "caddy-storage-redis"
// DefaultRedisHost define the Redis instance host
DefaultRedisHost = "127.0.0.1"
// DefaultRedisPort define the Redis instance port
DefaultRedisPort = "6379"
// DefaultRedisDB define the Redis DB number
DefaultRedisDB = 0
// DefaultRedisPassword define the Redis instance Username, if any
DefaultRedisUsername = ""
// DefaultRedisPassword define the Redis instance password, if any
DefaultRedisPassword = ""
// DefaultRedisTimeout define the Redis wait time in (s)
DefaultRedisTimeout = 5
// DefaultRedisTLS define the Redis TLS connection
DefaultRedisTLS = false
// DefaultRedisTLSInsecure define the Redis TLS connection
DefaultRedisTLSInsecure = true
// Environment Name
// EnvNameRedisHost defines the env variable name to override Redis host
EnvNameRedisHost = "CADDY_CLUSTERING_REDIS_HOST"
// EnvNameRedisPort defines the env variable name to override Redis port
EnvNameRedisPort = "CADDY_CLUSTERING_REDIS_PORT"
// EnvNameRedisDB defines the env variable name to override Redis db number
EnvNameRedisDB = "CADDY_CLUSTERING_REDIS_DB"
// EnvNameRedisUsername defines the env variable name to override Redis username
EnvNameRedisUsername = "CADDY_CLUSTERING_REDIS_USERNAME"
// EnvNameRedisPassword defines the env variable name to override Redis password
EnvNameRedisPassword = "CADDY_CLUSTERING_REDIS_PASSWORD"
// EnvNameRedisTimeout defines the env variable name to override Redis wait timeout for dial, read, write
EnvNameRedisTimeout = "CADDY_CLUSTERING_REDIS_TIMEOUT"
// EnvNameAESKey defines the env variable name to override AES key
EnvNameAESKey = "CADDY_CLUSTERING_REDIS_AESKEY"
// EnvNameKeyPrefix defines the env variable name to override KV key prefix
EnvNameKeyPrefix = "CADDY_CLUSTERING_REDIS_KEYPREFIX"
// EnvNameValuePrefix defines the env variable name to override KV value prefix
EnvNameValuePrefix = "CADDY_CLUSTERING_REDIS_VALUEPREFIX"
// EnvNameTLSEnabled defines the env variable name to whether enable Redis TLS Connection or not
EnvNameTLSEnabled = "CADDY_CLUSTERING_REDIS_TLS"
// EnvNameTLSInsecure defines the env variable name to whether verify Redis TLS Connection or not
EnvNameTLSInsecure = "CADDY_CLUSTERING_REDIS_TLS_INSECURE"
)
// RedisStorage contain Redis client, and plugin option
type RedisStorage struct {
Client *redis.Client
ClientLocker *redislock.Client
Logger *zap.SugaredLogger
ctx context.Context
Address string `json:"address"`
Host string `json:"host"`
Port string `json:"port"`
DB int `json:"db"`
Username string `json:"username"`
Password string `json:"password"`
Timeout int `json:"timeout"`
KeyPrefix string `json:"key_prefix"`
ValuePrefix string `json:"value_prefix"`
AesKey string `json:"aes_key"`
TlsEnabled bool `json:"tls_enabled"`
TlsInsecure bool `json:"tls_insecure"`
locks *sync.Map
}
// StorageData describe the data that is stored in KV storage
type StorageData struct {
Value []byte `json:"value"`
Modified time.Time `json:"modified"`
}
// CertMagicStorage converts s to a certmagic.Storage instance.
func (rd *RedisStorage) CertMagicStorage() (certmagic.Storage, error) {
return rd, nil
}
// helper function to prefix key
func (rd *RedisStorage) prefixKey(key string) string {
return path.Join(rd.KeyPrefix, key)
}
// GetRedisStorage build RedisStorage with it's client
func (rd *RedisStorage) BuildRedisClient() error {
rd.ctx = context.Background()
redisClient := redis.NewClient(&redis.Options{
Addr: rd.Address,
Username: rd.Username,
Password: rd.Password,
DB: rd.DB,
DialTimeout: time.Second * time.Duration(rd.Timeout),
ReadTimeout: time.Second * time.Duration(rd.Timeout),
WriteTimeout: time.Second * time.Duration(rd.Timeout),
})
if rd.TlsEnabled {
redisClient.Options().TLSConfig = &tls.Config{
InsecureSkipVerify: rd.TlsInsecure,
}
}
_, err := redisClient.Ping(rd.ctx).Result()
if err != nil {
return err
}
rd.Client = redisClient
rd.ClientLocker = redislock.New(rd.Client)
rd.locks = &sync.Map{}
return nil
}
// Store values at key
func (rd RedisStorage) Store(ctx context.Context, key string, value []byte) error {
data := &StorageData{
Value: value,
Modified: time.Now(),
}
encryptedValue, err := rd.EncryptStorageData(data)
if err != nil {
return fmt.Errorf("unable to encode data for %v: %v", key, err)
}
if err := rd.Client.Set(rd.ctx, rd.prefixKey(key), encryptedValue, 0).Err(); err != nil {
return fmt.Errorf("unable to store data for %v: %v", key, err)
}
return nil
}
// Load retrieves the value at key.
func (rd RedisStorage) Load(ctx context.Context, key string) ([]byte, error) {
data, err := rd.getDataDecrypted(key)
if err != nil {
return nil, err
}
return data.Value, nil
}
// Delete deletes key.
func (rd RedisStorage) Delete(ctx context.Context, key string) error {
_, err := rd.getData(key)
if err != nil {
return err
}
if err := rd.Client.Del(rd.ctx, rd.prefixKey(key)).Err(); err != nil {
return fmt.Errorf("unable to delete data for key %s: %v", key, err)
}
return nil
}
// Exists returns true if the key exists
func (rd RedisStorage) Exists(ctx context.Context, key string) bool {
_, err := rd.getData(key)
if err == nil {
return true
}
return false
}
// List returns all keys that match prefix.
func (rd RedisStorage) List(ctx context.Context, prefix string, recursive bool) ([]string, error) {
var keysFound []string
var tempKeys []string
var firstPointer uint64 = 0
var pointer uint64 = 0
var search string
// assuming we want to list all keys
if prefix == "*" {
search = rd.prefixKey(prefix)
} else if len(strings.TrimSpace(prefix)) == 0 {
search = rd.prefixKey("*")
} else {
search = rd.prefixKey(prefix) + "*"
}
// first SCAN command
keys, pointer, err := rd.Client.Scan(rd.ctx, pointer, search, ScanCount).Result()
if err != nil {
return keysFound, err
}
// store it temporarily
tempKeys = append(tempKeys, keys...)
// because SCAN command doesn't always return all possible, keep searching until pointer is equal to the firstPointer
for pointer != firstPointer {
keys, nextPointer, _ := rd.Client.Scan(rd.ctx, pointer, search, ScanCount).Result()
tempKeys = append(tempKeys, keys...)
pointer = nextPointer
}
if prefix == "*" || len(strings.TrimSpace(prefix)) == 0 {
search = rd.KeyPrefix
} else {
search = rd.prefixKey(prefix)
}
// remove default prefix from keys
for _, key := range tempKeys {
if strings.HasPrefix(key, search) {
key = strings.TrimPrefix(key, rd.KeyPrefix+"/")
keysFound = append(keysFound, key)
}
}
// if recursive wanted, or wildcard/empty prefix, just return all keys prefix is empty
if recursive || prefix == "*" || len(strings.TrimSpace(prefix)) == 0 {
return keysFound, nil
}
// for non-recursive split path and look for unique keys just under given prefix
keysMap := make(map[string]bool)
for _, key := range keysFound {
dir := strings.Split(strings.TrimPrefix(key, prefix+"/"), "/")
keysMap[dir[0]] = true
}
keysFound = make([]string, 0)
for key := range keysMap {
keysFound = append(keysFound, path.Join(prefix, key))
}
return keysFound, nil
}
// Stat returns information about key.
func (rd RedisStorage) Stat(ctx context.Context, key string) (certmagic.KeyInfo, error) {
data, err := rd.getDataDecrypted(key)
if err != nil {
return certmagic.KeyInfo{}, err
}
return certmagic.KeyInfo{
Key: key,
Modified: data.Modified,
Size: int64(len(data.Value)),
IsTerminal: false,
}, nil
}
// getData return data from redis by key as it is
func (rd RedisStorage) getData(key string) ([]byte, error) {
data, err := rd.Client.Get(rd.ctx, rd.prefixKey(key)).Bytes()
if err != nil {
return nil, fmt.Errorf("unable to obtain data for %s: %v", key, err)
} else if data == nil {
return nil, fs.ErrNotExist
}
return data, nil
}
// getDataDecrypted return StorageData by key
func (rd RedisStorage) getDataDecrypted(key string) (*StorageData, error) {
data, err := rd.getData(key)
if err != nil {
return nil, err
}
decryptedData, err := rd.DecryptStorageData(data)
if err != nil {
return nil, fmt.Errorf("unable to decrypt data for %s: %v", key, err)
}
return decryptedData, nil
}
// Lock is to lock value
func (rd *RedisStorage) Lock(ctx context.Context, key string) error {
for {
_, err := rd.obtainLock(key)
if err == nil {
// got the lock, yay
return nil
}
if err != redislock.ErrNotObtained {
// unexpected error
return fmt.Errorf("creating redis lock: %v", err)
}
// lock exists and is not stale;
// just wait a moment and try again,
// or return if context cancelled
select {
case <-time.After(LockPollInterval):
case <-ctx.Done():
return ctx.Err()
}
}
return nil
}
func (rd *RedisStorage) obtainLock(key string) (*redislock.Lock, error) {
lockName := rd.prefixKey(key) + ".lock"
if lockI, exists := rd.locks.Load(key); exists {
// check if the lock is stale and cleanup if needed
if lock, ok := lockI.(*redislock.Lock); ok {
if ttl, err := lock.TTL(rd.ctx); err != nil {
return nil, err
} else if ttl == 0 {
// lock is dead, clean it up from locks data
_ = lock.Release(rd.ctx)
rd.locks.Delete(key)
}
}
// lock already exists, unable to obtain
return nil, redislock.ErrNotObtained
} else {
// obtain new lock
lock, err := rd.ClientLocker.Obtain(rd.ctx, lockName, LockDuration, &redislock.Options{})
if err != nil {
return nil, err
}
// save it
rd.locks.Store(key, lock)
// keep the lock fresh as long as we hold it
go rd.keepRedisLockFresh(key)
return lock, nil
}
}
// keepRedisLockFresh continuously updates the lock TTL. It stops when
// the lock disappears from rd.locks. Since it pools every
// LockFreshnessInterval, this function might not terminate until up to
// LockFreshnessInterval after the lock is released.
func (rd *RedisStorage) keepRedisLockFresh(key string) {
defer func() {
if err := recover(); err != nil {
buf := make([]byte, stackTraceBufferSize)
buf = buf[:runtime.Stack(buf, false)]
rd.Logger.Errorf("panic: active locking: %v\n%s", err, buf)
}
}()
for {
time.Sleep(LockFreshnessInterval)
done, err := rd.updateRedisLockFreshness(key)
if err != nil {
rd.Logger.Errorf("[ERROR] Keeping redis lock fresh: %v - terminating lock maintenance (lock: %s)", err, key)
return
}
if done {
return
}
}
}
func (rd *RedisStorage) updateRedisLockFreshness(key string) (bool, error) {
l, exists := rd.locks.Load(key)
if !exists {
// lock released
return true, nil
}
lock, ok := l.(*redislock.Lock)
if !ok {
return true, fmt.Errorf("uable to cast to redislock")
}
// refresh the lock's TTL every LockFreshnessInterval
err := lock.Refresh(rd.ctx, LockDuration, nil)
if err != nil {
rd.Logger.Errorf("[ERROR] Keeping redis lock fresh: %v - terminating lock maintenance (lock: %s)", err, key)
return true, err
}
return false, nil
}
// Unlock is to unlock value
func (rd *RedisStorage) Unlock(ctx context.Context, key string) error {
if lockI, exists := rd.locks.Load(key); exists {
if lock, ok := lockI.(*redislock.Lock); ok {
err := lock.Release(rd.ctx)
rd.locks.Delete(key)
if err != nil {
return fmt.Errorf("we don't have this lock anymore, %v", err)
}
}
}
return nil
}
func (rd *RedisStorage) GetAESKeyByte() []byte {
return []byte(rd.AesKey)
}
func (rd RedisStorage) String() string {
redacted := `REDACTED`
if rd.Password != "" {
rd.Password = redacted
}
if rd.AesKey != "" {
rd.AesKey = redacted
}
strVal, _ := json.Marshal(rd)
return string(strVal)
}