shreemaan-abhishek commented on code in PR #12040:
URL: https://github.com/apache/apisix/pull/12040#discussion_r1998040377


##########
apisix/admin/consumers.lua:
##########
@@ -32,7 +34,50 @@ local function check_conf(username, conf, need_username, 
schema)
     if conf.plugins then
         ok, err = plugins.check_schema(conf.plugins, core.schema.TYPE_CONSUMER)
         if not ok then
-            return nil, {error_msg = "invalid plugins configuration: " .. err}
+            return nil, {
+                error_msg = "invalid plugins configuration: " .. err

Review Comment:
   we can keep this line unchanged.



##########
apisix/admin/consumers.lua:
##########
@@ -32,7 +34,50 @@ local function check_conf(username, conf, need_username, 
schema)
     if conf.plugins then
         ok, err = plugins.check_schema(conf.plugins, core.schema.TYPE_CONSUMER)
         if not ok then
-            return nil, {error_msg = "invalid plugins configuration: " .. err}
+            return nil, {
+                error_msg = "invalid plugins configuration: " .. err
+            }
+        end
+
+        -- check duplicate key
+        for plugin_name, plugin_conf in pairs(conf.plugins or {}) do
+            local plugin_obj = plugin.get(plugin_name)
+            if not plugin_obj then
+                return nil, {error_msg = "unknown plugin " .. plugin_name}
+            end
+
+            if plugin_obj.type == "auth" then
+                local decrypted_conf = core.table.deepcopy(plugin_conf)
+                plugin.decrypt_conf(plugin_name, decrypted_conf, 
core.schema.TYPE_CONSUMER)
+
+                local plugin_key_map = {
+                    ["key-auth"] = "key",
+                    ["basic-auth"] = "username",
+                    ["jwt-auth"] = "key",
+                    ["hmac-auth"] = "key_id"
+                }
+
+                local key_field = plugin_key_map[plugin_name]
+
+                if key_field then
+                    local key_value = decrypted_conf[key_field]
+
+                    if key_value then
+                        local consumer, _, err = consumer
+                            .find_consumer(plugin_name, key_field, key_value)
+                        if err then
+                            core.log.warn("failed to find consumer: ", err)

Review Comment:
   I don't understand the logic here. In the `find_consumer()` method call 
above. We are checking if the given consumer key already exists, no? If it 
doesn't exist then the duplication check should be successful (no duplicate 
found), right?
   
   If yes, then there is no point in printing a warn log.



##########
apisix/admin/consumers.lua:
##########
@@ -32,7 +34,50 @@ local function check_conf(username, conf, need_username, 
schema)
     if conf.plugins then
         ok, err = plugins.check_schema(conf.plugins, core.schema.TYPE_CONSUMER)
         if not ok then
-            return nil, {error_msg = "invalid plugins configuration: " .. err}
+            return nil, {
+                error_msg = "invalid plugins configuration: " .. err
+            }
+        end
+
+        -- check duplicate key
+        for plugin_name, plugin_conf in pairs(conf.plugins or {}) do
+            local plugin_obj = plugin.get(plugin_name)
+            if not plugin_obj then
+                return nil, {error_msg = "unknown plugin " .. plugin_name}
+            end
+
+            if plugin_obj.type == "auth" then
+                local decrypted_conf = core.table.deepcopy(plugin_conf)
+                plugin.decrypt_conf(plugin_name, decrypted_conf, 
core.schema.TYPE_CONSUMER)
+
+                local plugin_key_map = {
+                    ["key-auth"] = "key",
+                    ["basic-auth"] = "username",
+                    ["jwt-auth"] = "key",
+                    ["hmac-auth"] = "key_id"
+                }
+
+                local key_field = plugin_key_map[plugin_name]
+
+                if key_field then
+                    local key_value = decrypted_conf[key_field]
+
+                    if key_value then
+                        local consumer, _, err = consumer
+                            .find_consumer(plugin_name, key_field, key_value)
+                        if err then
+                            core.log.warn("failed to find consumer: ", err)
+                        end
+
+                        if consumer and consumer.username ~= conf.username then

Review Comment:
   1. Is it guaranteed that the returned consumer will have the field 
`username` in it? Different consumers have different key fields right? Take 
your own code snippet for example:
   
![image](https://github.com/user-attachments/assets/cda0af23-6013-45ae-a13b-13309eb94917)
   
   2. This block will execute only if a consumer with given `key_field` already 
exists. So, if it does, we should directly raise an error, no?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to