Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 21 additions & 5 deletions apisix/consumer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions t/config-center-yaml/consumer.t
Original file line number Diff line number Diff line change
Expand Up @@ -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