Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aws-lambda): add better type and error handling on proxy integration mode #11413

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@

## Unreleased

# Fixes
### Additions

#### Core

#### Plugins

- **AWS-Lambda**: the AWS-Lambda plugin has been refactored by using `lua-resty-aws` as an
underlying AWS library. The refactor simplifies the AWS-Lambda plugin code base and
adding support for multiple IAM authenticating scenarios.
[#11350](https://github.com/Kong/kong/pull/11350)

### Fixes

#### Core

Expand All @@ -26,8 +37,10 @@

#### Plugins

- For OAuth2 plugin, `scope` has been taken into account as a new criterion of the request validation. When refreshing token with `refresh_token`, the scopes associated with the `refresh_token` provided in the request must be same with or a subset of the scopes configured in the OAuth2 plugin instance hit by the request.
- **OAuth2**: For OAuth2 plugin, `scope` has been taken into account as a new criterion of the request validation. When refreshing token with `refresh_token`, the scopes associated with the `refresh_token` provided in the request must be same with or a subset of the scopes configured in the OAuth2 plugin instance hit by the request.
[#11342](https://github.com/Kong/kong/pull/11342)
- **AWS-Lambda**: fix an issue that the AWS-Lambda plugin cannot extract a json encoded proxy integration response.
[#11413](https://github.com/Kong/kong/pull/11413)

## 3.4.0

Expand Down
15 changes: 12 additions & 3 deletions kong/plugins/aws-lambda/request-util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,18 @@ end


local function extract_proxy_response(content)
local serialized_content, err = cjson.decode(content)
if not serialized_content then
return nil, err
local serialized_content, err
if type(content) == "string" then
serialized_content, err = cjson.decode(content)
if not serialized_content then
return nil, err
end

elseif type(content) == "table" then
serialized_content = content

else
return nil, "proxy response must be json format"
end

local ok, err = validate_custom_response(serialized_content)
Expand Down
33 changes: 33 additions & 0 deletions spec/03-plugins/27-aws-lambda/08-sam-integration_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ if sam.get_os_architecture() ~= "aarch64" then
log_type = "None",
},
}

local route2 = bp.routes:insert {
hosts = { "lambda2.com" },
}

bp.plugins:insert {
name = "aws-lambda",
route = { id = route2.id },
config = {
host = "localhost",
port = sam_port,
disable_https = true,
aws_key = "mock-key",
aws_secret = "mock-secret",
aws_region = "us-east-1",
function_name = "HelloWorldFunction",
log_type = "None",
is_proxy_integration = true,
},
}
end)

lazy_teardown(function()
Expand Down Expand Up @@ -96,6 +116,19 @@ if sam.get_os_architecture() ~= "aarch64" then
})
assert.res_status(200, res)
end)

it("can extract proxy response correctly", function ()
local res = assert(proxy_client:send {
method = "GET",
path = "/",
headers = {
host = "lambda2.com"
}
})
assert.res_status(201, res)
local body = assert.response(res).has.jsonbody()
assert.equal("hello world", body.message)
end)
end)
end)
end
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/aws-lambda.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ local fixtures = {
ngx.header["Content-Length"] = 0

elseif string.match(ngx.var.uri, "functionWithBase64EncodedResponse") then
ngx.header["Content-Type"] = "application/json"
ngx.say("{\"statusCode\": 200, \"body\": \"dGVzdA==\", \"isBase64Encoded\": true}")

elseif string.match(ngx.var.uri, "functionWithNotBase64EncodedResponse") then
ngx.header["Content-Type"] = "application/json"
ngx.say("{\"statusCode\": 200, \"body\": \"dGVzdA=\", \"isBase64Encoded\": false}")

elseif string.match(ngx.var.uri, "functionWithIllegalBase64EncodedResponse") then
ngx.say("{\"statusCode\": 200, \"body\": \"dGVzdA=\", \"isBase64Encoded\": \"abc\"}")

elseif string.match(ngx.var.uri, "functionWithMultiValueHeadersResponse") then
ngx.header["Content-Type"] = "application/json"
ngx.say("{\"statusCode\": 200, \"headers\": { \"Age\": \"3600\"}, \"multiValueHeaders\": {\"Access-Control-Allow-Origin\": [\"site1.com\", \"site2.com\"]}}")

elseif string.match(ngx.var.uri, "functionEcho") then
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/sam-app/hello_world/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def lambda_handler(event, context):
# raise e

return {
"statusCode": 200,
"statusCode": 201,
"body": json.dumps({
"message": "hello world",
# "location": ip.text.replace("\n", "")
Expand Down