fix(standalone): require consumer's id to be the same as username (#3394)

We already did the same thing with the configuration from etcd.
This commit is contained in:
罗泽轩 2021-01-22 03:30:59 -06:00 committed by GitHub
parent d698220341
commit c8d35cd73c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 5 deletions

View File

@ -97,13 +97,29 @@ local function check_consumer(consumer)
end
local function filter(consumer)
if not consumer.value then
return
end
-- We expect the id is the same as username. Fix up it here if it isn't.
consumer.value.id = consumer.value.username
end
function _M.init_worker()
local err
consumers, err = core.config.new("/consumers", {
automatic = true,
item_schema = core.schema.consumer,
checker = check_consumer,
})
local config = core.config.new()
local cfg = {
automatic = true,
item_schema = core.schema.consumer,
checker = check_consumer,
}
if config.type ~= "etcd" then
cfg.filter = filter
end
consumers, err = core.config.new("/consumers", cfg)
if not consumers then
error("failed to create etcd instance for fetching consumers: " .. err)
return

View File

@ -98,3 +98,32 @@ consumers:
--- request
GET /apisix/plugin/jwt/sign?key=user-key
--- error_code: 200
=== TEST 4: consummer restriction
--- apisix_yaml
consumers:
- username: jack
plugins:
key-auth:
key: user-key
routes:
- id: 1
methods:
- POST
uri: "/hello"
plugins:
key-auth:
consumer-restriction:
whitelist:
- jack
upstream:
type: roundrobin
nodes:
"127.0.0.1:1980": 1
#END
--- more_headers
apikey: user-key
--- request
POST /hello