Skip to content

Commit

Permalink
feat(log) include authenticated consumer in serializer
Browse files Browse the repository at this point in the history
* add `ngx.ctx.authenticated_consumer` to the basic log serializer
* provide unit tests for the basic serializer
* update Changelog to mention this feature

From #2367
  • Loading branch information
subnetmarco authored and thibaultcha committed Apr 11, 2017
1 parent 87cbd5d commit d1192b3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
upstream services. Thanks [Paul Austin](https://github.com/pauldaustin)
for the contribution.
[#2051](https://github.com/Mashape/kong/pull/2051)
- logging: Logging plugins now also log the authenticated Consumer.
[#2367](https://github.com/Mashape/kong/pull/2367)

### Fixed

Expand Down
1 change: 1 addition & 0 deletions kong/plugins/log-serializers/basic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function _M.serialize(ngx)
},
authenticated_entity = authenticated_entity,
api = ngx.ctx.api,
consumer = ngx.ctx.authenticated_consumer,
client_ip = ngx.var.remote_addr,
started_at = ngx.req.start_time() * 1000
}
Expand Down
100 changes: 100 additions & 0 deletions spec/01-unit/13-log_serializer_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
local basic = require "kong.plugins.log-serializers.basic"

describe("Log Serializer", function()
local ngx

before_each(function()
ngx = {
ctx = {},
var = {
request_uri = "/request_uri",
scheme = "http",
host = "test.com",
server_port = 80,
request_length = 200,
bytes_sent = 99,
request_time = 2,
remote_addr = "1.1.1.1"
},
req = {
get_uri_args = function() return {"arg1", "arg2"} end,
get_method = function() return "POST" end,
get_headers = function() return {"header1", "header2"} end,
start_time = function() return 3 end
},
resp = {
get_headers = function() return {"respheader1", "respheader2"} end
}
}
end)

describe("Basic", function()
it("serializes without API, Consumer or Authenticated entity", function()
local res = basic.serialize(ngx)
assert.is_table(res)

-- Simple properties
assert.equals("1.1.1.1", res.client_ip)
assert.equals(3000, res.started_at)

-- Latencies
assert.is_table(res.latencies)
assert.equal(0, res.latencies.kong)
assert.equal(-1, res.latencies.proxy)
assert.equal(2000, res.latencies.request)

-- Request
assert.is_table(res.request)
assert.same({"header1", "header2"}, res.request.headers)
assert.equal("POST", res.request.method)
assert.same({"arg1", "arg2"}, res.request.querystring)
assert.equal("http://test.com:80/request_uri", res.request.request_uri)
assert.equal(200, res.request.size)
assert.equal("/request_uri", res.request.uri)

-- Response
assert.is_table(res.response)
assert.same({"respheader1", "respheader2"}, res.response.headers)
assert.equal(99, res.response.size)

assert.is_nil(res.api)
assert.is_nil(res.consumer)
assert.is_nil(res.authenticated_entity)
end)

it("serializes the API object", function()
ngx.ctx.api = {id = "someapi"}

local res = basic.serialize(ngx)
assert.is_table(res)

assert.equal("someapi", res.api.id)
assert.is_nil(res.consumer)
assert.is_nil(res.authenticated_entity)
end)

it("serializes the Consumer object", function()
ngx.ctx.authenticated_consumer = {id = "someconsumer"}

local res = basic.serialize(ngx)
assert.is_table(res)

assert.equal("someconsumer", res.consumer.id)
assert.is_nil(res.api)
assert.is_nil(res.authenticated_entity)
end)

it("serializes the Authenticated Entity object", function()
ngx.ctx.authenticated_credential = {id = "somecred",
consumer_id = "user1"}

local res = basic.serialize(ngx)
assert.is_table(res)

assert.same({id = "somecred", consumer_id = "user1"},
res.authenticated_entity)
assert.is_nil(res.consumer)
assert.is_nil(res.api)
end)
end)
end)

0 comments on commit d1192b3

Please sign in to comment.