Skip to content

Commit 40f9bec

Browse files
authored
feat(aws-lambda) adding support for 'isBase64Encoded' flag in Lambda 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
1 parent cdfb043 commit 40f9bec

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Kong AWS Lambda plugin changelog
22

3+
## unreleased
4+
5+
- feat(body) adding support for 'isBase64Encoded' flag in Lambda function
6+
responses
7+
38
## aws-lambda 3.4.0 12-May-2020
49

510
- Change `luaossl` to `lua-resty-openssl`

kong/plugins/aws-lambda/handler.lua

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ local tonumber = tonumber
3636
local type = type
3737
local fmt = string.format
3838
local ngx_encode_base64 = ngx.encode_base64
39+
local ngx_decode_base64 = ngx.decode_base64
3940
local ngx_update_time = ngx.update_time
4041
local ngx_now = ngx.now
4142
local kong = kong
@@ -94,6 +95,11 @@ local function extract_proxy_response(content)
9495

9596
local headers = serialized_content.headers or {}
9697
local body = serialized_content.body or ""
98+
local isBase64Encoded = serialized_content.isBase64Encoded or false
99+
if isBase64Encoded then
100+
body = ngx_decode_base64(body)
101+
end
102+
97103
headers["Content-Length"] = #body
98104

99105
return {

spec/plugins/aws-lambda/99-access_spec.lua

+34
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ local fixtures = {
4242
elseif string.match(ngx.var.uri, "functionWithNoResponse") then
4343
ngx.header["Content-Length"] = 0
4444
45+
elseif string.match(ngx.var.uri, "functionWithBase64EncodedResponse") then
46+
ngx.say("{\"statusCode\": 200, \"body\": \"dGVzdA==\", \"isBase64Encoded\": true}")
47+
4548
elseif type(res) == 'string' then
4649
ngx.header["Content-Length"] = #res + 1
4750
ngx.say(res)
@@ -184,6 +187,12 @@ for _, strategy in helpers.each_strategy() do
184187
service = null,
185188
}
186189

190+
local route16 = bp.routes:insert {
191+
hosts = { "lambda16.com" },
192+
protocols = { "http", "https" },
193+
service = null,
194+
}
195+
187196
bp.plugins:insert {
188197
name = "aws-lambda",
189198
route = { id = route1.id },
@@ -393,6 +402,19 @@ for _, strategy in helpers.each_strategy() do
393402
},
394403
}
395404

405+
bp.plugins:insert {
406+
name = "aws-lambda",
407+
route = { id = route16.id },
408+
config = {
409+
port = 10001,
410+
aws_key = "mock-key",
411+
aws_secret = "mock-secret",
412+
aws_region = "us-east-1",
413+
function_name = "functionWithBase64EncodedResponse",
414+
is_proxy_integration = true,
415+
}
416+
}
417+
396418
assert(helpers.start_kong({
397419
database = strategy,
398420
plugins = "aws-lambda",
@@ -946,6 +968,18 @@ for _, strategy in helpers.each_strategy() do
946968
assert.equal("some_value1", body.key1)
947969
assert.is_nil(res.headers["X-Amz-Function-Error"])
948970
end)
971+
972+
it("returns decoded base64 response from a Lambda function", function()
973+
local res = assert(proxy_client:send {
974+
method = "GET",
975+
path = "/get?key1=some_value1&key2=some_value2&key3=some_value3",
976+
headers = {
977+
["Host"] = "lambda16.com"
978+
}
979+
})
980+
assert.res_status(200, res)
981+
assert.equal("test", res:read_body())
982+
end)
949983
end)
950984
end)
951985
end

0 commit comments

Comments
 (0)