forked from cohix/redicrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredicrypt.go
140 lines (115 loc) · 2.58 KB
/
redicrypt.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
package redicrypt
import (
"context"
"encoding/base64"
"fmt"
"io"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"golang.org/x/crypto/acme/autocert"
)
type RediCrypt struct {
Addr string
Conn redis.Conn
Logger io.Writer
}
func RediCryptWithAddr(addr string) (*RediCrypt, error) {
c, err := redis.Dial("tcp", addr)
if err != nil {
return nil, errors.Wrap(err, "RediCryptWithAddr failed to Dial")
}
rc := &RediCrypt{
Addr: addr,
Conn: c,
}
return rc, nil
}
// Get reads certificate data from redis.
func (rc *RediCrypt) Get(ctx context.Context, name string) ([]byte, error) {
key := redisKeyForName(name)
rc.log("getting cert for key " + key)
data := ""
done := make(chan error)
go func() {
var err error
data, err = redis.String(rc.Conn.Do("GET", key))
if err == redis.ErrNil {
done <- autocert.ErrCacheMiss
} else {
done <- err
}
}()
select {
case <-ctx.Done():
return nil, ctx.Err()
case err := <-done:
if err != nil {
return nil, err
}
}
certBytes, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, errors.Wrap(err, "Get failed to DecodeString")
}
return certBytes, nil
}
// Put writes certificate data to redis.
func (rc *RediCrypt) Put(ctx context.Context, name string, data []byte) error {
key := redisKeyForName(name)
rc.log("writing cert for key " + key)
encodedData := base64.StdEncoding.EncodeToString(data)
done := make(chan error)
go func() {
select {
case <-ctx.Done():
// Don't overwrite the file if the context was canceled.
default:
_, err := rc.Conn.Do("SET", key, encodedData)
done <- err
}
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-done:
if err != nil {
return err
}
}
return nil
}
// Delete removes the specified redis key.
func (rc *RediCrypt) Delete(ctx context.Context, name string) error {
key := redisKeyForName(name)
rc.log("removing cert for key " + key)
done := make(chan error)
go func() {
_, err := rc.Conn.Do("DELETE", key)
done <- err
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-done:
if err != nil {
return err
}
}
return nil
}
// log writes to the io.Writer defined by Logger or standard out if Logger is not set.
// All input values are prefixed with "redicrypt: ".
func (rc *RediCrypt) log(input string) {
if input == "" {
return
}
output := fmt.Sprintf("redicrypt: %s", input)
if rc == nil || rc.Logger == nil {
fmt.Println(output)
return
}
rc.Logger.Write([]byte(output))
}
func redisKeyForName(name string) string {
return fmt.Sprintf("redicrypt/%s", name)
}