forked from 3scale/APIcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapicast-policy-tls.t
221 lines (205 loc) · 6.59 KB
/
apicast-policy-tls.t
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
use lib 't';
use Test::APIcast::Blackbox 'no_plan';
$ENV{TEST_NGINX_HTML_DIR} ||= "$Test::Nginx::Util::ServRoot/html";
run_tests();
__DATA__
=== TEST 1: tls accepts configuration
--- env eval
(
'APICAST_HTTPS_PORT' => "$Test::Nginx::Util::ServerPortForClient",
)
--- configuration
{
"services": [
{
"proxy": {
"policy_chain": [
{
"name": "apicast.policy.tls",
"configuration": {
"certificates": [
{
"certificate_path": "$TEST_NGINX_HTML_DIR/server.crt",
"certificate_key_path": "$TEST_NGINX_HTML_DIR/server.key"
}
]
}
},
{
"name": "apicast.policy.upstream",
"configuration": {
"rules": [
{
"regex": "/",
"url": "http://echo"
}
]
}
}
]
}
}
]
}
--- test env
lua_ssl_trusted_certificate $TEST_NGINX_HTML_DIR/server.crt;
content_by_lua_block {
local function request(path)
local sock = ngx.socket.tcp()
sock:settimeout(2000)
local ok, err = sock:connect(ngx.var.server_addr, ngx.var.apicast_port)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ngx.say("connected: ", ok)
local sess, err = sock:sslhandshake(nil, "localhost", true)
if not sess then
ngx.say("failed to do SSL handshake: ", err)
return
end
ngx.say("ssl handshake: ", type(sess))
sock:send("GET " .. path .. "?user_key=123 HTTP/1.1\r\nHost: localhost\r\n\r\n")
local data = sock:receive()
ngx.say(data)
end
request('/')
}
--- response_body
connected: 1
ssl handshake: userdata
HTTP/1.1 200 OK
--- no_error_log
[error]
--- user_files
>>> server.crt
-----BEGIN CERTIFICATE-----
MIIBRzCB7gIJAPHi8uNGM8wDMAoGCCqGSM49BAMCMCwxFjAUBgNVBAoMDVRlc3Q6
OkFQSWNhc3QxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xODA2MDUwOTQ0MjRaFw0y
ODA2MDIwOTQ0MjRaMCwxFjAUBgNVBAoMDVRlc3Q6OkFQSWNhc3QxEjAQBgNVBAMM
CWxvY2FsaG9zdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABI3IZUvpJsaQbiLy
/yfthJDd/+BIaKzAbgMAimth4ePOi3a/YICwsHyq6sBxbgvMeTwxNJIHpe3td4tB
VZ5Wr10wCgYIKoZIzj0EAwIDSAAwRQIhAPRkfbxowt0H7p5xZYpwoMKanUXz9eKQ
0sGkOw+TqqGXAiAMKJRqtjnCF2LIjGygHG6BlgjM4NgIMDHteZPEr4qEmw==
-----END CERTIFICATE-----
>>> server.key
-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIH22v43xtXcHWJyH3BEB9N30ahrCOLripkoSWW/WujUxoAoGCCqGSM49
AwEHoUQDQgAEjchlS+kmxpBuIvL/J+2EkN3/4EhorMBuAwCKa2Hh486Ldr9ggLCw
fKrqwHFuC8x5PDE0kgel7e13i0FVnlavXQ==
-----END EC PRIVATE KEY-----
=== TEST 2: tls failed on invalid certificate.
--- env eval
(
'APICAST_HTTPS_PORT' => "$Test::Nginx::Util::ServerPortForClient",
)
--- configuration
{
"services": [
{
"proxy": {
"policy_chain": [
{
"name": "apicast.policy.tls",
"configuration": {
"certificates": [
{
"certificate_path": "$TEST_NGINX_HTML_DIR/server_invalid.crt",
"certificate_key_path": "$TEST_NGINX_HTML_DIR/server.key"
}
]
}
},
{
"name": "apicast.policy.upstream",
"configuration": {
"rules": [
{
"regex": "/",
"url": "http://echo"
}
]
}
}
]
}
}
]
}
--- test env
lua_ssl_trusted_certificate $TEST_NGINX_HTML_DIR/server.crt;
content_by_lua_block {
local function request(path)
local sock = ngx.socket.tcp()
sock:settimeout(2000)
local ok, err = sock:connect(ngx.var.server_addr, ngx.var.apicast_port)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ngx.say("connected: ", ok)
local sess, err = sock:sslhandshake(nil, "localhost", true)
if not sess then
ngx.say("failed to do SSL handshake: ", err)
return
end
ngx.say("ssl handshake: ", type(sess))
sock:send("GET " .. path .. "?user_key=123 HTTP/1.1\r\nHost: localhost\r\n\r\n")
local data = sock:receive()
ngx.say(data)
end
request('/')
}
--- response_body
connected: 1
failed to do SSL handshake: handshake failed
--- error_log
ssl3_read_bytes:sslv3 alert handshake failure:SSL
sslv3 alert handshake failure
--- user_files
>>> server_invalid.crt
-----BEGIN CERTIFICATE-----
MIID1zCCAr8CFFIaIIw9n2Afgxf7IvPTRDdweQnFMA0GCSqGSIb3DQEBCwUAMEIx
CzAJBgNVBAYTAlhYMRUwEwYDVQQHDAxEZWZhdWx0IENpdHkxHDAaBgNVBAoME0Rl
ZmF1bHQgQ29tcGFueSBMdGQwHhcNMTkwODE1MDYyMDU2WhcNMjAxMjI3MDYyMDU2
WjAOMQwwCgYDVQQDDANvbmUwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoIC
AQDF9a4UJKH2CDKKPMcfRGZnajvr2Wp8Xm6QEr5/L2XzioWx+ciahI0IJN4D1KpK
dlYBUbKfn3UK1t+EMdXiq00Z9Mh3FTns9qGwH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kFQSWNhc3QxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xODA2MDUwOTQ0MjRaFw0y
ODA2MDIwOTQ0MjRaMCwxFjAUBgNVBAoMDVRlc3Q6OkFQSWNhc3QxEjAQBgNVBAMM
CWxvY2FsaG9zdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABI3IZUvpJsaQbiLy
/yfthJDd/+BIaKzAbgMAimth4ePOi3a/YICwsHyq6sBxbgvMeTwxNJIHpe3td4tB
VZ5Wr10wCgYIKoZIzj0EAwIDSAAwRQIhAPRkfbxowt0H7p5xZYpwoMKanUXz9eKQ
0sGkOw+TqqGXAiAMKJRqtjnCF2LIjGygHG6BlgjM4NgIMDHteZPEr4qEmw==
-----END CERTIFICATE-----
>>> server.key
-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIH22v43xtXcHWJyH3BEB9N30ahrCOLripkoSWW/WujUxoAoGCCqGSM49
AwEHoUQDQgAEjchlS+kmxpBuIvL/J+2EkN3/4EhorMBuAwCKa2Hh486Ldr9ggLCw
fKrqwHFuC8x5PDE0kgel7e13i0FVnlavXQ==
-----END EC PRIVATE KEY-----