Skip to content

Commit

Permalink
fix bkapp config maybe empty, get data from cache maybe error (#47)
Browse files Browse the repository at this point in the history
* fix bkapp config maybe empty, get data from cache maybe error

* add tests
  • Loading branch information
alex-smile committed Sep 21, 2023
1 parent 0ac963a commit c4e7b93
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/apisix/editions/ee/plugins/bk-components/ssm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local string_format = string.format
local VERIFY_ACCESS_TOKEN_URL = "/api/v1/auth/access-tokens/verify"
local SSM_TIMEOUT_MS = 5 * 1000

local bkapp = bk_core.config.get_bkapp()
local bkapp = bk_core.config.get_bkapp() or {}

local _M = {
host = bk_core.config.get_ssm_addr(),
Expand Down
7 changes: 4 additions & 3 deletions src/apisix/plugins/bk-cache/jwt-key.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
-- We undertake not to change the open source license (MIT license) applicable
-- to the current version of the project delivered to anyone in the future.
--

local core = require("apisix.core")
local bk_apigateway_core_component = require("apisix.plugins.bk-components.bk-apigateway-core")

Expand All @@ -34,9 +33,11 @@ local _M = {}

function _M.get_jwt_public_key(gateway_name)
local key = gateway_name
local result = jwt_public_key_lrucache(key, nil, bk_apigateway_core_component.get_apigw_public_key, gateway_name)
local result, err = jwt_public_key_lrucache(
key, nil, bk_apigateway_core_component.get_apigw_public_key, gateway_name
)
if result == nil then
return nil, "get_jwt_public_key of " .. gateway_name .. " failed"
return nil, "get jwt public_key of gateway " .. gateway_name .. " failed, err: " .. err
end
return result.public_key, result.err
end
Expand Down
2 changes: 1 addition & 1 deletion src/apisix/plugins/bk-components/bkauth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local VERIFY_ACCESS_TOKEN_URL = "/api/v1/oauth/tokens/verify"

local BKAUTH_TIMEOUT_MS = 5 * 1000

local bkapp = bk_core.config.get_bkapp()
local bkapp = bk_core.config.get_bkapp() or {}

local _M = {
host = bk_core.config.get_bkauth_addr(),
Expand Down
40 changes: 35 additions & 5 deletions src/apisix/plugins/bk-core/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
-- We undertake not to change the open source license (MIT license) applicable
-- to the current version of the project delivered to anyone in the future.
--

local core = require("apisix.core")
local multipart = require("multipart")
local string_split = require("pl.stringx").split
Expand All @@ -34,6 +33,7 @@ local _M = {}

local function get_content_type(ctx)
local content_type = core.request.header(ctx, "Content-Type")

if content_type == nil or content_type == "" then
return "application/octet-stream"
end
Expand All @@ -43,17 +43,47 @@ end

local function is_urlencoded_form(ctx)
local content_type = get_content_type(ctx)
if core.string.find(string_lower(content_type), "application/x-www-form-urlencoded") then
return true
local urlencoded_header = "application/x-www-form-urlencoded"

if type(content_type) == "string" then
if core.string.find(string_lower(content_type), urlencoded_header) then
return true
end
return false
end

if type(content_type) == "table" then
for _, value in ipairs(content_type) do
if core.string.find(string_lower(value), urlencoded_header) then
return true
end
end
return false
end

return false
end

local function is_multipart_form(ctx)
local content_type = get_content_type(ctx)
if core.string.find(string_lower(content_type), "multipart/form-data") then
return true
local multipart_header = "multipart/form-data"

if type(content_type) == "string" then
if core.string.find(string_lower(content_type), multipart_header) then
return true
end
return false
end

if type(content_type) == "table" then
for _, value in ipairs(content_type) do
if core.string.find(string_lower(value), multipart_header) then
return true
end
end
return false
end

return false
end

Expand Down
49 changes: 48 additions & 1 deletion src/apisix/tests/bk-core/test-request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
-- We undertake not to change the open source license (MIT license) applicable
-- to the current version of the project delivered to anyone in the future.
--

local core = require("apisix.core")
local bk_core = require("apisix.plugins.bk-core.init")
local ngx = ngx
Expand Down Expand Up @@ -105,6 +104,30 @@ describe(
assert.is_true(result)
end
)

it(
"header is table", function()
headers = {
["Content-Type"] = {
"foo",
"application/x-www-form-urlencoded; charset=utf8",
},
}

local result = bk_core.request._is_urlencoded_form()
assert.is_true(result)

headers = {
["Content-Type"] = {
"foo",
"bar",
},
}

local result = bk_core.request._is_urlencoded_form()
assert.is_false(result)
end
)
end
)

Expand All @@ -129,6 +152,30 @@ describe(
assert.is_true(result)
end
)

it(
"header is table", function()
headers = {
["Content-Type"] = {
"foo",
"multipart/form-data; charset=utf8",
},
}

local result = bk_core.request._is_multipart_form()
assert.is_true(result)

headers = {
["Content-Type"] = {
"foo",
"bar",
},
}

local result = bk_core.request._is_multipart_form()
assert.is_false(result)
end
)
end
)

Expand Down

0 comments on commit c4e7b93

Please sign in to comment.