Skip to content

Commit

Permalink
pkg, clientv3, etcdmain: let grpcproxy rise an error when its cert ha…
Browse files Browse the repository at this point in the history
…s non empty CN

Fix etcd-io#9521
  • Loading branch information
mitake committed Jan 24, 2019
1 parent fa521f4 commit 65887ae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion etcdmain/grpc_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func newTLS(ca, cert, key string) *transport.TLSInfo {
if ca == "" && cert == "" && key == "" {
return nil
}
return &transport.TLSInfo{TrustedCAFile: ca, CertFile: cert, KeyFile: key}
return &transport.TLSInfo{TrustedCAFile: ca, CertFile: cert, KeyFile: key, EmptyCN: true}
}

func mustListenCMux(lg *zap.Logger, tlsinfo *transport.TLSInfo) cmux.CMux {
Expand Down
1 change: 1 addition & 0 deletions pkg/tlsutil/tlsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func NewCertPool(CAFiles []string) (*x509.CertPool, error) {
if err != nil {
return nil, err
}

certPool.AddCert(cert)
}
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/transport/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ type TLSInfo struct {
// Logger logs TLS errors.
// If nil, all logs are discarded.
Logger *zap.Logger

// EmptyCN indicates that the cert must have empty CN.
// If true, ClientConfig() will return an error for a cert with non empty CN.
EmptyCN bool
}

func (info TLSInfo) String() string {
Expand Down Expand Up @@ -378,6 +382,28 @@ func (info TLSInfo) ClientConfig() (*tls.Config, error) {
if info.selfCert {
cfg.InsecureSkipVerify = true
}

if info.EmptyCN {
hasNonEmptyCN := false
cn := ""
tlsutil.NewCert(info.CertFile, info.KeyFile, func(certPEMBlock []byte, keyPEMBlock []byte) (tls.Certificate, error) {
var block *pem.Block
block, _ = pem.Decode(certPEMBlock)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return tls.Certificate{}, err
}
if len(cert.Subject.CommonName) != 0 {
hasNonEmptyCN = true
cn = cert.Subject.CommonName
}
return tls.X509KeyPair(certPEMBlock, keyPEMBlock)
})
if hasNonEmptyCN {
return nil, fmt.Errorf("cert has non empty Common Name (%s)", cn)
}
}

return cfg, nil
}

Expand Down

0 comments on commit 65887ae

Please sign in to comment.