@@ -24,7 +24,9 @@ import (
24
24
"encoding/pem"
25
25
"io/ioutil"
26
26
"math/big"
27
+ "net/http"
27
28
"os"
29
+ "path/filepath"
28
30
"time"
29
31
30
32
"github.com/go-sql-driver/mysql"
@@ -215,6 +217,56 @@ func (ts *tidbTestSuite) TestStatusAPIWithTLS(c *C) {
215
217
server .Close ()
216
218
}
217
219
220
+ func (ts * tidbTestSuite ) TestStatusAPIWithTLSCNCheck (c * C ) {
221
+ c .Skip ("need add ca-tidb-test-1.crt to OS" )
222
+ root := filepath .Join (os .Getenv ("GOPATH" ), "/src/github.com/pingcap/tidb" )
223
+ ca := filepath .Join (root , "/tests/cncheckcert/ca-tidb-test-1.crt" )
224
+
225
+ cli := newTestServerClient ()
226
+ cli .statusScheme = "https"
227
+ cfg := config .NewConfig ()
228
+ cfg .Port = cli .port
229
+ cfg .Status .StatusPort = cli .statusPort
230
+ cfg .Security .ClusterSSLCA = ca
231
+ cfg .Security .ClusterSSLCert = filepath .Join (root , "/tests/cncheckcert/server-cert.pem" )
232
+ cfg .Security .ClusterSSLKey = filepath .Join (root , "/tests/cncheckcert/server-key.pem" )
233
+ cfg .Security .ClusterVerifyCN = []string {"tidb-client-2" }
234
+ server , err := NewServer (cfg , ts .tidbdrv )
235
+ c .Assert (err , IsNil )
236
+ go server .Run ()
237
+ time .Sleep (time .Millisecond * 100 )
238
+
239
+ hc := newTLSHttpClient (c , ca ,
240
+ filepath .Join (root , "/tests/cncheckcert/client-cert-1.pem" ),
241
+ filepath .Join (root , "/tests/cncheckcert/client-key-1.pem" ),
242
+ )
243
+ _ , err = hc .Get (cli .statusURL ("/status" ))
244
+ c .Assert (err , NotNil )
245
+
246
+ hc = newTLSHttpClient (c , ca ,
247
+ filepath .Join (root , "/tests/cncheckcert/client-cert-2.pem" ),
248
+ filepath .Join (root , "/tests/cncheckcert/client-key-2.pem" ),
249
+ )
250
+ _ , err = hc .Get (cli .statusURL ("/status" ))
251
+ c .Assert (err , IsNil )
252
+ }
253
+
254
+ func newTLSHttpClient (c * C , caFile , certFile , keyFile string ) * http.Client {
255
+ cert , err := tls .LoadX509KeyPair (certFile , keyFile )
256
+ c .Assert (err , IsNil )
257
+ caCert , err := ioutil .ReadFile (caFile )
258
+ c .Assert (err , IsNil )
259
+ caCertPool := x509 .NewCertPool ()
260
+ caCertPool .AppendCertsFromPEM (caCert )
261
+ tlsConfig := & tls.Config {
262
+ Certificates : []tls.Certificate {cert },
263
+ RootCAs : caCertPool ,
264
+ InsecureSkipVerify : true ,
265
+ }
266
+ tlsConfig .BuildNameToCertificate ()
267
+ return & http.Client {Transport : & http.Transport {TLSClientConfig : tlsConfig }}
268
+ }
269
+
218
270
func (ts * tidbTestSuite ) TestMultiStatements (c * C ) {
219
271
c .Parallel ()
220
272
ts .runTestMultiStatements (c )
0 commit comments