@@ -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
0 commit comments