Skip to content
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

Sidecar: Loads the TLS certificate during startup. #5995

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re

- [#5990](https://github.com/thanos-io/thanos/pull/5990) Cache/Redis: add support for Redis Sentinel via new option `master_name`.

### Fixed
- [#5995] (https://github.com/thanos-io/thanos/pull/5993) Sidecar: Loads the TLS certificate during startup.

## [v0.30.0](https://github.com/thanos-io/thanos/tree/release-0.30) - in progress.

### Fixed
Expand Down
16 changes: 11 additions & 5 deletions pkg/tls/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
)

// NewServerConfig provides new server TLS configuration.
func NewServerConfig(logger log.Logger, cert, key, clientCA string) (*tls.Config, error) {
if key == "" && cert == "" {
func NewServerConfig(logger log.Logger, certPath, keyPath, clientCA string) (*tls.Config, error) {
if keyPath == "" && certPath == "" {
if clientCA != "" {
return nil, errors.New("when a client CA is used a server key and certificate must also be provided")
}
Expand All @@ -29,17 +29,23 @@ func NewServerConfig(logger log.Logger, cert, key, clientCA string) (*tls.Config

level.Info(logger).Log("msg", "enabling server side TLS")

if key == "" || cert == "" {
if keyPath == "" || certPath == "" {
return nil, errors.New("both server key and certificate must be provided")
}

tlsCfg := &tls.Config{
MinVersion: tls.VersionTLS13,
}
// Certificate is loaded during server startup to check for any errors.
certificate, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, errors.Wrap(err, "server credentials")
}

mngr := &serverTLSManager{
srvCertPath: cert,
srvKeyPath: key,
srvCertPath: certPath,
srvKeyPath: keyPath,
srvCert: &certificate,
}

tlsCfg.GetCertificate = mngr.getCertificate
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,15 @@ type ecServer struct {
func (s *ecServer) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
return &pb.EchoResponse{Message: req.Message}, nil
}

func TestInvalidCertAndKey(t *testing.T) {
defer leaktest.CheckTimeout(t, 10*time.Second)()
logger := log.NewLogfmtLogger(os.Stderr)
tmpDirSrv := t.TempDir()
caSrv := filepath.Join(tmpDirSrv, "ca")
certSrv := filepath.Join(tmpDirSrv, "cert")
keySrv := filepath.Join(tmpDirSrv, "key")
// Certificate and key are not present in the above path
_, err := thTLS.NewServerConfig(logger, certSrv, keySrv, caSrv)
testutil.NotOk(t, err)
}