From aebff937a8753c2e5f5d64b6ec2af1594d548feb Mon Sep 17 00:00:00 2001 From: Dimitri Gnidash Date: Mon, 9 Jul 2018 13:41:38 +0300 Subject: [PATCH 1/5] -Add new values --- ...in-aws-lambda-status-code-0.1.0-1.rockspec | 35 ------------------- .../aws-lambda-status-code/handler.lua | 13 ++++++- 2 files changed, 12 insertions(+), 36 deletions(-) delete mode 100644 kong-plugin-aws-lambda-status-code-0.1.0-1.rockspec diff --git a/kong-plugin-aws-lambda-status-code-0.1.0-1.rockspec b/kong-plugin-aws-lambda-status-code-0.1.0-1.rockspec deleted file mode 100644 index 6cc0256..0000000 --- a/kong-plugin-aws-lambda-status-code-0.1.0-1.rockspec +++ /dev/null @@ -1,35 +0,0 @@ -package = "kong-plugin-aws-lambda-status-code" -- TODO: rename, must match the info in the filename of this rockspec! - -- as a convention; stick to the prefix: `kong-plugin-` -version = "0.1.0-1" -- TODO: renumber, must match the info in the filename of this rockspec! --- The version '0.1.0' is the source code version, the trailing '1' is the version of this rockspec. --- whenever the source version changes, the rockspec should be reset to 1. The rockspec version is only --- updated (incremented) when this file changes, but the source remains the same. - --- This is the name to set in the Kong configuration `custom_plugins` setting. -local pluginName = "aws-lambda-status-code" - -supported_platforms = {"linux", "macosx"} -source = { - -- these are initially not required to make it work - url = "git://github.com/iyp-uk/aws-lambda-status-code", - tag = "0.1.0" -} - -description = { - summary = "Kong custom plugin for changing AWS Lambda response status code based on a property in the body", - homepage = "https://github.com/iyp-uk/aws-lambda-status-code", - license = "MIT" -} - -dependencies = { - "lua >= 5.1" -} - -build = { - type = "builtin", - modules = { - ["kong.plugins."..pluginName..".handler"] = "kong/plugins/"..pluginName.."/handler.lua", - ["kong.plugins."..pluginName..".schema"] = "kong/plugins/"..pluginName.."/schema.lua", - ["kong.plugins."..pluginName..".v4"] = "kong/plugins/"..pluginName.."/v4.lua", - } -} diff --git a/kong/plugins/aws-lambda-status-code/handler.lua b/kong/plugins/aws-lambda-status-code/handler.lua index c26203d..272d70c 100644 --- a/kong/plugins/aws-lambda-status-code/handler.lua +++ b/kong/plugins/aws-lambda-status-code/handler.lua @@ -55,6 +55,8 @@ function AWSLambdaStatusCodeHandler:access(conf) upstream_body.request_uri_args = ngx_req_get_uri_args() end + ngx.log(ngx.ERR, "Hello!") + if conf.forward_request_body then ngx_req_read_body() @@ -78,6 +80,8 @@ function AWSLambdaStatusCodeHandler:access(conf) upstream_body = utils.table_merge(ngx_req_get_uri_args(), body_args) end + ngx.log(ngx.ERR, "Hello!") + local upstream_body_json, err = cjson.encode(upstream_body) if not upstream_body_json then ngx.log(ngx.ERR, "[aws-lambda-status-code] could not JSON encode upstream body", @@ -123,6 +127,8 @@ function AWSLambdaStatusCodeHandler:access(conf) return responses.send_HTTP_INTERNAL_SERVER_ERROR(err) end + print("hello, from Plugin") + local res, err = client:request { method = "POST", path = request.url, @@ -154,6 +160,7 @@ function AWSLambdaStatusCodeHandler:access(conf) params, err = cjson.decode(body) local statusCode = params.statusCode local resource = params.resource + local lambdaHeaders = params.headers if statusCode ~= nil then ngx.header['X-lambda-original-status'] = res.status ngx.status = statusCode @@ -166,11 +173,15 @@ function AWSLambdaStatusCodeHandler:access(conf) end end - -- Send response to client + --Set headers returned in JSON body + for k,v in pairs(lambdaHeaders) do + ngx.header[k] = v + for k, v in pairs(headers) do ngx.header[k] = v end + -- Send response to client ngx.say(body) return ngx.exit(res.status) From 37fd03da4f75c3322c49d90486a3c9fe275c5ed0 Mon Sep 17 00:00:00 2001 From: Dimitri Gnidash Date: Mon, 9 Jul 2018 14:26:33 +0300 Subject: [PATCH 2/5] - Adding headers to the values that we are also returning --- .../aws-lambda-status-code/handler.lua | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/kong/plugins/aws-lambda-status-code/handler.lua b/kong/plugins/aws-lambda-status-code/handler.lua index 272d70c..0f9494f 100644 --- a/kong/plugins/aws-lambda-status-code/handler.lua +++ b/kong/plugins/aws-lambda-status-code/handler.lua @@ -55,8 +55,6 @@ function AWSLambdaStatusCodeHandler:access(conf) upstream_body.request_uri_args = ngx_req_get_uri_args() end - ngx.log(ngx.ERR, "Hello!") - if conf.forward_request_body then ngx_req_read_body() @@ -80,8 +78,6 @@ function AWSLambdaStatusCodeHandler:access(conf) upstream_body = utils.table_merge(ngx_req_get_uri_args(), body_args) end - ngx.log(ngx.ERR, "Hello!") - local upstream_body_json, err = cjson.encode(upstream_body) if not upstream_body_json then ngx.log(ngx.ERR, "[aws-lambda-status-code] could not JSON encode upstream body", @@ -127,8 +123,6 @@ function AWSLambdaStatusCodeHandler:access(conf) return responses.send_HTTP_INTERNAL_SERVER_ERROR(err) end - print("hello, from Plugin") - local res, err = client:request { method = "POST", path = request.url, @@ -158,7 +152,7 @@ function AWSLambdaStatusCodeHandler:access(conf) local content_type = headers["Content-Type"] if content_type:find("application/json", nil, true) then params, err = cjson.decode(body) - local statusCode = params.statusCode + local statusCode = params.status local resource = params.resource local lambdaHeaders = params.headers if statusCode ~= nil then @@ -170,13 +164,16 @@ function AWSLambdaStatusCodeHandler:access(conf) headers['Content-Length'] = nil body = cjson.encode(resource) end + + --Set headers returned in JSON body + if lambdaHeaders ~= nil then + for k,v in pairs(lambdaHeaders) do + headers[k] = v + end + end end end - --Set headers returned in JSON body - for k,v in pairs(lambdaHeaders) do - ngx.header[k] = v - for k, v in pairs(headers) do ngx.header[k] = v end From 47a675abea55c215905987959d682bd6af6593c7 Mon Sep 17 00:00:00 2001 From: Dimitri Gnidash Date: Mon, 9 Jul 2018 14:28:28 +0300 Subject: [PATCH 3/5] -Add the spec and change the status to be StatusCode --- ...in-aws-lambda-status-code-0.2.0-1.rockspec | 35 +++++++++++++++++++ .../aws-lambda-status-code/handler.lua | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 kong-plugin-aws-lambda-status-code-0.2.0-1.rockspec diff --git a/kong-plugin-aws-lambda-status-code-0.2.0-1.rockspec b/kong-plugin-aws-lambda-status-code-0.2.0-1.rockspec new file mode 100644 index 0000000..ebe5102 --- /dev/null +++ b/kong-plugin-aws-lambda-status-code-0.2.0-1.rockspec @@ -0,0 +1,35 @@ +package = "kong-plugin-aws-lambda-status-code" -- TODO: rename, must match the info in the filename of this rockspec! + -- as a convention; stick to the prefix: `kong-plugin-` +version = "0.2.0-1" -- TODO: renumber, must match the info in the filename of this rockspec! +-- The version '0.1.0' is the source code version, the trailing '1' is the version of this rockspec. +-- whenever the source version changes, the rockspec should be reset to 1. The rockspec version is only +-- updated (incremented) when this file changes, but the source remains the same. + +-- This is the name to set in the Kong configuration `custom_plugins` setting. +local pluginName = "aws-lambda-status-code" + +supported_platforms = {"linux", "macosx"} +source = { + -- these are initially not required to make it work + url = "git://github.com/iyp-uk/aws-lambda-status-code", + tag = "0.2.0" +} + +description = { + summary = "Kong custom plugin for changing AWS Lambda response status code based on a property in the body", + homepage = "https://github.com/iyp-uk/aws-lambda-status-code", + license = "MIT" +} + +dependencies = { + "lua >= 5.1" +} + +build = { + type = "builtin", + modules = { + ["kong.plugins."..pluginName..".handler"] = "kong/plugins/"..pluginName.."/handler.lua", + ["kong.plugins."..pluginName..".schema"] = "kong/plugins/"..pluginName.."/schema.lua", + ["kong.plugins."..pluginName..".v4"] = "kong/plugins/"..pluginName.."/v4.lua", + } +} diff --git a/kong/plugins/aws-lambda-status-code/handler.lua b/kong/plugins/aws-lambda-status-code/handler.lua index 0f9494f..bfad1a7 100644 --- a/kong/plugins/aws-lambda-status-code/handler.lua +++ b/kong/plugins/aws-lambda-status-code/handler.lua @@ -152,7 +152,7 @@ function AWSLambdaStatusCodeHandler:access(conf) local content_type = headers["Content-Type"] if content_type:find("application/json", nil, true) then params, err = cjson.decode(body) - local statusCode = params.status + local statusCode = params.statusCode local resource = params.resource local lambdaHeaders = params.headers if statusCode ~= nil then From b517645cd20cf8e0e0ae8ab80cf66a627e15273a Mon Sep 17 00:00:00 2001 From: Dimitri Gnidash Date: Mon, 9 Jul 2018 15:32:46 +0300 Subject: [PATCH 4/5] -Fix an issue for 400 requests the headers are not converted to tables --- kong/plugins/aws-lambda-status-code/handler.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kong/plugins/aws-lambda-status-code/handler.lua b/kong/plugins/aws-lambda-status-code/handler.lua index bfad1a7..95399c3 100644 --- a/kong/plugins/aws-lambda-status-code/handler.lua +++ b/kong/plugins/aws-lambda-status-code/handler.lua @@ -152,7 +152,7 @@ function AWSLambdaStatusCodeHandler:access(conf) local content_type = headers["Content-Type"] if content_type:find("application/json", nil, true) then params, err = cjson.decode(body) - local statusCode = params.statusCode + local statusCode = params.status local resource = params.resource local lambdaHeaders = params.headers if statusCode ~= nil then @@ -164,9 +164,10 @@ function AWSLambdaStatusCodeHandler:access(conf) headers['Content-Length'] = nil body = cjson.encode(resource) end - + --Set headers returned in JSON body - if lambdaHeaders ~= nil then + if type(lambdaHeaders) == type({}) then + for k,v in pairs(lambdaHeaders) do headers[k] = v end From b25186d95185f55fd4be9580857e5781445271f3 Mon Sep 17 00:00:00 2001 From: Dimitri Gnidash Date: Mon, 9 Jul 2018 15:33:56 +0300 Subject: [PATCH 5/5] -Correct the status to StatusCode --- kong/plugins/aws-lambda-status-code/handler.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/plugins/aws-lambda-status-code/handler.lua b/kong/plugins/aws-lambda-status-code/handler.lua index 95399c3..27615bb 100644 --- a/kong/plugins/aws-lambda-status-code/handler.lua +++ b/kong/plugins/aws-lambda-status-code/handler.lua @@ -152,7 +152,7 @@ function AWSLambdaStatusCodeHandler:access(conf) local content_type = headers["Content-Type"] if content_type:find("application/json", nil, true) then params, err = cjson.decode(body) - local statusCode = params.status + local statusCode = params.statusCode local resource = params.resource local lambdaHeaders = params.headers if statusCode ~= nil then