-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathreloadablecert.go
43 lines (36 loc) · 921 Bytes
/
reloadablecert.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
package j8a
import (
"crypto/tls"
"crypto/x509"
"github.com/rs/zerolog/log"
"sync"
)
type ReloadableCert struct {
Cert *tls.Certificate
mu sync.Mutex
Init bool
//required to use runtime internally without global pointer for testing.
runtime *Runtime
}
func (r *ReloadableCert) GetCertificateFunc(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
return r.Cert, nil
}
func (r *ReloadableCert) triggerInit() error {
r.mu.Lock()
defer r.mu.Unlock()
r.Init = true
var cert tls.Certificate
var err error
c := []byte(r.runtime.Connection.Downstream.Tls.Cert)
k := []byte(r.runtime.Connection.Downstream.Tls.Key)
cert, err = tls.X509KeyPair(c, k)
if err == nil {
r.Cert = &cert
r.Cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
}
if err == nil {
log.Info().Msgf("TLS certificate #%v initialized", formatSerial(cert.Leaf.SerialNumber))
}
r.Init = false
return err
}