Skip to content

Commit

Permalink
fix: authz_keycloak plugin giving 500 error (#10763)
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshya8066 authored Jan 9, 2024
1 parent e01b7e4 commit 580c1b9
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion apisix/plugins/authz-keycloak.lua
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ local function authz_keycloak_resolve_resource(conf, uri, sa_access_token)
if not resource_registration_endpoint then
local err = "Unable to determine registration endpoint."
log.error(err)
return 503, err
return nil, err
end

log.debug("Resource registration endpoint: ", resource_registration_endpoint)
Expand Down
87 changes: 87 additions & 0 deletions t/plugin/authz-keycloak2.t
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,90 @@ true
GET /t
--- response_body
true



=== TEST 16: add plugin with lazy_load_paths when resource_registration_endpoint is neither in config nor in the discovery doc
--- 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,
[[{
"plugins": {
"authz-keycloak": {
"discovery": "http://127.0.0.1:8080/realms/University/.well-known/openid-configuration",
"client_id": "course_management",
"client_secret": "d1ec69e9-55d2-4109-a3ea-befa071579d5",
"lazy_load_paths": true
}
},
"upstream": {
"nodes": {
"127.0.0.1:1982": 1
},
"type": "roundrobin"
},
"uri": "/course/foo"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed



=== TEST 17: Get access token for student and access view course route.
--- config
location /t {
content_by_lua_block {
local json_decode = require("toolkit.json").decode
local http = require "resty.http"
local httpc = http.new()
local uri = "http://127.0.0.1:8080/realms/University/protocol/openid-connect/token"
local res, err = httpc:request_uri(uri, {
method = "POST",
body = "grant_type=password&client_id=course_management&client_secret=d1ec69e9-55d2-4109-a3ea-befa071579d5&username=student@gmail.com&password=123456",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded"
}
})

if res.status == 200 then
local body = json_decode(res.body)
local accessToken = body["access_token"]


uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/course/foo"
local res, err = httpc:request_uri(uri, {
method = "GET",
headers = {
["Authorization"] = "Bearer " .. accessToken,
}
})

if res.status == 503 then
ngx.say(true)
else
ngx.say(false)
end
else
ngx.say(false)
end
}
}
--- request
GET /t
--- response_body
true
--- error_log
Unable to determine registration endpoint.

0 comments on commit 580c1b9

Please sign in to comment.