From 85f7814c01c1f476e856108ed9b8e35747c5ae44 Mon Sep 17 00:00:00 2001 From: Vladislav Grubov Date: Fri, 12 May 2023 22:35:55 +0300 Subject: [PATCH 1/2] Fixes lua-resty-aws for S3 * validate operation.input first before validate against it s3/listBuckets does not accept any input.validator * refix S3 fixes path: if path=="" we need to restore path="/" * return raw body reader if operation output expects blob or streaming content --- src/resty/aws/init.lua | 25 +++++++--- src/resty/aws/request/build.lua | 74 +++++++++++++++--------------- src/resty/aws/request/execute.lua | 15 +++++- src/resty/aws/request/validate.lua | 3 ++ 4 files changed, 73 insertions(+), 44 deletions(-) diff --git a/src/resty/aws/init.lua b/src/resty/aws/init.lua index 266b737..7a33f5c 100644 --- a/src/resty/aws/init.lua +++ b/src/resty/aws/init.lua @@ -279,12 +279,13 @@ local function s3_patch(request, bucket) end request.host = bucket .. "." .. request.host + request.headers['Host'] = request.host local path = request.path if bucket and path then path = path:sub(#bucket + 2) - if path == "/" then - path = "" + if path == "" then + path = "/" end request.path = path @@ -307,9 +308,11 @@ local function generate_service_methods(service) --print(require("pl.pretty").write(self.config)) -- validate parameters - local ok, err = validate_input(params, operation.input, "params") - if not ok then - return nil, operation_prefix .. " validation error: " .. tostring(err) + if operation.input then + local ok, err = validate_input(params, operation.input, "params") + if not ok then + return nil, operation_prefix .. " validation error: " .. tostring(err) + end end -- generate request data and format it according to the protocol @@ -354,8 +357,18 @@ local function generate_service_methods(service) --print(require("pl.pretty").write(signed_request)) + local need_raw_reader = false + if operation.output then + for key, shape in pairs(operation.output.members) do + if shape.type == 'blob' or shape.streaming then + need_raw_reader = true + break + end + end + end + -- execute the request - local response, err = execute_request(signed_request) + local response, err = execute_request(signed_request, need_raw_reader) if not response then return nil, operation_prefix .. " " .. tostring(err) end diff --git a/src/resty/aws/request/build.lua b/src/resty/aws/request/build.lua index e4b2458..24d5966 100644 --- a/src/resty/aws/request/build.lua +++ b/src/resty/aws/request/build.lua @@ -157,45 +157,47 @@ local function build_request(operation, config, params) -- inject parameters in the right places; path/query/header/body -- this assumes they all live on the top-level of the structure, is this correct?? - for name, member_config in pairs(operation.input.members) do - local param_value = params[name] - -- TODO: date-time value should be properly formatted??? - if param_value ~= nil then - - -- a parameter value is provided - local location = member_config.location - local locationName = member_config.locationName - -- print(name," = ", param_value, ": ",location, " (", locationName,")") - - if location == "uri" then - local place_holder = "{" .. locationName .. "%+?}" - local replacement = escape_uri(param_value):gsub("%%", "%%%%") - request.path = request.path:gsub(place_holder, replacement) - - elseif location == "querystring" then - request.query[locationName] = param_value - - elseif location == "header" then - request.headers[locationName] = param_value - - elseif location == "headers" then - for k,v in pairs(param_value) do - request.headers[locationName .. k] = v - end + if operation.input then + for name, member_config in pairs(operation.input.members) do + local param_value = params[name] + -- TODO: date-time value should be properly formatted??? + if param_value ~= nil then + + -- a parameter value is provided + local location = member_config.location + local locationName = member_config.locationName + -- print(name," = ", param_value, ": ",location, " (", locationName,")") + + if location == "uri" then + local place_holder = "{" .. locationName .. "%+?}" + local replacement = escape_uri(param_value):gsub("%%", "%%%%") + request.path = request.path:gsub(place_holder, replacement) + + elseif location == "querystring" then + request.query[locationName] = param_value + + elseif location == "header" then + request.headers[locationName] = param_value + + elseif location == "headers" then + for k,v in pairs(param_value) do + request.headers[locationName .. k] = v + end + + elseif location == nil then + if config.protocol == "query" then + -- no location specified, but protocol is query, so it goes into query + request.query[name] = param_value + elseif member_config.type == "blob" then + request.body = param_value + else + -- nowhere else to go, so put it in the body (for json and xml) + body_tbl[name] = param_value + end - elseif location == nil then - if config.protocol == "query" then - -- no location specified, but protocol is query, so it goes into query - request.query[name] = param_value - elseif member_config.type == "blob" then - request.body = param_value else - -- nowhere else to go, so put it in the body (for json and xml) - body_tbl[name] = param_value + error("Unknown location: " .. location) end - - else - error("Unknown location: " .. location) end end end diff --git a/src/resty/aws/request/execute.lua b/src/resty/aws/request/execute.lua index 09e43ac..94e0112 100644 --- a/src/resty/aws/request/execute.lua +++ b/src/resty/aws/request/execute.lua @@ -12,7 +12,7 @@ local json_decode = require("cjson.safe").new().decode -- -- Input parameters: -- * signed_request table -local function execute_request(signed_request) +local function execute_request(signed_request, return_raw_body) local httpc = http.new() httpc:set_timeout(60000) @@ -22,7 +22,7 @@ local function execute_request(signed_request) port = signed_request.port, scheme = signed_request.tls and "https" or "http", ssl_server_name = signed_request.host, - ssl_verify = true, + ssl_verify = false, } if not ok then return nil, ("failed to connect to '%s://%s:%s': %s"):format( @@ -49,6 +49,17 @@ local function execute_request(signed_request) local body do if response.has_body then + if return_raw_body then + return { + httpc = httpc, + status = response.status, + reason = response.reason, + headers = response.headers, + has_body = response.has_body, + body_reader = response.body_reader, + read_body = response.read_body, + } + end body, err = response:read_body() if not body then return nil, ("failed reading response body from '%s:%s': %s"):format( diff --git a/src/resty/aws/request/validate.lua b/src/resty/aws/request/validate.lua index 6cfbab7..a40cf3d 100644 --- a/src/resty/aws/request/validate.lua +++ b/src/resty/aws/request/validate.lua @@ -435,6 +435,9 @@ local validators do type = always_pass, deprecated = always_pass, box = always_pass, + locationName = always_pass, + location = always_pass, + deprecatedMessage = always_pass, },ops_mt) From 740293c497e064fe95a926d57e8613bd99c3b5e7 Mon Sep 17 00:00:00 2001 From: Vladislav Grubov Date: Fri, 12 May 2023 23:22:31 +0300 Subject: [PATCH 2/2] execute: return verify ssl_verify=true back --- src/resty/aws/request/execute.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resty/aws/request/execute.lua b/src/resty/aws/request/execute.lua index 94e0112..d1da9a3 100644 --- a/src/resty/aws/request/execute.lua +++ b/src/resty/aws/request/execute.lua @@ -22,7 +22,7 @@ local function execute_request(signed_request, return_raw_body) port = signed_request.port, scheme = signed_request.tls and "https" or "http", ssl_server_name = signed_request.host, - ssl_verify = false, + ssl_verify = true, } if not ok then return nil, ("failed to connect to '%s://%s:%s': %s"):format(