This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
tlsinfo.go
176 lines (151 loc) · 3.5 KB
/
tlsinfo.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
package k8sutils
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"kubevirt.io/client-go/log"
)
const (
CertFilename = "tls.crt"
KeyFilename = "tls.key"
retryInterval = 1 * time.Minute
)
type TLSInfo struct {
CertsDirectory string
cert *tls.Certificate
certLock sync.Mutex
stopCertReload chan struct{}
}
func (ti *TLSInfo) IsEnabled() bool {
return ti.CertsDirectory != ""
}
func (ti *TLSInfo) Init() {
if !ti.IsEnabled() {
return
}
directory := ti.CertsDirectory
filesChanged, watcherCloser, err := watchDirectory(directory)
if err != nil {
panic(err)
}
ti.stopCertReload = make(chan struct{})
notify(filesChanged)
go func() {
defer watcherCloser.Close()
for {
select {
case <-filesChanged:
err := ti.updateCertificates(directory)
if err != nil {
go func() {
time.Sleep(retryInterval)
notify(filesChanged)
}()
}
case <-ti.stopCertReload:
return
}
}
}()
}
func (ti *TLSInfo) Clean() {
if ti.stopCertReload != nil {
close(ti.stopCertReload)
}
}
func watchDirectory(directory string) (chan struct{}, io.Closer, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Log.Reason(err).Critical("Failed to create an inotify watcher")
return nil, nil, err
}
err = watcher.Add(directory)
if err != nil {
watcher.Close()
log.Log.Reason(err).Criticalf("Failed to establish a watch on %s", directory)
return nil, nil, err
}
filesChanged := make(chan struct{}, 1)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op != fsnotify.Chmod {
notify(filesChanged)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Log.Reason(err).Errorf("An error occurred when watching %s", directory)
}
}
}()
return filesChanged, watcher, nil
}
func notify(channel chan struct{}) {
select {
case channel <- struct{}{}:
default:
}
}
func (ti *TLSInfo) updateCertificates(directory string) error {
cert, err := loadCertificates(directory)
if err != nil {
log.Log.Reason(err).Infof("failed to load the certificate in %s", directory)
return err
}
ti.certLock.Lock()
defer ti.certLock.Unlock()
ti.cert = cert
log.Log.Infof("certificate from %s with common name '%s' retrieved.", directory, cert.Leaf.Subject.CommonName)
return nil
}
func loadCertificates(directory string) (serverCrt *tls.Certificate, err error) {
certPath := filepath.Join(directory, CertFilename)
keyPath := filepath.Join(directory, KeyFilename)
certBytes, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, err
}
keyBytes, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
cert, err := tls.X509KeyPair(certBytes, keyBytes)
if err != nil {
return nil, fmt.Errorf("failed to load certificate: %v\n", err)
}
leaf, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, fmt.Errorf("failed to load leaf certificate: %v\n", err)
}
cert.Leaf = leaf
return &cert, nil
}
func (ti *TLSInfo) getCertificate() *tls.Certificate {
ti.certLock.Lock()
defer ti.certLock.Unlock()
return ti.cert
}
func (ti *TLSInfo) CrateTlsConfig() *tls.Config {
return &tls.Config{
GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
cert := ti.getCertificate()
if cert == nil {
return nil, errors.New("no server certificate, server is not yet ready to receive traffic")
}
return cert, nil
},
}
}