From a92d7e80b3792044defde3bab20c561c351057f3 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Mon, 25 Jul 2022 12:18:58 +0800 Subject: [PATCH 1/6] fix: auth plugin repeats the rewrite phase --- apisix/plugin.lua | 10 +++++-- t/node/consumer-plugin2.t | 60 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/apisix/plugin.lua b/apisix/plugin.lua index e87dbc750025..752cb22499d3 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -875,14 +875,20 @@ function _M.run_plugin(phase, plugins, api_ctx) return api_ctx end + local ori_phase = phase if phase ~= "log" and phase ~= "header_filter" and phase ~= "body_filter" and phase ~= "delayed_body_filter" then for i = 1, #plugins, 2 do - if phase == "rewrite_in_consumer" and plugins[i + 1]._from_consumer - and plugins[i].type ~= "auth"then + if ori_phase == "rewrite_in_consumer" and not plugins[i + 1]._from_consumer + and plugins[i].type == "auth" then + plugins[i + 1]._skip_rewrite_in_consumer = true + end + + if ori_phase == "rewrite_in_consumer" and plugins[i + 1]._from_consumer + and plugins[i].type ~= "auth" then phase = "rewrite" end local phase_func = plugins[i][phase] diff --git a/t/node/consumer-plugin2.t b/t/node/consumer-plugin2.t index 249441a6c1a4..b69636634037 100644 --- a/t/node/consumer-plugin2.t +++ b/t/node/consumer-plugin2.t @@ -238,3 +238,63 @@ x-real-ip: 127.0.0.1 } --- response_body {"key-auth":true,"proxy-rewrite":true} + + + +=== TEST 7: configure non-auth plugins in the consumer and run it's rewrite phase +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/consumers/jack', + ngx.HTTP_PUT, + [[{ + "username": "jack", + "plugins": { + "key-auth": { + "key": "auth-jack" + }, + "ip-restriction": { + "blacklist": [ + "127.0.0.0/24" + ] + } + } + }]] + ) + + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "key-auth": {} + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 8: hit routes and ip-restriction work well +--- request +GET /hello +--- more_headers +apikey: auth-jack +--- error_code: 403 +--- response_body +{"message":"Your IP address is not allowed"} From 0a6e7fc98ba2b9e229b18916b54a941cfef1b915 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Tue, 26 Jul 2022 13:04:22 +0800 Subject: [PATCH 2/6] resolve code review --- apisix/plugin.lua | 21 ++++++++++----------- apisix/plugins/key-auth.lua | 1 + 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 752cb22499d3..01f8c2f55b0e 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -875,28 +875,27 @@ function _M.run_plugin(phase, plugins, api_ctx) return api_ctx end - local ori_phase = phase if phase ~= "log" and phase ~= "header_filter" and phase ~= "body_filter" and phase ~= "delayed_body_filter" then for i = 1, #plugins, 2 do - if ori_phase == "rewrite_in_consumer" and not plugins[i + 1]._from_consumer - and plugins[i].type == "auth" then - plugins[i + 1]._skip_rewrite_in_consumer = true - end - - if ori_phase == "rewrite_in_consumer" and plugins[i + 1]._from_consumer - and plugins[i].type ~= "auth" then - phase = "rewrite" + local phase_func + if phase == "rewrite_in_consumer" then + if not plugins[i + 1]._from_consumer and plugins[i].type == "auth" then + plugins[i + 1]._skip_rewrite_in_consumer = true + end + phase_func = plugins[i]["rewrite"] + else + phase_func = plugins[i][phase] end - local phase_func = plugins[i][phase] - if phase == "rewrite" and plugins[i + 1]._skip_rewrite_in_consumer then + if phase == "rewrite_in_consumer" and plugins[i + 1]._skip_rewrite_in_consumer then goto CONTINUE end + ngx.log(ngx.WARN, "phase_func : ", require("inspect")(phase_func)) if phase_func then plugin_run = true local conf = plugins[i + 1] diff --git a/apisix/plugins/key-auth.lua b/apisix/plugins/key-auth.lua index 3c0f8a97acb2..e515e6f2723d 100644 --- a/apisix/plugins/key-auth.lua +++ b/apisix/plugins/key-auth.lua @@ -89,6 +89,7 @@ end function _M.rewrite(conf, ctx) + ngx.log(ngx.WARN, "first enter key-auth") local from_header = true local key = core.request.header(ctx, conf.header) From 50c0b1f3528bf95a8c2b8212273bc9eabcbbeaf3 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Tue, 26 Jul 2022 13:06:39 +0800 Subject: [PATCH 3/6] rm log --- apisix/plugins/key-auth.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/apisix/plugins/key-auth.lua b/apisix/plugins/key-auth.lua index e515e6f2723d..3c0f8a97acb2 100644 --- a/apisix/plugins/key-auth.lua +++ b/apisix/plugins/key-auth.lua @@ -89,7 +89,6 @@ end function _M.rewrite(conf, ctx) - ngx.log(ngx.WARN, "first enter key-auth") local from_header = true local key = core.request.header(ctx, conf.header) From 41d37a7e271f011f90d3e0f87e4cb7e90f0bea11 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Tue, 26 Jul 2022 15:34:49 +0800 Subject: [PATCH 4/6] ignore more auth plugin in consumer --- apisix/plugin.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 01f8c2f55b0e..22100030f281 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -883,7 +883,7 @@ function _M.run_plugin(phase, plugins, api_ctx) for i = 1, #plugins, 2 do local phase_func if phase == "rewrite_in_consumer" then - if not plugins[i + 1]._from_consumer and plugins[i].type == "auth" then + if plugins[i].type == "auth" then plugins[i + 1]._skip_rewrite_in_consumer = true end phase_func = plugins[i]["rewrite"] From 37dcae04d78951e1eb3c169c35471ecdba838ede Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Tue, 26 Jul 2022 17:46:13 +0800 Subject: [PATCH 5/6] rm log --- apisix/plugin.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 22100030f281..a624a56961c6 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -895,7 +895,6 @@ function _M.run_plugin(phase, plugins, api_ctx) goto CONTINUE end - ngx.log(ngx.WARN, "phase_func : ", require("inspect")(phase_func)) if phase_func then plugin_run = true local conf = plugins[i + 1] From f1f71f267f879887657b7bb29a9a73cf8300df57 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 27 Jul 2022 16:04:42 +0800 Subject: [PATCH 6/6] resolve code review --- t/node/consumer-plugin2.t | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/t/node/consumer-plugin2.t b/t/node/consumer-plugin2.t index b69636634037..c05762f40e2d 100644 --- a/t/node/consumer-plugin2.t +++ b/t/node/consumer-plugin2.t @@ -261,7 +261,12 @@ x-real-ip: 127.0.0.1 } } }]] - ) + ) + if code >= 300 then + ngx.status = code + ngx.say(body) + return + end local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, @@ -277,7 +282,7 @@ x-real-ip: 127.0.0.1 }, "uri": "/hello" }]] - ) + ) if code >= 300 then ngx.status = code