forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert_test.go
710 lines (551 loc) · 20.7 KB
/
cert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
_ "crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"math/big"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/certs"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/test"
"github.com/TykTechnologies/tyk/user"
)
func getTLSClient(cert *tls.Certificate, caCert []byte) *http.Client {
// Setup HTTPS client
tlsConfig := &tls.Config{}
if cert != nil {
tlsConfig.Certificates = []tls.Certificate{*cert}
}
if len(caCert) > 0 {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.RootCAs = caCertPool
tlsConfig.BuildNameToCertificate()
} else {
tlsConfig.InsecureSkipVerify = true
}
transport := &http.Transport{TLSClientConfig: tlsConfig}
return &http.Client{Transport: transport}
}
func genCertificate(template *x509.Certificate) ([]byte, []byte, []byte, tls.Certificate) {
priv, _ := rsa.GenerateKey(rand.Reader, 512)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, _ := rand.Int(rand.Reader, serialNumberLimit)
template.SerialNumber = serialNumber
template.BasicConstraintsValid = true
template.NotBefore = time.Now()
template.NotAfter = template.NotBefore.Add(time.Hour)
derBytes, _ := x509.CreateCertificate(rand.Reader, template, template, &priv.PublicKey, priv)
var certPem, keyPem bytes.Buffer
pem.Encode(&certPem, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
pem.Encode(&keyPem, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
clientCert, _ := tls.X509KeyPair(certPem.Bytes(), keyPem.Bytes())
combinedPEM := bytes.Join([][]byte{certPem.Bytes(), keyPem.Bytes()}, []byte("\n"))
return certPem.Bytes(), keyPem.Bytes(), combinedPEM, clientCert
}
func genServerCertificate() ([]byte, []byte, []byte, tls.Certificate) {
certPem, privPem, combinedPEM, cert := genCertificate(&x509.Certificate{
DNSNames: []string{"localhost"},
IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::")},
})
return certPem, privPem, combinedPEM, cert
}
const (
internalTLSErr = "tls: internal error"
badcertErr = "tls: bad certificate"
)
func TestGatewayTLS(t *testing.T) {
// Configure server
serverCertPem, serverPrivPem, combinedPEM, _ := genServerCertificate()
dir, _ := ioutil.TempDir("", "certs")
defer os.RemoveAll(dir)
client := getTLSClient(nil, nil)
t.Run("Without certificates", func(t *testing.T) {
config.Global.HttpServerOptions.UseSSL = true
defer resetTestConfig()
ts := newTykTestServer()
defer ts.Close()
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
})
ts.Run(t, test.TestCase{ErrorMatch: internalTLSErr, Client: client})
})
t.Run("Legacy TLS certificate path", func(t *testing.T) {
certFilePath := filepath.Join(dir, "server.crt")
ioutil.WriteFile(certFilePath, serverCertPem, 0666)
certKeyPath := filepath.Join(dir, "server.key")
ioutil.WriteFile(certKeyPath, serverPrivPem, 0666)
config.Global.HttpServerOptions.Certificates = []config.CertData{{
Name: "localhost",
CertFile: certFilePath,
KeyFile: certKeyPath,
}}
config.Global.HttpServerOptions.UseSSL = true
defer resetTestConfig()
ts := newTykTestServer()
defer ts.Close()
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
})
ts.Run(t, test.TestCase{Code: 200, Client: client})
CertificateManager.FlushCache()
})
t.Run("File certificate path", func(t *testing.T) {
certPath := filepath.Join(dir, "server.pem")
ioutil.WriteFile(certPath, combinedPEM, 0666)
config.Global.HttpServerOptions.SSLCertificates = []string{certPath}
config.Global.HttpServerOptions.UseSSL = true
defer resetTestConfig()
ts := newTykTestServer()
defer ts.Close()
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
})
ts.Run(t, test.TestCase{Code: 200, Client: client})
CertificateManager.FlushCache()
})
t.Run("Redis certificate", func(t *testing.T) {
certID, err := CertificateManager.Add(combinedPEM, "")
if err != nil {
t.Fatal(err)
}
defer CertificateManager.Delete(certID)
config.Global.HttpServerOptions.SSLCertificates = []string{certID}
config.Global.HttpServerOptions.UseSSL = true
defer resetTestConfig()
ts := newTykTestServer()
defer ts.Close()
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
})
ts.Run(t, test.TestCase{Code: 200, Client: client})
CertificateManager.FlushCache()
})
}
func TestGatewayControlAPIMutualTLS(t *testing.T) {
// Configure server
serverCertPem, _, combinedPEM, _ := genServerCertificate()
config.Global.HttpServerOptions.UseSSL = true
config.Global.Security.ControlAPIUseMutualTLS = true
defer resetTestConfig()
dir, _ := ioutil.TempDir("", "certs")
defer func() {
os.RemoveAll(dir)
CertificateManager.FlushCache()
}()
clientCertPem, _, _, clientCert := genCertificate(&x509.Certificate{})
clientWithCert := getTLSClient(&clientCert, serverCertPem)
clientWithoutCert := getTLSClient(nil, nil)
t.Run("Separate domain", func(t *testing.T) {
certID, _ := CertificateManager.Add(combinedPEM, "")
defer CertificateManager.Delete(certID)
config.Global.ControlAPIHostname = "localhost"
config.Global.HttpServerOptions.SSLCertificates = []string{certID}
ts := newTykTestServer()
defer ts.Close()
defer func() {
CertificateManager.FlushCache()
config.Global.HttpServerOptions.SSLCertificates = nil
config.Global.Security.Certificates.ControlAPI = nil
}()
unknownErr := "x509: certificate signed by unknown authority"
badcertErr := "tls: bad certificate"
ts.Run(t, []test.TestCase{
// Should acess tyk without client certificates
{Client: clientWithoutCert},
// Should raise error for ControlAPI without certificate
{ControlRequest: true, ErrorMatch: unknownErr},
// Should raise error for for unknown certificate
{ControlRequest: true, ErrorMatch: badcertErr, Client: clientWithCert},
}...)
clientCertID, _ := CertificateManager.Add(clientCertPem, "")
defer CertificateManager.Delete(clientCertID)
config.Global.Security.Certificates.ControlAPI = []string{clientCertID}
// Should pass request with valid client cert
ts.Run(t, test.TestCase{
Path: "/tyk/certs", Code: 200, ControlRequest: true, AdminAuth: true, Client: clientWithCert,
})
})
t.Run("Same domain", func(t *testing.T) {
certID, _ := CertificateManager.Add(combinedPEM, "")
defer CertificateManager.Delete(certID)
config.Global.ControlAPIHostname = "localhost"
config.Global.HttpServerOptions.SSLCertificates = []string{certID}
defer func() {
config.Global.HttpServerOptions.SSLCertificates = nil
config.Global.Security.Certificates.ControlAPI = nil
CertificateManager.FlushCache()
}()
ts := newTykTestServer()
defer ts.Close()
certNotMatchErr := `Certificate with SHA256 ` + certs.HexSHA256(clientCert.Certificate[0]) + ` not allowed`
t.Run("Without or not valid certificates", func(t *testing.T) {
ts.Run(t, []test.TestCase{
// Should acess tyk without client certificates
{Client: clientWithoutCert},
// Error for client without certificate
{Path: "/tyk/certs", AdminAuth: true, Code: 403, BodyMatch: `"message":"Client TLS certificate is required"`, Client: clientWithoutCert},
// Error for client with unknown certificate
{Path: "/tyk/certs", AdminAuth: true, Code: 403, BodyMatch: `"message":"` + certNotMatchErr, Client: clientWithCert},
}...)
})
t.Run("Redis certificate", func(t *testing.T) {
clientCertID, _ := CertificateManager.Add(clientCertPem, "")
defer CertificateManager.Delete(clientCertID)
config.Global.Security.Certificates.ControlAPI = []string{clientCertID}
ts.Run(t, []test.TestCase{
{Path: "/tyk/certs", AdminAuth: true, Code: 200, Client: clientWithCert},
}...)
})
t.Run("File certificate", func(t *testing.T) {
certPath := filepath.Join(dir, "client.pem")
ioutil.WriteFile(certPath, clientCertPem, 0666)
config.Global.Security.Certificates.ControlAPI = []string{certPath}
ts.Run(t, []test.TestCase{
{Path: "/tyk/certs", AdminAuth: true, Code: 200, Client: clientWithCert},
}...)
})
})
}
func TestAPIMutualTLS(t *testing.T) {
// Configure server
serverCertPem, _, combinedPEM, _ := genServerCertificate()
certID, _ := CertificateManager.Add(combinedPEM, "")
defer CertificateManager.Delete(certID)
config.Global.EnableCustomDomains = true
config.Global.HttpServerOptions.UseSSL = true
config.Global.ListenPort = 0
config.Global.HttpServerOptions.SSLCertificates = []string{certID}
defer resetTestConfig()
ts := newTykTestServer()
defer ts.Close()
// Initialize client certificates
clientCertPem, _, _, clientCert := genCertificate(&x509.Certificate{})
t.Run("SNI and domain per API", func(t *testing.T) {
t.Run("API without mutual TLS", func(t *testing.T) {
client := getTLSClient(&clientCert, serverCertPem)
buildAndLoadAPI(func(spec *APISpec) {
spec.Domain = "localhost"
spec.Proxy.ListenPath = "/"
})
ts.Run(t, test.TestCase{Path: "/", Code: 200, Client: client, Domain: "localhost"})
})
t.Run("MutualTLSCertificate not set", func(t *testing.T) {
client := getTLSClient(nil, nil)
buildAndLoadAPI(func(spec *APISpec) {
spec.Domain = "localhost"
spec.Proxy.ListenPath = "/"
spec.UseMutualTLSAuth = true
})
ts.Run(t, test.TestCase{
ErrorMatch: badcertErr,
Client: client,
Domain: "localhost",
})
})
t.Run("Client certificate match", func(t *testing.T) {
client := getTLSClient(&clientCert, serverCertPem)
clientCertID, _ := CertificateManager.Add(clientCertPem, "")
buildAndLoadAPI(func(spec *APISpec) {
spec.Domain = "localhost"
spec.Proxy.ListenPath = "/"
spec.UseMutualTLSAuth = true
spec.ClientCertificates = []string{clientCertID}
})
ts.Run(t, test.TestCase{
Code: 200, Client: client, Domain: "localhost",
})
CertificateManager.Delete(clientCertID)
CertificateManager.FlushCache()
client = getTLSClient(&clientCert, serverCertPem)
ts.Run(t, test.TestCase{
Client: client, Domain: "localhost", ErrorMatch: badcertErr,
})
})
t.Run("Client certificate differ", func(t *testing.T) {
client := getTLSClient(&clientCert, serverCertPem)
clientCertPem2, _, _, _ := genCertificate(&x509.Certificate{})
clientCertID2, _ := CertificateManager.Add(clientCertPem2, "")
defer CertificateManager.Delete(clientCertID2)
buildAndLoadAPI(func(spec *APISpec) {
spec.Domain = "localhost"
spec.Proxy.ListenPath = "/"
spec.UseMutualTLSAuth = true
spec.ClientCertificates = []string{clientCertID2}
})
ts.Run(t, test.TestCase{
Client: client, ErrorMatch: badcertErr, Domain: "localhost",
})
})
})
t.Run("Multiple APIs on same domain", func(t *testing.T) {
clientCertID, _ := CertificateManager.Add(clientCertPem, "")
defer CertificateManager.Delete(clientCertID)
loadAPIS := func(certs ...string) {
buildAndLoadAPI(
func(spec *APISpec) {
spec.Proxy.ListenPath = "/with_mutual"
spec.UseMutualTLSAuth = true
spec.ClientCertificates = certs
},
func(spec *APISpec) {
spec.Proxy.ListenPath = "/without_mutual"
},
)
}
t.Run("Without certificate", func(t *testing.T) {
clientWithoutCert := getTLSClient(nil, nil)
loadAPIS()
certNotMatchErr := "Client TLS certificate is required"
ts.Run(t, []test.TestCase{
{
Path: "/with_mutual",
Client: clientWithoutCert,
Code: 403,
BodyMatch: `"error": "` + certNotMatchErr,
},
{
Path: "/without_mutual",
Client: clientWithoutCert,
Code: 200,
},
}...)
})
t.Run("Client certificate not match", func(t *testing.T) {
client := getTLSClient(&clientCert, serverCertPem)
loadAPIS()
certNotAllowedErr := `Certificate with SHA256 ` + certs.HexSHA256(clientCert.Certificate[0]) + ` not allowed`
ts.Run(t, test.TestCase{
Path: "/with_mutual",
Client: client,
Code: 403,
BodyMatch: `"error": "` + certNotAllowedErr,
})
})
t.Run("Client certificate match", func(t *testing.T) {
loadAPIS(clientCertID)
client := getTLSClient(&clientCert, serverCertPem)
ts.Run(t, test.TestCase{
Path: "/with_mutual",
Client: client,
Code: 200,
})
})
})
}
func TestUpstreamMutualTLS(t *testing.T) {
_, _, combinedClientPEM, clientCert := genCertificate(&x509.Certificate{})
clientCert.Leaf, _ = x509.ParseCertificate(clientCert.Certificate[0])
upstream := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
// Mutual TLS protected upstream
pool := x509.NewCertPool()
upstream.TLS = &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: pool,
InsecureSkipVerify: true,
}
upstream.StartTLS()
defer upstream.Close()
t.Run("Without API", func(t *testing.T) {
client := getTLSClient(&clientCert, nil)
if _, err := client.Get(upstream.URL); err == nil {
t.Error("Should reject without certificate")
}
pool.AddCert(clientCert.Leaf)
if _, err := client.Get(upstream.URL); err != nil {
t.Error("Should pass with valid certificate")
}
})
t.Run("Upstream API", func(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()
clientCertID, _ := CertificateManager.Add(combinedClientPEM, "")
defer CertificateManager.Delete(clientCertID)
pool.AddCert(clientCert.Leaf)
config.Global.ProxySSLInsecureSkipVerify = true
defer resetTestConfig()
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = upstream.URL
spec.UpstreamCertificates = map[string]string{
"*": clientCertID,
}
})
// Should pass with valid upstream certificate
ts.Run(t, test.TestCase{Code: 200})
})
}
func TestPublicKeyPinning(t *testing.T) {
_, _, _, serverCert := genServerCertificate()
x509Cert, _ := x509.ParseCertificate(serverCert.Certificate[0])
pubDer, _ := x509.MarshalPKIXPublicKey(x509Cert.PublicKey)
pubPem := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubDer})
pubID, _ := CertificateManager.Add(pubPem, "")
defer CertificateManager.Delete(pubID)
if pubID != certs.HexSHA256(pubDer) {
t.Error("Certmanager returned wrong pub key fingerprint:", certs.HexSHA256(pubDer), pubID)
}
upstream := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
upstream.TLS = &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{serverCert},
}
upstream.StartTLS()
defer upstream.Close()
t.Run("Pub key match", func(t *testing.T) {
// For host using pinning, it should ignore standard verification in all cases, e.g setting variable below does nothing
config.Global.ProxySSLInsecureSkipVerify = false
defer resetTestConfig()
ts := newTykTestServer()
defer ts.Close()
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.PinnedPublicKeys = map[string]string{"127.0.0.1": pubID}
spec.Proxy.TargetURL = upstream.URL
})
ts.Run(t, test.TestCase{Code: 200})
})
t.Run("Pub key not match", func(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.PinnedPublicKeys = map[string]string{"127.0.0.1": "wrong"}
spec.Proxy.TargetURL = upstream.URL
})
ts.Run(t, test.TestCase{Code: 500})
})
t.Run("Global setting", func(t *testing.T) {
ts := newTykTestServer()
defer ts.Close()
config.Global.Security.PinnedPublicKeys = map[string]string{"127.0.0.1": "wrong"}
defer resetTestConfig()
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = upstream.URL
})
ts.Run(t, test.TestCase{Code: 500})
})
}
func TestKeyWithCertificateTLS(t *testing.T) {
_, _, combinedPEM, _ := genServerCertificate()
serverCertID, _ := CertificateManager.Add(combinedPEM, "")
defer CertificateManager.Delete(serverCertID)
_, _, _, clientCert := genCertificate(&x509.Certificate{})
clientCertID := certs.HexSHA256(clientCert.Certificate[0])
config.Global.HttpServerOptions.UseSSL = true
config.Global.HttpServerOptions.SSLCertificates = []string{serverCertID}
defer resetTestConfig()
ts := newTykTestServer()
defer ts.Close()
buildAndLoadAPI(func(spec *APISpec) {
spec.UseKeylessAccess = false
spec.BaseIdentityProvidedBy = apidef.AuthToken
spec.Auth.UseCertificate = true
spec.Proxy.ListenPath = "/"
})
client := getTLSClient(&clientCert, nil)
t.Run("Cert unknown", func(t *testing.T) {
ts.Run(t, test.TestCase{Code: 403, Client: client})
})
t.Run("Cert known", func(t *testing.T) {
createSession(func(s *user.SessionState) {
s.Certificate = clientCertID
s.AccessRights = map[string]user.AccessDefinition{"test": {
APIID: "test", Versions: []string{"v1"},
}}
})
ts.Run(t, test.TestCase{Path: "/", Code: 200, Client: client})
})
}
func TestCertificateHandlerTLS(t *testing.T) {
_, _, combinedServerPEM, serverCert := genServerCertificate()
serverCertID := certs.HexSHA256(serverCert.Certificate[0])
clientPEM, _, _, clientCert := genCertificate(&x509.Certificate{})
clientCertID := certs.HexSHA256(clientCert.Certificate[0])
ts := newTykTestServer()
defer ts.Close()
t.Run("List certificates, empty", func(t *testing.T) {
ts.Run(t, test.TestCase{
Path: "/tyk/certs", Code: 200, AdminAuth: true, BodyMatch: `{"certs":null}`,
})
})
t.Run("Should add certificates with and without private keys", func(t *testing.T) {
ts.Run(t, []test.TestCase{
// Public Certificate
{Method: "POST", Path: "/tyk/certs", Data: string(clientPEM), AdminAuth: true, Code: 200, BodyMatch: `"id":"` + clientCertID},
// Public + Private
{Method: "POST", Path: "/tyk/certs", Data: string(combinedServerPEM), AdminAuth: true, Code: 200, BodyMatch: `"id":"` + serverCertID},
}...)
})
t.Run("List certificates, non empty", func(t *testing.T) {
ts.Run(t, []test.TestCase{
{Method: "GET", Path: "/tyk/certs", AdminAuth: true, Code: 200, BodyMatch: clientCertID},
{Method: "GET", Path: "/tyk/certs", AdminAuth: true, Code: 200, BodyMatch: serverCertID},
}...)
})
certMetaTemplate := `{"id":"%s","fingerprint":"%s","has_private":%s`
t.Run("Certificate meta info", func(t *testing.T) {
clientCertMeta := fmt.Sprintf(certMetaTemplate, clientCertID, clientCertID, "false")
serverCertMeta := fmt.Sprintf(certMetaTemplate, serverCertID, serverCertID, "true")
ts.Run(t, []test.TestCase{
{Method: "GET", Path: "/tyk/certs/" + clientCertID, AdminAuth: true, Code: 200, BodyMatch: clientCertMeta},
{Method: "GET", Path: "/tyk/certs/" + serverCertID, AdminAuth: true, Code: 200, BodyMatch: serverCertMeta},
{Method: "GET", Path: "/tyk/certs/" + serverCertID + "," + clientCertID, AdminAuth: true, Code: 200, BodyMatch: "[" + serverCertMeta},
{Method: "GET", Path: "/tyk/certs/" + serverCertID + "," + clientCertID, AdminAuth: true, Code: 200, BodyMatch: clientCertMeta},
}...)
})
t.Run("Certificate removal", func(t *testing.T) {
ts.Run(t, []test.TestCase{
{Method: "DELETE", Path: "/tyk/certs/" + serverCertID, AdminAuth: true, Code: 200},
{Method: "DELETE", Path: "/tyk/certs/" + clientCertID, AdminAuth: true, Code: 200},
{Method: "GET", Path: "/tyk/certs", AdminAuth: true, Code: 200, BodyMatch: `{"certs":null}`},
}...)
})
}
func TestCipherSuites(t *testing.T) {
//configure server so we can useSSL and utilize the logic, but skip verification in the clients
_, _, combinedPEM, _ := genServerCertificate()
serverCertID, _ := CertificateManager.Add(combinedPEM, "")
defer CertificateManager.Delete(serverCertID)
config.Global.HttpServerOptions.UseSSL = true
config.Global.HttpServerOptions.Ciphers = []string{"TLS_RSA_WITH_RC4_128_SHA", "TLS_RSA_WITH_3DES_EDE_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"}
config.Global.HttpServerOptions.SSLCertificates = []string{serverCertID}
defer resetTestConfig()
ts := newTykTestServer()
defer ts.Close()
buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
})
//matching ciphers
t.Run("Cipher match", func(t *testing.T) {
client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{
CipherSuites: getCipherAliases([]string{"TLS_RSA_WITH_RC4_128_SHA", "TLS_RSA_WITH_3DES_EDE_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"}),
InsecureSkipVerify: true,
}}}
// If there is an internal TLS error it will fail test
ts.Run(t, test.TestCase{Client: client, Path: "/"})
})
t.Run("Cipher non-match", func(t *testing.T) {
client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{
CipherSuites: getCipherAliases([]string{"TLS_RSA_WITH_AES_256_CBC_SHA"}), // not matching ciphers
InsecureSkipVerify: true,
}}}
ts.Run(t, test.TestCase{Client: client, Path: "/", ErrorMatch: "tls: handshake failure"})
})
}