Skip to content

Commit 07b2eca

Browse files
committed
fix: merge function
1 parent 3717039 commit 07b2eca

File tree

3 files changed

+44
-74
lines changed

3 files changed

+44
-74
lines changed

Diff for: apisix/admin/consumers.lua

+1-39
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,8 @@ local plugins = require("apisix.admin.plugins")
1919
local resource = require("apisix.admin.resource")
2020
local plugin = require("apisix.plugin")
2121
local pairs = pairs
22-
local consumer = require("apisix.consumer")
2322
local utils = require("apisix.admin.utils")
2423

25-
26-
local function check_duplicate_key(username, plugins_conf)
27-
if not plugins_conf then
28-
return true
29-
end
30-
31-
for plugin_name, plugin_conf in pairs(plugins_conf) do
32-
local plugin_obj = plugin.get(plugin_name)
33-
if not plugin_obj then
34-
return nil, "unknown plugin " .. plugin_name
35-
end
36-
37-
if plugin_obj.type ~= "auth" then
38-
goto continue
39-
end
40-
41-
local key_field = utils.plugin_key_map[plugin_name]
42-
if not key_field then
43-
goto continue
44-
end
45-
46-
local key_value = plugin_conf[key_field]
47-
if not key_value then
48-
goto continue
49-
end
50-
51-
local consumer = consumer.find_consumer(plugin_name, key_field, key_value)
52-
if consumer and consumer.username ~= username then
53-
return nil, "duplicate key found with consumer: " .. consumer.username
54-
end
55-
56-
::continue::
57-
end
58-
59-
return true
60-
end
61-
6224
local function check_conf(username, conf, need_username, schema)
6325
local ok, err = core.schema.check(schema, conf)
6426
if not ok then
@@ -77,7 +39,7 @@ local function check_conf(username, conf, need_username, schema)
7739
if not ok then
7840
return nil, {error_msg = "invalid plugins configuration: " .. err}
7941
end
80-
local ok, err = check_duplicate_key(conf.username, conf_plugins_copy)
42+
local ok, err = utils.check_duplicate_key(conf_plugins_copy, conf.username)
8143
if not ok then
8244
return nil, {error_msg = err}
8345
end

Diff for: apisix/admin/credentials.lua

+1-34
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,9 @@ local core = require("apisix.core")
1818
local plugins = require("apisix.admin.plugins")
1919
local plugin = require("apisix.plugin")
2020
local resource = require("apisix.admin.resource")
21-
local consumer = require("apisix.consumer")
2221
local utils = require("apisix.admin.utils")
2322
local pairs = pairs
2423

25-
local function check_duplicate_key(id, plugins)
26-
for name, plugin_conf in pairs(plugins) do
27-
local plugin_obj = plugin.get(name)
28-
if not plugin_obj then
29-
goto continue
30-
end
31-
32-
if plugin_obj.type ~= "auth" then
33-
goto continue
34-
end
35-
36-
local key_field = utils.plugin_key_map[name]
37-
if not key_field then
38-
goto continue
39-
end
40-
41-
local key_value = plugin_conf[key_field]
42-
if not key_value then
43-
goto continue
44-
end
45-
46-
local consumer = consumer.find_consumer(name, key_field, key_value)
47-
if consumer and consumer.credential_id ~= id then
48-
return nil, "duplicate key found with consumer: " .. consumer.username
49-
end
50-
51-
::continue::
52-
end
53-
54-
return true
55-
end
56-
5724
local function check_conf(id, conf, _need_id, schema)
5825
local ok, err = core.schema.check(schema, conf)
5926
if not ok then
@@ -69,7 +36,7 @@ local function check_conf(id, conf, _need_id, schema)
6936
return nil, {error_msg = "invalid plugins configuration: " .. err}
7037
end
7138

72-
local ok, err = check_duplicate_key(id, conf_plugins_copy)
39+
local ok, err = utils.check_duplicate_key(conf_plugins_copy, nil, id)
7340
if not ok then
7441
return nil, {error_msg = err}
7542
end

Diff for: apisix/admin/utils.lua

+42-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ local ngx_time = ngx.time
1919
local tonumber = tonumber
2020
local ipairs = ipairs
2121
local pairs = pairs
22+
local consumer = require("apisix.consumer")
23+
local plugin = require("apisix.plugin")
2224

2325

2426
local _M = {}
@@ -110,11 +112,50 @@ function _M.decrypt_params(decrypt_func, body, schema_type)
110112
end
111113
end
112114

113-
_M.plugin_key_map = {
115+
116+
local plugin_key_map = {
114117
["key-auth"] = "key",
115118
["basic-auth"] = "username",
116119
["jwt-auth"] = "key",
117120
["hmac-auth"] = "key_id"
118121
}
119122

123+
124+
function _M.check_duplicate_key(plugins_conf, username, credential_id)
125+
if not plugins_conf then
126+
return true
127+
end
128+
129+
for plugin_name, plugin_conf in pairs(plugins_conf) do
130+
local plugin_obj = plugin.get(plugin_name)
131+
if not plugin_obj then
132+
return nil, "unknown plugin " .. plugin_name
133+
end
134+
135+
if plugin_obj.type ~= "auth" then
136+
goto continue
137+
end
138+
139+
local key_field = plugin_key_map[plugin_name]
140+
if not key_field then
141+
goto continue
142+
end
143+
144+
local key_value = plugin_conf[key_field]
145+
if not key_value then
146+
goto continue
147+
end
148+
149+
local consumer = consumer.find_consumer(plugin_name, key_field, key_value)
150+
if consumer and
151+
((username and consumer.username ~= username) or (credential_id and consumer.credential_id ~= credential_id)) then
152+
return nil, "duplicate key found with consumer: " .. consumer.username
153+
end
154+
155+
::continue::
156+
end
157+
158+
return true
159+
end
160+
120161
return _M

0 commit comments

Comments
 (0)