-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] *: watch tls certificate changes #45
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,172 @@ | ||
/* | ||
Copyright 2017 Frederic Branczyk All rights reserved. | ||
|
||
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. | ||
*/ | ||
|
||
package tls | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
"github.com/golang/glog" | ||
) | ||
|
||
// CertReloader is the struct that parses a certificate/key pair, | ||
// providing a goroutine safe GetCertificate method to retrieve the parsed content. | ||
// | ||
// The GetCertificate signature is compatible with https://golang.org/pkg/crypto/tls/#Config.GetCertificate | ||
// and can be used to hot-reload a certificate/key pair. | ||
// | ||
// For hot-reloading the Watch method must be started explicitly. | ||
type CertReloader struct { | ||
certPath, keyPath string | ||
|
||
// contains the watch targets (keys) | ||
// and the watch locations (values) | ||
watchables map[string]string | ||
|
||
mu sync.RWMutex // protects the fields below | ||
cert *tls.Certificate | ||
} | ||
|
||
func NewCertReloader(certPath, keyPath string) (*CertReloader, error) { | ||
watchables := make(map[string]string) | ||
|
||
w, in, err := newWatchable(certPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error adding cert watchable: %v", err) | ||
} | ||
watchables[w] = in | ||
|
||
w, in, err = newWatchable(keyPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error adding key watchable: %v", err) | ||
} | ||
watchables[w] = in | ||
|
||
r := &CertReloader{ | ||
certPath: certPath, | ||
keyPath: keyPath, | ||
watchables: watchables, | ||
} | ||
|
||
if err := r.reload(); err != nil { | ||
return nil, fmt.Errorf("error loading certificates: %v", err) | ||
} | ||
|
||
return r, nil | ||
} | ||
|
||
func newWatchable(target string) (watchable, in string, _ error) { | ||
// simple case: the target is a file. | ||
// in that case we watch for changes in its own directory. | ||
watchable = filepath.Clean(target) | ||
in, _ = filepath.Split(target) | ||
in = filepath.Clean(in) | ||
|
||
stat, err := os.Lstat(target) | ||
if err != nil { | ||
return "", "", fmt.Errorf("lstat on %q failed: %v", target, err) | ||
} | ||
|
||
// k8s case: the target is a symlink. | ||
// Here, we watch the intermediate symlink (named `..data`) | ||
// in the same directory as the target. | ||
if stat.Mode()&os.ModeSymlink != 0 { | ||
dest, err := os.Readlink(target) | ||
if err != nil { | ||
return "", "", fmt.Errorf("lstat on %q failed: %v", target, err) | ||
|
||
} | ||
|
||
watchable, _ = filepath.Split(dest) | ||
watchable = filepath.Clean(watchable) | ||
} | ||
|
||
return | ||
} | ||
|
||
// Watch watches the configured certificate and key path and blocks the current goroutine | ||
// until the scenario context is done or an error occurred during reloading. | ||
func (r *CertReloader) Watch(ctx context.Context) error { | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return fmt.Errorf("error creating fsnotify watcher: %v", err) | ||
} | ||
|
||
for _, v := range r.watchables { | ||
glog.V(4).Infof("watching: %q", v) | ||
|
||
if err := watcher.Add(v); err != nil { | ||
return fmt.Errorf("error adding watchable: %v", err) | ||
} | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
|
||
case event := <-watcher.Events: | ||
glog.V(5).Infof("watcher event %v", event) | ||
|
||
if _, ok := r.watchables[filepath.Clean(event.Name)]; !ok { | ||
continue | ||
} | ||
|
||
if event.Op&(fsnotify.Write|fsnotify.Create) == 0 { | ||
continue | ||
} | ||
|
||
case err := <-watcher.Errors: | ||
glog.Errorf("watch failed: %v", err) | ||
continue | ||
} | ||
|
||
if err := r.reload(); err != nil { | ||
return fmt.Errorf("reloading failed: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func (r *CertReloader) reload() error { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
glog.V(4).Info("reloading key ", r.keyPath, " certificate ", r.certPath) | ||
|
||
cert, err := tls.LoadX509KeyPair(r.certPath, r.keyPath) | ||
if err != nil { | ||
return fmt.Errorf("error loading certificate: %v", err) | ||
} | ||
|
||
r.cert = &cert | ||
return nil | ||
} | ||
|
||
// GetCertificate returns the current valid certificate. | ||
// The ClientHello message is ignored | ||
// and is just there to be compatible with https://golang.org/pkg/crypto/tls/#Config.GetCertificate. | ||
func (r *CertReloader) GetCertificate(_ *tls.ClientHelloInfo) (*tls.Certificate, error) { | ||
r.mu.RLock() | ||
defer r.mu.RUnlock() | ||
|
||
return r.cert, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we, in addition should add a ticker anyways. I am not sure we have 100% guarantees to catch all file system events, cc @lucab.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious: for this very case, why not just read the files once per minute and apply if they changed? For other cases I agree that reloading as quickly as possible when available makes perfect sense, but here we have a lot of time to apply cert rotation anyways no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good yes, this will simplify a lot of things and we don't have to think about edge cases 👍 let me implement a ticker-only based approach here and reuse the implemented logic here for prometheus-config-reloader.