-
Notifications
You must be signed in to change notification settings - Fork 47
/
tlsinfo.go
204 lines (176 loc) · 4.37 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
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
package tlsinfo
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"kubevirt.io/ssp-operator/internal/common"
"kubevirt.io/ssp-operator/internal/template-validator/logger"
)
const (
CertFilename = "tls.crt"
KeyFilename = "tls.key"
retryInterval = 1 * time.Minute
CiphersEnvName = "TLS_CIPHERS"
TLSMinVersionEnvName = "TLS_MIN_VERSION"
)
type TLSInfo struct {
CertsDirectory string
cert *tls.Certificate
certLock sync.Mutex
stopCertReload chan struct{}
sspTLSOptions common.SSPTLSOptions
}
func (ti *TLSInfo) IsEnabled() bool {
return ti.CertsDirectory != ""
}
func (ti *TLSInfo) Init() {
ti.InitCerts()
ti.InitCryptoConfig()
}
func (ti *TLSInfo) InitCryptoConfig() {
nonSplitCiphers, _ := os.LookupEnv(CiphersEnvName)
ti.sspTLSOptions.OpenSSLCipherNames = strings.Split(nonSplitCiphers, ",")
ti.sspTLSOptions.MinTLSVersion, _ = os.LookupEnv(TLSMinVersionEnvName)
}
func (ti *TLSInfo) InitCerts() {
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 {
logger.Log.Error(err, "Failed to create an inotify watcher")
return nil, nil, err
}
err = watcher.Add(directory)
if err != nil {
watcher.Close()
logger.Log.Error(err, "Failed to establish a watch", "directory", 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
}
logger.Log.Error(err, "An error occurred while watching", "directory", 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 {
logger.Log.Info("failed to load certificates",
"directory", directory,
"error", err)
return err
}
ti.certLock.Lock()
defer ti.certLock.Unlock()
ti.cert = cert
logger.Log.Info("certificate retrieved", "directory", directory, "name", 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 := os.ReadFile(certPath)
if err != nil {
return nil, err
}
keyBytes, err := os.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) CreateTlsConfig() *tls.Config {
tlsConfig := &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
},
}
if !ti.sspTLSOptions.IsEmpty() {
tlsConfig.CipherSuites = common.CipherIDs(ti.sspTLSOptions.OpenSSLCipherNames, nil)
minVersion, err := ti.sspTLSOptions.MinTLSVersionId()
if err != nil {
panic(fmt.Sprintf("TLS Configuration broken, min version misconfigured %v", err))
}
tlsConfig.MinVersion = minVersion
}
return tlsConfig
}