You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ju...@apache.org on 2020/11/10 08:08:57 UTC

[apisix] branch master updated: fix(key-auth): 'key' is required in consumer configuration (#2689)

This is an automated email from the ASF dual-hosted git repository.

juzhiyuan 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 2c37f0e  fix(key-auth): 'key' is required in consumer configuration (#2689)
2c37f0e is described below

commit 2c37f0e5f290307a30a198a9be18c6cb4b8c69d0
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Tue Nov 10 16:08:46 2020 +0800

    fix(key-auth): 'key' is required in consumer configuration (#2689)
    
    Close #2686.
    
    According to the
    https://github.com/apache/apisix/pull/2120#discussion_r477153261,
    instead of skipping consumer without key, we should make 'key' required
    only for consumer configuration.
---
 apisix/plugins/key-auth.lua | 23 +++++++++++++++++------
 t/plugin/key-auth.t         | 23 +++++++----------------
 2 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/apisix/plugins/key-auth.lua b/apisix/plugins/key-auth.lua
index e72e729..0f713f0 100644
--- a/apisix/plugins/key-auth.lua
+++ b/apisix/plugins/key-auth.lua
@@ -26,9 +26,17 @@ local lrucache = core.lrucache.new({
 
 local schema = {
     type = "object",
+    additionalProperties = false,
+    properties = {},
+}
+
+local consumer_schema = {
+    type = "object",
+    additionalProperties = false,
     properties = {
         key = {type = "string"},
-    }
+    },
+    required = {"key"},
 }
 
 
@@ -38,6 +46,7 @@ local _M = {
     type = 'auth',
     name = plugin_name,
     schema = schema,
+    consumer_schema = consumer_schema,
 }
 
 
@@ -50,9 +59,7 @@ do
 
         for _, consumer in ipairs(consumers.nodes) do
             core.log.info("consumer node: ", core.json.delay_encode(consumer))
-            if consumer.auth_conf.key then
-                consumer_ids[consumer.auth_conf.key] = consumer
-            end
+            consumer_ids[consumer.auth_conf.key] = consumer
         end
 
         return consumer_ids
@@ -61,8 +68,12 @@ do
 end -- do
 
 
-function _M.check_schema(conf)
-    return core.schema.check(schema, conf)
+function _M.check_schema(conf, schema_type)
+    if schema_type == core.schema.TYPE_CONSUMER then
+        return core.schema.check(consumer_schema, conf)
+    else
+        return core.schema.check(schema, conf)
+    end
 end
 
 
diff --git a/t/plugin/key-auth.t b/t/plugin/key-auth.t
index 2909ccc..767c625 100644
--- a/t/plugin/key-auth.t
+++ b/t/plugin/key-auth.t
@@ -27,8 +27,9 @@ __DATA__
 --- config
     location /t {
         content_by_lua_block {
+            local core = require("apisix.core")
             local plugin = require("apisix.plugins.key-auth")
-            local ok, err = plugin.check_schema({key = 'test-key'})
+            local ok, err = plugin.check_schema({key = 'test-key'}, core.schema.TYPE_CONSUMER)
             if not ok then
                 ngx.say(err)
             end
@@ -49,8 +50,9 @@ done
 --- config
     location /t {
         content_by_lua_block {
+            local core = require("apisix.core")
             local plugin = require("apisix.plugins.key-auth")
-            local ok, err = plugin.check_schema({key = 123})
+            local ok, err = plugin.check_schema({key = 123}, core.schema.TYPE_CONSUMER)
             if not ok then
                 ngx.say(err)
             end
@@ -247,24 +249,13 @@ apikey: auth-13
                 )
 
             ngx.status = code
-            ngx.say(body)
+            ngx.print(body)
         }
     }
 --- request
 GET /t
+--- error_code: 400
 --- response_body
-passed
---- no_error_log
-[error]
-
-
-
-=== TEST 10: valid consumer
---- request
-GET /hello
---- more_headers
-apikey: auth-one
---- response_body
-hello world
+{"error_msg":"invalid plugins configuration: failed to check the configuration of plugin key-auth err: property \"key\" is required"}
 --- no_error_log
 [error]