Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
27 changes: 27 additions & 0 deletions apisix/admin/standalone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ 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 not identifier then
return
end

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

local function get_config()
local config = shared_dict:get("config")
Expand Down Expand Up @@ -142,6 +161,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 +187,13 @@ local function update(ctx)
core.response.exit(400, {error_msg = err_prefix .. err})
end
end
-- check duplicate resource
local err = check_duplicate(item, key, id_set)
if err 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