Skip to content

Commit 427c582

Browse files
committed
etcdmain, pkg: CN based auth for inter peer connection
This commit adds an authentication mechanism to inter peer connection (rafthttp). If the cert based peer auth is enabled and a new option `--peer-cert-allowed-cn` is passed, an etcd process denies a peer connection whose CN doesn't match.
1 parent 554298d commit 427c582

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

etcdmain/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func newConfig() *config {
184184
fs.StringVar(&cfg.PeerTLSInfo.TrustedCAFile, "peer-trusted-ca-file", "", "Path to the peer server TLS trusted CA file.")
185185
fs.BoolVar(&cfg.PeerAutoTLS, "peer-auto-tls", false, "Peer TLS using generated certificates")
186186
fs.StringVar(&cfg.PeerTLSInfo.CRLFile, "peer-crl-file", "", "Path to the peer certificate revocation list file.")
187+
fs.StringVar(&cfg.PeerTLSInfo.AllowedCN, "peer-cert-allowed-cn", "", "Allowed CN for inter peer authentication.")
187188

188189
// logging
189190
fs.BoolVar(&cfg.Debug, "debug", false, "Enable debug-level logging for etcd.")

pkg/transport/listener.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"crypto/x509"
2323
"crypto/x509/pkix"
2424
"encoding/pem"
25+
"errors"
2526
"fmt"
2627
"math/big"
2728
"net"
@@ -76,6 +77,9 @@ type TLSInfo struct {
7677
// parseFunc exists to simplify testing. Typically, parseFunc
7778
// should be left nil. In that case, tls.X509KeyPair will be used.
7879
parseFunc func([]byte, []byte) (tls.Certificate, error)
80+
81+
// AllowedCN is a CN which must be provided by a client
82+
AllowedCN string
7983
}
8084

8185
func (info TLSInfo) String() string {
@@ -174,6 +178,23 @@ func (info TLSInfo) baseConfig() (*tls.Config, error) {
174178
MinVersion: tls.VersionTLS12,
175179
ServerName: info.ServerName,
176180
}
181+
182+
if info.AllowedCN != "" {
183+
cfg.VerifyPeerCertificate = func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
184+
for _, chains := range verifiedChains {
185+
for _, chain := range chains {
186+
if info.AllowedCN == chain.Subject.CommonName {
187+
return nil
188+
} else {
189+
return fmt.Errorf("CommonName authentication failed (allowed: %s, client: %s)", info.AllowedCN, chains[0].Subject.CommonName)
190+
}
191+
192+
}
193+
}
194+
return errors.New("CommonName authentication failed")
195+
}
196+
}
197+
177198
// this only reloads certs when there's a client request
178199
// TODO: support server-side refresh (e.g. inotify, SIGHUP), caching
179200
cfg.GetCertificate = func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {

0 commit comments

Comments
 (0)