Skip to content

Commit

Permalink
fix(wolf-rbac): other plugin in consumer not effective when consumer …
Browse files Browse the repository at this point in the history
…used wolf-rbac plugin (apache#9287) (apache#9298)
  • Loading branch information
lingliy authored and AlinsRan committed Jun 25, 2023
1 parent fece2fb commit 455fafa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
11 changes: 6 additions & 5 deletions apisix/plugins/wolf-rbac.lua
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,13 @@ function _M.rewrite(conf, ctx)
local consumers = consumer.consumers_kv(plugin_name, consumer_conf, "appid")

core.log.info("------ consumers: ", core.json.delay_encode(consumers))
local consumer = consumers[appid]
if not consumer then
local cur_consumer = consumers[appid]
if not cur_consumer then
core.log.error("consumer [", appid, "] not found")
return 401, fail_response("Invalid appid in rbac token")
end
core.log.info("consumer: ", core.json.delay_encode(consumer))
local server = consumer.auth_conf.server
core.log.info("consumer: ", core.json.delay_encode(cur_consumer))
local server = cur_consumer.auth_conf.server

local res = check_url_permission(server, appid, action, url,
client_ip, wolf_token)
Expand All @@ -287,7 +287,7 @@ function _M.rewrite(conf, ctx)
local userId = userInfo.id
username = userInfo.username
nickname = userInfo.nickname or userInfo.username
local prefix = consumer.auth_conf.header_prefix or ''
local prefix = cur_consumer.auth_conf.header_prefix or ''
core.response.set_header(prefix .. "UserId", userId)
core.response.set_header(prefix .. "Username", username)
core.response.set_header(prefix .. "Nickname", ngx.escape_uri(nickname))
Expand All @@ -303,6 +303,7 @@ function _M.rewrite(conf, ctx)
") failed, res: ",core.json.delay_encode(res))
return res.status, fail_response(res.err, { username = username, nickname = nickname })
end
consumer.attach_consumer(ctx, cur_consumer, consumer_conf)
core.log.info("wolf-rbac check permission passed")
end

Expand Down
50 changes: 50 additions & 0 deletions t/plugin/wolf-rbac.t
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,53 @@ passed
ngx.status = code
}
}
=== TEST 36: add consumer with echo plugin
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/consumers',
ngx.HTTP_PUT,
[[{
"username": "wolf_rbac_with_other_plugins",
"plugins": {
"wolf-rbac": {
"appid": "wolf-rbac-app",
"server": "http://127.0.0.1:1982"
},
"echo": {
"body": "consumer merge echo plugins\n"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
--- no_error_log
[error]
=== TEST 37: verify echo plugin in consumer
--- request
GET /hello
--- more_headers
Authorization: V1#wolf-rbac-app#wolf-rbac-token
--- response_headers
X-UserId: 100
X-Username: admin
X-Nickname: administrator
--- response_body
consumer merge echo plugins
--- no_error_log
[error]

0 comments on commit 455fafa

Please sign in to comment.