forked from 3scale/APIcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy-protocol.t
189 lines (174 loc) · 4.5 KB
/
proxy-protocol.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
use lib 't';
use Test::APIcast::Blackbox 'no_plan';
# These test are a bit difficult to understand. Due to the Nginx is set to listen
# proxy_protocol, all backends upstream need to use another port, if not the
# backend client and API will fail, this is why a new server is added on
# --backend test section
#
# Additionally, this is the reason why the IP Check policy is tested here.
require("policies.pl");
repeat_each(3);
$ENV{TEST_NGINX_HTML_DIR} ||= "$Test::Nginx::Util::ServRoot/html";
run_tests();
__DATA__
=== TEST 1: Simple proxy-procol connection
--- init eval
$Test::Nginx::Util::BACKEND_PORT = Test::APIcast::get_random_port();
--- env eval
(
"BACKEND_ENDPOINT_OVERRIDE"=> "http://127.0.0.1:$Test::Nginx::Util::BACKEND_PORT",
"APICAST_HTTP_PROXY_PROTOCOL" => "true"
)
--- configuration eval
<<EOF
{
"services": [
{
"id": 42,
"backend_version": 1,
"backend_authentication_type": "service_token",
"backend_authentication_value": "token-value",
"proxy": {
"hosts": [
"localhost"
],
"api_backend": "http://127.0.0.1:$Test::Nginx::Util::BACKEND_PORT/",
"proxy_rules": [
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 1 }
],
"policy_chain": [
{
"name": "apicast",
"version": "builtin",
"configuration": {}
}
]
}
}
]
}
EOF
--- backend eval
<<EOF
}
server {
listen $Test::Nginx::Util::BACKEND_PORT;
server_name _ default_server;
location /transactions/authrep.xml {
content_by_lua_block {
ngx.exit(200)
}
}
location /foo {
access_by_lua_block {
ngx.say("API BACKEND")
}
}
EOF
--- test env
content_by_lua_block {
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)
sock:send(string.format("PROXY TCP4 127.0.0.1 %s %s %s\r\n\r\n\r\n", ngx.var.server_addr, "10000", ngx.var.apicast_port))
sock:send("GET /foo?user_key=123 HTTP/1.1\r\nHost: localhost\r\n\r\n")
local data = sock:receive()
ngx.say(data)
}
--- response_body env
connected: 1
HTTP/1.1 200 OK
--- error_code: 200
--- no_error_log
[error]
=== TEST 2: Simple IP check policy using proxy_protocol
--- init eval
$Test::Nginx::Util::BACKEND_PORT = Test::APIcast::get_random_port();
--- env eval
(
"BACKEND_ENDPOINT_OVERRIDE"=> "http://127.0.0.1:$Test::Nginx::Util::BACKEND_PORT",
"APICAST_HTTP_PROXY_PROTOCOL" => "true"
)
--- configuration eval
<<EOF
{
"services": [
{
"id": 42,
"backend_version": 1,
"backend_authentication_type": "service_token",
"backend_authentication_value": "token-value",
"proxy": {
"hosts": [
"localhost"
],
"api_backend": "http://127.0.0.1:$Test::Nginx::Util::BACKEND_PORT/",
"proxy_rules": [
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 1 }
],
"policy_chain": [
{
"name": "apicast.policy.ip_check",
"configuration": {
"ips": [ "9.9.9.9" ],
"client_ip_sources": [
"proxy_protocol_addr"
],
"check_type": "blacklist",
"error_msg": "A custom error message"
}
},
{
"name": "apicast",
"version": "builtin",
"configuration": {}
}
]
}
}
]
}
EOF
--- backend eval
<<EOF
}
server {
listen $Test::Nginx::Util::BACKEND_PORT;
server_name _ default_server;
location /transactions/authrep.xml {
content_by_lua_block {
ngx.exit(200)
}
}
location /foo {
access_by_lua_block {
ngx.say("API BACKEND")
}
}
EOF
--- test env
content_by_lua_block {
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)
sock:send(string.format("PROXY TCP4 9.9.9.9 %s %s %s\r\n\r\n\r\n", ngx.var.server_addr, "10000", ngx.var.apicast_port))
sock:send("GET /foo?user_key=123 HTTP/1.1\r\nHost: localhost\r\n\r\n")
local data = sock:receive()
ngx.say(data)
}
--- response_body env
connected: 1
HTTP/1.1 403 Forbidden
--- error_code: 200
--- no_error_log
[error]