fix[key-auth]: skip consumer when config has no key (#2120)
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 @@
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 @@
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 @@
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 @@
["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]