From 1bc4b7ee79588081f4a875d0c373172b055f7324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Magyar?= Date: Tue, 11 Jan 2022 10:04:25 +0100 Subject: [PATCH] Add keypair reloader --- LICENSE | 9 +++++ go.mod | 8 +++-- go.sum | 4 +++ keypair_reloader.go | 81 +++++++++++++++++++++++++++++++++++++++++++++ server.go | 7 +++- 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 LICENSE create mode 100644 go.sum create mode 100644 keypair_reloader.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b8d6ea5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2021 Vladimir Magyar + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/go.mod b/go.mod index d93c8b0..1052d15 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ -module gitlab.com/microo8/server +module git.sr.ht/~ghost08/server -go 1.14 +go 1.17 + +require github.com/fsnotify/fsnotify v1.5.1 + +require golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..131b41b --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= +github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/keypair_reloader.go b/keypair_reloader.go new file mode 100644 index 0000000..e97ec6f --- /dev/null +++ b/keypair_reloader.go @@ -0,0 +1,81 @@ +package server + +import ( + "crypto/tls" + "log" + "net/http" + "path/filepath" + "sync" + + "github.com/fsnotify/fsnotify" +) + +type keypairReloader struct { + certMu sync.RWMutex + cert *tls.Certificate + certPath string + keyPath string +} + +func NewKeypairReloader(certPath, keyPath string) (*keypairReloader, error) { + result := &keypairReloader{ + certPath: certPath, + keyPath: keyPath, + } + cert, err := tls.LoadX509KeyPair(certPath, keyPath) + if err != nil { + return nil, err + } + result.cert = &cert + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + defer watcher.Close() + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + log.Printf("INFO: File modified %s, reloading TLS certificate and key from %q and %q", event.Name, certPath, keyPath) + if err := result.maybeReload(); err != nil { + log.Printf("Keeping old TLS certificate because the new one could not be loaded: %v", err) + } + } + } + }() + watcher.Add(filepath.Dir(certPath)) + watcher.Add(filepath.Dir(keyPath)) + return result, nil +} + +func (kpr *keypairReloader) maybeReload() error { + newCert, err := tls.LoadX509KeyPair(kpr.certPath, kpr.keyPath) + if err != nil { + return err + } + kpr.certMu.Lock() + defer kpr.certMu.Unlock() + kpr.cert = &newCert + return nil +} + +func (kpr *keypairReloader) GetCertificateFunc() func(*tls.ClientHelloInfo) (*tls.Certificate, error) { + return func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) { + kpr.certMu.RLock() + defer kpr.certMu.RUnlock() + return kpr.cert, nil + } +} + +func WithKeyPairReloader(certPath, keyPath string) func(*http.Server) { + return func(srv *http.Server) { + kpr, err := NewKeypairReloader(certPath, keyPath) + if err != nil { + log.Fatal(err) + } + srv.TLSConfig.GetCertificate = kpr.GetCertificateFunc() + } +} diff --git a/server.go b/server.go index f677e9f..69470f0 100644 --- a/server.go +++ b/server.go @@ -6,7 +6,9 @@ import ( "time" ) -func New(handler http.Handler, serverAddress string) *http.Server { +type Option func(*http.Server) + +func New(handler http.Handler, serverAddress string, options ...Option) *http.Server { // See https://blog.cloudflare.com/exposing-go-on-the-internet/ for details // about these settings tlsConfig := &tls.Config{ @@ -38,5 +40,8 @@ func New(handler http.Handler, serverAddress string) *http.Server { TLSConfig: tlsConfig, Handler: handler, } + for _, o := range options { + o(srv) + } return srv } -- 2.45.2