Skip to content

Commit

Permalink
feat(aws-lambda) adding support for 'isBase64Encoded' flag in Lambda …
Browse files Browse the repository at this point in the history
…function responses (#37)

Implementing functionality to support 'isBase64Encoded' flag in Lambda function response, plugin will now base64 decode response body from Lambda before forwarding to caller
  • Loading branch information
jonminter authored Sep 22, 2020
1 parent cdfb043 commit 40f9bec
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Kong AWS Lambda plugin changelog

## unreleased

- feat(body) adding support for 'isBase64Encoded' flag in Lambda function
responses

## aws-lambda 3.4.0 12-May-2020

- Change `luaossl` to `lua-resty-openssl`
Expand Down
6 changes: 6 additions & 0 deletions kong/plugins/aws-lambda/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ local tonumber = tonumber
local type = type
local fmt = string.format
local ngx_encode_base64 = ngx.encode_base64
local ngx_decode_base64 = ngx.decode_base64
local ngx_update_time = ngx.update_time
local ngx_now = ngx.now
local kong = kong
Expand Down Expand Up @@ -94,6 +95,11 @@ local function extract_proxy_response(content)

local headers = serialized_content.headers or {}
local body = serialized_content.body or ""
local isBase64Encoded = serialized_content.isBase64Encoded or false
if isBase64Encoded then
body = ngx_decode_base64(body)
end

headers["Content-Length"] = #body

return {
Expand Down
34 changes: 34 additions & 0 deletions spec/plugins/aws-lambda/99-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ local fixtures = {
elseif string.match(ngx.var.uri, "functionWithNoResponse") then
ngx.header["Content-Length"] = 0
elseif string.match(ngx.var.uri, "functionWithBase64EncodedResponse") then
ngx.say("{\"statusCode\": 200, \"body\": \"dGVzdA==\", \"isBase64Encoded\": true}")
elseif type(res) == 'string' then
ngx.header["Content-Length"] = #res + 1
ngx.say(res)
Expand Down Expand Up @@ -184,6 +187,12 @@ for _, strategy in helpers.each_strategy() do
service = null,
}

local route16 = bp.routes:insert {
hosts = { "lambda16.com" },
protocols = { "http", "https" },
service = null,
}

bp.plugins:insert {
name = "aws-lambda",
route = { id = route1.id },
Expand Down Expand Up @@ -393,6 +402,19 @@ for _, strategy in helpers.each_strategy() do
},
}

bp.plugins:insert {
name = "aws-lambda",
route = { id = route16.id },
config = {
port = 10001,
aws_key = "mock-key",
aws_secret = "mock-secret",
aws_region = "us-east-1",
function_name = "functionWithBase64EncodedResponse",
is_proxy_integration = true,
}
}

assert(helpers.start_kong({
database = strategy,
plugins = "aws-lambda",
Expand Down Expand Up @@ -946,6 +968,18 @@ for _, strategy in helpers.each_strategy() do
assert.equal("some_value1", body.key1)
assert.is_nil(res.headers["X-Amz-Function-Error"])
end)

it("returns decoded base64 response from a Lambda function", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/get?key1=some_value1&key2=some_value2&key3=some_value3",
headers = {
["Host"] = "lambda16.com"
}
})
assert.res_status(200, res)
assert.equal("test", res:read_body())
end)
end)
end)
end

0 comments on commit 40f9bec

Please sign in to comment.