-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtlsreconciler.go
403 lines (338 loc) · 10.4 KB
/
tlsreconciler.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
// Package tlsreconciler implements hitless TLS certificate rotation reconciliation,
// by using certificate selection during the TLS handshake that tls.Config exposes.
package tlsreconciler
import (
"container/list"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"os"
"sync"
"sync/atomic"
"syscall"
"time"
"golang.org/x/sync/singleflight"
)
// Provider provides tls certificates. Any type that implements
// it may be used to reload and provide rotated certificates.
type Provider interface {
// Certificates retruns last rotated client or server (leaf) certificate
// alongside root CA, or an error if occurs.
//
// Certificate may return nil for root CAs, if a predefined ca pool sat in tls.Config.
Certificates() (*tls.Certificate, []*x509.Certificate, error)
}
// Option configures reconciler using the functional options paradigm
// popularized by Rob Pike and Dave Cheney. If you're unfamiliar with this style,
// see https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html and
// https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis.
type Option interface {
apply(*reconciler)
}
// OptionFunc implements Option interface.
type optionFunc func(*reconciler)
// apply the configuration to the provided config.
func (fn optionFunc) apply(r *reconciler) {
fn(r)
}
// WithVerifyConnection set tls.Config.VerifyConnection to verify
// tls conn certificate, using the reconciler CA pool that
// fulfilled from the provider based on previous rotations,
// Otherwise, the system roots or the platform verifier are used.
//
// WithVerifyConnection also set tls.Config.InsecureSkipVerify
// to skip the default golang validation that tlsreconciler
// replacing. This will not disable VerifyConnection.
//
// See the documentation of WithRootsLimit.
//
// Note: tls.Config RootCAs and ClientCAs ignored
// when this option take place.
func WithVerifyConnection() Option {
return optionFunc(func(r *reconciler) {
r.config.VerifyConnection = r.verifyConnection
r.config.InsecureSkipVerify = true
})
}
// WithProvider sets tlsreconciler provider to retrieve
// latest certificates when there a reload signal.
func WithProvider(p Provider) Option {
return optionFunc(func(r *reconciler) {
r.p = p
r.config.GetCertificate = r.getCertificate
r.config.GetClientCertificate = r.getClientCertificate
})
}
// WithCertificatesPaths sets tlsreconciler provider to retrieve
// latest certificates from the given paths when there a reload signal.
//
// Note: ca can be empty if reload CA not needed, or it can be path to CA bundle.
func WithCertificatesPaths(cert, key, ca string) Option {
return WithProvider(fileSystemProvider{ca, cert, key})
}
// WithRootsLimit limits the number of old root CA to keep in the pool.
// One use case for this feature would be in a situation to keep backward
// compatibility to verify leaf certs of services that haven't reconciled
// there certificates yet.
//
// WithRootsLimit used WithVerifyConnection, Otherwise, It's noop
//
// Default 2.
func WithRootsLimit(n uint) Option {
return optionFunc(func(r *reconciler) {
r.rootsLimit = n
})
}
// WithReloadFunc registers func to determine if
// need to reload certificate and call the provider
// to retrieve the latest certificates.
//
// Note: multiple goroutines may call f simultaneously.
func WithReloadFunc(f func() bool) Option {
return optionFunc(func(r *reconciler) {
r.reload = f
})
}
// WithSIGHUPReload reload certificate and call the provider
// to retrieve the latest certificates when SIGHUP received.
func WithSIGHUPReload(c chan os.Signal) Option {
return optionFunc(func(r *reconciler) {
r.reload = func() bool {
select {
case sig := <-c:
if sig == syscall.SIGHUP {
return true
}
return false
default:
return false
}
}
})
}
// WithDurationRelaod reload certificate and call the provider
// to retrieve the latest certificates when each duration elapse.
func WithDurationRelaod(dur time.Duration) Option {
return optionFunc(func(r *reconciler) {
mu := new(sync.Mutex)
t := time.Now().Add(dur)
r.reload = func() bool {
mu.Lock()
defer mu.Unlock()
if time.Now().After(t) {
t = time.Now().Add(dur)
return true
}
return false
}
})
}
// WithOnReload registers a function to call on relaod.
// this can be used to rotate session tickets, or any
// additional purposes like loging.
//
// Reconciler calls f in its own goroutine.
func WithOnReload(f func(*tls.Config)) Option {
return optionFunc(func(r *reconciler) {
r.onReload = f
})
}
// TLSConfig returns new tls.Config that reconcile certificates after a rotation.
// Calling TLSConfig without any option is similar to
//
// new(tls.Config)
//
// See the documentation of options for more information.
func TLSConfig(opts ...Option) *tls.Config {
if len(opts) == 0 {
return new(tls.Config)
}
r := newReconciler()
for _, opt := range opts {
opt.apply(r)
}
return r.config
}
func newReconciler() *reconciler {
return &reconciler{
rootsLimit: 2,
flight: &singleflight.Group{},
cond: sync.NewCond(noopLocker{}),
p: noopProvider{},
ll: list.New(),
config: new(tls.Config),
reload: func() (ok bool) { return },
}
}
type reconciler struct {
// reloading indicates whether the certificate reload in is progress.
// It is first in the struct because it is used in the hot path.
// The hot path is inlined at every call site.
// Placing reloading first allows more compact instructions on some architectures (amd64/386),
// and fewer instructions (to calculate offset) on other architectures.
reloading uint32
// limit the number of rotated root certificates.
rootsLimit uint
// flight ensures that each reload is only fetched once
// regardless of the number of concurrent callers.
flight *singleflight.Group
// trigger reload at least one time on first use.
once sync.Once
// cond to waiting for or announcing the occurrence of an relaod to finish.
cond *sync.Cond
// pool hold *x509.CertPool accessible by goroutines simultaneously.
pool atomic.Value
// cert hold *tls.Certificate accessible by goroutines simultaneously.
cert atomic.Value
// p represent the certs provider.
p Provider
// ll hold new and old root certs guarded by rootsLimit.
ll *list.List
// config that the reconciler works on.
config *tls.Config
// reload indicates if need reload.
reload func() bool
// invoked on reload evnet.
onReload func(*tls.Config)
}
// getCertificate returns the last reloaded certificate.
// getCertificate implements tls.Config.GetCertificate.
func (r *reconciler) getCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error) {
cert, _, err := r.certificates()
return cert, err
}
// getClientCertificate returns the last reloaded certificate.
// getClientCertificate implements tls.Config.GetClientCertificate.
func (r *reconciler) getClientCertificate(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
cert, _, err := r.certificates()
return cert, err
}
// verifyConnection verify tls conn certificate using the last reloaded roots.
// verifyConnection implements tls.Config.VerifyConnection.
func (r *reconciler) verifyConnection(cs tls.ConnectionState) error {
_, pool, err := r.certificates()
if err != nil {
return err
}
if r.config.ClientAuth < tls.VerifyClientCertIfGiven &&
len(cs.PeerCertificates) == 0 {
return nil
}
opts := x509.VerifyOptions{
Roots: pool,
DNSName: r.config.ServerName,
Intermediates: x509.NewCertPool(),
}
if r.config.Time != nil {
opts.CurrentTime = r.config.Time()
}
if r.config.ClientAuth >= tls.VerifyClientCertIfGiven {
opts.KeyUsages = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
}
// Copy intermediates certificates to verify options from cs if needed.
// ignore cs.PeerCertificates[0] it refer to client certificates.
for _, inter := range cs.PeerCertificates[1:] {
opts.Intermediates.AddCert(inter)
}
_, err = cs.PeerCertificates[0].Verify(opts)
return err
}
func (r *reconciler) certificates() (cert *tls.Certificate, pool *x509.CertPool, err error) {
// Wait for new certificates.
for atomic.LoadUint32(&r.reloading) == 1 {
r.cond.Wait()
}
if r.needReload() {
// Prevent returning old certificates.
atomic.StoreUint32(&r.reloading, 1)
// Ensures that each reload is only fetched once.
// the reloading mechanism can only dedup calls that
// overlap concurrently, therefore, there a chance
// of goroutines bypass atomic reloading and enter
// reload, if there more than one signal.
_, err, _ = r.flight.Do("reconciler", func() (interface{}, error) {
cert, roots, err := r.p.Certificates()
if err != nil {
return nil, err
}
if len(roots) > 0 {
pool := x509.NewCertPool()
for _, ca := range roots {
r.ll.PushFront(ca)
// Remove last root ca, reached the limit.
if uint(r.ll.Len()) > r.rootsLimit {
e := r.ll.Back()
r.ll.Remove(e)
}
}
for e := r.ll.Front(); e != nil; e = e.Next() {
pool.AddCert(e.Value.(*x509.Certificate))
}
r.pool.Store(pool)
}
r.cert.Store(cert)
if r.onReload != nil {
go r.onReload(r.config)
}
// Release goroutines to read latest certs.
atomic.StoreUint32(&r.reloading, 0)
r.cond.Broadcast()
return nil, nil
})
}
if v, ok := r.cert.Load().(*tls.Certificate); ok {
cert = v
}
if v, ok := r.pool.Load().(*x509.CertPool); ok {
pool = v
}
return cert, pool, err
}
func (r *reconciler) needReload() (ok bool) {
r.once.Do(func() {
ok = true
})
return ok || r.reload()
}
type fileSystemProvider []string
func (fsp fileSystemProvider) Certificates() (*tls.Certificate, []*x509.Certificate, error) {
if len(fsp) != 3 {
return nil, nil, errors.New("tlsreconciler: certificates path missing")
}
caFile, certFile, keyFile := fsp[0], fsp[1], fsp[2]
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, nil, err
}
if len(caFile) == 0 {
return &cert, nil, nil
}
caPEMBlock, err := os.ReadFile(caFile)
if err != nil {
return nil, nil, err
}
var (
p *pem.Block
roots []*x509.Certificate
)
for {
p, caPEMBlock = pem.Decode(caPEMBlock)
if p == nil {
break
}
cert, err := x509.ParseCertificate(p.Bytes)
if err != nil {
return nil, nil, err
}
roots = append(roots, cert)
}
return &cert, roots, err
}
type noopLocker struct{}
func (noopLocker) Lock() {}
func (noopLocker) Unlock() {}
type noopProvider struct{}
func (noopProvider) Certificates() (*tls.Certificate, []*x509.Certificate, error) {
return nil, nil, nil
}