@@ -85,6 +85,7 @@ type TestInputs struct {
8585 CurvePreferences []tls.CurveID
8686 Username string
8787 Password string
88+ ClientCertificate string
8889}
8990
9091func TestYAMLFiles (t * testing.T ) {
@@ -287,6 +288,33 @@ func TestServerBehaviour(t *testing.T) {
287288 UseTLSClient : true ,
288289 ExpectedError : ErrorMap ["Bad certificate" ],
289290 },
291+ {
292+ Name : `valid tls config yml and tls client with RequireAnyClientCert (present certificate)` ,
293+ YAMLConfigPath : "testdata/tls_config_noAuth.requireanyclientcert.good.yml" ,
294+ UseTLSClient : true ,
295+ ClientCertificate : "client_selfsigned" ,
296+ ExpectedError : nil ,
297+ },
298+ {
299+ Name : `valid tls config yml and tls client with RequireAndVerifyClientCert` ,
300+ YAMLConfigPath : "testdata/tls_config_noAuth.requireandverifyclientcert.good.yml" ,
301+ UseTLSClient : true ,
302+ ExpectedError : ErrorMap ["Bad certificate" ],
303+ },
304+ {
305+ Name : `valid tls config yml and tls client with RequireAndVerifyClientCert (present certificate)` ,
306+ YAMLConfigPath : "testdata/tls_config_noAuth.requireandverifyclientcert.good.yml" ,
307+ UseTLSClient : true ,
308+ ClientCertificate : "client_selfsigned" ,
309+ ExpectedError : nil ,
310+ },
311+ {
312+ Name : `valid tls config yml and tls client with RequireAndVerifyClientCert (present wrong certificate)` ,
313+ YAMLConfigPath : "testdata/tls_config_noAuth.requireandverifyclientcert.good.yml" ,
314+ UseTLSClient : true ,
315+ ClientCertificate : "client2_selfsigned" ,
316+ ExpectedError : ErrorMap ["Bad certificate" ],
317+ }
290318 }
291319 for _ , testInputs := range testTables {
292320 t .Run (testInputs .Name , testInputs .Test )
@@ -330,7 +358,7 @@ func TestConfigReloading(t *testing.T) {
330358 recordConnectionError (err )
331359 }()
332360
333- client := getTLSClient ()
361+ client := getTLSClient ("" )
334362
335363 TestClientConnection := func () error {
336364 time .Sleep (250 * time .Millisecond )
@@ -404,7 +432,7 @@ func (test *TestInputs) Test(t *testing.T) {
404432 var client * http.Client
405433 var proto string
406434 if test .UseTLSClient {
407- client = getTLSClient ()
435+ client = getTLSClient (test . ClientCertificate )
408436 t := client .Transport .(* http.Transport )
409437 t .TLSClientConfig .MaxVersion = test .ClientMaxTLSVersion
410438 if len (test .CipherSuites ) > 0 {
@@ -496,11 +524,23 @@ func (test *TestInputs) isCorrectError(returnedError error) bool {
496524 return true
497525}
498526
499- func getTLSClient () * http.Client {
527+ func getTLSClient (clientCertName string ) * http.Client {
500528 cert , err := ioutil .ReadFile ("testdata/tls-ca-chain.pem" )
501529 if err != nil {
502530 panic ("Unable to start TLS client. Check cert path" )
503531 }
532+
533+ var clientCertficate tls.Certificate
534+ if clientCertName != "" {
535+ clientCertficate , err = tls .LoadX509KeyPair (
536+ "testdata/" + clientCertName + ".pem" ,
537+ "testdata/" + clientCertName + ".key" ,
538+ )
539+ if err != nil {
540+ panic (fmt .Sprintf ("failed to load client certificate: %v" , err ))
541+ }
542+ }
543+
504544 client := & http.Client {
505545 Transport : & http.Transport {
506546 TLSClientConfig : & tls.Config {
@@ -509,6 +549,9 @@ func getTLSClient() *http.Client {
509549 caCertPool .AppendCertsFromPEM (cert )
510550 return caCertPool
511551 }(),
552+ GetClientCertificate : func (req * tls.CertificateRequestInfo ) (* tls.Certificate , error ) {
553+ return & clientCertficate , nil
554+ },
512555 },
513556 },
514557 }
0 commit comments