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(key-auth) add endpoint to list all key-auths #2955

Closed
wants to merge 2 commits into from

Conversation

hbagdi
Copy link
Member

@hbagdi hbagdi commented Oct 13, 2017

Summary

Adding an endpoint to list all the keys of all consumers.
This is an initial commit to get better feedback from the community.

  • The endpoint could be changed to something else.
  • Grouping of multiple keys belonging to the same consumer is something I'm considering
  • A similar thing could be done for the ACL plugin.

Tests are missing right now.

Full changelog

  • Implement an endpoint GET /plugins/key-auth/consumers to list all the consumers with their key(s).

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.

Hi,

Thank you for giving this a try! This is a feature we are very interested in. If you are willing to work this us on updating this PR, we would gladly accept it.

I gave some suggestions on the endpoints themselves. We will also require of you that you write tests for these new endpoints. Those tests will have to belong to the key-auth plugin test suite.

Thank you!

@@ -52,5 +52,10 @@ return {
DELETE = function(self, dao_factory)
crud.delete(self.keyauth_credential, dao_factory.keyauth_credentials)
end
},
["/plugins/key-auth/consumers"] = {
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 to me like the intent of such an endpoint is to retrieve a paginated list key-auth credentials. One of the use-cases that this solves is "retrieving a particular key-auth in order to read it's consumer_id and retrieve the Consumer associated with a key-auth credential" (as the title of this PR suggests). I think this would mean that users need to make the following request:

/plugins/key-auth/consumers?id=...
or
/plugins/key-auth/consumers?key=...

However, such a request could only return a single result, as a key-auth credential can only be associated with a single Consumer. Additionally, the current implementation seems to return key-auth credential, and makes forces the client to make a subsequent request to retrieve the full Consumer entity. Finally, the URL of this request is confusing in the sense that user isn't sure if the response will be a list of Plugins, key-auth, or Consumers...

If we want to add both a paginated set of all key-auth credentials, and the ability to retrieve the Consumer associated with a credential, it might be friendlier and more appropriate (RESTful) to solve those two requirements with two endpoints:

/key-auth/
/consumers/key-auth/:credential_key_or_id

The reasoning is that in our RESTful interface, we try to prefix a resource's endpoint with the type of entities the endpoint will return. The first endpoint returns a list of key-auth credentials. The second one directly returns a single Consumer associated with this credential (or 404 Not Found) without the need for the client to send 2 requests (the first one for the key-auth credential, and the next one to finally request the Consumer from the retrieved consumer_id).

What do you think?

@thibaultcha
Copy link
Member

thibaultcha commented Oct 13, 2017

Grouping of multiple keys belonging to the same consumer is something I'm considering

That'd be great too! The endpoint could relate to the one I suggested in my review, which would get us 2 new endpoints:

/key-auth/
/consumers/:consumer/key-auth/

A similar thing could be done for the ACL plugin.

We are also interested similar endpoints for the ACL, basic-auth, hmac and JWT plugins if they do not exist yet! But let's process step-by-step. We should keep the scope of this PR limited to the key-auth plugin only :) We will gladly accept more contributions for other credentials-based plugins once we're square on this first plugin.

Tests are missing right now.

Great!

@hbagdi
Copy link
Member Author

hbagdi commented Oct 13, 2017

@thibaultcha Thanks a lot for the feedback.

  • /key-auth/ endpoint to list all the key-auth credential makes a lot of sense really since what it returns is key-auths. Other auth plugins could have similar GET all endpoints. I'll get this one done first.
  • The endpoint you metion/consumers/key-auth/:credential_key_or_id to retrieve a consumer for a given key-credential is a very useful endpoint to have.
    However, consider /consumers/:consumer_id_or_name/key-auth, which returns all the key-auths for the specific consumer. To maintain consistency, we can instead have an endpoint /key-auth/: credential_key_or_id/consumer to retrieve an consumer for a key or key_id.

Thoughts?

I'll try to get this into a good shape and once we're done here, we can take on other plugins as well.

@thibaultcha
Copy link
Member

@hbagdi Yes, sorry, I meant /consumers/:consumer/key-auth/. I updated my comment to reflect this. Your proposal resonates to me. If I understand correctly, this would add the following two endpoints to our key-auth plugin:

1. /key-auth/
2. /key-auth/:credential_key_or_id/consumer
  1. List all key-auth credentials in a paginated fashion. It's also a way to retrieve all credentials belonging to a Consumer, via filtering (/key-auth?consumer_id=...).
  2. In a single call, retrieve the Consumer associated with a given key-auth credential.

Am I missing anything?

@hbagdi
Copy link
Member Author

hbagdi commented Oct 14, 2017

Sounds good to me.

So, this PR will add the following:

  1. /key-auth/
  2. /key-auth/:credential_key_or_id/consumer
  3. Tests for 1 and 2

I'm done with 2 and half of 1 (need filtering).
I'll add tests and let you know.

Thanks for being prompt!

@hbagdi hbagdi force-pushed the list-consumer-keys branch 2 times, most recently from 3b198a1 to bd3d64e Compare October 14, 2017 05:14
@hbagdi
Copy link
Member Author

hbagdi commented Oct 14, 2017

@thibaultcha I've updated the PR to reflect our discussion.

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.

This looks great! I haven't run it myself yet, but for now, I don't think the endpoints are missing anything. I do think we are missing a few tests though.

Thanks!

@@ -310,4 +311,117 @@ describe("Plugin: key-auth (API)", function()
assert.equal(key_name, body.config.key_names[1])
end)
end)
describe("/Key-auth", function()
Copy link
Member

Choose a reason for hiding this comment

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

Please name this test suite as per the endpoint (not capitalized): /key-auth

method = "GET",
path = "/key-auth",
body = {
consumer_id = 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.

I think this might be the same test case as above ("/key-auth?consumer_id=" .. consumer.id) with a different syntax allowed by lua-resty-http/our spec helpers? If not, then GET requests shouldn't contain a body anyways. Better remove this test-case regardless: it's either a duplicate of the above one, or a use-case we don't wish to support (and that should be fixed and tested in lua-resty-http or our helpers functions)

method = "GET",
path = "/key-auth",
body = {
consumer_id = utils.uuid()
Copy link
Member

Choose a reason for hiding this comment

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

ditto, please specify URI arguments with the following syntax to avoid confusion:

path = "/key-auth?consumer_id=" .. utils.uuid()

end)
describe("/key-auth/:credential_key_or_id/consumer", function()
describe("GET", function()
local credential;
Copy link
Member

Choose a reason for hiding this comment

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

style: please remove this semicolon

it("retrieve all the key-auths", function()
local res = assert(admin_client:send {
method = "GET",
path = "/key-auth"
Copy link
Member

Choose a reason for hiding this comment

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

Could we add a test case with a URI containing a trailing slash (/key-auth/)? We've had issues with our Lapis routes and trailing slashes in the last few months and we want to avoid it from happening again. A test will help prevent that :) Thanks!

self.params.username_or_id = credentials[1].consumer_id
crud.find_consumer_by_username_or_id(self, dao_factory, helpers)
end,
GET = function(self, dao_factory,helpers)
Copy link
Member

Choose a reason for hiding this comment

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

style: we are missing a single line jump above this handler declaration (see other handlers in this file)

it("returns 404 for a random id", function()
local res = assert(admin_client:send {
method = "GET",
path = "/key-auth/" .. utils.uuid() .. "/consumer"
Copy link
Member

Choose a reason for hiding this comment

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

Could we also add a test case with some non-UUID garbage? The reason is that if the value is not recognized as a valid UUID, then the admin API will treat it as the credential key. We need to make sure a non-existing key also returns HTTP 404. Thanks!

local json = cjson.decode(body)
assert.same(consumer,json)
end)
it("retrieve consumer from a keyid", function()
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 this test should rather be named: "retrieves a Consumer from a credential's 'key'"

@hbagdi hbagdi force-pushed the list-consumer-keys branch 2 times, most recently from 7b902aa to e9e6bc1 Compare October 16, 2017 03:37
@hbagdi
Copy link
Member Author

hbagdi commented Oct 16, 2017

@thibaultcha I've added and corrected the tests.

it("retrieve all the key-auths", function()
local res = assert(admin_client:send {
method = "GET",
path = "/key-auth/"
Copy link
Member

Choose a reason for hiding this comment

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

We should test with, and without the trailing slash here is what I meant :) Sorry if it wasn't clear in my previous comment!

Copy link
Member Author

Choose a reason for hiding this comment

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

@thibaultcha Sorry, my bad. I intended to do what you suggested but it somehow ended up being something else; fixed it.

@hbagdi hbagdi force-pushed the list-consumer-keys branch from e9e6bc1 to 4eceb7e Compare October 16, 2017 15:04
@hbagdi
Copy link
Member Author

hbagdi commented Oct 19, 2017

@thibaultcha having the endpoint as /key-auths makes more sense?

@hbagdi hbagdi changed the title Add endpoint to list all consumers using key-auth plugin and their keys feat(key-auth) Add endpoint to list all consumers using key-auth plugin and their keys Oct 31, 2017
@hbagdi hbagdi changed the title feat(key-auth) Add endpoint to list all consumers using key-auth plugin and their keys feat(key-auth) Add endpoint to list all key-auths Oct 31, 2017
@hbagdi hbagdi changed the title feat(key-auth) Add endpoint to list all key-auths feat(key-auth) add endpoint to list all key-auths Nov 1, 2017
@thibaultcha
Copy link
Member

having the endpoint as /key-auths makes more sense?

Hmmm yeah, probably. This would stay consistent with the /acls endpoint proposed in #2371. Unfortunately our existing key-auth endpoints use a singular form (/consumers/:username_or_id/key-auth/), but let's try to avoid this mistake again.

If you update this PR to use the plural form you suggested, I don't see any further blockers to merge it. Thanks!

@thibaultcha thibaultcha added the pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. label Nov 1, 2017
@hbagdi
Copy link
Member Author

hbagdi commented Nov 2, 2017

@thibaultcha I'll send PRs for similar endpoints for JWT and hmac soon then.

thibaultcha pushed a commit that referenced this pull request Nov 2, 2017
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

From #2955

Signed-off-by: Thibault Charbonnier <thibaultcha@me.com>
@thibaultcha
Copy link
Member

Manually merged to master with an updated commit message and some minor edits, including a new test ensuring the /key-auths endpoint is paginated :)

This is awesome, thank you very much for giving a stab at those! Onwards to JWT, basic-auth, and acls 😉

@thibaultcha thibaultcha closed this Nov 2, 2017
@thibaultcha
Copy link
Member

@hbagdi Oh, I was almost forgetting: will you also contribute the appropriate documentation on our open source docs repository: https://github.com/Kong/getkong.org/ ? Would be greatly appreciated!

You can find the key-auth plugin documentation here: https://getkong.org/plugins/key-authentication/ with, in the sidebar, some anchors that lead to various sections of the page describing the additional endpoints provided by this plugin. Maybe 2 more sections would be appropriate here:

  1. Browse all key-auth credentials
  2. Retrieve the Consumer associated with a key-auth credential

We do value complete contributions including code, tests and documentation :)

@hbagdi hbagdi deleted the list-consumer-keys branch November 3, 2017 02:54
@hbagdi
Copy link
Member Author

hbagdi commented Nov 3, 2017

@thibaultcha I did have documentation on mind. I'll send out a PR soon.

hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 7, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 7, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 7, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 19, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 22, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 22, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 22, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 22, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 22, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 22, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
hbagdi added a commit to hbagdi/getkong.org that referenced this pull request Nov 22, 2017
Two new endpoints have been added to the key-auth plugin:
* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
associated with a credential

See: Kong/kong#2955
thibaultcha pushed a commit to Kong/docs.konghq.com that referenced this pull request Nov 22, 2017
Two new endpoints have been added to the key-auth plugin:

* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
  associated with a credential

See: Kong/kong#2955
From: #546

Signed-off-by: Thibault Charbonnier <thibaultcha@me.com>
thibaultcha pushed a commit to Kong/docs.konghq.com that referenced this pull request Nov 23, 2017
Two new endpoints have been added to the key-auth plugin:

* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
  associated with a credential

See: Kong/kong#2955
From: #546

Signed-off-by: Thibault Charbonnier <thibaultcha@me.com>
thibaultcha pushed a commit to Kong/docs.konghq.com that referenced this pull request Nov 29, 2017
Two new endpoints have been added to the key-auth plugin:

* `/key-auths/` to paginate through key-auth credentials
* `/key-auths/:credential_key_or_id/consumer` to retrieve the Consumer
  associated with a credential

See: Kong/kong#2955
From: #546

Signed-off-by: Thibault Charbonnier <thibaultcha@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants