Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server+runtime: add TLS cert refreshing (#4107)
This adds a new flag to `opa run`, intended for server usage with HTTPS listeners: `--tls-cert-refresh-period`. If used with a positive duration, such as "5m" (5 minutes), "24h", etc, the server will track the certificate and key files' contents. When their content changes, the certificates will be reloaded. On an error in reloading, it will log (info) the error and try again in the next round. Fixes #2500. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
- Loading branch information
Showing
14 changed files
with
488 additions
and
34 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
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,76 @@ | ||
// Copyright 2021 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
package server | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"crypto/tls" | ||
"io" | ||
"os" | ||
"time" | ||
|
||
"github.com/open-policy-agent/opa/logging" | ||
) | ||
|
||
func (s *Server) getCertificate(h *tls.ClientHelloInfo) (*tls.Certificate, error) { | ||
s.certMtx.RLock() | ||
defer s.certMtx.RUnlock() | ||
return s.cert, nil | ||
} | ||
|
||
func (s *Server) certLoop(logger logging.Logger) Loop { | ||
return func() error { | ||
for range time.NewTicker(s.certRefresh).C { | ||
certHash, err := hash(s.certFile) | ||
if err != nil { | ||
logger.Info("Failed to refresh server certificate: %s.", err.Error()) | ||
continue | ||
} | ||
certKeyHash, err := hash(s.certKeyFile) | ||
if err != nil { | ||
logger.Info("Failed to refresh server certificate: %s.", err.Error()) | ||
continue | ||
} | ||
|
||
s.certMtx.Lock() | ||
|
||
different := !bytes.Equal(s.certFileHash, certHash) || | ||
!bytes.Equal(s.certKeyFileHash, certKeyHash) | ||
|
||
if different { // load and store | ||
newCert, err := tls.LoadX509KeyPair(s.certFile, s.certKeyFile) | ||
if err != nil { | ||
logger.Info("Failed to refresh server certificate: %s.", err.Error()) | ||
s.certMtx.Unlock() | ||
continue | ||
} | ||
s.cert = &newCert | ||
s.certFileHash = certHash | ||
s.certKeyFileHash = certKeyHash | ||
logger.Debug("Refreshed server certificate.") | ||
} | ||
|
||
s.certMtx.Unlock() | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func hash(file string) ([]byte, error) { | ||
f, err := os.Open(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
h := sha256.New() | ||
if _, err := io.Copy(h, f); err != nil { | ||
return nil, err | ||
} | ||
|
||
return h.Sum(nil), nil | ||
} |
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
Oops, something went wrong.