forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert_go1.10_test.go
238 lines (193 loc) · 6.67 KB
/
cert_go1.10_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
// +build go1.10
package gateway
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"net/http"
"net/http/httptest"
"testing"
"github.com/TykTechnologies/tyk/certs"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/test"
)
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) {
globalConf := config.Global()
// For host using pinning, it should ignore standard verification in all cases, e.g setting variable below does nothing
globalConf.ProxySSLInsecureSkipVerify = false
config.SetGlobal(globalConf)
defer ResetTestConfig()
ts := StartTest()
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 := StartTest()
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) {
globalConf := config.Global()
globalConf.Security.PinnedPublicKeys = map[string]string{"127.0.0.1": "wrong"}
config.SetGlobal(globalConf)
defer ResetTestConfig()
ts := StartTest()
defer ts.Close()
BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = upstream.URL
})
ts.Run(t, test.TestCase{Code: 500})
})
t.Run("Though proxy", func(t *testing.T) {
_, _, _, proxyCert := genServerCertificate()
proxy := initProxy("https", &tls.Config{
Certificates: []tls.Certificate{proxyCert},
})
globalConf := config.Global()
globalConf.ProxySSLInsecureSkipVerify = true
config.SetGlobal(globalConf)
defer ResetTestConfig()
defer proxy.Stop()
ts := StartTest()
defer ts.Close()
BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = upstream.URL
spec.Proxy.Transport.ProxyURL = proxy.URL
spec.PinnedPublicKeys = map[string]string{"*": "wrong"}
})
ts.Run(t, test.TestCase{Code: 500})
})
}
func TestProxyTransport(t *testing.T) {
upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("test"))
}))
defer upstream.Close()
defer ResetTestConfig()
ts := StartTest()
defer ts.Close()
//matching ciphers
t.Run("Global: Cipher match", func(t *testing.T) {
globalConf := config.Global()
globalConf.ProxySSLInsecureSkipVerify = true
// force creating new transport on each reque
globalConf.MaxConnTime = -1
globalConf.ProxySSLCipherSuites = []string{"TLS_RSA_WITH_AES_128_CBC_SHA"}
config.SetGlobal(globalConf)
BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = upstream.URL
})
ts.Run(t, test.TestCase{Path: "/", Code: 200})
})
t.Run("Global: Cipher not match", func(t *testing.T) {
globalConf := config.Global()
globalConf.ProxySSLInsecureSkipVerify = true
// force creating new transport on each reque
globalConf.MaxConnTime = -1
globalConf.ProxySSLCipherSuites = []string{"TLS_RSA_WITH_RC4_128_SHA"}
config.SetGlobal(globalConf)
BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = upstream.URL
})
ts.Run(t, test.TestCase{Path: "/", Code: 500})
})
t.Run("API: Cipher override", func(t *testing.T) {
globalConf := config.Global()
globalConf.ProxySSLInsecureSkipVerify = true
// force creating new transport on each reque
globalConf.MaxConnTime = -1
globalConf.ProxySSLCipherSuites = []string{"TLS_RSA_WITH_RC4_128_SHA"}
config.SetGlobal(globalConf)
BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = upstream.URL
spec.Proxy.Transport.SSLCipherSuites = []string{"TLS_RSA_WITH_AES_128_CBC_SHA"}
})
ts.Run(t, test.TestCase{Path: "/", Code: 200})
})
t.Run("API: MinTLS not match", func(t *testing.T) {
globalConf := config.Global()
globalConf.ProxySSLInsecureSkipVerify = true
// force creating new transport on each reque
globalConf.MaxConnTime = -1
globalConf.ProxySSLMinVersion = 772
config.SetGlobal(globalConf)
BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = upstream.URL
spec.Proxy.Transport.SSLCipherSuites = []string{"TLS_RSA_WITH_AES_128_CBC_SHA"}
})
ts.Run(t, test.TestCase{Path: "/", Code: 500})
})
t.Run("API: Invalid proxy", func(t *testing.T) {
globalConf := config.Global()
globalConf.ProxySSLInsecureSkipVerify = true
// force creating new transport on each reque
globalConf.MaxConnTime = -1
globalConf.ProxySSLMinVersion = 771
config.SetGlobal(globalConf)
BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = upstream.URL
spec.Proxy.Transport.SSLCipherSuites = []string{"TLS_RSA_WITH_AES_128_CBC_SHA"}
// Invalid proxy
spec.Proxy.Transport.ProxyURL = upstream.URL
})
ts.Run(t, test.TestCase{Path: "/", Code: 500})
})
t.Run("API: Valid proxy", func(t *testing.T) {
globalConf := config.Global()
globalConf.ProxySSLInsecureSkipVerify = true
// force creating new transport on each reque
globalConf.MaxConnTime = -1
globalConf.ProxySSLMinVersion = 771
config.SetGlobal(globalConf)
_, _, _, proxyCert := genServerCertificate()
proxy := initProxy("https", &tls.Config{
Certificates: []tls.Certificate{proxyCert},
})
defer proxy.Stop()
BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.Transport.SSLCipherSuites = []string{"TLS_RSA_WITH_AES_128_CBC_SHA"}
spec.Proxy.Transport.ProxyURL = proxy.URL
})
client := GetTLSClient(nil, nil)
client.Transport = &http.Transport{
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
ts.Run(t, test.TestCase{Path: "/", Code: 200, Client: client})
})
}