mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
transport: catch new cert error
in pkg/transport, we should catch NewCert error.
This commit is contained in:
parent
a1ff0d5373
commit
826573586f
@ -438,7 +438,7 @@ func (info TLSInfo) ClientConfig() (*tls.Config, error) {
|
|||||||
if info.EmptyCN {
|
if info.EmptyCN {
|
||||||
hasNonEmptyCN := false
|
hasNonEmptyCN := false
|
||||||
cn := ""
|
cn := ""
|
||||||
tlsutil.NewCert(info.CertFile, info.KeyFile, func(certPEMBlock []byte, keyPEMBlock []byte) (tls.Certificate, error) {
|
_, err := tlsutil.NewCert(info.CertFile, info.KeyFile, func(certPEMBlock []byte, keyPEMBlock []byte) (tls.Certificate, error) {
|
||||||
var block *pem.Block
|
var block *pem.Block
|
||||||
block, _ = pem.Decode(certPEMBlock)
|
block, _ = pem.Decode(certPEMBlock)
|
||||||
cert, err := x509.ParseCertificate(block.Bytes)
|
cert, err := x509.ParseCertificate(block.Bytes)
|
||||||
@ -451,6 +451,9 @@ func (info TLSInfo) ClientConfig() (*tls.Config, error) {
|
|||||||
}
|
}
|
||||||
return tls.X509KeyPair(certPEMBlock, keyPEMBlock)
|
return tls.X509KeyPair(certPEMBlock, keyPEMBlock)
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if hasNonEmptyCN {
|
if hasNonEmptyCN {
|
||||||
return nil, fmt.Errorf("cert has non empty Common Name (%s): %s", cn, info.CertFile)
|
return nil, fmt.Errorf("cert has non empty Common Name (%s): %s", cn, info.CertFile)
|
||||||
}
|
}
|
||||||
|
@ -292,14 +292,28 @@ func TestTLSInfoParseFuncError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer del()
|
defer del()
|
||||||
|
|
||||||
tlsinfo.parseFunc = fakeCertificateParserFunc(tls.Certificate{}, errors.New("fake"))
|
tests := []struct {
|
||||||
|
info TLSInfo
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
info: *tlsinfo,
|
||||||
|
},
|
||||||
|
|
||||||
if _, err = tlsinfo.ServerConfig(); err == nil {
|
{
|
||||||
t.Errorf("expected non-nil error from ServerConfig()")
|
info: TLSInfo{CertFile: "", KeyFile: "", TrustedCAFile: tlsinfo.CertFile, EmptyCN: true},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = tlsinfo.ClientConfig(); err == nil {
|
for i, tt := range tests {
|
||||||
t.Errorf("expected non-nil error from ClientConfig()")
|
tt.info.parseFunc = fakeCertificateParserFunc(tls.Certificate{}, errors.New("fake"))
|
||||||
|
|
||||||
|
if _, err = tt.info.ServerConfig(); err == nil {
|
||||||
|
t.Errorf("#%d: expected non-nil error from ServerConfig()", i)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = tt.info.ClientConfig(); err == nil {
|
||||||
|
t.Errorf("#%d: expected non-nil error from ClientConfig()", i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user