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

hotfix(admin-api) fixes an edge-case and returns 405 on invalid methods #2213

Merged
merged 2 commits into from
Mar 24, 2017

Conversation

subnetmarco
Copy link
Member

Full changelog

  • Fixes an edge-case when requesting the Admin API with an invalid method

@Tieske
Copy link
Member

Tieske commented Mar 16, 2017

The better solution would be to add a metatable to the methods table here; https://github.com/Mashape/kong/blob/8f8b4ef3e38029b422f89440f852dd1beace3ce6/kong/api/init.lua#L99-L112

If a lookup fails in that table, it should return a function that executes a 405.

@Tieske
Copy link
Member

Tieske commented Mar 20, 2017

@thibaultcha @thefosk I updated this PR. It seems that Lapis is adding too much magic under the hood for the metatable approach to work properly, hence falling back on the error string checking as implemented by @thefosk.

With the exception that the existing error mechanism (that didn't catch custom http methods) is now removed, as it doubled.

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.

Ok, noted, that's fine then. A couple points to still address imo:

  1. Some new code was added which doesn't respect a couple best practices (else coupled with a returning branch and string.match performing a pattern search when not needed)
  2. If we also update this module to use the new code style, then let's fully apply it

Note: Some code in there is returning from branches, but this patch is not touching it (except for style), so I don't feel like we should update them necessarily. Just adding a precision since 1. pointed the same issue, but only in the context of code appearing in this patch.

if err and err:match("don't know how to respond to") then
return responses.send_HTTP_METHOD_NOT_ALLOWED()

else
Copy link
Member

Choose a reason for hiding this comment

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

no else here as the previous branch returns

ngx.log(ngx.ERR, err.."\n"..trace)
-- We just logged the error so no need to give it to responses and log it twice
return responses.send_HTTP_INTERNAL_SERVER_ERROR()

Copy link
Member

Choose a reason for hiding this comment

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

style: no blank line here

local app = lapis.Application()

local needs_body = tablex.readonly({ PUT = 1, POST = 2, PATCH = 3 })
Copy link
Member

Choose a reason for hiding this comment

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

It's good that we are switching to the new code style in this module, but then let's also respect the 2 lines jump here as well: between the require section, the "globals caching section", the "Lapis App creation", and the constants definition for this module.

local function attach_routes(routes)

Copy link
Member

Choose a reason for hiding this comment

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

style: there should be no such blank line when entering a method

local function parse_params(fn)

Copy link
Member

Choose a reason for hiding this comment

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

style: blank line when entering a function is not part of the code style

-- Loading plugins routes
if singletons.configuration and singletons.configuration.plugins then

Copy link
Member

Choose a reason for hiding this comment

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

style: no such blank lines required when entering a block/function

for k in pairs(singletons.configuration.plugins) do

Copy link
Member

Choose a reason for hiding this comment

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

ditto: blank line entering a block

else
ngx.log(ngx.DEBUG, "No API endpoints loaded for plugin: ", k)
end

Copy link
Member

Choose a reason for hiding this comment

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

No such blank line before closing a block either, or at least, not enforced.

-- We just logged the error so no need to give it to responses and log it twice
return responses.send_HTTP_INTERNAL_SERVER_ERROR()

if err and err:match("don't know how to respond to") then
Copy link
Member

@thibaultcha thibaultcha Mar 20, 2017

Choose a reason for hiding this comment

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

string.match should probably just perform a plain text search if no patterns are needed here.

@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/needs review labels Mar 20, 2017
@p0pr0ck5
Copy link
Contributor

Can we avoid style changes in the context of this PR? We could open a separate PR if we care deeply for such style, but adding here seems to introduce unnecessary complexity, dirties the git history, and reduces atomicity of the commit.

@thibaultcha thibaultcha force-pushed the hotfix/admin-invalid-method branch from bb5ddab to fa23ee8 Compare March 21, 2017 18:01
@thibaultcha thibaultcha force-pushed the hotfix/admin-invalid-method branch from fa23ee8 to 232f4f1 Compare March 21, 2017 18:30
@thibaultcha
Copy link
Member

Re-worked this patch (new history) to properly introduce those 2 different changes, fix + style.

@thibaultcha thibaultcha added pr/ready (but hold merge) No more concerns, but do not merge yet (probably a conflict of interest with another PR or release) and removed pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. labels Mar 21, 2017
@Tieske Tieske merged commit 172f267 into master Mar 24, 2017
@Tieske Tieske deleted the hotfix/admin-invalid-method branch March 24, 2017 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/ready (but hold merge) No more concerns, but do not merge yet (probably a conflict of interest with another PR or release)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants