-
Notifications
You must be signed in to change notification settings - Fork 1
/
reloader.go
271 lines (230 loc) · 6.78 KB
/
reloader.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
package tlsconfig
import (
"crypto/tls"
"crypto/x509"
"os"
"path"
"sync"
"sync/atomic"
"time"
"github.com/effective-security/xlog"
"github.com/pkg/errors"
)
// Wrap time.Tick so we can override it in tests.
var makeTicker = func(interval time.Duration) (func(), <-chan time.Time) {
t := time.NewTicker(interval)
return t.Stop, t.C
}
// OnReloadFunc is a callback to handle cert reload
type OnReloadFunc func(pair *tls.Certificate)
// KeypairReloader keeps necessary info to provide reloaded certificate
type KeypairReloader struct {
label string
lock sync.RWMutex
loadedAt time.Time
count uint32
keypair *tls.Certificate
certPath string
certModifiedAt time.Time
keyPath string
keyModifiedAt time.Time
inProgress bool
stopChan chan<- struct{}
closed bool
handlers []OnReloadFunc
}
// NewKeypairReloader return an instance of the TLS cert loader
func NewKeypairReloader(label, certPath, keyPath string, checkInterval time.Duration) (*KeypairReloader, error) {
if label == "" {
label = path.Base(certPath)
}
result := &KeypairReloader{
label: label,
certPath: certPath,
keyPath: keyPath,
stopChan: make(chan struct{}),
}
logger.KV(xlog.TRACE, "label", label, "status", "started")
err := result.Reload()
if err != nil {
return nil, err
}
stopChan := make(chan struct{})
tickerStop, tickChan := makeTicker(checkInterval)
go func() {
for {
select {
case <-stopChan:
tickerStop()
logger.KV(xlog.TRACE, "status", "closed", "label", result.label, "count", result.LoadedCount())
return
case <-tickChan:
modified := false
fi, err := os.Stat(certPath)
if err == nil {
modified = fi.ModTime().After(result.certModifiedAt)
} else {
logger.KV(xlog.WARNING, "reason", "stat", "label", result.label, "file", certPath, "err", err)
}
if !modified {
fi, err = os.Stat(keyPath)
if err == nil {
modified = fi.ModTime().After(result.keyModifiedAt)
} else {
logger.KV(xlog.WARNING, "reason", "stat", "label", result.label, "file", keyPath, "err", err)
}
}
// reload on modified, or force to reload each hour
if modified || result.loadedAt.Add(1*time.Hour).Before(time.Now().UTC()) {
err := result.Reload()
if err != nil {
logger.KV(xlog.ERROR, "label", result.label, "err", err)
}
}
}
}
}()
result.stopChan = stopChan
return result, nil
}
// OnReload allows to add OnReloadFunc handler
func (k *KeypairReloader) OnReload(f OnReloadFunc) *KeypairReloader {
k.lock.Lock()
defer k.lock.Unlock()
if f != nil {
k.handlers = append(k.handlers, f)
}
return k
}
// Reload will explicitly load TLS certs from the disk
func (k *KeypairReloader) Reload() error {
k.lock.Lock()
if k.inProgress {
k.lock.Unlock()
return nil
}
k.inProgress = true
defer func() {
k.inProgress = false
k.lock.Unlock()
}()
oldModifiedAt := k.certModifiedAt
var newCert *tls.Certificate
var err error
for i := 0; i < 3; i++ {
// sleep a little as notification occurs right after process starts writing the file,
// so it needs to finish writing the file
time.Sleep(100 * time.Millisecond)
newCert, err = LoadX509KeyPairWithOCSP(k.certPath, k.keyPath)
if err == nil {
break
}
logger.KV(xlog.WARNING, "reason", "LoadX509KeyPair", "label", k.label, "file", k.certPath, "err", err)
}
if err != nil {
return errors.WithMessagef(err, "count: %d", k.count)
}
atomic.AddUint32(&k.count, 1)
k.loadedAt = time.Now().UTC()
certFileInfo, err := os.Stat(k.certPath)
if err == nil {
k.certModifiedAt = certFileInfo.ModTime()
} else {
logger.KV(xlog.WARNING, "reason", "stat", "label", k.label, "file", k.certPath, "err", err)
}
keyFileInfo, err := os.Stat(k.keyPath)
if err == nil {
k.keyModifiedAt = keyFileInfo.ModTime()
} else {
logger.KV(xlog.WARNING, "reason", "stat", "label", k.label, "file", k.keyPath, "err", err)
}
logger.KV(xlog.WARNING, "label", k.label, "count", k.count, "cert", k.certPath, "modifiedAt", k.certModifiedAt.Format(time.RFC3339))
k.keypair = newCert
keypair := k.tlsCert()
if oldModifiedAt != k.certModifiedAt {
// execute notifications outside of the lock
for _, h := range k.handlers {
go h(keypair)
}
}
return nil
}
func (k *KeypairReloader) tlsCert() *tls.Certificate {
var err error
kp := k.keypair
if kp.Leaf == nil && len(kp.Certificate) > 0 {
kp.Leaf, err = x509.ParseCertificate(kp.Certificate[0])
if err != nil {
logger.KV(xlog.WARNING, "reason", "ParseCertificate", "label", k.label, "err", err)
}
}
if kp.Leaf != nil {
now := time.Now()
if kp.Leaf.NotAfter.Before(now) {
logger.KV(xlog.ERROR, "label", k.label, "count", k.count, "cert", k.certPath, "expired", kp.Leaf.NotAfter.Format(time.RFC3339))
logger.Panic("cert expired")
} else if kp.Leaf.NotAfter.Add(1 * time.Hour).Before(now) {
logger.KV(xlog.WARNING, "label", k.label, "count", k.count, "cert", k.certPath, "expires_soon", kp.Leaf.NotAfter.Format(time.RFC3339))
}
}
return kp
}
// GetKeypairFunc is a callback for TLSConfig to provide TLS certificate and key pair for Server
func (k *KeypairReloader) GetKeypairFunc() func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
return func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
k.lock.RLock()
defer k.lock.RUnlock()
return k.tlsCert(), nil
}
}
// GetClientCertificateFunc is a callback for TLSConfig to provide TLS certificate and key pair for Client
func (k *KeypairReloader) GetClientCertificateFunc() func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
return func(_ *tls.CertificateRequestInfo) (*tls.Certificate, error) {
k.lock.RLock()
defer k.lock.RUnlock()
return k.tlsCert(), nil
}
}
// Keypair returns current pair
func (k *KeypairReloader) Keypair() *tls.Certificate {
if k == nil {
return nil
}
k.lock.RLock()
defer k.lock.RUnlock()
return k.tlsCert()
}
// CertAndKeyFiles returns cert and key files
func (k *KeypairReloader) CertAndKeyFiles() (string, string) {
if k == nil {
return "", ""
}
k.lock.RLock()
defer k.lock.RUnlock()
return k.certPath, k.keyPath
}
// LoadedAt return the last time when the pair was loaded
func (k *KeypairReloader) LoadedAt() time.Time {
k.lock.RLock()
defer k.lock.RUnlock()
return k.loadedAt
}
// LoadedCount returns the number of times the pair was loaded from disk
func (k *KeypairReloader) LoadedCount() uint32 {
return atomic.LoadUint32(&k.count)
}
// Close will close the reloader and release its resources
func (k *KeypairReloader) Close() error {
if k == nil {
return nil
}
k.lock.RLock()
defer k.lock.RUnlock()
if k.closed {
return errors.New("already closed")
}
logger.KV(xlog.DEBUG, "label", k.label, "count", k.count, "cert", k.certPath, "key", k.keyPath)
k.closed = true
k.stopChan <- struct{}{}
return nil
}