This is an automated email from the ASF dual-hosted git repository.
membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 89f4389 fix[key-auth]: skip consumer when config has no key (#2120)
89f4389 is described below
commit 89f4389ba726dc839660b9801b3c7ff825635d99
Author: Vinci Xu <[email protected]>
AuthorDate: Fri Sep 18 21:49:01 2020 +0800
fix[key-auth]: skip consumer when config has no key (#2120)
---
apisix/plugins/key-auth.lua | 4 +++-
t/plugin/key-auth.t | 57 +++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 58 insertions(+), 3 deletions(-)
diff --git a/apisix/plugins/key-auth.lua b/apisix/plugins/key-auth.lua
index 15c451f..50fbdbf 100644
--- a/apisix/plugins/key-auth.lua
+++ b/apisix/plugins/key-auth.lua
@@ -46,7 +46,9 @@ do
for _, consumer in ipairs(consumers.nodes) do
core.log.info("consumer node: ", core.json.delay_encode(consumer))
- consumer_ids[consumer.auth_conf.key] = consumer
+ if consumer.auth_conf.key then
+ consumer_ids[consumer.auth_conf.key] = consumer
+ end
end
return consumer_ids
diff --git a/t/plugin/key-auth.t b/t/plugin/key-auth.t
index a47292e..2909ccc 100644
--- a/t/plugin/key-auth.t
+++ b/t/plugin/key-auth.t
@@ -73,7 +73,7 @@ done
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
- local code, body = t('/apisix/admin/consumers',
+ local code, body = t('/apisix/admin/consumers/1',
ngx.HTTP_PUT,
[[{
"username": "jack",
@@ -194,7 +194,7 @@ GET /hello
for i = 1, 20 do
username = "user_" .. tostring(i)
key = "auth-" .. tostring(i)
- code, body = t('/apisix/admin/consumers',
+ code, body = t(string.format('/apisix/admin/consumers/%d',
tostring(i)),
ngx.HTTP_PUT,
string.format('{"username":"%s","plugins":{"key-auth":{"key":"%s"}}}',
username, key),
string.format('{"node":{"value":{"username":"%s","plugins":{"key-auth":{"key":"%s"}}}},"action":"set"}',
username, key)
@@ -215,3 +215,56 @@ apikey: auth-13
["passed\n", "hello world\n"]
--- no_error_log
[error]
+
+
+
+=== TEST 9: add consumer with empty key
+--- 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": "error",
+ "plugins": {
+ "key-auth": {
+ }
+ }
+ }]],
+ [[{
+ "node": {
+ "value": {
+ "username": "error",
+ "plugins": {
+ "key-auth": {
+ }
+ }
+ }
+ },
+ "action": "set"
+ }]]
+ )
+
+ ngx.status = code
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 10: valid consumer
+--- request
+GET /hello
+--- more_headers
+apikey: auth-one
+--- response_body
+hello world
+--- no_error_log
+[error]