Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangfucheng committed Jul 11, 2023
1 parent f2337f2 commit b027870
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 15 deletions.
8 changes: 7 additions & 1 deletion apisix/plugins/consumer-restriction.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,13 @@ function _M.access(conf, ctx)
local method = ngx.req.get_method()

if not value then
return 401, { message = "Missing authentication or identity verification."}
local err_msg
if conf.type == "consumer_name" then
err_msg = "Missing authentication in request."
else
err_msg = "Please ensure " .. conf.type .. " is present in request"
end
return 401, { message = err_msg}
end
core.log.info("value: ", value)

Expand Down
12 changes: 6 additions & 6 deletions t/plugin/consumer-restriction.t
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ passed
GET /hello
--- error_code: 401
--- response_body
{"message":"Missing authentication or identity verification."}
{"message":"Missing authentication in request."}



Expand All @@ -325,7 +325,7 @@ GET /hello
Authorization: Basic amFjazIwMTk6MTIzNDU2
--- error_code: 401
--- response_body
{"message":"Missing authentication or identity verification."}
{"message":"Missing authentication in request."}



Expand All @@ -336,7 +336,7 @@ GET /hello
Authorization: Basic amFjazIwMjA6MTIzNDU2
--- error_code: 401
--- response_body
{"message":"Missing authentication or identity verification."}
{"message":"Missing authentication in request."}



Expand Down Expand Up @@ -383,7 +383,7 @@ passed
GET /hello
--- error_code: 401
--- response_body
{"message":"Missing authentication or identity verification."}
{"message":"Missing authentication in request."}



Expand All @@ -394,7 +394,7 @@ GET /hello
Authorization: Basic amFjazIwMTk6MTIzNDU2
--- error_code: 401
--- response_body
{"message":"Missing authentication or identity verification."}
{"message":"Missing authentication in request."}



Expand All @@ -405,7 +405,7 @@ GET /hello
Authorization: Basic amFjazIwMjA6MTIzNDU2
--- error_code: 401
--- response_body
{"message":"Missing authentication or identity verification."}
{"message":"Missing authentication in request."}



Expand Down
155 changes: 147 additions & 8 deletions t/plugin/consumer-restriction2.t
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,37 @@ passed
=== TEST 5: set whitelist
=== TEST 5: consumer jack3 with no consumer group
--- 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": "jack3",
"plugins": {
"basic-auth": {
"username": "jack2021",
"password": "123456"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 6: set whitelist
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -175,7 +205,7 @@ passed
=== TEST 6: verify unauthorized
=== TEST 7: verify unauthorized
--- request
GET /hello
--- error_code: 401
Expand All @@ -184,7 +214,7 @@ GET /hello
=== TEST 7: verify jack1
=== TEST 8: verify jack1
--- request
GET /hello
--- more_headers
Expand All @@ -194,7 +224,7 @@ hello world
=== TEST 8: verify jack2
=== TEST 9: verify jack2
--- request
GET /hello
--- more_headers
Expand All @@ -205,7 +235,7 @@ Authorization: Basic amFjazIwMjA6MTIzNDU2
=== TEST 9: set blacklist
=== TEST 10: set blacklist
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -246,7 +276,7 @@ passed
=== TEST 10: verify unauthorized
=== TEST 11: verify unauthorized
--- request
GET /hello
--- error_code: 401
Expand All @@ -255,7 +285,7 @@ GET /hello
=== TEST 11: verify jack1
=== TEST 12: verify jack1
--- request
GET /hello
--- more_headers
Expand All @@ -266,10 +296,119 @@ Authorization: Basic amFjazIwMTk6MTIzNDU2
=== TEST 12: verify jack2
=== TEST 13: verify jack2
--- request
GET /hello
--- more_headers
Authorization: Basic amFjazIwMjA6MTIzNDU2
--- response_body
hello world
=== TEST 14: verify jack2
--- request
GET /hello
--- more_headers
Authorization: Basic amFjazIwMjE6MTIzNDU2
--- error_code: 401
--- response_body
{"message":"Please ensure consumer_group_id is present in request"}
=== TEST 15: set blacklist with service_id
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
},
"plugins": {
"consumer-restriction": {
"type": "service_id",
"blacklist": [
"1"
],
"rejected_msg": "request is forbidden"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 16: hit
--- request
GET /hello
--- error_code: 401
--- response_body
{"message":"Please ensure service_id is present in request"}
=== TEST 17: set white with service_id
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
},
"plugins": {
"consumer-restriction": {
"type": "service_id",
"whitelist": [
"1"
],
"rejected_msg": "request is forbidden"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 18: hit
--- request
GET /hello
--- error_code: 401
--- response_body
{"message":"Please ensure service_id is present in request"}

0 comments on commit b027870

Please sign in to comment.