diff --git a/kong/plugins/log-serializers/alf.lua b/kong/plugins/log-serializers/alf.lua index f38baaab385..fe8a3f8322d 100644 --- a/kong/plugins/log-serializers/alf.lua +++ b/kong/plugins/log-serializers/alf.lua @@ -17,6 +17,8 @@ local stringy = require "stringy" +local ngx_encode_base64 = ngx.encode_base64 + local EMPTY_ARRAY_PLACEHOLDER = "__empty_array_placeholder__" -- Transform a key/value lua table into an array of elements with `name`, `value`. @@ -67,6 +69,9 @@ function _M.serialize_entry(ngx) local alf_res_body = analytics_data.res_body or "" local alf_req_post_args = analytics_data.req_post_args or {} + local alf_base64_req_body = ngx_encode_base64(alf_req_body) + local alf_base64_res_body = ngx_encode_base64(alf_res_body) + -- timers local proxy_started_at, proxy_ended_at = ngx.ctx.proxy_started_at, ngx.ctx.proxy_ended_at @@ -130,7 +135,7 @@ function _M.serialize_entry(ngx) postData = { mimeType = alf_req_mimeType, params = dic_to_array(alf_req_post_args), - text = alf_req_body + text = alf_base64_req_body } }, response = { @@ -145,7 +150,7 @@ function _M.serialize_entry(ngx) content = { size = tonumber(ngx.var.body_bytes_sent), mimeType = alf_res_mimeType, - text = alf_res_body + text = alf_base64_res_body } }, cache = {}, diff --git a/kong/tools/ngx_stub.lua b/kong/tools/ngx_stub.lua index 4d0b0b66320..c5d9c202f09 100644 --- a/kong/tools/ngx_stub.lua +++ b/kong/tools/ngx_stub.lua @@ -26,6 +26,9 @@ _G.ngx = { return res_str, sub_made end }, + encode_base64 = function(str) + return string.format("base64_%s", str) + end, -- Builds a querystring from a table, separated by `&` -- @param `tab` The key/value parameters -- @param `key` The parent key if the value is multi-dimensional (optional) diff --git a/spec/plugins/mashape-analytics/alf_serializer_spec.lua b/spec/plugins/mashape-analytics/alf_serializer_spec.lua index f3eff8354e4..b75e2f0a86e 100644 --- a/spec/plugins/mashape-analytics/alf_serializer_spec.lua +++ b/spec/plugins/mashape-analytics/alf_serializer_spec.lua @@ -1,3 +1,4 @@ +require "kong.tools.ngx_stub" local fixtures = require "spec.plugins.mashape-analytics.fixtures.requests" local ALFSerializer = require "kong.plugins.log-serializers.alf" diff --git a/spec/plugins/mashape-analytics/fixtures/requests.lua b/spec/plugins/mashape-analytics/fixtures/requests.lua index b0f05cf8600..b861bb394fd 100644 --- a/spec/plugins/mashape-analytics/fixtures/requests.lua +++ b/spec/plugins/mashape-analytics/fixtures/requests.lua @@ -52,7 +52,7 @@ return { {name = "hello", value = "world"}, {name = "hello", value = "earth"} }, - text = "hello=world&hello=earth" + text = "base64_hello=world&hello=earth" }, queryString = { {name = "foo", value = "bar"}, @@ -66,7 +66,7 @@ return { content = { mimeType = "application/json", size = 934, - text = "{\"message\":\"response body\"}" + text = "base64_{\"message\":\"response body\"}" }, cookies = {EMPTY_ARRAY_PLACEHOLDER}, headers = { @@ -144,7 +144,7 @@ return { {name = "hello", value = "world"}, {name = "hello", value = "earth"} }, - text = "hello=world&hello=earth" + text = "base64_hello=world&hello=earth" }, queryString = { {name = "foo", value = "bar"}, @@ -157,7 +157,7 @@ return { content = { mimeType = "application/json", size = 934, - text = "{\"message\":\"response body\"}" + text = "base64_{\"message\":\"response body\"}" }, cookies = {EMPTY_ARRAY_PLACEHOLDER}, headers = {