-
Notifications
You must be signed in to change notification settings - Fork 6
feat(tls): add support for manual TLS certificate management #90
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,118 @@ | ||
| package tlsreload | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "fmt" | ||
| "os" | ||
| "sync" | ||
| "sync/atomic" | ||
| "time" | ||
|
|
||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| type fileState struct { | ||
| modTime time.Time | ||
| size int64 | ||
| } | ||
|
|
||
| // FileReloader watches certificate and key files and reloads them when they change. | ||
| type FileReloader struct { | ||
| certPath string | ||
| keyPath string | ||
| logger *zap.Logger | ||
|
|
||
| cert atomic.Value // *tls.Certificate | ||
| mu sync.Mutex | ||
| certState fileState | ||
| keyState fileState | ||
| } | ||
|
|
||
| // NewFileReloader loads the initial certificate/key pair and prepares for reloads. | ||
| func NewFileReloader(certPath, keyPath string, logger *zap.Logger) (*FileReloader, error) { | ||
| certInfo, err := os.Stat(certPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("stat cert file: %w", err) | ||
| } | ||
| keyInfo, err := os.Stat(keyPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("stat key file: %w", err) | ||
| } | ||
|
|
||
| cert, err := tls.LoadX509KeyPair(certPath, keyPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("load key pair: %w", err) | ||
| } | ||
|
|
||
| reloader := &FileReloader{ | ||
| certPath: certPath, | ||
| keyPath: keyPath, | ||
| logger: logger, | ||
| certState: fileState{ | ||
| modTime: certInfo.ModTime(), | ||
| size: certInfo.Size(), | ||
| }, | ||
| keyState: fileState{ | ||
| modTime: keyInfo.ModTime(), | ||
| size: keyInfo.Size(), | ||
| }, | ||
| } | ||
| reloader.cert.Store(&cert) | ||
| return reloader, nil | ||
| } | ||
|
|
||
| func (r *FileReloader) maybeReload() error { | ||
| r.mu.Lock() | ||
| defer r.mu.Unlock() | ||
|
|
||
| certInfo, err := os.Stat(r.certPath) | ||
| if err != nil { | ||
| return fmt.Errorf("stat cert file: %w", err) | ||
| } | ||
| keyInfo, err := os.Stat(r.keyPath) | ||
| if err != nil { | ||
| return fmt.Errorf("stat key file: %w", err) | ||
| } | ||
|
|
||
| newCertState := fileState{modTime: certInfo.ModTime(), size: certInfo.Size()} | ||
| newKeyState := fileState{modTime: keyInfo.ModTime(), size: keyInfo.Size()} | ||
|
|
||
| if newCertState.modTime.Equal(r.certState.modTime) && newCertState.size == r.certState.size && | ||
| newKeyState.modTime.Equal(r.keyState.modTime) && newKeyState.size == r.keyState.size { | ||
| return nil | ||
| } | ||
|
|
||
| cert, err := tls.LoadX509KeyPair(r.certPath, r.keyPath) | ||
| if err != nil { | ||
| return fmt.Errorf("load key pair: %w", err) | ||
| } | ||
|
|
||
| r.cert.Store(&cert) | ||
| r.certState = newCertState | ||
| r.keyState = newKeyState | ||
|
|
||
| if r.logger != nil { | ||
| r.logger.Info("Reloaded TLS certificate files", | ||
| zap.String("certFile", r.certPath), | ||
| zap.String("keyFile", r.keyPath), | ||
| zap.Time("certModTime", newCertState.modTime), | ||
| zap.Time("keyModTime", newKeyState.modTime), | ||
| ) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // GetCertificate reloads certificate/key as needed and returns the current pair. | ||
| func (r *FileReloader) GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error) { | ||
| if err := r.maybeReload(); err != nil { | ||
| if r.logger != nil { | ||
| r.logger.Warn("Failed to reload TLS certificate", zap.Error(err)) | ||
| } | ||
| } | ||
|
|
||
| value := r.cert.Load() | ||
| if value == nil { | ||
| return nil, fmt.Errorf("no TLS certificate loaded") | ||
| } | ||
| return value.(*tls.Certificate), nil | ||
| } |
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.
[nitpick] The else block has been removed, changing the control flow. The previous structure with an explicit else clause was clearer. Consider keeping the else block for better readability, or add a blank line before the return statement to separate the conditional logic.