-
Notifications
You must be signed in to change notification settings - Fork 31
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
4 changed files
with
119 additions
and
14 deletions.
There are no files selected for viewing
Binary file not shown.
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,97 @@ | ||
/* | ||
Copyright 2022 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// this is copied from https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/77b08a845e451b695cfa25b79ebe277d85064345/pkg/manager/server.go | ||
// we will remove this once we update to a version of controller-runitme that has this included | ||
// https://github.com/kubernetes-sigs/controller-runtime/pull/2473 | ||
|
||
package server | ||
|
||
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() | ||
t.Logger.Info("tls-key and tls-cert initialized", "tls-cert", t.CertFile, "tls-key", t.KeyFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return err | ||
} | ||
defer watcher.Close() | ||
|
||
if path.Dir(t.CertFile) == path.Dir(t.KeyFile) { | ||
watcher.Add(path.Dir(t.CertFile)) | ||
} else { | ||
watcher.Add(path.Dir(t.CertFile)) | ||
watcher.Add(path.Dir(t.KeyFile)) | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case event, _ := <-watcher.Events: | ||
if event.Has(fsnotify.Write) { | ||
t.loadCertificate() | ||
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 | ||
} |