Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Policy: add on_failed policy #1286

Merged
merged 1 commit into from
Jul 16, 2021
Merged

Conversation

eloycoto
Copy link
Contributor

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 eloy.coto@acalustra.com

@eloycoto eloycoto requested a review from a team as a code owner June 29, 2021 14:50
@eloycoto eloycoto changed the title Policy: add on_failed policy WIP: Policy: add on_failed policy Jun 29, 2021
@eloycoto eloycoto changed the title WIP: Policy: add on_failed policy Policy: add on_failed policy Jul 1, 2021
policy_error_callback = function(policy_name, error_message)
ngx.log(ngx.DEBUG, "Stop request because policy: '", policy_name, "', failed")
ngx.exit(self.error_status_code or ngx.HTTP_SERVICE_UNAVAILABLE)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indentation.

return {
policy_error_callback = function(policy_name, error_message)
ngx.log(ngx.DEBUG, "Stop request because policy: '", policy_name, "', failed")
ngx.exit(self.error_status_code or ngx.HTTP_SERVICE_UNAVAILABLE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This or is not necessary because it's already in the new function.

function _M:export()
return {
policy_error_callback = function(policy_name, error_message)
ngx.log(ngx.DEBUG, "Stop request because policy: '", policy_name, "', failed")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this log also the error_message received in the params?

return policy
end

function _M:access()
if self.fail_access then
self.fail()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this fail function defined?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is intentional to force an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup!

GET /test
--- error_code: 503
--- error_log
Policy example_policy crashed in .new()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this error message correct? In the export function of the on failed policy, it doesn't mention crashed, and it does not mention the name of the function where the error happens.

'APICAST_POLICY_LOAD_PATH' => abs_path($ENV{TEST_NGINX_APICAST_POLICY_LOAD_PATH}),
);

repeat_each(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why these tests cannot run twice, as defined by default?

@@ -178,9 +178,13 @@ function _M:add_policy(name, version, ...)
return self:insert(policy)

elseif err then
-- This will only report the last one that failed, but at least users
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it a bit counter-intuitive that it returns the last one instead of the first one that fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors are logged, but only on request time we log the last one.

@@ -178,9 +178,13 @@ function _M:add_policy(name, version, ...)
return self:insert(policy)

elseif err then
-- This will only report the last one that failed, but at least users
-- can be aware of the issue
self.init_failed = true
Copy link
Contributor

@davidor davidor Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 3 attrs are related, would it make sense to store just one table that contains a policy_name and an error ?

@davidor
Copy link
Contributor

davidor commented Jul 6, 2021

I wonder if providing an option in the service config instead of the "on_failed" policy could be a better option. The only downside I see is that it would require changes in Porta (add a bool param).

@eloycoto
Copy link
Contributor Author

Porta is having soo many pending things, that this cannot be achieved at all. This is the reason of moving to a policy.

@eloycoto eloycoto force-pushed the THREESCALE-6705 branch 2 times, most recently from 7675442 to 225c63d Compare July 15, 2021 13:46
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 <eloy.coto@acalustra.com>
@eloycoto eloycoto merged commit 9c92992 into 3scale:master Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants