Skip to content
Open
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
39 changes: 17 additions & 22 deletions lib/util/security/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,7 @@ func (ci *CertInfo) buildClientConfig(lg *zap.Logger) (*tls.Config, error) {
lg.Warn("specified auto-certs in a client tls config, ignored")
}

if !cfg.HasCA() {
if cfg.SkipCA {
// still enable TLS without verify server certs
return &tls.Config{
InsecureSkipVerify: true,
MinVersion: GetMinTLSVer(cfg.MinTLSVersion, lg),
}, nil
}
if !cfg.HasCA() && !cfg.SkipCA {
lg.Debug("no CA to verify server connections, disable TLS")
return nil, nil
}
Expand All @@ -251,30 +244,32 @@ func (ci *CertInfo) buildClientConfig(lg *zap.Logger) (*tls.Config, error) {
GetCertificate: ci.getCert,
GetClientCertificate: ci.getClientCert,
InsecureSkipVerify: true,
VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
return ci.verifyCA(rawCerts)
},
}

caPEM, err := os.ReadFile(cfg.CA)
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(caPEM) {
return nil, errors.New("failed to append ca certs")
if cfg.HasCA() {
tcfg.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
return ci.verifyCA(rawCerts)
}
caPEM, err := os.ReadFile(cfg.CA)
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(caPEM) {
return nil, errors.New("failed to append ca certs")
}
ci.ca.Store(certPool)
tcfg.RootCAs = certPool
}
ci.ca.Store(certPool)
tcfg.RootCAs = certPool

if !cfg.HasCert() {
if cfg.Cert == "" || cfg.Key == "" {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject incomplete client cert/key configuration

Treating cfg.Cert == "" || cfg.Key == "" as success silently accepts a half-configured client certificate. With CA configured, this changes fail-fast behavior into runtime handshake failures (no client cert is sent) and hides configuration mistakes during reload. Return an error when only one of cert or key is set so misconfiguration is surfaced immediately.

Useful? React with 👍 / 👎.

lg.Debug("no certificates, server may reject the connection")
return tcfg, nil
Comment on lines +265 to 267

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Clear cached cert when client cert config is removed

This return path exits without resetting ci.cert. Because this change now lets skip-ca/no-CA clients build a TLS config with GetClientCertificate, reloading from a config that previously had cert/key to one that removes them will still present the old certificate on new connections. That makes certificate revocation via config reload ineffective until restart; clear the cached cert before returning when cert/key are absent.

Useful? React with 👍 / 👎.

}

cert, err := tls.LoadX509KeyPair(cfg.Cert, cfg.Key)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
ci.cert.Store(&cert)

Expand Down
2 changes: 2 additions & 0 deletions lib/util/security/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func TestCertServer(t *testing.T) {
require.Nil(t, c.RootCAs)
require.Nil(t, ci.cert.Load())
require.Equal(t, tls.VersionTLS12, int(c.MinVersion))
require.NotNil(t, c.GetClientCertificate, "skip-ca should set GetClientCertificate")
},
},
{
Expand Down Expand Up @@ -336,6 +337,7 @@ func TestSetConfig(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, tcfg)
require.True(t, tcfg.InsecureSkipVerify)
require.NotNil(t, tcfg.GetClientCertificate, "skip-ca should set GetClientCertificate")

cfg = config.TLSConfig{
SkipCA: false,
Expand Down
26 changes: 11 additions & 15 deletions lib/util/security/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,28 +204,24 @@ func CreateTLSConfigForTest() (serverTLSConf *tls.Config, clientTLSConf *tls.Con

func BuildClientTLSConfig(logger *zap.Logger, cfg config.TLSConfig) (*tls.Config, error) {
logger = logger.With(zap.String("tls", "client"))
if !cfg.HasCA() {
if cfg.SkipCA {
// still enable TLS without verify server certs
return &tls.Config{
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS11,
}, nil
}
if !cfg.HasCA() && !cfg.SkipCA {
logger.Info("no CA to verify server connections, disable TLS")
return nil, nil
}

tcfg := &tls.Config{
MinVersion: tls.VersionTLS11,
InsecureSkipVerify: cfg.SkipCA,
}
tcfg.RootCAs = x509.NewCertPool()
certBytes, err := os.ReadFile(cfg.CA)
if err != nil {
return nil, errors.Errorf("failed to read CA: %w", err)
}
if !tcfg.RootCAs.AppendCertsFromPEM(certBytes) {
return nil, errors.Errorf("failed to append CA")
if cfg.HasCA() {
tcfg.RootCAs = x509.NewCertPool()
certBytes, err := os.ReadFile(cfg.CA)
if err != nil {
return nil, errors.Errorf("failed to read CA: %w", err)
}
if !tcfg.RootCAs.AppendCertsFromPEM(certBytes) {
return nil, errors.Errorf("failed to append CA")
}
}

if !cfg.HasCert() {
Expand Down