From 23a47b9eaca3ef73e2a2953d57dd35449b5f46bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Sun, 15 Nov 2020 17:28:56 +0800 Subject: [PATCH] fix: user's route was prior to the plugin's one (#2733) --- apisix/init.lua | 12 +++-- t/plugin/plugin.t | 109 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 116 insertions(+), 5 deletions(-) diff --git a/apisix/init.lua b/apisix/init.lua index 769de3c2..43934885 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -365,8 +365,8 @@ function _M.http_access_phase() api_ctx.global_rules = router.global_rules end + local uri = api_ctx.var.uri if local_conf.apisix and local_conf.apisix.delete_uri_tail_slash then - local uri = api_ctx.var.uri if str_byte(uri, #uri) == str_byte("/") then api_ctx.var.uri = str_sub(api_ctx.var.uri, 1, #uri - 1) core.log.info("remove the end of uri '/', current uri: ", @@ -374,11 +374,15 @@ function _M.http_access_phase() end end - local user_defined_route_matched = router.router_http.match(api_ctx) - if not user_defined_route_matched then - router.api.match(api_ctx) + if core.string.has_prefix(uri, "/apisix/") then + local matched = router.api.match(api_ctx) + if matched then + return + end end + router.router_http.match(api_ctx) + local route = api_ctx.matched_route if not route then core.log.info("not find any matched route") diff --git a/t/plugin/plugin.t b/t/plugin/plugin.t index 9b502c2c..367df8b1 100644 --- a/t/plugin/plugin.t +++ b/t/plugin/plugin.t @@ -20,7 +20,10 @@ add_block_preprocessor(sub { my ($block) = @_; $block->set_value("no_error_log", "[error]"); - $block->set_value("request", "GET /t"); + + if (!defined $block->request) { + $block->set_value("request", "GET /t"); + } $block; }); @@ -52,3 +55,107 @@ __DATA__ } --- response_body ok + + + +=== TEST 2: define route for /* +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers', + ngx.HTTP_PUT, + [[{ + "username": "jack", + "plugins": { + "jwt-auth": { + "key": "user-key", + "secret": "my-secret-key" + } + } + }]]) + + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end + + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "jwt-auth": {} + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/*" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 3: sign +--- request +GET /apisix/plugin/jwt/sign?key=user-key +--- response_body_like eval +qr/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.\w+.\w+/ + + + +=== TEST 4: delete /* and define route for /apisix/plugin/blah +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code = t('/apisix/admin/routes/1', "DELETE") + if code >= 300 then + ngx.status = code + return + end + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "jwt-auth": {} + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/apisix/plugin/blah" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 5: hit +--- request +GET /apisix/plugin/blah +--- error_code: 401 +--- response_body +{"message":"Missing JWT token in request"}