-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathreloader.go
54 lines (47 loc) · 1.07 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
package main
import (
"log"
"sync"
"time"
tls "github.com/jmhodges/howsmyssl/tls110"
)
func newKeypairReloader(certPath, keyPath string) (*keypairReloader, error) {
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, err
}
kpr := &keypairReloader{
certPath: certPath,
keyPath: keyPath,
cert: &cert,
}
return kpr, nil
}
func reloadKeypairForever(kpr *keypairReloader, tick *time.Ticker) {
for range tick.C {
if err := kpr.maybeReload(); err != nil {
log.Printf("error when attempting reload of TLS keypair: %s", err)
}
}
}
type keypairReloader struct {
certMu sync.RWMutex
cert *tls.Certificate
certPath string
keyPath string
}
func (kpr *keypairReloader) maybeReload() error {
newCert, err := tls.LoadX509KeyPair(kpr.certPath, kpr.keyPath)
if err != nil {
return err
}
kpr.certMu.Lock()
defer kpr.certMu.Unlock()
kpr.cert = &newCert
return nil
}
func (kpr *keypairReloader) GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error) {
kpr.certMu.RLock()
defer kpr.certMu.RUnlock()
return kpr.cert, nil
}