Skip to content

Commit

Permalink
Policy: Content Caching disable totally
Browse files Browse the repository at this point in the history
When using content_cache and proxy_no_cache, some directives are still
in place, and was causing issues to a few users regarding HEAD request,
etc..

This commit disabled the proxy_cache globally based on cache_zone
variable.

FIX THREESCALE-7016

Signed-off-by: Eloy Coto <eloy.coto@acalustra.com>
  • Loading branch information
eloycoto committed Jun 4, 2021
1 parent 5f7b751 commit 417b7dc
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gateway/conf.d/apicast.conf
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ location @upstream {
}

#{% capture proxy_cache_valid %}
#{#} proxy_cache apicast_cache;
#{#} proxy_cache $cache_zone;
#{#} proxy_cache_key $scheme$request_method$proxy_host$request_uri$service_id;
#{#} proxy_no_cache $cache_request;
#{#} proxy_cache_valid {{ env.APICAST_CACHE_STATUS_CODES | default: '200 302'}} {{ env.APICAST_CACHE_MAX_TIME | default: '1m' }};
Expand Down Expand Up @@ -166,6 +166,7 @@ location / {

# Variable to enable/disable content cache
set $cache_request 'true';
set $cache_zone 'off';

set $original_request_id $request_id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local metrics_updater = require('apicast.metrics.updater')

local content_cache_metric = prometheus('counter', "content_caching", "Content caching status", {"status"})

local cache_zone = "apicast_cache"

local new = _M.new

function _M.new(config)
Expand All @@ -30,7 +32,9 @@ function _M.new(config)
end

function _M:access(context)
ngx.var.cache_zone = cache_zone
ngx.var.cache_request = "true"

for _, rule in ipairs(self.rules or {}) do
local cond_is_true = rule.condition:evaluate(context)
if cond_is_true and rule.cache then
Expand Down
100 changes: 100 additions & 0 deletions t/apicast-policy-content-caching.t
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,103 @@ __DATA__
["X-Cache-Status: MISS", "X-Cache-Status: MISS"]
--- no_error_log
[error]
=== TEST 8: HEAD request is enabled if no content-caching
--- configuration
{
"services": [
{
"id": 42,
"proxy": {
"hosts": ["one"],
"policy_chain": [
{
"name": "apicast.policy.upstream",
"configuration":
{
"rules": [ { "regex": "/", "url": "http://test:$TEST_NGINX_SERVER_PORT/" } ]
}
}
]
}
}
]
}
--- upstream
location / {
content_by_lua_block {
local assert = require('luassert')
assert.same(ngx.var.request_method, "HEAD")
}
}
--- request
HEAD /foo
--- more_headers
Host: one
--- expected_response_body_like_multiple eval
yay, api backend
--- error_code: 200
--- no_error_log
[error]
=== TEST 9: HEAD request is disabled when using content-caching
--- configuration
{
"services": [
{
"id": 42,
"proxy": {
"hosts": ["one"],
"policy_chain": [
{
"name": "apicast.policy.content_caching",
"version": "builtin",
"configuration": {
"rules": [
{
"cache": true,
"header": "X-Cache-Status",
"condition": {
"combine_op": "and",
"operations": [
{
"left": "oo",
"op": "==",
"right": "oo"
}
]
}
}
]
}
},
{
"name": "apicast.policy.upstream",
"configuration":
{
"rules": [ { "regex": "/", "url": "http://test:$TEST_NGINX_SERVER_PORT/" } ]
}
}
]
}
}
]
}
--- upstream
location / {
content_by_lua_block {
local assert = require('luassert')
assert.same(ngx.var.request_method, "GET")
}
}
--- request
HEAD /foo
--- more_headers
Host: one
--- expected_response_body_like_multiple eval
yay, api backend
--- error_code: 200
--- no_error_log
[error]

0 comments on commit 417b7dc

Please sign in to comment.