Skip to content

Commit 39d97e1

Browse files
committed
fix(*-proxy): proxy policy doesn't send headers set by APIcast to API backend
1 parent 12020d8 commit 39d97e1

5 files changed

Lines changed: 34 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- Correct FAPI header to `x-fapi-interaction-id` [PR #1557](https://github.com/3scale/APIcast/pull/1557) [THREESCALE-11957](https://issues.redhat.com/browse/THREESCALE-11957)
1717
- Only validate oidc setting if authentication method is set to oidc [PR #1568](https://github.com/3scale/APIcast/pull/1568) [THREESCALE-11441](https://issues.redhat.com/browse/THREESCALE-11441)
1818
- Reduce memory consumption when returning large response that has been routed through a proxy server. [PR #1572](https://github.com/3scale/APIcast/pull/1572) [THREESCALE-12258](https://issues.redhat.com/browse/THREESCALE-12258)
19+
- Fix proxy policy doesn't send headers set by APIcast to the API Backend. [PR #1588](https://github.com/3scale/APIcast/pull/1588) [THREESCALE-10151](https://redhat.atlassian.net/browse/THREESCALE-10151)
1920

2021
### Added
2122
- Update APIcast schema manifest [PR #1550](https://github.com/3scale/APIcast/pull/1550)

gateway/src/apicast/http_proxy.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,15 @@ local function forward_https_request(proxy_uri, uri, proxy_opts)
138138
end
139139
end
140140

141+
local headers = ngx_req_get_headers(0, true)
142+
headers["X-Real-IP"] = ngx.var.remote_addr
143+
headers["X-3scale-debug"] = ""
144+
headers["X-3scale-proxy-secret-token"] = ngx.var.secret_token
145+
141146
local request = {
142147
uri = uri,
143148
method = req_method,
144-
headers = ngx_req_get_headers(0, true),
149+
headers = headers,
145150
path = (ngx.var.uri or '') .. (ngx.var.is_args or '') .. (ngx.var.query_string or ''),
146151
body = body,
147152
proxy_uri = proxy_uri,
@@ -159,6 +164,8 @@ local function forward_https_request(proxy_uri, uri, proxy_opts)
159164
local res
160165
res, err = httpc:request(request)
161166

167+
local inspect = require 'inspect'
168+
ngx.log(ngx.INFO, "\n-------\n", inspect(res.headers))
162169
if res then
163170
if opts.request_unbuffered and raw then
164171
err = send_response(sock, res, DEFAULT_CHUNKSIZE)

t/apicast-policy-camel.t

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sub large_body {
1212
}
1313

1414
$ENV{'LARGE_BODY'} = large_body();
15+
require("policies.pl");
1516

1617
repeat_each(1);
1718

@@ -140,6 +141,7 @@ $Test::Nginx::Util::ENDPOINT_SSL_PORT = Test::APIcast::get_random_port();
140141
{
141142
"backend_version": 1,
142143
"proxy": {
144+
"secret_token": "token",
143145
"api_backend": "https://localhost:$Test::Nginx::Util::ENDPOINT_SSL_PORT",
144146
"proxy_rules": [
145147
{ "pattern": "/test", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
@@ -177,6 +179,9 @@ EOF
177179
server_name _ default_server;
178180

179181
location /test {
182+
echo_foreach_split '\r\n' \$echo_client_request_headers;
183+
echo \$echo_it;
184+
echo_end;
180185
access_by_lua_block {
181186
assert = require('luassert')
182187
assert.equal('https', ngx.var.scheme)
@@ -186,8 +191,6 @@ EOF
186191

187192
local host = ngx.req.get_headers()["Host"]
188193
assert.equal(host, 'localhost:$Test::Nginx::Util::ENDPOINT_SSL_PORT')
189-
ngx.say("yay, endpoint backend")
190-
191194
}
192195
}
193196
}
@@ -211,6 +214,16 @@ GET /test?user_key=test3
211214
--- more_headers
212215
User-Agent: Test::APIcast::Blackbox
213216
ETag: foobar
217+
--- expected_response_body_like_multiple eval
218+
[[
219+
qr{GET \/test\?user_key=test3 HTTP\/1\.1},
220+
qr{ETag\: foobar},
221+
qr{Connection\: close},
222+
qr{User\-Agent\: Test\:\:APIcast\:\:Blackbox},
223+
qr{Host\: localhost\:\d+},
224+
qr{X\-Real\-IP\: 127.0.0.1},
225+
qr{X\-3scale\-proxy\-secret\-token\: token}
226+
]]
214227
--- error_code: 200
215228
--- user_files fixture=tls.pl eval
216229
--- error_log eval

t/apicast-policy-http-proxy.t

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ sub large_body {
1313

1414

1515
$ENV{'LARGE_BODY'} = large_body();
16+
require("policies.pl");
1617

1718
repeat_each(3);
1819

@@ -132,12 +133,14 @@ using proxy: $TEST_NGINX_HTTP_PROXY
132133
133134
134135
=== TEST 3: using HTTPS proxy for backend
136+
--- ONLY
135137
--- configuration random_port env
136138
{
137139
"services": [
138140
{
139141
"backend_version": 1,
140142
"proxy": {
143+
"secret_token": "token",
141144
"api_backend": "https://test-upstream.lvh.me:$TEST_NGINX_RANDOM_PORT",
142145
"proxy_rules": [
143146
{ "pattern": "/test", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
@@ -197,7 +200,9 @@ ETag: foobar
197200
qr{ETag\: foobar},
198201
qr{Connection\: close},
199202
qr{User\-Agent\: Test\:\:APIcast\:\:Blackbox},
200-
qr{Host\: test-upstream.lvh.me\:\d+}
203+
qr{Host\: test-upstream.lvh.me\:\d+},
204+
qr{X\-Real\-IP\: 127.0.0.1},
205+
qr{X\-3scale\-proxy\-secret\-token\: token},
201206
]]
202207
--- error_code: 200
203208
--- error_log env

t/http-proxy.t

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ proxy request: GET http://test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT/test?user
451451
{
452452
"backend_version": 1,
453453
"proxy": {
454+
"secret_token": "token",
454455
"api_backend": "https://test-upstream.lvh.me:$TEST_NGINX_RANDOM_PORT",
455456
"proxy_rules": [
456457
{ "pattern": "/test", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
@@ -495,7 +496,9 @@ ETag: foobar
495496
qr{ETag\: foobar},
496497
qr{Connection\: close},
497498
qr{User\-Agent\: Test\:\:APIcast\:\:Blackbox},
498-
qr{Host\: test-upstream.lvh.me\:\d+}
499+
qr{Host\: test-upstream.lvh.me\:\d+},
500+
qr{X\-Real\-IP\: 127.0.0.1},
501+
qr{X\-3scale\-proxy\-secret\-token\: token}
499502
]]
500503
--- error_code: 200
501504
--- error_log env

0 commit comments

Comments
 (0)