Skip to content

Commit

Permalink
feat(plugin/azure-function): clear upstream uri and request uri injec…
Browse files Browse the repository at this point in the history
…t plugin logic (Kong#11850)

KAG-2841
  • Loading branch information
oowl authored Nov 16, 2023
1 parent c468b77 commit 13dbed3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "**azure-functions**: azure-functions plugin now eliminates upstream/request URI and only use `routeprefix` configuration field to construct request path when requesting Azure API"
type: breaking_change
scope: Plugin
27 changes: 4 additions & 23 deletions kong/plugins/azure-functions/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ local kong_meta = require "kong.meta"

local kong = kong
local fmt = string.format
local sub = string.sub
local find = string.find
local byte = string.byte
local match = string.match
local var = ngx.var
Expand All @@ -26,10 +24,6 @@ local azure = {

function azure:access(conf)
local path do
-- strip any query args
local upstream_uri = var.upstream_uri or var.request_uri
local s = find(upstream_uri, "?", 1, true)
upstream_uri = s and sub(upstream_uri, 1, s - 1) or upstream_uri

-- strip pre-/postfix slashes
path = match(conf.routeprefix or "", STRIP_SLASHES_PATTERN)
Expand All @@ -39,24 +33,11 @@ function azure:access(conf)
path = "/" .. path
end

path = path .. "/" .. func

-- concatenate path with upstream uri
local upstream_uri_first_byte = byte(upstream_uri, 1)
local path_last_byte = byte(path, -1)
if path_last_byte == SLASH then
if upstream_uri_first_byte == SLASH then
path = path .. sub(upstream_uri, 2, -1)
else
path = path .. upstream_uri
end

local functionname_first_byte = byte(func, 1)
if functionname_first_byte == SLASH then
path = path .. func
else
if upstream_uri_first_byte == SLASH then
path = path .. upstream_uri
elseif upstream_uri ~= "" then
path = path .. "/" .. upstream_uri
end
path = path .. "/" .. func
end
end

Expand Down
45 changes: 44 additions & 1 deletion spec/03-plugins/35-azure-functions/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ for _, strategy in helpers.each_strategy() do
dns_mock = helpers.dns_mock.new()
}

local route3 = db.routes:insert {
hosts = { "azure3.com" },
protocols = { "http", "https" },
service = db.services:insert(
{
name = "azure3",
host = "azure.example.com", -- just mock service, it will not be requested
port = 80,
path = "/request",
}
),
}

-- this plugin definition results in an upstream url to
-- http://mockbin.org/request
-- which will echo the request for inspection
db.plugins:insert {
name = "azure-functions",
route = { id = route3.id },
config = {
https = false,
appname = "azure",
hostdomain = "example.com",
routeprefix = "request",
functionname = "test-func-name",
apikey = "anything_but_an_API_key",
clientid = "and_no_clientid",
},
}

fixtures.dns_mock:A({
name = "azure.example.com",
address = "127.0.0.1",
Expand Down Expand Up @@ -169,7 +199,7 @@ for _, strategy in helpers.each_strategy() do

assert.response(res).has.status(200)
local json = assert.response(res).has.jsonbody()
assert.matches("/request/test%-func%-name/and/then/some", json.uri)
assert.matches("/request/test%-func%-name", json.uri)
end)

it("passes the method", function()
Expand Down Expand Up @@ -243,5 +273,18 @@ for _, strategy in helpers.each_strategy() do
assert(tonumber(res.headers["Content-Length"]) > 100)
end)

it("service upstream uri and request uri can not influence azure function", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/",
query = { hello = "world" },
headers = {
["Host"] = "azure3.com"
}
})

assert(tonumber(res.headers["Content-Length"]) > 100)
end)

end) -- describe
end

0 comments on commit 13dbed3

Please sign in to comment.