Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions apisix/admin/standalone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ local EVENT_UPDATE = "standalone-api-configuration-update"

local _M = {}

local function check_duplicate(item, key, id_set)
local identifier, identifier_type
if key == "consumers" then
identifier = item.username
identifier_type = "username"
else
identifier = item.id
identifier_type = "id"
end

if id_set[identifier] then
return true,"duplicate " .. identifier_type .. " found " .. identifier
end
id_set[identifier] = true
return false
end

local function get_config()
local config = shared_dict:get("config")
Expand Down Expand Up @@ -142,6 +158,7 @@ local function update(ctx)
apisix_yaml[key] = table_new(#items, 0)
local item_schema = obj.item_schema
local item_checker = obj.checker
local id_set = {}

for index, item in ipairs(items) do
local item_temp = tbl_deepcopy(item)
Expand All @@ -167,6 +184,14 @@ local function update(ctx)
core.response.exit(400, {error_msg = err_prefix .. err})
end
end
-- prevent updating resource with the same ID
-- (e.g., service ID or other resource IDs) in a single request
local duplicated, err = check_duplicate(item, key, id_set)
if duplicated then
core.log.error(err)
core.response.exit(400, { error_msg = err })
end

table_insert(apisix_yaml[key], item)
end
end
Expand Down
30 changes: 30 additions & 0 deletions t/admin/standalone.t
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,33 @@ x-apisix-conf-version-routes: 100
--- error_code: 400
--- response_body
{"error_msg":"routes_conf_version must be greater than or equal to (1062)"}



=== TEST 11: duplicate route id found
--- config
location /t11 {}
--- request
PUT /apisix/admin/configs
{"routes_conf_version":1063,"routes":[{"id":"r1","uri":"/r2","upstream_id":"u1","plugins":{"proxy-rewrite":{"uri":"/hello"}}},
{"id":"r1","uri":"/r2","upstream_id":"u1","plugins":{"proxy-rewrite":{"uri":"/hello"}}}]}
--- more_headers
X-API-KEY: edd1c9f034335f136f87ad84b625c8f1
--- error_code: 400
--- response_body
{"error_msg":"duplicate id found r1"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include the resource type in error message.




=== TEST 12: duplicate consumer username found
--- config
location /t12 {}
--- request
PUT /apisix/admin/configs
{"consumers_conf_version":1064,"consumers":[{"username":"consumer1","plugins":{"key-auth":{"key":"consumer1"}}},
{"username":"consumer1","plugins":{"key-auth":{"key":"consumer1"}}}]}
--- more_headers
X-API-KEY: edd1c9f034335f136f87ad84b625c8f1
--- error_code: 400
--- response_body
{"error_msg":"duplicate username found consumer1"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Loading