-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some users complained that if a policy fails, the request didn't terminate. This problem is strategic for some users because it can raise a security flaw if a policy is not executed correctly(jwt_claim_check policy as an example) This pull request adds some "pcalls" on policy_chain, where the error is checked and, if the context has a callback function defined, is called. Fix THREESCALE-6705 Signed-off-by: Eloy Coto <[email protected]>
- Loading branch information
Showing
10 changed files
with
240 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Policy on_failed | ||
|
||
When any policy fails, this policy block the request and send back a given | ||
status code to the user. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"$schema": "http://apicast.io/policy-v1.1/schema#manifest#", | ||
"name": "On fail", | ||
"summary": "Block request if any policy fails", | ||
"description": "When a policy fails, this policy allows to set an error message back to the user and stop processing the request to the upstream API.", | ||
"version": "builtin", | ||
"order": { | ||
"before": [ | ||
{ | ||
"name": "apicast", | ||
"version": "builtin" | ||
} | ||
] | ||
}, | ||
"configuration": { | ||
"type": "object", | ||
"properties": { | ||
"error_status_code": { | ||
"description": "Status code that will send to the user if any policy fails", | ||
"type": "integer", | ||
"minimum": 100, | ||
"exclusiveMaximum": 700 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
return require("on_failed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
local _M = require('apicast.policy').new('On failed', 'builtin') | ||
local new = _M.new | ||
|
||
|
||
function _M.new(config) | ||
local self = new(config) | ||
self.error_status_code = config.error_status_code or ngx.HTTP_SERVICE_UNAVAILABLE | ||
return self | ||
end | ||
|
||
function _M:export() | ||
return { | ||
policy_error_callback = function(policy_name, error_message) | ||
ngx.log(ngx.DEBUG, "Stop request because policy: '", policy_name, "' failed, error='", error_message, "'") | ||
ngx.exit(self.error_status_code) | ||
end | ||
} | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use lib 't'; | ||
use Test::APIcast::Blackbox 'no_plan'; | ||
|
||
use Cwd qw(abs_path); | ||
|
||
BEGIN { | ||
$ENV{TEST_NGINX_APICAST_POLICY_LOAD_PATH} = 't/fixtures/policies'; | ||
} | ||
|
||
env_to_apicast( | ||
'APICAST_POLICY_LOAD_PATH' => abs_path($ENV{TEST_NGINX_APICAST_POLICY_LOAD_PATH}), | ||
); | ||
|
||
repeat_each(); | ||
run_tests(); | ||
|
||
__DATA__ | ||
=== TEST 1: policy with invalid configuration return 503 | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"id": 42, | ||
"proxy": { | ||
"policy_chain": [ | ||
{ "name": "example_policy", "version": "1.0.0", "configuration": { } }, | ||
{ "name": "apicast.policy.on_failed", "configuration": {} }, | ||
{ "name": "apicast.policy.echo" } | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- request | ||
GET /test | ||
--- error_code: 503 | ||
--- error_log | ||
Stop request because policy: 'example_policy' failed, error= | ||
=== TEST 2: policy with access phase issues return 503 | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"id": 42, | ||
"proxy": { | ||
"policy_chain": [ | ||
{ | ||
"name": "example_policy", | ||
"version": "1.0.0", | ||
"configuration": { | ||
"message": "foo", | ||
"fail_access": true | ||
} | ||
}, | ||
{ | ||
"name": "apicast.policy.on_failed", | ||
"configuration": {} | ||
}, | ||
{ | ||
"name": "apicast.policy.echo" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- request | ||
GET /test | ||
--- error_code: 503 | ||
--- error_log | ||
Stop request because policy: 'example_policy' failed, error= | ||
=== TEST 3: policy with access phase issues return provided status code | ||
--- configuration | ||
{ | ||
"services": [ | ||
{ | ||
"id": 42, | ||
"proxy": { | ||
"policy_chain": [ | ||
{ | ||
"name": "example_policy", | ||
"version": "1.0.0", | ||
"configuration": { | ||
"message": "foo", | ||
"fail_access": true | ||
} | ||
}, | ||
{ | ||
"name": "apicast.policy.on_failed", | ||
"configuration": { | ||
"error_status_code": 401 | ||
} | ||
}, | ||
{ | ||
"name": "apicast.policy.echo" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
--- request | ||
GET /test | ||
--- error_code: 401 | ||
--- error_log | ||
Stop request because policy: 'example_policy' failed, error=' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters