Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pavol Loffay <ploffay@redhat.com>
  • Loading branch information
pavolloffay committed Aug 18, 2020
1 parent 3b5408d commit cbd3917
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
32 changes: 17 additions & 15 deletions pkg/config/tlscfg/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,8 @@ var systemCertPool = x509.SystemCertPool // to allow overriding in unit test

// Config loads TLS certificates and returns a TLS Config.
func (p Options) Config(logger *zap.Logger) (*tls.Config, error) {
w, err := newWatchCerts(p, logger)
if err != nil {
return nil, err
}
p.watcher = w

certPool, err := p.loadCertPool(logger)
certPool, err := p.loadCertPool()
if err != nil {
return nil, fmt.Errorf("failed to load CA CertPool: %w", err)
}
Expand All @@ -57,6 +52,20 @@ func (p Options) Config(logger *zap.Logger) (*tls.Config, error) {
ServerName: p.ServerName,
InsecureSkipVerify: p.SkipHostVerify,
}
if p.ClientCAPath != "" {
certPool := x509.NewCertPool()
if err := addCertToPool(p.ClientCAPath, certPool); err != nil {
return nil, err
}
tlsCfg.ClientCAs = certPool
tlsCfg.ClientAuth = tls.RequireAndVerifyClientCert
}

w, err := newWatchCerts(p, logger)
if err != nil {
return nil, err
}
p.watcher = w

if (p.CertPath == "" && p.KeyPath != "") || (p.CertPath != "" && p.KeyPath == "") {
return nil, fmt.Errorf("for client auth via TLS, either both client certificate and key must be supplied, or neither")
Expand All @@ -71,19 +80,11 @@ func (p Options) Config(logger *zap.Logger) (*tls.Config, error) {
}
}

if p.ClientCAPath != "" {
certPool := x509.NewCertPool()
if err := addCertToPool(p.ClientCAPath, certPool); err != nil {
return nil, err
}
tlsCfg.ClientCAs = certPool
tlsCfg.ClientAuth = tls.RequireAndVerifyClientCert
}
go p.watcher.watchChangesLoop(tlsCfg.RootCAs, tlsCfg.ClientCAs)
return tlsCfg, nil
}

func (p Options) loadCertPool(logger *zap.Logger) (*x509.CertPool, error) {
func (p Options) loadCertPool() (*x509.CertPool, error) {
if len(p.CAPath) == 0 { // no truststore given, use SystemCertPool
certPool, err := systemCertPool()
if err != nil {
Expand All @@ -102,6 +103,7 @@ func (p Options) loadCertPool(logger *zap.Logger) (*x509.CertPool, error) {
func addCertToPool(caPath string, certPool *x509.CertPool) error {
caPEM, err := ioutil.ReadFile(filepath.Clean(caPath))
if err != nil {
fmt.Println("AAAA here")
return fmt.Errorf("failed to load CA %s: %w", caPath, err)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/config/tlscfg/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package tlscfg
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"path/filepath"
"sync"
Expand All @@ -41,7 +42,7 @@ func newWatchCerts(opts Options, logger *zap.Logger) (*watchCerts, error) {
// load certs at startup to catch missing certs error early
c, err := tls.LoadX509KeyPair(filepath.Clean(opts.CertPath), filepath.Clean(opts.KeyPath))
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to load server TLS cert and key: %w", err)
}
cert = c
}
Expand Down

0 comments on commit cbd3917

Please sign in to comment.