-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03224ae
commit c1502bb
Showing
3 changed files
with
495 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
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" | ||
"hash/fnv" | ||
"io/ioutil" | ||
"sync" | ||
"time" | ||
|
||
"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 | ||
interval time.Duration | ||
|
||
mu sync.RWMutex // protects the fields below | ||
cert *tls.Certificate | ||
hash uint64 | ||
} | ||
|
||
func NewCertReloader(certPath, keyPath string, interval time.Duration) (*CertReloader, error) { | ||
r := &CertReloader{ | ||
certPath: certPath, | ||
keyPath: keyPath, | ||
interval: interval, | ||
} | ||
|
||
if err := r.reload(); err != nil { | ||
return nil, fmt.Errorf("error loading certificates: %v", err) | ||
} | ||
|
||
return r, nil | ||
} | ||
|
||
// 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 { | ||
t := time.NewTicker(r.interval) | ||
|
||
for { | ||
select { | ||
case <-t.C: | ||
case <-ctx.Done(): | ||
return nil | ||
} | ||
|
||
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() | ||
|
||
var err error | ||
|
||
crt, err := ioutil.ReadFile(r.certPath) | ||
key, err := ioutil.ReadFile(r.keyPath) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error loading certificate: %v", err) | ||
} | ||
|
||
h := fnv.New64a() | ||
_, err = h.Write(crt) | ||
_, err = h.Write(key) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error hashing certificate: %v", err) | ||
} | ||
|
||
newHash := h.Sum64() | ||
if newHash == r.hash { | ||
return nil | ||
} | ||
|
||
cert, err := tls.X509KeyPair(crt, key) | ||
if err != nil { | ||
return fmt.Errorf("error parsing certificate: %v", err) | ||
} | ||
|
||
glog.V(4).Info("reloading key ", r.keyPath, " certificate ", r.certPath) | ||
|
||
r.cert = &cert | ||
r.hash = newHash | ||
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.