| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package configtls |
| 5 |
|
| 6 |
import ( |
| 7 |
"crypto/tls" |
| 8 |
"crypto/x509" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"sync" |
| 12 |
|
| 13 |
"github.com/fsnotify/fsnotify" |
| 14 |
) |
| 15 |
|
| 16 |
type clientCAsFileReloader struct { |
| 17 |
clientCAsFile string |
| 18 |
certPool *x509.CertPool |
| 19 |
lastReloadError error |
| 20 |
lock sync.RWMutex |
| 21 |
loader clientCAsFileLoader |
| 22 |
watcher *fsnotify.Watcher |
| 23 |
shutdownCH chan bool |
| 24 |
} |
| 25 |
|
| 26 |
type clientCAsFileLoader interface { |
| 27 |
loadClientCAFile() (*x509.CertPool, error) |
| 28 |
} |
| 29 |
|
| 30 |
func newClientCAsReloader(clientCAsFile string, loader clientCAsFileLoader) (*clientCAsFileReloader, error) { |
| 31 |
certPool, err := loader.loadClientCAFile() |
| 32 |
if err != nil { |
| 33 |
return nil, fmt.Errorf("failed to load client CA CertPool: %w", err) |
| 34 |
} |
| 35 |
|
| 36 |
reloader := &clientCAsFileReloader{ |
| 37 |
clientCAsFile: clientCAsFile, |
| 38 |
certPool: certPool, |
| 39 |
loader: loader, |
| 40 |
shutdownCH: nil, |
| 41 |
watcher: nil, |
| 42 |
} |
| 43 |
|
| 44 |
return reloader, nil |
| 45 |
} |
| 46 |
|
| 47 |
func (r *clientCAsFileReloader) getClientConfig(original *tls.Config) (*tls.Config, error) { |
| 48 |
r.lock.RLock() |
| 49 |
defer r.lock.RUnlock() |
| 50 |
return &tls.Config{ |
| 51 |
RootCAs: original.RootCAs, |
| 52 |
GetCertificate: original.GetCertificate, |
| 53 |
GetClientCertificate: original.GetClientCertificate, |
| 54 |
MinVersion: original.MinVersion, |
| 55 |
MaxVersion: original.MaxVersion, |
| 56 |
NextProtos: original.NextProtos, |
| 57 |
ClientCAs: r.certPool, |
| 58 |
ClientAuth: tls.RequireAndVerifyClientCert, |
| 59 |
}, nil |
| 60 |
} |
| 61 |
|
| 62 |
func (r *clientCAsFileReloader) reload() { |
| 63 |
r.lock.Lock() |
| 64 |
defer r.lock.Unlock() |
| 65 |
certPool, err := r.loader.loadClientCAFile() |
| 66 |
if err != nil { |
| 67 |
r.lastReloadError = err |
| 68 |
} else { |
| 69 |
r.certPool = certPool |
| 70 |
r.lastReloadError = nil |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func (r *clientCAsFileReloader) getLastError() error { |
| 75 |
r.lock.Lock() |
| 76 |
defer r.lock.Unlock() |
| 77 |
return r.lastReloadError |
| 78 |
} |
| 79 |
|
| 80 |
func (r *clientCAsFileReloader) startWatching() error { |
| 81 |
if r.shutdownCH != nil { |
| 82 |
return errors.New("client CA file watcher already started") |
| 83 |
} |
| 84 |
|
| 85 |
watcher, err := fsnotify.NewWatcher() |
| 86 |
if err != nil { |
| 87 |
return fmt.Errorf("failed to create watcher to reload client CA CertPool: %w", err) |
| 88 |
} |
| 89 |
r.watcher = watcher |
| 90 |
|
| 91 |
err = watcher.Add(r.clientCAsFile) |
| 92 |
if err != nil { |
| 93 |
return fmt.Errorf("failed to add client CA file to watcher: %w", err) |
| 94 |
} |
| 95 |
|
| 96 |
r.shutdownCH = make(chan bool) |
| 97 |
go r.handleWatcherEvents() |
| 98 |
|
| 99 |
return nil |
| 100 |
} |
| 101 |
|
| 102 |
func (r *clientCAsFileReloader) handleWatcherEvents() { |
| 103 |
defer r.watcher.Close() |
| 104 |
for { |
| 105 |
select { |
| 106 |
case _, ok := <-r.shutdownCH: |
| 107 |
_ = ok |
| 108 |
return |
| 109 |
case event, ok := <-r.watcher.Events: |
| 110 |
if !ok { |
| 111 |
continue |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if event.Has(fsnotify.Remove) || event.Has(fsnotify.Chmod) { |
| 117 |
|
| 118 |
if err := r.watcher.Remove(event.Name); err != nil { |
| 119 |
r.lastReloadError = err |
| 120 |
} |
| 121 |
|
| 122 |
if err := r.watcher.Add(r.clientCAsFile); err != nil { |
| 123 |
r.lastReloadError = err |
| 124 |
} |
| 125 |
r.reload() |
| 126 |
} |
| 127 |
if event.Has(fsnotify.Write) { |
| 128 |
r.reload() |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func (r *clientCAsFileReloader) shutdown() error { |
| 135 |
if r.shutdownCH == nil { |
| 136 |
return errors.New("client CAs file watcher is not running") |
| 137 |
} |
| 138 |
r.shutdownCH <- true |
| 139 |
close(r.shutdownCH) |
| 140 |
r.shutdownCH = nil |
| 141 |
return nil |
| 142 |
} |