-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file watcher for TLS cert and key
Also adds error for missing either tls-key or tls-cert arguments. Signed-off-by: Tayler Geiger <tayler@redhat.com>
- Loading branch information
Showing
3 changed files
with
110 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package certwatcher | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"path" | ||
"sync" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
"github.com/go-logr/logr" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
) | ||
|
||
var _ manager.Runnable = (*TLSCertificateWatcher)(nil) | ||
|
||
type TLSCertificateWatcher struct { | ||
sync.Mutex | ||
certificate *tls.Certificate | ||
Logger logr.Logger | ||
CertFile string | ||
KeyFile string | ||
} | ||
|
||
func (t *TLSCertificateWatcher) Start(ctx context.Context) error { | ||
// Run once on startup | ||
err := t.loadCertificate() | ||
if err != nil { | ||
return err | ||
} | ||
t.Logger.Info("tls-key and tls-cert initialized", "tls-cert", t.CertFile, "tls-key", t.KeyFile) | ||
|
||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return err | ||
} | ||
defer watcher.Close() | ||
|
||
if path.Dir(t.CertFile) == path.Dir(t.KeyFile) { | ||
err = watcher.Add(path.Dir(t.CertFile)) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
for _, i := range []string{t.CertFile, t.KeyFile} { | ||
err = watcher.Add(path.Dir(i)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case event := <-watcher.Events: | ||
if event.Has(fsnotify.Write) { | ||
err := t.loadCertificate() | ||
if err != nil { | ||
return err | ||
} | ||
t.Logger.Info("write detected, reloaded tls-cert and tls-key", "tls-cert", t.CertFile, "tls-key", t.KeyFile) | ||
} | ||
case err := <-watcher.Errors: | ||
t.Logger.Error(err, "issue watching TLS certificate files") | ||
} | ||
} | ||
} | ||
|
||
func (t *TLSCertificateWatcher) GetCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) { | ||
t.Lock() | ||
defer t.Unlock() | ||
return t.certificate, nil | ||
} | ||
|
||
func (t *TLSCertificateWatcher) loadCertificate() error { | ||
t.Lock() | ||
defer t.Unlock() | ||
|
||
cert, err := tls.LoadX509KeyPair(t.CertFile, t.KeyFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.certificate = &cert | ||
return nil | ||
} |