Skip to content

Commit 98dd2f2

Browse files
committed
security: allow skip-ca without certificates to continue building tls config (#951)
1 parent 9aba8c4 commit 98dd2f2

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

lib/util/security/cert.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,7 @@ func (ci *CertInfo) buildClientConfig(lg *zap.Logger) (*tls.Config, error) {
234234
lg.Warn("specified auto-certs in a client tls config, ignored")
235235
}
236236

237-
if !cfg.HasCA() {
238-
if cfg.SkipCA {
239-
// still enable TLS without verify server certs
240-
return &tls.Config{
241-
InsecureSkipVerify: true,
242-
MinVersion: GetMinTLSVer(cfg.MinTLSVersion, lg),
243-
}, nil
244-
}
237+
if !cfg.HasCA() && !cfg.SkipCA {
245238
lg.Debug("no CA to verify server connections, disable TLS")
246239
return nil, nil
247240
}
@@ -251,30 +244,32 @@ func (ci *CertInfo) buildClientConfig(lg *zap.Logger) (*tls.Config, error) {
251244
GetCertificate: ci.getCert,
252245
GetClientCertificate: ci.getClientCert,
253246
InsecureSkipVerify: true,
254-
VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
255-
return ci.verifyCA(rawCerts)
256-
},
257247
}
258248

259-
caPEM, err := os.ReadFile(cfg.CA)
260-
if err != nil {
261-
return nil, err
262-
}
263-
certPool := x509.NewCertPool()
264-
if !certPool.AppendCertsFromPEM(caPEM) {
265-
return nil, errors.New("failed to append ca certs")
249+
if cfg.HasCA() {
250+
tcfg.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
251+
return ci.verifyCA(rawCerts)
252+
}
253+
caPEM, err := os.ReadFile(cfg.CA)
254+
if err != nil {
255+
return nil, err
256+
}
257+
certPool := x509.NewCertPool()
258+
if !certPool.AppendCertsFromPEM(caPEM) {
259+
return nil, errors.New("failed to append ca certs")
260+
}
261+
ci.ca.Store(certPool)
262+
tcfg.RootCAs = certPool
266263
}
267-
ci.ca.Store(certPool)
268-
tcfg.RootCAs = certPool
269264

270-
if !cfg.HasCert() {
265+
if cfg.Cert == "" || cfg.Key == "" {
271266
lg.Debug("no certificates, server may reject the connection")
272267
return tcfg, nil
273268
}
274269

275270
cert, err := tls.LoadX509KeyPair(cfg.Cert, cfg.Key)
276271
if err != nil {
277-
return nil, errors.WithStack(err)
272+
return nil, err
278273
}
279274
ci.cert.Store(&cert)
280275

lib/util/security/cert_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func TestCertServer(t *testing.T) {
187187
require.Nil(t, c.RootCAs)
188188
require.Nil(t, ci.cert.Load())
189189
require.Equal(t, tls.VersionTLS12, int(c.MinVersion))
190+
require.NotNil(t, c.GetClientCertificate, "skip-ca should set GetClientCertificate")
190191
},
191192
},
192193
{
@@ -336,6 +337,7 @@ func TestSetConfig(t *testing.T) {
336337
require.NoError(t, err)
337338
require.NotNil(t, tcfg)
338339
require.True(t, tcfg.InsecureSkipVerify)
340+
require.NotNil(t, tcfg.GetClientCertificate, "skip-ca should set GetClientCertificate")
339341

340342
cfg = config.TLSConfig{
341343
SkipCA: false,

lib/util/security/tls.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -204,28 +204,24 @@ func CreateTLSConfigForTest() (serverTLSConf *tls.Config, clientTLSConf *tls.Con
204204

205205
func BuildClientTLSConfig(logger *zap.Logger, cfg config.TLSConfig) (*tls.Config, error) {
206206
logger = logger.With(zap.String("tls", "client"))
207-
if !cfg.HasCA() {
208-
if cfg.SkipCA {
209-
// still enable TLS without verify server certs
210-
return &tls.Config{
211-
InsecureSkipVerify: true,
212-
MinVersion: tls.VersionTLS11,
213-
}, nil
214-
}
207+
if !cfg.HasCA() && !cfg.SkipCA {
215208
logger.Info("no CA to verify server connections, disable TLS")
216209
return nil, nil
217210
}
218211

219212
tcfg := &tls.Config{
220213
MinVersion: tls.VersionTLS11,
214+
InsecureSkipVerify: cfg.SkipCA,
221215
}
222-
tcfg.RootCAs = x509.NewCertPool()
223-
certBytes, err := os.ReadFile(cfg.CA)
224-
if err != nil {
225-
return nil, errors.Errorf("failed to read CA: %w", err)
226-
}
227-
if !tcfg.RootCAs.AppendCertsFromPEM(certBytes) {
228-
return nil, errors.Errorf("failed to append CA")
216+
if cfg.HasCA() {
217+
tcfg.RootCAs = x509.NewCertPool()
218+
certBytes, err := os.ReadFile(cfg.CA)
219+
if err != nil {
220+
return nil, errors.Errorf("failed to read CA: %w", err)
221+
}
222+
if !tcfg.RootCAs.AppendCertsFromPEM(certBytes) {
223+
return nil, errors.Errorf("failed to append CA")
224+
}
229225
}
230226

231227
if !cfg.HasCert() {

0 commit comments

Comments
 (0)