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

fix: authz-keycloak plugin should not be marked as auth-type and should run in access phase, basic-auth plugin should run in rewrite phase. #2859

Conversation

jenskeiner
Copy link
Contributor

What this PR does / why we need it:

See #2857.

Pre-submission checklist:

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible? If it is not backward compatible, please discuss on the mailing list first

@membphis
Copy link
Member

Authentication type plug-ins should run in the rewrite phase

@jenskeiner
Copy link
Contributor Author

Authentication type plug-ins should run in the rewrite phase

How do you suggest this get's fixed then when openid-connect and other authentication plugins currently run in the access phase?

The plugin development guidelines are currently not in line with the actual code base.

Also, what's the rationale for running authentication/authorization in the rewrite phase when the access phase is the one that specifically caters for that purpose? It's not obvious why you would run in a different phase.

@membphis
Copy link
Member

reply you later

need more time to read your issue and pr first

@spacewander
Copy link
Member

@membphis @jenskeiner
authz-keycloak is for authorization but not authentication. It doesn't interact with consumers, so I think this change is fine.

@@ -168,8 +168,8 @@ local _M = {
Determine which phase to run, generally access or rewrite. If you don't know the [Openresty life cycle](https://openresty-reference.readthedocs.io/en/latest/Directives/), it's
recommended to know it in advance. For example key-auth is an authentication plugin, thus the authentication should be completed
before forwarding the request to any upstream service. Therefore, the plugin can be executed in the rewrite and access phases.
In APISIX, the authentication logic is implemented in the rewrite phase. Generally, IP access and interface
permission are completed in the access phase.
In APISIX, the authentication logic is implemented in the access phase. Generally, IP access and interface
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to change it as authz-keycloak is for authorization.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I can revert this one. However, note that some authentication plugins currently run in the access phase, not the rewrite one. So the document doesn’t reflect the current state correctly, I believe.

Copy link
Member

Choose a reason for hiding this comment

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

@jenskeiner
We can fix it later, but in those plugins, not the documentation.

Copy link
Member

Choose a reason for hiding this comment

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

@jenskeiner
Thank you for the problem you point out. I just search for all plugins which has type = 'auth' (excluding the authz-keycloak) and find out basic-auth doesn't run the phase correctly.

Copy link
Member

Choose a reason for hiding this comment

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

The reason why we need to run authentication plugins in the rewrite phases:

apisix/apisix/init.lua

Lines 508 to 520 in f4161d3

run_plugin("rewrite", plugins, api_ctx)
if api_ctx.consumer then
local changed
route, changed = plugin.merge_consumer_route(
route,
api_ctx.consumer,
api_ctx
)
if changed then
core.table.clear(api_ctx.plugins)
api_ctx.plugins = plugin.filter(route, api_ctx.plugins)
end
end

And:
if plugin_obj.type == 'auth' then

Plugins has type = 'auth' should run in rewrite phase and set up the consumer for current api_ctx.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, understood why you need to run in the rewrite phase. Then I would propose to change the phase for all authentication-type plugins to rewrite, instead of adjusting the phase for authz-keycloak from rewrite to access. Also, I think it would make the plugin development guidelines clearer if the reason they need to run in the rewrite phase was pointed out more clearly.

If you're fine with this, I can update this PR accordingly. Just let me know what you think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need to change it as authz-keycloak is for authorization.

Reverted.

@membphis
Copy link
Member

@membphis @jenskeiner
authz-keycloak is for authorization but not authentication. It doesn't interact with consumers, so I think this change is fine.

many thx for your explain. @spacewander
we can continue this PR

… authz-keycloak as authentication plugin anymore, since it's only concerned with authorization.
@@ -125,7 +125,7 @@ do
end
end

function _M.access(conf, ctx)
function _M.rewrite(conf, ctx)
Copy link
Member

Choose a reason for hiding this comment

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

Need a test case to check if the ctx.consumer set by this plugin can be used by other plugin runs in access phase.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to add a test case. I don't have much experience writing them yet, so if you could point to a similar example, that would be helpful.

Copy link
Member

Choose a reason for hiding this comment

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

You can add a stub:

diff --git apisix/init.lua apisix/init.lua
index 7333dd1..411cdd4 100644
--- apisix/init.lua
+++ apisix/init.lua
@@ -514,6 +514,10 @@ function _M.http_access_phase()
                 api_ctx.consumer,
                 api_ctx
             )
+
+            core.log.info("find consumer ", api_ctx.consumer.username,
+                          ", config changed: ", changed)
+
             if changed then
                 core.table.clear(api_ctx.plugins)
                 api_ctx.plugins = plugin.filter(route, api_ctx.plugins)

And then check it in:

=== TEST 8: verify

Add something like:

--- error_log
find consumer foo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I think I've seen an example in the test cases for key-auth. I'll try to add test cases acceding for basic-auth.

@@ -168,8 +168,8 @@ local _M = {
Determine which phase to run, generally access or rewrite. If you don't know the [Openresty life cycle](https://openresty-reference.readthedocs.io/en/latest/Directives/), it's
recommended to know it in advance. For example key-auth is an authentication plugin, thus the authentication should be completed
before forwarding the request to any upstream service. Therefore, the plugin can be executed in the rewrite and access phases.
In APISIX, the authentication logic is implemented in the rewrite phase. Generally, IP access and interface
permission are completed in the access phase.
In APISIX, the authentication logic is implemented in the access phase. Generally, IP access and interface
Copy link
Member

Choose a reason for hiding this comment

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

Remember to revert this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -125,7 +125,7 @@ do
end
end

function _M.access(conf, ctx)
function _M.rewrite(conf, ctx)
Copy link
Member

Choose a reason for hiding this comment

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

why do we need to change another plugin?

one PR for one thing, we should only update something about plugin authz-keycloak

please revert this code

@@ -49,6 +49,7 @@ local schema = {
local _M = {
version = 0.1,
priority = 2599,
type = 'auth',
Copy link
Member

Choose a reason for hiding this comment

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

ditto

you can create a new PR to fix this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm sorry but I currently can't afford the time to split this into separate PRs. I think the change set is reasonably small to be considered in one PR. If you prefer more fine-grained PRs, then please consider creating these yourself, e.g. based on the changes in this branch.

Copy link
Member

Choose a reason for hiding this comment

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

I found you have submitted new PR: https://github.com/apache/apisix/pull/2905/files

Many thanks for your nice job.

Small PR is important, it easy to find problems and we can merge it asap when it is small.

By the way, it is a clearer way to show what we are doing.

Many thx for nice contribution again @jenskeiner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, you're very welcome. Thanks for the info. I'll try to put the remaining outstanding changes from this one into a separate new PR and then we take it from there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have created #2910 which should contain the outstanding changes from this PR.

@jenskeiner jenskeiner changed the title fix: authz keycloak should run in access phase fix: authz-keycloak plugin should not be marked as auth-type and should run in access phase, basic-auth plugin should run in rewrite phase. Nov 30, 2020
@jenskeiner
Copy link
Contributor Author

Closing this one as it has since been superseded by other PRs addressing the same issues.

@jenskeiner jenskeiner closed this Dec 1, 2020
@jenskeiner jenskeiner deleted the 2857-authz-keycloak-should-run-in-access-phase branch December 1, 2020 08:59
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.

3 participants