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

feat(acl) allow acl plugin to use a consumer without credentials #2722

Merged
merged 2 commits into from
Jul 26, 2017

Conversation

bungle
Copy link
Member

@bungle bungle commented Jul 21, 2017

Summary

Sometimes Kong is not the provider of credentials, but a third-party identity provider does that. E.g. with OpenID Connect and LDAP the credentials are stored in 3rd party IdP or directory.

ACL plugin on the other hand did only work with ngx.ctx.authenticated_credential, this patch adds a support to ngx.ctx.authenticated_consumer as well.

Copy link
Member

@kikito kikito left a comment

Choose a reason for hiding this comment

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

Just a style comment. @bungle if you don't agree it can still be merged.

consumer_id = ngx.ctx.authenticated_credential.consumer_id
else
end

Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be simpler to write an elseif here?

if ngx.ctx.authenticated_consumer then
  consumer_id = ngx.ctx.authenticated_consumer.id
elseif ngx.ctx.authenticated_credential then
  consumer_id = ngx.ctx.authenticated_credential.consumer_id
end

Or even

local consumer_id = (ngx.ctx.authenticated_consumer and ngx.ctx.authenticated_consumer.id) or
                    (ngx.ctx.authenticated_credential and ngx.ctx.authenticated_credential.consumer_id)

(That second one might be a bit too much)

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd be in favor of the latter approach ^ though the parens aren't necessary I don't think.

Copy link
Member Author

@bungle bungle Jul 21, 2017

Choose a reason for hiding this comment

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

Actually no. There is always change that ngx.ctx.authenticated_credential is set but ngx.ctx.authenticated_credential.customer_id results nil. For example right now I'm working this around by setting ngx.ctx.authenticated_credential.customer_id, but that feels wrong as I cannot set ngx.ctx.authenticated_credential.id as there is no credential (and that could screw another plugin). My approach works even if people do stupid things, and it doesn't hurt anything (maybe just one extra nil check).

Copy link
Member Author

@bungle bungle Jul 21, 2017

Choose a reason for hiding this comment

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

What I mean here is that even if there is ngx.ctx.authenticated_consumer table or ngx.ctx. authenticated_credential table, there is no gurantee that it has any properties. We could go on, and even argue that the current and this patched code will crash if ngx.ctx.authenticated_consumer or ngx.ctx.authenticated_credential are not tables.

@bungle bungle force-pushed the feat/acl-plugin-consumer branch from 7ebdaba to ddf2adf Compare July 21, 2017 21:19
@bungle
Copy link
Member Author

bungle commented Jul 21, 2017

I pushed a new version that also doesn't crash if the used context variables are not tables (be liberal in what you accept).

Copy link
Member

@thibaultcha thibaultcha left a comment

Choose a reason for hiding this comment

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

LGTM, except for the mix of feature/style/perf changes in the same PR/commit. It'd be great to have then separated!


local ACLHandler = BasePlugin:extend()


Copy link
Member

Choose a reason for hiding this comment

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

It seems like this PR has many style changes. As we've agreed upon by the past, could we limit the scope of the changes in the PRs? Or if we want those changes in the same PR, could they be in separate commits at least?

  • feat(acl) work with consumer
  • style(acl) update to new style
  • perf(acl) minor improvements


if not consumer_id then
return responses.send_HTTP_FORBIDDEN(
"Cannot identify the consumer, add an authentication plugin to use the ACL plugin"
Copy link
Member

Choose a reason for hiding this comment

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

aside note: welp, I never saw this before, but I don't think user-facing errors/responses should talk about the notion of "plugins". This should probably be logged for the Kong admin to see (ngx.log(ngx.WARN, ...) maybe), and the 403 response shown to the user should be blank. This is, of course, out of the scope of this PR and is definitely not a blocker for this PR imo :)

cache.acls_key(consumer_id),
nil,
load_acls_into_memory,
consumer_id)
Copy link
Member

Choose a reason for hiding this comment

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

Any reason for this change and the new line below? I think it was fine as it was (per the code style), except for the second line not aligning with the first argument. Such style changes should be part of another commit/PR anyways.

@@ -60,14 +89,17 @@ function ACLHandler:access(conf)
if next(conf.whitelist or empty) then
if not next(acls or empty) then
block = true

Copy link
Member

Choose a reason for hiding this comment

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

ditto: all those style changes - while appreciated, it'd be great if they could at least be included in a separate commit :) Thanks!

@@ -83,7 +115,9 @@ function ACLHandler:access(conf)
for _, v in ipairs(acls) do
table_insert(str_acls, v.group)
end
ngx.req.set_header(constants.HEADERS.CONSUMER_GROUPS, table_concat(str_acls, ", "))

set_header(constants.HEADERS.CONSUMER_GROUPS, table_concat(str_acls, ", "))
Copy link
Member

Choose a reason for hiding this comment

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

Such a change should also be separated in its own perf(acl) commit as well

@thibaultcha thibaultcha added pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. and removed pr/status/please review labels Jul 22, 2017
@Tieske Tieske force-pushed the feat/acl-plugin-consumer branch from ddf2adf to 1225a7d Compare July 26, 2017 11:49
@thibaultcha thibaultcha merged commit 2461e04 into master Jul 26, 2017
@Tieske Tieske added pr/ready This PR is considered ready and can be merged at anytime (given it received no subsequent changes) and removed pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. labels Jul 27, 2017
@Tieske Tieske deleted the feat/acl-plugin-consumer branch July 27, 2017 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/ready This PR is considered ready and can be merged at anytime (given it received no subsequent changes)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants