Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: fix tls setup and error log (#15287) #15292

Merged
merged 3 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (s *Security) ToTLSConfig() (*tls.Config, error) {
tlsConfig = &tls.Config{
Certificates: certificates,
RootCAs: certPool,
ClientCAs: certPool,
}
}

Expand Down
1 change: 1 addition & 0 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func (s *Server) startHTTPServer() {
return
}
tlsConfig = s.setCNChecker(tlsConfig)
logutil.Logger(context.Background()).Info("HTTP/gRPC status server secure connection is enabled", zap.Bool("CN verification enabled", tlsConfig.VerifyPeerCertificate != nil))
ln = tls.NewListener(ln, tlsConfig)
}

Expand Down
9 changes: 6 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ func NewServer(cfg *config.Config, driver IDriver) (*Server, error) {
if err != nil {
logutil.Logger(context.Background()).Error("secure connection cert/key/ca load fail", zap.Error(err))
}
logutil.Logger(context.Background()).Info("secure connection is enabled", zap.Bool("client verification enabled", len(variable.SysVars["ssl_ca"].Value) > 0))
setSSLVariable(s.cfg.Security.SSLCA, s.cfg.Security.SSLKey, s.cfg.Security.SSLCert)
atomic.StorePointer(&s.tlsConfig, unsafe.Pointer(tlsConfig))
if tlsConfig != nil {
setSSLVariable(s.cfg.Security.SSLCA, s.cfg.Security.SSLKey, s.cfg.Security.SSLCert)
atomic.StorePointer(&s.tlsConfig, unsafe.Pointer(tlsConfig))
logutil.Logger(context.Background()).Info("mysql protocol server secure connection is enabled", zap.Bool("client verification enabled", len(variable.SysVars["ssl_ca"].Value) > 0))
}

setSystemTimeZoneVariable()

Expand Down Expand Up @@ -369,6 +371,7 @@ func (s *Server) Close() {
func (s *Server) onConn(conn *clientConn) {
ctx := logutil.WithConnID(context.Background(), conn.connectionID)
if err := conn.handshake(ctx); err != nil {
terror.Log(err)
if plugin.IsEnable(plugin.Audit) {
conn.ctx.GetSessionVars().ConnectionInfo = conn.connectInfo()
}
Expand Down
42 changes: 30 additions & 12 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,32 +161,50 @@ func (ts *TidbTestSuite) TestStatusAPI(c *C) {
}

func (ts *TidbTestSuite) TestStatusAPIWithTLSCNCheck(c *C) {
c.Skip("need add ca-tidb-test-1.crt to OS")
root := filepath.Join(os.Getenv("GOPATH"), "/src/github.com/pingcap/tidb")
ca := filepath.Join(root, "/tests/cncheckcert/ca-tidb-test-1.crt")
caPath := filepath.Join(os.TempDir(), "ca-cert-cn.pem")
serverKeyPath := filepath.Join(os.TempDir(), "server-key-cn.pem")
serverCertPath := filepath.Join(os.TempDir(), "server-cert-cn.pem")
client1KeyPath := filepath.Join(os.TempDir(), "client-key-cn-check-a.pem")
client1CertPath := filepath.Join(os.TempDir(), "client-cert-cn-check-a.pem")
client2KeyPath := filepath.Join(os.TempDir(), "client-key-cn-check-b.pem")
client2CertPath := filepath.Join(os.TempDir(), "client-cert-cn-check-b.pem")

statusURL := fmt.Sprintf("%s://localhost:%d%s", "https", 4100, "/status")

caCert, caKey, err := generateCert(0, "TiDB CA CN CHECK", nil, nil, filepath.Join(os.TempDir(), "ca-key-cn.pem"), caPath)
c.Assert(err, IsNil)
_, _, err = generateCert(1, "tidb-server-cn-check", caCert, caKey, serverKeyPath, serverCertPath)
c.Assert(err, IsNil)
_, _, err = generateCert(2, "tidb-client-cn-check-a", caCert, caKey, client1KeyPath, client1CertPath, func(c *x509.Certificate) {
c.Subject.CommonName = "tidb-client-1"
})
c.Assert(err, IsNil)
_, _, err = generateCert(3, "tidb-client-cn-check-b", caCert, caKey, client2KeyPath, client2CertPath, func(c *x509.Certificate) {
c.Subject.CommonName = "tidb-client-2"
})
c.Assert(err, IsNil)

cfg := config.NewConfig()
cfg.Status.StatusPort = 4100
cfg.Security.ClusterSSLCA = ca
cfg.Security.ClusterSSLCert = filepath.Join(root, "/tests/cncheckcert/server-cert.pem")
cfg.Security.ClusterSSLKey = filepath.Join(root, "/tests/cncheckcert/server-key.pem")
cfg.Security.ClusterSSLCA = caPath
cfg.Security.ClusterSSLCert = serverCertPath
cfg.Security.ClusterSSLKey = serverKeyPath
cfg.Security.ClusterVerifyCN = []string{"tidb-client-2"}
server, err := NewServer(cfg, ts.tidbdrv)
c.Assert(err, IsNil)
go server.Run()
time.Sleep(time.Millisecond * 100)

hc := newTLSHttpClient(c, ca,
filepath.Join(root, "/tests/cncheckcert/client-cert-1.pem"),
filepath.Join(root, "/tests/cncheckcert/client-key-1.pem"),
hc := newTLSHttpClient(c, caPath,
client1CertPath,
client1KeyPath,
)
_, err = hc.Get(statusURL)
c.Assert(err, NotNil)

hc = newTLSHttpClient(c, ca,
filepath.Join(root, "/tests/cncheckcert/client-cert-2.pem"),
filepath.Join(root, "/tests/cncheckcert/client-key-2.pem"),
hc = newTLSHttpClient(c, caPath,
client2CertPath,
client2KeyPath,
)
_, err = hc.Get(statusURL)
c.Assert(err, IsNil)
Expand Down