/
tls-certs.go
126 lines (105 loc) · 2.87 KB
/
tls-certs.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
package server
import (
"context"
"crypto/tls"
"fmt"
"path/filepath"
"sync"
"github.com/rs/zerolog"
"github.com/italypaleale/traefik-forward-auth/pkg/utils"
"github.com/italypaleale/traefik-forward-auth/pkg/utils/fsnotify"
)
const (
tlsCertFile = "tls-cert.pem"
tlsKeyFile = "tls-key.pem"
tlsCAFile = "tls-ca.pem"
minTLSVersion = tls.VersionTLS12
)
type tlsCertWatchFn = func(ctx context.Context) error
type tlsCertProvider struct {
lock sync.RWMutex
tlsCert *tls.Certificate
path string
cert string
key string
}
// Creates a new tlsCertProvider object
// If we cannot find a TLS certificates, the returned object will be nil
func newTLSCertProvider(path string) (*tlsCertProvider, error) {
var exists bool
// Check if the certificate and key exist
cert := filepath.Join(path, tlsCertFile)
if exists, _ = utils.FileExists(cert); !exists {
return nil, nil
}
key := filepath.Join(path, tlsKeyFile)
if exists, _ = utils.FileExists(key); !exists {
return nil, nil
}
// Load the certificates initially
tlsCert, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return nil, err
}
return &tlsCertProvider{
tlsCert: &tlsCert,
path: path,
cert: cert,
key: key,
}, nil
}
// GetCertificateFn returns a function that can be used as the GetCertificate property in a tls.Config object.
func (p *tlsCertProvider) GetCertificateFn() func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
return func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
p.lock.RLock()
defer p.lock.RUnlock()
return p.tlsCert, nil
}
}
// Reload the certificate from disk.
func (p *tlsCertProvider) Reload() error {
tlsCert, err := tls.LoadX509KeyPair(p.cert, p.key)
if err != nil {
return err
}
p.SetTLSCert(&tlsCert)
return nil
}
// SetTLSCert updates the TLS certificate object.
func (p *tlsCertProvider) SetTLSCert(tlsCert *tls.Certificate) {
p.lock.Lock()
p.tlsCert = tlsCert
p.lock.Unlock()
}
// Watch starts watching (in background) for changes to the TLS certificate and key on disk, and triggers a reload when that happens.
func (p *tlsCertProvider) Watch(ctx context.Context) error {
log := zerolog.Ctx(ctx)
watcher, err := fsnotify.WatchFolder(ctx, p.path)
if err != nil {
return fmt.Errorf("failed to start watching for changes on disk: %w", err)
}
// Start the background watcher
go func() {
var reloadErr error
for {
select {
case <-watcher:
// Reload
log.Info().Msg("Found changes in folder containing TLS certificates; will reload certificates")
reloadErr = p.Reload()
if reloadErr != nil {
// Log errors only
log.Error().
Err(reloadErr).
Msg("Failed to load updated TLS certificates from disk")
continue
}
log.Info().Msg("TLS certificates have been reloaded")
case <-ctx.Done():
// Stop on context cancellation
return
}
}
}()
return nil
}